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

Match Postgres for stddev and variance on less than 3 values #4843

Closed
jonmmease opened this issue Jan 7, 2023 · 1 comment · Fixed by #4847
Closed

Match Postgres for stddev and variance on less than 3 values #4843

jonmmease opened this issue Jan 7, 2023 · 1 comment · Fixed by #4847
Labels
bug Something isn't working

Comments

@jonmmease
Copy link
Contributor

Describe the bug
The behavior of the stddev and variance aggregation differs from Postgres for less than three values. DataFusion crashes in these cases, whereas Postgres computes a variance for an array with two values and returns NULL for an array of one value.

In DataFusion CLI:

select stddev(sq.column1) from (values (1.0), (2.0), (3.0)) as sq;
+--------------------+
| STDDEV(sq.column1) |
+--------------------+
| 1                  |
+--------------------+select stddev(sq.column1) from (values (1.0), (2.0)) as sq;
ArrowError(ExternalError(Internal("At least two values are needed to calculate variance")))

❯  select stddev(sq.column1) from (values (1.0)) as sq;
ArrowError(ExternalError(Internal("At least two values are needed to calculate variance")))

In Postgres

select stddev(sq.column1) from (values (1.0), (2.0), (3.0)) as sq;
1select stddev(sq.column1) from (values (1.0), (2.0)) as sq;
0.7071067811865475244select stddev(sq.column1) from (values (1.0)) as sq;
NULL

I would like to suggest DataFusion match the behavior of Postgres. Otherwise every usage of the variance function risks raising an error (depending on the distribution of the groupby column).

Additional context
Variance was first added by @realno in #1525.

@jonmmease jonmmease added the bug Something isn't working label Jan 7, 2023
@ozankabak
Copy link
Contributor

We should definitely calculate something for the two-sample case -- erroring simply doesn't make sense in that case. The second example has various behaviors going around, see the discussion in Snowflake docs. I agree with your sentiment that we should align with Postgres.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants