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

Update variance/stddev to work with single values #4847

Merged
merged 4 commits into from
Jan 8, 2023

Conversation

jonmmease
Copy link
Contributor

Which issue does this PR close?

Closes #4843.

Rationale for this change

Rather than crash when performing a variance-based aggregation on a single value, follow Postgres' semantics:

  • var/stddev of single element is NULL
  • var_pop/stddev_pop of single element is 0

There's documentation of this behavior in Snowflake (https://docs.snowflake.com/en/sql-reference/functions/stddev.html), and it's what Postgres does, but in searching briefly I haven't found this behavior described in the Postgres docs.

Are these changes tested?

I added a pair of sqllogictest tests for the one and two element cases.

Are there any user-facing changes?

Queries that used to crash when aggregating single values with variance-based aggregations will no longer crash.

Following Postgres:
 - var/stddev of single element is NULL
 - var_pop/stddev_pop of single element is 0
@github-actions github-actions bot added core Core DataFusion crate physical-expr Physical Expressions sqllogictest SQL Logic Tests (.slt) labels Jan 7, 2023
Copy link
Contributor

@ozankabak ozankabak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one minor inline comment.

Ok(ScalarValue::Float64(match self.count {
0 => None,
1 => {
if matches!(self.stats_type, StatsType::Population) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: In this case I think if let would be slightly more idiomatic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in bc65eeb

@ozankabak
Copy link
Contributor

Hmm, some tests seem to be failing -- needs some investigation

@jonmmease
Copy link
Contributor Author

Ok, the tests are sorted out now. Thanks for taking a look!

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jonmmease -- looks great to me

@alamb alamb merged commit 3d75bb8 into apache:master Jan 8, 2023
@alamb
Copy link
Contributor

alamb commented Jan 8, 2023

@ozankabak -- thank you as well for your help reviewing. It is really appreciated

@ursabot
Copy link

ursabot commented Jan 8, 2023

Benchmark runs are scheduled for baseline = dcd52ee and contender = 3d75bb8. 3d75bb8 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

jonmmease added a commit to jonmmease/arrow-datafusion that referenced this pull request Jan 8, 2023
* Update variance/stddev to work with single values

Following Postgres:
 - var/stddev of single element is NULL
 - var_pop/stddev_pop of single element is 0

* Fix tests

* matches! to if let

* fix test_stddev_1_input test

(cherry picked from commit 3d75bb8)
jonmmease added a commit to jonmmease/arrow-datafusion that referenced this pull request Jan 8, 2023
* Update variance/stddev to work with single values

Following Postgres:
 - var/stddev of single element is NULL
 - var_pop/stddev_pop of single element is 0

* Fix tests

* matches! to if let

* fix test_stddev_1_input test

(cherry picked from commit 3d75bb8)
@jonmmease jonmmease deleted the jonmmease/GH4843 branch January 18, 2023 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate physical-expr Physical Expressions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Match Postgres for stddev and variance on less than 3 values
4 participants