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

Remove AggregateState wrapper #4582

Merged
merged 5 commits into from
Dec 14, 2022

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Dec 11, 2022

Which issue does this PR close?

As promised on #4488

Rationale for this change

All actual aggregate implementations use AggregateState::Scalar

It is bad to have AggregateState::Array because:

  1. It does not work for queries with more than 1 partition (which is almost any real world query), see Fix panic in median "AggregateState is not a scalar aggregate" #4488 for more details
  2. It obscures how aggregates actually work, which is already complicated, with an extra layer of indirection

What changes are included in this PR?

  1. Remove AggregateState wrapper

Are these changes tested?

yes, covered by existing tests

Are there any user-facing changes?

yes -- User defined aggregates are now simpler to write / less error prone

cc @andygrove as he originally wrote this code in #3009

Note that @tustvold is contemplating more significant changes in the Grouping code, so this PR should hopefully help him

@alamb alamb added the api change Changes the API exposed to users of the crate label Dec 11, 2022
@github-actions github-actions bot added core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Physical Expressions labels Dec 11, 2022
@alamb alamb changed the title Remove AggregateState wrapper Remove AggregateState wrapper Dec 11, 2022
@alamb alamb requested a review from andygrove December 11, 2022 13:14
@@ -519,7 +519,7 @@ fn create_batch_from_map(
accumulators.group_states.iter().map(|group_state| {
group_state.accumulator_set[x]
.state()
.and_then(|x| x[y].as_scalar().map(|v| v.clone()))
.map(|x| x[y].clone())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removing the extra layer of indirection simplifies the code significantly

@alamb
Copy link
Contributor Author

alamb commented Dec 12, 2022

cc @crepererum and @tustvold as I believe you mentioned plans to simplify aggregates and grouping state, perhaps

@@ -108,10 +107,10 @@ impl Accumulator for GeometricMean {
// This function serializes our state to `ScalarValue`, which DataFusion uses
// to pass this state between execution stages.
// Note that this can be arbitrary data.
fn state(&self) -> Result<Vec<AggregateState>> {
fn state(&self) -> Result<Vec<ScalarValue>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Eventually it might be nice to change this to be ArrayRef or something, but we will see what shakes out of #2723

@alamb
Copy link
Contributor Author

alamb commented Dec 13, 2022

I plan to merge this tomorrow unless anyone else would like more time to review or has additional comments

.map(|s| vec![s.clone()])
.and_then(ScalarValue::iter_to_array)
})
.map(|s| ScalarValue::iter_to_array(vec![s.clone()]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just an observation, this doesn't need to be a vec?

Copy link
Contributor Author

@alamb alamb Dec 14, 2022

Choose a reason for hiding this comment

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

Good call -- changed to

        .map(|s| s.to_array_of_size(1))

in commit 1165cff

@alamb alamb merged commit 5d424ef into apache:master Dec 14, 2022
@alamb alamb deleted the alamb/remove_unecessary_aggregate_state branch December 14, 2022 14:53
@ursabot
Copy link

ursabot commented Dec 14, 2022

Benchmark runs are scheduled for baseline = 84d3ae8 and contender = 5d424ef. 5d424ef 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api change Changes the API exposed to users of the crate core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Physical Expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants