[BugFix] Preserve TopN projection when eliminating sort columns with equality predicates - #78021
Merged
stephen-shelby merged 1 commit intoAug 19, 2026
Conversation
…equality predicates
EliminateSortColumnWithEqualityPredicateRule drops the projection carried
by the TopN operator when it eliminates the sort because every ORDER BY
column is pinned by an equality predicate. When
HoistHeavyCostExprsUponTopnRule has hoisted a heavy decimal128/largeint
divide above the TopN (later merged into the TopN operator as its
projection), the hoisted expression loses its definition and plan
translation fails with:
Cannot convert ColumnRefOperator to Expr, please check the input
expression: N: expr
Merge the TopN's projection into the scan's projection before eliminating
the sort. This is a partial cherry-pick of StarRocks#58345, which fixed this spot
on main but was never backported to branch-3.5.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Smith Cruise <chendingchao1@126.com>
stephen-shelby
approved these changes
Aug 19, 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.
Why I'm doing:
On branch-3.5 , a query combining a
decimal128/largeintdivide in the select list withORDER BY <column pinned by an equality predicate> LIMIT Nfails at planning time:Minimal repro:
Root cause — an interaction of two rules:
HoistHeavyCostExprsUponTopnRule([Enhancement] Hoist heavy-cost(decimal divide) upon top-n #55417) hoists the heavy divide into a project above the TopN so it is only evaluated for the top N rows;MergeProjectWithChildRulelater merges that project into the TopN operator as itsprojection.EliminateSortColumnWithEqualityPredicateRule([Enhancement] Eliminate sort column when all predicates of the column are equivalent predicates #54177) then eliminates the TopN because every ORDER BY column is pinned by an equality predicate — but it drops the TopN'sprojectionon the floor. The hoisted divide loses its only definition, and plan translation fails when it finds the column mapped to itself with no child producing it.main is not affected: #58345 added the "merge the TopN's projection into the scan's projection" logic to this rule. That PR was backported to branch-3.5-cc (#70879) but never to branch-3.5, and the later #75581 backport of #74983 only carried the global-limit fix (the projection-merge lines were context in that diff, not part of it).
Workaround for affected versions:
SET_VAR(cbo_disabled_rules='TF_HOIST_HEAVY_COST_UPON_TOPN').What I'm doing:
EliminateSortColumnWithEqualityPredicateRulehunk from [Enhancement] support defer project after top n #58345 (without the DeferProjectAfterTopN feature itself): before eliminating the sort, merge the TopN's projection into the scan's projection, only adding entries the scan does not already define.Fixes #issue
What type of PR is this:
Does this PR entail a change in behavior?
If yes, please specify the type of change:
Checklist:
Bugfix cherry-pick branch check:
🤖 Generated with Claude Code