planner: preserve rollup alias grouping positions#68435
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughTracks the SELECT-field origin of each GROUP BY item through resolver and rollup expansion, preserves/restores GROUP BY positions when required for WITH ROLLUP, and performs field-index-aware grouping-set substitution; includes a cascades regression test covering explicit, positional, and mixed GROUP BY variants. ChangesWITH ROLLUP grouping key deduplication and position tracking
Sequence DiagramsequenceDiagram
participant Client
participant Planner as PlanBuilder.resolveGbyExprs
participant BuildExpand as PlanBuilder.buildExpand
participant LogicalExpand
participant Projection
Client->>Planner: parse SELECT with GROUP BY (incl. aliases/positions)
Planner->>BuildExpand: resolved gbyExprs + sourceFieldIndices
BuildExpand->>LogicalExpand: deduped/ordered gbyExprs + GbyItemSourceFieldIndices
Projection->>LogicalExpand: TrySubstituteExprWithGroupingSetColByFieldIndex(fieldIndex)
LogicalExpand-->>Projection: substituted grouping-set column expr
Projection-->>Client: final projected rows (ROLLUP nullability/layout applied)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/planner/core/logical_plan_builder.go`:
- Around line 153-157: The current logic uses the single boolean
keepGbyItemPositions (from needPreserveRollupGbyItemPositions) to skip
deduplication for the entire ROLLUP list; change this to compute and use a
set/list of specific positions to preserve (e.g., preservePositions or
preserveIndexMask) so only alias/ordinal-backed duplicate positions are kept and
the rest of gbyItems can be deduplicated. Update the code paths that call
expression.DeduplicateGbyExpression and deriveDeduplicatedGbySourceFieldIndices
(including the similar blocks around the other mentioned regions) to: 1) compute
deduplicated expandGbyExprs/gbyExprsRefPos by skipping only the preserved
indices, 2) derive expandGbySourceFieldIndices for the deduplicated result using
deriveDeduplicatedGbySourceFieldIndices with the preserved-index mapping, and 3)
ensure buildExpand logic uses the preserved positions to reinsert aliases while
still allowing ordinary repeated items to be removed by the deduplication
routine.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 725da1e3-21fd-4d92-bc6d-4d5c1b5098db
📒 Files selected for processing (3)
pkg/planner/core/issuetest/planner_issue_test.gopkg/planner/core/logical_plan_builder.gopkg/planner/core/operator/logicalop/logical_expand.go
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #68435 +/- ##
================================================
- Coverage 77.2762% 76.4886% -0.7877%
================================================
Files 2010 1992 -18
Lines 555477 557697 +2220
================================================
- Hits 429252 426575 -2677
- Misses 125305 131077 +5772
+ Partials 920 45 -875
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
/test check-dev |
|
@hawkingrei: The specified target(s) for Use DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
What problem does this PR solve?
Issue Number: close #65965
Problem Summary:
GROUP BY ... WITH ROLLUPcould return duplicate visible rows when aGROUP BYitem was a SELECT alias or ordinal that repeated an earlier grouping expression. For example,SELECT a, b, a AS d, SUM(c) FROM t1 GROUP BY a, b, d WITH ROLLUPreused the same projected grouping column for bothaandd, so the{a,b,d}and{a,b}rollup levels both rendereddas non-NULL.What changed and how does it work?
This PR preserves original
GROUP BYitem positions in theExpandplan only when a repeated grouping expression is introduced through a SELECT alias or ordinal. That lets different rollup levels null the repeated visible positions independently.The existing deduplicated grouping-expression path is kept for ordinary repeated grouping expressions, avoiding broad planner behavior churn.
Check List
Tests
Validation:
go test ./pkg/planner/core/issuetest -run TestPlannerIssueRegressions -count=1 -tags=intest,deadlock ./tools/check/failpoint-go-test.sh pkg/planner/core/casetest/physicalplantest -run TestExplainExpand -count=1 git diff --check make lintManual replay covered the issue query,
GROUP BY 1, 2, 3 WITH ROLLUP, ordinary repeated grouping keys, and warning checks.make bazel_preparewas not required because this PR only changes existing Go files and extends an existing top-level Go test function. It does not add, remove, move, or rename Go files, add a new top-levelTestXxx, or touch Bazel/go.mod/go.sum files.Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.
Summary by CodeRabbit
Bug Fixes
Tests