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

*: refactor limit hint #50730

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pkg/planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2679,10 +2679,10 @@ func pushLimitOrTopNForcibly(p LogicalPlan) bool {
var preferPushDown *bool
switch lp := p.(type) {
case *LogicalTopN:
preferPushDown = &lp.limitHints.PreferLimitToCop
preferPushDown = &lp.PreferLimitToCop
meetThreshold = lp.Count+lp.Offset <= uint64(lp.SCtx().GetSessionVars().LimitPushDownThreshold)
case *LogicalLimit:
preferPushDown = &lp.limitHints.PreferLimitToCop
preferPushDown = &lp.PreferLimitToCop
meetThreshold = true // always push Limit down in this case since it has no side effect
default:
return false
Expand Down
8 changes: 4 additions & 4 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2435,7 +2435,7 @@ func (b *PlanBuilder) buildLimit(src LogicalPlan, limit *ast.Limit) (LogicalPlan
Count: count,
}.Init(b.ctx, b.getSelectOffset())
if hint := b.TableHints(); hint != nil {
li.limitHints = hint.Limit
li.PreferLimitToCop = hint.PreferLimitToCop
}
li.SetChildren(src)
return li, nil
Expand Down Expand Up @@ -3966,7 +3966,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
tiflashTables, tikvTables []h.HintedTable
aggHints h.AggHints
timeRangeHint ast.HintTimeRange
limitHints h.LimitHints
preferLimitToCop bool
cteMerge bool
leadingJoinOrder []h.HintedTable
hjBuildTables, hjProbeTables []h.HintedTable
Expand Down Expand Up @@ -4076,7 +4076,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
case h.HintTimeRange:
timeRangeHint = hint.HintData.(ast.HintTimeRange)
case h.HintLimitToCop:
limitHints.PreferLimitToCop = true
preferLimitToCop = true
case h.HintMerge:
if hint.Tables != nil {
b.ctx.GetSessionVars().StmtCtx.SetHintWarning(ErrInternal.FastGen("The MERGE hint is not used correctly, maybe it inputs a table name."))
Expand Down Expand Up @@ -4128,7 +4128,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, currentLev
Agg: aggHints,
IndexMergeHintList: indexMergeHintList,
TimeRangeHint: timeRangeHint,
Limit: limitHints,
PreferLimitToCop: preferLimitToCop,
CTEMerge: cteMerge,
LeadingJoinOrder: leadingJoinOrder,
HJBuild: hjBuildTables,
Expand Down
18 changes: 9 additions & 9 deletions pkg/planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1917,10 +1917,10 @@ type LogicalTopN struct {

ByItems []*util.ByItems
// PartitionBy is used for extended TopN to consider K heaps. Used by rule_derive_topn_from_window
PartitionBy []property.SortItem // This is used for enhanced topN optimization
Offset uint64
Count uint64
limitHints h.LimitHints
PartitionBy []property.SortItem // This is used for enhanced topN optimization
Offset uint64
Count uint64
PreferLimitToCop bool
}

// GetPartitionBy returns partition by fields
Expand All @@ -1946,11 +1946,11 @@ func (lt *LogicalTopN) isLimit() bool {
type LogicalLimit struct {
logicalSchemaProducer

PartitionBy []property.SortItem // This is used for enhanced topN optimization
Offset uint64
Count uint64
limitHints h.LimitHints
IsPartial bool
PartitionBy []property.SortItem // This is used for enhanced topN optimization
Offset uint64
Count uint64
PreferLimitToCop bool
IsPartial bool
}

// GetPartitionBy returns partition by fields
Expand Down
18 changes: 9 additions & 9 deletions pkg/planner/core/rule_topn_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func (lt *LogicalTopN) setChild(p LogicalPlan, opt *logicalOptimizeOp) LogicalPl

if lt.isLimit() {
limit := LogicalLimit{
Count: lt.Count,
Offset: lt.Offset,
limitHints: lt.limitHints,
PartitionBy: lt.GetPartitionBy(),
Count: lt.Count,
Offset: lt.Offset,
PreferLimitToCop: lt.PreferLimitToCop,
PartitionBy: lt.GetPartitionBy(),
}.Init(lt.SCtx(), lt.QueryBlockOffset())
limit.SetChildren(p)
appendTopNPushDownTraceStep(limit, p, opt)
Expand All @@ -94,7 +94,7 @@ func (ls *LogicalSort) pushDownTopN(topN *LogicalTopN, opt *logicalOptimizeOp) L
}

func (p *LogicalLimit) convertToTopN(opt *logicalOptimizeOp) *LogicalTopN {
topn := LogicalTopN{Offset: p.Offset, Count: p.Count, limitHints: p.limitHints}.Init(p.SCtx(), p.QueryBlockOffset())
topn := LogicalTopN{Offset: p.Offset, Count: p.Count, PreferLimitToCop: p.PreferLimitToCop}.Init(p.SCtx(), p.QueryBlockOffset())
appendConvertTopNTraceStep(p, topn, opt)
return topn
}
Expand All @@ -111,7 +111,7 @@ func (p *LogicalUnionAll) pushDownTopN(topN *LogicalTopN, opt *logicalOptimizeOp
for i, child := range p.children {
var newTopN *LogicalTopN
if topN != nil {
newTopN = LogicalTopN{Count: topN.Count + topN.Offset, limitHints: topN.limitHints}.Init(p.SCtx(), topN.QueryBlockOffset())
newTopN = LogicalTopN{Count: topN.Count + topN.Offset, PreferLimitToCop: topN.PreferLimitToCop}.Init(p.SCtx(), topN.QueryBlockOffset())
for _, by := range topN.ByItems {
newTopN.ByItems = append(newTopN.ByItems, &util.ByItems{Expr: by.Expr, Desc: by.Desc})
}
Expand Down Expand Up @@ -187,9 +187,9 @@ func (p *LogicalJoin) pushDownTopNToChild(topN *LogicalTopN, idx int, opt *logic
}

newTopN := LogicalTopN{
Count: topN.Count + topN.Offset,
ByItems: make([]*util.ByItems, len(topN.ByItems)),
limitHints: topN.limitHints,
Count: topN.Count + topN.Offset,
ByItems: make([]*util.ByItems, len(topN.ByItems)),
PreferLimitToCop: topN.PreferLimitToCop,
}.Init(topN.SCtx(), topN.QueryBlockOffset())
for i := range topN.ByItems {
newTopN.ByItems[i] = topN.ByItems[i].Clone()
Expand Down
13 changes: 4 additions & 9 deletions pkg/util/hint/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,10 @@ type PlanHints struct {
HJProbe []HintedTable // hash_join_probe

// Hints belows are not associated with any particular table.
Agg AggHints // hash_agg, merge_agg, agg_to_cop
Limit LimitHints // limit_to_cop
CTEMerge bool // merge
TimeRangeHint ast.HintTimeRange
}

// LimitHints stores limit hint information.
type LimitHints struct {
PreferLimitToCop bool
Agg AggHints // hash_agg, merge_agg, agg_to_cop
PreferLimitToCop bool // limit_to_cop
CTEMerge bool // merge
TimeRangeHint ast.HintTimeRange
}

// HintedTable indicates which table this hint should take effect on.
Expand Down