You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "masonh22 (via GitHub)" <gi...@apache.org> on 2026/09/24 15:23:00 UTC

[PR] Add a test to enforce compatibility between an aggregate's accumulators [datafusion]

masonh22 opened a new pull request, #25710:
URL: https://github.com/apache/datafusion/pull/25710

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #25660.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   
   Please explain the problem you are trying to solve in terms of the user-visible
   behavior, rather than the implementation.
   
   For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the
   implementation. "COUNT(DISTINCT) returns wrong results when the column contains
   nulls" is the user-visible problem.
   -->
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## What is the testing strategy for this PR?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'.
   
   If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it.
   
   You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised.
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#issuecomment-6151752327

   > I went ahead and updated HyperLogLog so that it produces hash collisions when the feature is enabled. The hash collision tests passed on my machine with this change.
   
   
   Sounds good to me -- thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "masonh22 (via GitHub)" <gi...@apache.org>.
masonh22 commented on PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#issuecomment-6042030980

   @alamb tagging you because you might be interested in the optimization this enables, given that github considers you a contributor to [Datafusion Query Cache](https://github.com/datafusion-contrib/datafusion-query-cache)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "masonh22 (via GitHub)" <gi...@apache.org>.
masonh22 commented on code in PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#discussion_r4232616488


##########
datafusion/functions-aggregate/src/testing/state_compat.rs:
##########
@@ -0,0 +1,1017 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Checks that the intermediate state produced by an aggregate's
+//! [`Accumulator`] and its [`GroupsAccumulator`] are interchangeable.
+//!
+//! # Built-in aggregate invariant
+//!
+//! DataFusion's own execution never mixes the two kinds of accumulator when
+//! merging state, so this is not a requirement of the `AggregateUDFImpl` API.
+//! It is, however, an invariant that every built-in aggregate function with a
+//! native [`GroupsAccumulator`] maintains, so that systems built on DataFusion
+//! can merge state produced by either kind with the other:
+//!
+//! * Each row of the state returned by `GroupsAccumulator::state` or
+//!   `GroupsAccumulator::convert_to_state` is accepted by
+//!   `Accumulator::merge_batch`.
+//! * `GroupsAccumulator::merge_batch` accepts state returned by
+//!   `Accumulator::state`.
+//! * Merging state from the other kind produces the same result as merging
+//!   the equivalent state from the same kind.
+//!
+//! The `builtin_accumulator_and_groups_accumulator_states_are_compatible` test
+//! below enforces this for every function in
+//! [`all_default_aggregate_functions`]. A new built-in function that the test
+//! cannot exercise must be added to its `NOT_EXERCISED` list with the reason.
+//!
+//! [`all_default_aggregate_functions`]: crate::all_default_aggregate_functions
+//! [`Accumulator`]: datafusion_expr::Accumulator
+//! [`GroupsAccumulator`]: datafusion_expr::GroupsAccumulator
+
+use std::collections::BTreeSet;
+use std::panic::{AssertUnwindSafe, catch_unwind};
+use std::sync::Arc;
+
+use arrow::array::{Array, ArrayRef, Int64Array, UInt32Array};
+use arrow::compute::{cast, concat, take};
+use arrow::datatypes::{DataType, Field, FieldRef, Schema, TimeUnit};
+use arrow::record_batch::{RecordBatch, RecordBatchOptions};
+use datafusion_common::{DataFusionError, Result, ScalarValue, exec_err};
+use datafusion_expr::type_coercion::functions::fields_with_udf;
+use datafusion_expr::{AggregateUDF, EmitTo};
+use datafusion_physical_expr::PhysicalSortExpr;
+use datafusion_physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr};
+use datafusion_physical_expr::expressions::{Column, Literal};
+use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
+
+/// Checks that `udaf`'s [`Accumulator`] and [`GroupsAccumulator`] can each

Review Comment:
   I moved this into its own integration test and got rid of the `testing` feature



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "masonh22 (via GitHub)" <gi...@apache.org>.
masonh22 commented on PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#issuecomment-6149871738

   @alamb it looks like CI failed in the hash collisions mode for the test I added because `HyperLogLog` doesn't do anything different when the hash collisions feature is enabled, but the groups accumulator for `approx_distinct()` does, so they end up generating different values.
   
   I went ahead and updated `HyperLogLog` so that it produces hash collisions when the feature is enabled.  The hash collision tests passed on my machine with this change.
   
   Alternatively, I could disable the test I added when the hash collision feature is enabled.  Let me know what you would prefer.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "masonh22 (via GitHub)" <gi...@apache.org>.
masonh22 commented on code in PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#discussion_r4232613757


##########
docs/source/library-user-guide/upgrading/56.0.0.md:
##########
@@ -623,6 +623,17 @@ This guarantees unique aggregate state field names and allows
 Users or integrations that inspect aggregate state field names directly,
 including custom UDAFs and FFI integrations.
 
+### `avg` intermediate state for groups with no values
+
+The `Accumulator` for `avg` now returns `NULL` for both the count and the sum

