Skip to content

Preserve grouping ID during aggregate CSE - #24144

Merged
gabotechs merged 2 commits into
apache:mainfrom
notfilippo:filippo.rossi/preserve-grouping-id-during-aggregate-cse-wnmpwslywnzn
Aug 7, 2026
Merged

Preserve grouping ID during aggregate CSE#24144
gabotechs merged 2 commits into
apache:mainfrom
notfilippo:filippo.rossi/preserve-grouping-id-during-aggregate-cse-wnmpwslywnzn

Conversation

@notfilippo

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

See #24143.

What changes are included in this PR?

  • Preserve __grouping_id in CSE recovery projections for grouping-set aggregates.
  • Add regression coverage.

Are these changes tested?

Yes.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added the optimizer Optimizer rules label Aug 6, 2026
Comment on lines +1112 to +1113
#[test]
fn common_aggregate_grouping_set_preserves_internal_id() -> Result<()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a chance we can reproduce this with a SLT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I poked around at doing this but I couldn't see an easy way to do it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same, tried a bunch of ways.

@neilconway neilconway left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm! Clean fix with a nicely scoped unit test.

There's a few places where we construct the grouping ID column via Expr::Column(Column::from_name(Aggregate::INTERNAL_GROUPING_ID)), which maybe would benefit from centralizing into a helper, but I think it's marginal.

@codecov-commenter

codecov-commenter commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.05%. Comparing base (24483db) to head (be22446).

Files with missing lines Patch % Lines
...tafusion/optimizer/src/common_subexpr_eliminate.rs 86.66% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24144      +/-   ##
==========================================
- Coverage   81.05%   81.05%   -0.01%     
==========================================
  Files        1106     1106              
  Lines      380556   380582      +26     
  Branches   380556   380582      +26     
==========================================
+ Hits       308477   308492      +15     
- Misses      53861    53866       +5     
- Partials    18218    18224       +6     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@notfilippo

notfilippo commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

lgtm! Clean fix with a nicely scoped unit test.

There's a few places where we construct the grouping ID column via Expr::Column(Column::from_name(Aggregate::INTERNAL_GROUPING_ID)), which maybe would benefit from centralizing into a helper, but I think it's marginal.

@neilconway I can follow up with a new issue and a new PR, I think it would definitely help a bit.

@neilconway

Copy link
Copy Markdown
Contributor

@neilconway I can follow up with a new issue and a new PR, I think it would definitely help a bit.

Sure, that would be great! Feel free to at-me on it.

@neilconway
neilconway added this pull request to the merge queue Aug 6, 2026

@gabotechs gabotechs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 6, 2026
@gabotechs
gabotechs added this pull request to the merge queue Aug 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 6, 2026
@neilconway
neilconway added this pull request to the merge queue Aug 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 6, 2026
@gabotechs
gabotechs enabled auto-merge August 6, 2026 16:40
@gabotechs
gabotechs disabled auto-merge August 7, 2026 07:05
@gabotechs
gabotechs enabled auto-merge August 7, 2026 07:05
@gabotechs
gabotechs added this pull request to the merge queue Aug 7, 2026
Merged via the queue into apache:main with commit e64e3f7 Aug 7, 2026
63 of 65 checks passed
@notfilippo
notfilippo deleted the filippo.rossi/preserve-grouping-id-during-aggregate-cse-wnmpwslywnzn branch August 7, 2026 08:07
notfilippo added a commit to DataDog/datafusion that referenced this pull request Aug 7, 2026
## 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#24143.

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

See apache#24143.

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

- Preserve `__grouping_id` in CSE recovery projections for grouping-set
aggregates.
- Add regression coverage.

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

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

Co-authored-by: Gabriel <45515538+gabotechs@users.noreply.github.com>
(cherry picked from commit e64e3f7)
notfilippo added a commit to DataDog/datafusion that referenced this pull request Aug 7, 2026
## 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#24143.

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

See apache#24143.

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

- Preserve `__grouping_id` in CSE recovery projections for grouping-set
aggregates.
- Add regression coverage.

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

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

Co-authored-by: Gabriel <45515538+gabotechs@users.noreply.github.com>
(cherry picked from commit e64e3f7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CommonSubexprEliminate drops __grouping_id from GROUPING SETS aggregates

4 participants