Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix an unexpected result of the inlineprojection of TopN #58771

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/planner/core/operator/logicalop/logical_top_n.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (lt *LogicalTopN) PruneColumns(parentUsedCols []*expression.Column, opt *op
child := lt.Children()[0]
var cols []*expression.Column

lt.InlineProjection(parentUsedCols, opt)
snapParentUsedCols := make([]*expression.Column, 0)
EricZequan marked this conversation as resolved.
Show resolved Hide resolved
snapParentUsedCols = append(snapParentUsedCols, parentUsedCols...)

lt.ByItems, cols = pruneByItems(lt, lt.ByItems, opt)
parentUsedCols = append(parentUsedCols, cols...)
Expand All @@ -92,6 +93,15 @@ func (lt *LogicalTopN) PruneColumns(parentUsedCols []*expression.Column, opt *op
if err != nil {
return nil, err
}
// If the length of parentUsedCols is 0, it means that the parent plan does not need this plan to output related
// results, such as: select count(*) from t
// So we set the schema of topN to 0. After inlineprojection, the schema of topN will be set to the shortest column
// in its child plan, and this column will not be used later.
if len(snapParentUsedCols) == 0 {
lt.SetSchema(nil)
}
lt.InlineProjection(snapParentUsedCols, opt)
EricZequan marked this conversation as resolved.
Show resolved Hide resolved

return lt, nil
}

Expand Down