Skip to content

Commit

Permalink
VAR and STDDEV report 0.0 when variance negative (opendp#550)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua <[email protected]>
  • Loading branch information
joshua-oss and joshua-oss committed Apr 24, 2023
1 parent f20614d commit d4a19d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sql/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# SmartNoise SQL v0.2.12 Release Notes

* Fix bug where other counts would borrow `SELECT COUNT(*)` when `SELECT COUNT(*)` was used first
* Fix bug where queries would fail if multiple tables in a single metadata had different `max_contrib`.
* Change behavior of VAR and STDDEV to return zero if VAR is negative.

Thanks to @mhauru for reporting these issues!

# SmartNoise SQL v0.2.11 Release Notes

Expand Down
18 changes: 18 additions & 0 deletions sql/snsql/sql/private_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ def _execute_ast(self, query, *ignore, accuracy:bool=False, pre_aggregated=None,
raise ValueError("Please pass AST to _execute_ast.")

_orig_query = query

agg_names = []
for col in _orig_query.select.namedExpressions:
if isinstance(col.expression, ast.AggFunction):
agg_names.append(col.expression.name)
else:
agg_names.append(None)

subquery, query = self._rewrite_ast(query)

if pre_aggregated is not None:
Expand Down Expand Up @@ -614,6 +622,16 @@ def convert(val, type):
def process_out_row(row):
bindings = dict((name.lower(), val) for name, val in zip(source_col_names, row))
out_row = [c.expression.evaluate(bindings) for c in query.select.namedExpressions]
# fix up case where variance is negative
out_row_fixed = []
for val, agg in zip(out_row, agg_names):
if agg == 'VAR' and val < 0:
out_row_fixed.append(0.0)
elif agg == 'STDDEV' and np.isnan(val):
out_row_fixed.append(0.0)
else:
out_row_fixed.append(val)
out_row = out_row_fixed
try:
out_row =[convert(val, type) for val, type in zip(out_row, out_types)]
except Exception as e:
Expand Down

0 comments on commit d4a19d8

Please sign in to comment.