Review Comment:
   I removed this from the upgrade guide



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #25710:
URL: https://github.com/apache/datafusion/pull/25710


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#issuecomment-6001441944

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/25710?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `75.80645%` with `15 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 82.68%. Comparing base ([`8248a57`](https://app.codecov.io/gh/apache/datafusion/commit/8248a579695b92b7ca94709eedd63a71689513ce?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`52043c8`](https://app.codecov.io/gh/apache/datafusion/commit/52043c8aa139702b63b7bb51e2b8b2ae9ea2a7e0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 3 commits behind head on main.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion/pull/25710?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...afusion/functions-aggregate/src/approx\_distinct.rs](https://app.codecov.io/gh/apache/datafusion/pull/25710?src=pr&el=tree&filepath=datafusion%2Ffunctions-aggregate%2Fsrc%2Fapprox_distinct.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9mdW5jdGlvbnMtYWdncmVnYXRlL3NyYy9hcHByb3hfZGlzdGluY3QucnM=) | 77.77% | [7 Missing and 3 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25710?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [datafusion/functions-aggregate/src/average.rs](https://app.codecov.io/gh/apache/datafusion/pull/25710?src=pr&el=tree&filepath=datafusion%2Ffunctions-aggregate%2Fsrc%2Faverage.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9mdW5jdGlvbnMtYWdncmVnYXRlL3NyYy9hdmVyYWdlLnJz) | 70.58% | [4 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25710?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##             main   #25710      +/-   ##
   ==========================================
   + Coverage   82.66%   82.68%   +0.01%     
   ==========================================
     Files        1147     1147              
     Lines      446542   446555      +13     
     Branches   446542   446555      +13     
   ==========================================
   + Hits       369154   369225      +71     
   + Misses      54986    54933      -53     
   + Partials    22402    22397       -5     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/25710?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Document and enforce compatibility between an aggregate's accumulators [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #25710:
URL: https://github.com/apache/datafusion/pull/25710#discussion_r4232342894


##########
docs/source/library-user-guide/upgrading/56.0.0.md:
##########
@@ -623,6 +623,17 @@ This guarantees unique aggregate state field names and allows
 Users or integrations that inspect aggregate state field names directly,
 including custom UDAFs and FFI integrations.
 
+### `avg` intermediate state for groups with no values
+
+The `Accumulator` for `avg` now returns `NULL` for both the count and the sum

Review Comment:
   I think this is a bug fix -- I am not sure it needs an entry in the upgrade guide 🤔 



##########
datafusion/functions-aggregate/src/testing/state_compat.rs:
##########
@@ -0,0 +1,1017 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Checks that the intermediate state produced by an aggregate's
+//! [`Accumulator`] and its [`GroupsAccumulator`] are interchangeable.
+//!
+//! # Built-in aggregate invariant
+//!
+//! DataFusion's own execution never mixes the two kinds of accumulator when
+//! merging state, so this is not a requirement of the `AggregateUDFImpl` API.
+//! It is, however, an invariant that every built-in aggregate function with a
+//! native [`GroupsAccumulator`] maintains, so that systems built on DataFusion
+//! can merge state produced by either kind with the other:
+//!
+//! * Each row of the state returned by `GroupsAccumulator::state` or
+//!   `GroupsAccumulator::convert_to_state` is accepted by
+//!   `Accumulator::merge_batch`.
+//! * `GroupsAccumulator::merge_batch` accepts state returned by
+//!   `Accumulator::state`.
+//! * Merging state from the other kind produces the same result as merging
+//!   the equivalent state from the same kind.
+//!
+//! The `builtin_accumulator_and_groups_accumulator_states_are_compatible` test
+//! below enforces this for every function in
+//! [`all_default_aggregate_functions`]. A new built-in function that the test
+//! cannot exercise must be added to its `NOT_EXERCISED` list with the reason.
+//!
+//! [`all_default_aggregate_functions`]: crate::all_default_aggregate_functions
+//! [`Accumulator`]: datafusion_expr::Accumulator
+//! [`GroupsAccumulator`]: datafusion_expr::GroupsAccumulator
+
+use std::collections::BTreeSet;
+use std::panic::{AssertUnwindSafe, catch_unwind};
+use std::sync::Arc;
+
+use arrow::array::{Array, ArrayRef, Int64Array, UInt32Array};
+use arrow::compute::{cast, concat, take};
+use arrow::datatypes::{DataType, Field, FieldRef, Schema, TimeUnit};
+use arrow::record_batch::{RecordBatch, RecordBatchOptions};
+use datafusion_common::{DataFusionError, Result, ScalarValue, exec_err};
+use datafusion_expr::type_coercion::functions::fields_with_udf;
+use datafusion_expr::{AggregateUDF, EmitTo};
+use datafusion_physical_expr::PhysicalSortExpr;
+use datafusion_physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr};
+use datafusion_physical_expr::expressions::{Column, Literal};
+use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
+
+/// Checks that `udaf`'s [`Accumulator`] and [`GroupsAccumulator`] can each

Review Comment:
   this feels like a lot of code / complexity for DataFusion 🤔 I understand it might be useful to people who implement accumulators out of the core, but it seems to me like this would be better placed in the `#[test]` config as a real unit / integration test and then duplicated if necesary into a downstream (e.g. Coralogix's) code base



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org