Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for aggregate functions applied to empty sets #964

Merged
merged 2 commits into from
Jan 10, 2018

Conversation

alexpeattie
Copy link
Contributor

Current behaviour

When you call most of the built in aggregate functions (STDEV, VAR etc.) on an empty results set (e.g. SELECT STDDEV(col) FROM table WHERE 1=0) an error is thrown, as detailed in #917.

Expected behaviour

No error should be thrown and undefined (or null?) should be returned.

Cause

An aggregate function has 3 stages (as detailed here), stage 1 for the first value, stage 2 for each subsequent value, and stage 3 to perform any last transforms on the accumulator. The problem is that with an empty set, we skip straight to stage 3 - but the accumulator will always be undefined, because it hasn't been set to anything in stages 1/2 (which won't have run, since there's no first value or subsequent values) which usually leads to errors.

Fix

I propose we can wrap all the aggregate functions in an outer functions which checks whether the accumulator is undefined at the post-processing state and then just short-circuits (by returning undefined). An alternative would be to add that condition to all the affected aggregate functions, but figured this approach might be simpler ☺️.

The assumption is that user-defined aggregators would handle this case themselves (might be worth mentioning in the docs?).

@mathiasrw
Copy link
Member

Thank you!

@codecov-io
Copy link

Codecov Report

Merging #964 into develop will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #964      +/-   ##
===========================================
+ Coverage    68.09%   68.11%   +0.01%     
===========================================
  Files            1        1              
  Lines         9918     9924       +6     
  Branches      2966     2967       +1     
===========================================
+ Hits          6754     6760       +6     
  Misses        3164     3164
Impacted Files Coverage Δ
dist/alasql.fs.js 68.11% <100%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b3e6f13...7f8920d. Read the comment docs.

@mathiasrw mathiasrw merged commit dbcbedc into AlaSQL:develop Jan 10, 2018
@alexpeattie alexpeattie deleted the fix-aggregate-empty-sets branch January 18, 2018 08:56
jacqt pushed a commit to jacqt/alasql that referenced this pull request Jan 1, 2019
* Return undefined from aggregate functions when the accumulator is undefined (fixes AlaSQL#917)

* Build from src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants