Include OFFSET in physical optimizer group-trim limit pushdown#18600
Merged
yashmayya merged 1 commit intoMay 28, 2026
Conversation
The group-trim rule pushed limit=fetch and dropped the offset, so paginated DISTINCT/GROUP BY queries on the physical optimizer (usePhysicalOptimizer=true) came back short. Push offset+fetch instead so the leaf/final aggregate keeps enough groups to cover the OFFSET window. The legacy path is unaffected since its sort-exchange-copy rule already folds the offset before the aggregate rule. Fixes apache#18592
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #18600 +/- ##
============================================
- Coverage 64.31% 64.26% -0.05%
Complexity 1137 1137
============================================
Files 3335 3335
Lines 205710 206046 +336
Branches 32085 32142 +57
============================================
+ Hits 132295 132415 +120
- Misses 62774 62976 +202
- Partials 10641 10655 +14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Jackie-Jiang
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On the physical optimizer path (
usePhysicalOptimizer=true), the group-trim rule pushedlimit = fetchinto the leaf/final aggregate and ignored the queryOFFSET. PaginatedDISTINCT/GROUP BYqueries that hit group trim then trimmed away rows the outerOFFSET ... FETCHwindow still needed and came back short — silently wrong results, no error. It now pushesoffset + fetch(clamped to avoid int overflow) so the leaf/final aggregate keeps enough groups.The default/legacy planner isn't affected: its sort-exchange-copy rule already folds the offset into the inner sort's fetch before the aggregate rule runs, so that rule sees
offset + fetchthere.PINOT_POST_RULES_V2has no sort-exchange rule, soPinotLogicalAggregateRulematches the raw outer sort and has to add the offset itself.Tests
PhysicalOptimizerPlans.json): no-aggregateDISTINCT ... LIMIT n OFFSET mand hinted aggregate, asserting leaf/finallimit=[offset+fetch].GroupByOptionsTest,usePhysicalOptimizer=true): paginated DISTINCT and hinted-aggregate paths return a full page. Verified it fails (0 rows) without the fix.Fixes #18592