Skip to content

[branch-55] Cherry-pick apache/datafusion#24444: apply grouped FILTER before evaluating arguments - #230

Draft
xavlee wants to merge 2 commits into
branch-55from
xavlee/df55-24444-grouped-filter
Draft

xavlee wants to merge 2 commits into
branch-55from
xavlee/df55-24444-grouped-filter

Conversation

@xavlee

@xavlee xavlee commented Nov 30, 2026 •

Copy link
Copy Markdown

PR stack

Which issue does this PR close?

Backports apache/datafusion#24444 and its regression case for apache/datafusion#24443.

Rationale for this change

A filtered-out row could still raise a division-by-zero error. For example, SUM(100 / divisor) FILTER (WHERE divisor <> 0) should evaluate the division only on rows that pass the filter.

divisor                    0    4
passes divisor <> 0?      no  yes
contribution to SUM        -   25

What changes are included in this PR?

The first commit cherry-picks Apache commit 5610e58b9163 for the grouped hash table. The second applies the same evaluation order to DF55's older grouped execution path. Both evaluate aggregate arguments against the selected rows and preserve the mapping back to their groups. When replaying spilled aggregate states, the reader uses the previously evaluated states.

Are these changes tested?

The upstream SQL regression exercises a filtered-out invalid argument. Workspace formatting and Clippy with all targets and features passed on this DF55 head. The earlier integrated build also passed the connector checks. This is a correctness fix. We have no isolated performance estimate for it.

Are there any user-facing changes?

A grouped aggregate with FILTER can now succeed when an argument would have errored only on the excluded rows.

lyne7-sc and others added 2 commits November 30, 2026 05:02
## 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 apache#24443.

## 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.
-->

Grouped aggregates evaluate their arguments before applying `FILTER`,
which can cause errors on rows that should not participate in the
aggregate.

## 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.
-->

Evaluate aggregate filters first and use the resulting filters as
selections when evaluating arguments.

This covers migrated hash and ordered aggregation, legacy grouped hash
aggregation, and both skip-partial paths.

## Are these changes tested?

<!--
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

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

Yes. slts and unit tests cover migrated, legacy, ordered, and
skip-partial execution.

## 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.
-->

No public APIs are changed. Grouped aggregate arguments are no longer
evaluated for rows rejected by `FILTER`.

(cherry picked from commit 5610e58)
Environment: Datadog workspace
Co-authored-by: Codex GPT-6.2 Astra <noreply@localhost>
DF55 still dispatches fully clustered Single aggregates to the legacy
stream. Apply the per-aggregate filter before evaluating fallible
arguments there and when converting input rows to partial states.
Spill replay continues to evaluate the saved unfiltered states.

Environment: Datadog workspace

Co-Authored-By: Codex GPT-6.2 Astra <noreply@localhost>
@codecov-commenter

codecov-commenter commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 67.50000% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.22%. Comparing base (a348975) to head (4521f00).

Files with missing lines Patch % Lines
...hysical-plan/src/aggregates/grouped_hash_stream.rs 55.55% 8 Missing and 4 partials ⚠️
...plan/src/aggregates/aggregate_hash_table/common.rs 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           branch-55     #230   +/-   ##
==========================================
  Coverage      81.22%   81.22%           
==========================================
  Files           1112     1112           
  Lines         391023   391046   +23     
  Branches      391023   391046   +23     
==========================================
+ Hits          317601   317638   +37     
+ Misses         54445    54435   -10     
+ Partials       18977    18973    -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants