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: refactor pattern dir output memo related logic #52117

Merged
merged 3 commits into from
Mar 27, 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
2 changes: 2 additions & 0 deletions pkg/planner/cascades/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ go_library(
"//pkg/planner/core",
"//pkg/planner/implementation",
"//pkg/planner/memo",
"//pkg/planner/pattern",
"//pkg/planner/property",
"//pkg/planner/util",
"//pkg/types",
Expand Down Expand Up @@ -52,6 +53,7 @@ go_test(
"//pkg/parser/model",
"//pkg/planner/core",
"//pkg/planner/memo",
"//pkg/planner/pattern",
"//pkg/planner/property",
"//pkg/testkit/testdata",
"//pkg/testkit/testsetup",
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/cascades/enforcer_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/implementation"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/planner/property"
"github.com/pingcap/tidb/pkg/planner/util"
)
Expand All @@ -38,7 +39,7 @@ type Enforcer interface {
// GetEnforcerRules gets all candidate enforcer rules based
// on required physical property.
func GetEnforcerRules(g *memo.Group, prop *property.PhysicalProperty) (enforcers []Enforcer) {
if g.EngineType != memo.EngineTiDB {
if g.EngineType != pattern.EngineTiDB {
return
}
if !prop.IsSortItemEmpty() {
Expand Down
51 changes: 26 additions & 25 deletions pkg/planner/cascades/implementation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
plannercore "github.com/pingcap/tidb/pkg/planner/core"
impl "github.com/pingcap/tidb/pkg/planner/implementation"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/planner/property"
"github.com/pingcap/tidb/pkg/util/dbterror/plannererrors"
)
Expand All @@ -34,59 +35,59 @@ type ImplementationRule interface {
OnImplement(expr *memo.GroupExpr, reqProp *property.PhysicalProperty) ([]memo.Implementation, error)
}

var defaultImplementationMap = map[memo.Operand][]ImplementationRule{
memo.OperandTableDual: {
var defaultImplementationMap = map[pattern.Operand][]ImplementationRule{
pattern.OperandTableDual: {
&ImplTableDual{},
},
memo.OperandMemTableScan: {
pattern.OperandMemTableScan: {
&ImplMemTableScan{},
},
memo.OperandProjection: {
pattern.OperandProjection: {
&ImplProjection{},
},
memo.OperandTableScan: {
pattern.OperandTableScan: {
&ImplTableScan{},
},
memo.OperandIndexScan: {
pattern.OperandIndexScan: {
&ImplIndexScan{},
},
memo.OperandTiKVSingleGather: {
pattern.OperandTiKVSingleGather: {
&ImplTiKVSingleReadGather{},
},
memo.OperandShow: {
pattern.OperandShow: {
&ImplShow{},
},
memo.OperandSelection: {
pattern.OperandSelection: {
&ImplSelection{},
},
memo.OperandSort: {
pattern.OperandSort: {
&ImplSort{},
},
memo.OperandAggregation: {
pattern.OperandAggregation: {
&ImplHashAgg{},
},
memo.OperandLimit: {
pattern.OperandLimit: {
&ImplLimit{},
},
memo.OperandTopN: {
pattern.OperandTopN: {
&ImplTopN{},
&ImplTopNAsLimit{},
},
memo.OperandJoin: {
pattern.OperandJoin: {
&ImplHashJoinBuildLeft{},
&ImplHashJoinBuildRight{},
&ImplMergeJoin{},
},
memo.OperandUnionAll: {
pattern.OperandUnionAll: {
&ImplUnionAll{},
},
memo.OperandApply: {
pattern.OperandApply: {
&ImplApply{},
},
memo.OperandMaxOneRow: {
pattern.OperandMaxOneRow: {
&ImplMaxOneRow{},
},
memo.OperandWindow: {
pattern.OperandWindow: {
&ImplWindow{},
},
}
Expand Down Expand Up @@ -272,9 +273,9 @@ func (*ImplSelection) OnImplement(expr *memo.GroupExpr, reqProp *property.Physic
Conditions: logicalSel.Conditions,
}.Init(logicalSel.SCtx(), expr.Group.Prop.Stats.ScaleByExpectCnt(reqProp.ExpectedCnt), logicalSel.QueryBlockOffset(), reqProp.CloneEssentialFields())
switch expr.Group.EngineType {
case memo.EngineTiDB:
case pattern.EngineTiDB:
return []memo.Implementation{impl.NewTiDBSelectionImpl(physicalSel)}, nil
case memo.EngineTiKV:
case pattern.EngineTiKV:
return []memo.Implementation{impl.NewTiKVSelectionImpl(physicalSel)}, nil
default:
return nil, plannererrors.ErrInternal.GenWithStack("Unsupported EngineType '%s' for Selection.", expr.Group.EngineType.String())
Expand Down Expand Up @@ -333,9 +334,9 @@ func (*ImplHashAgg) OnImplement(expr *memo.GroupExpr, reqProp *property.Physical
)
hashAgg.SetSchema(expr.Group.Prop.Schema.Clone())
switch expr.Group.EngineType {
case memo.EngineTiDB:
case pattern.EngineTiDB:
return []memo.Implementation{impl.NewTiDBHashAggImpl(hashAgg)}, nil
case memo.EngineTiKV:
case pattern.EngineTiKV:
return []memo.Implementation{impl.NewTiKVHashAggImpl(hashAgg)}, nil
default:
return nil, plannererrors.ErrInternal.GenWithStack("Unsupported EngineType '%s' for HashAggregation.", expr.Group.EngineType.String())
Expand Down Expand Up @@ -372,7 +373,7 @@ type ImplTopN struct {
// Match implements ImplementationRule Match interface.
func (*ImplTopN) Match(expr *memo.GroupExpr, prop *property.PhysicalProperty) (matched bool) {
topN := expr.ExprNode.(*plannercore.LogicalTopN)
if expr.Group.EngineType != memo.EngineTiDB {
if expr.Group.EngineType != pattern.EngineTiDB {
return prop.IsSortItemEmpty()
}
return plannercore.MatchItems(prop, topN.ByItems)
Expand All @@ -388,9 +389,9 @@ func (*ImplTopN) OnImplement(expr *memo.GroupExpr, _ *property.PhysicalProperty)
Offset: lt.Offset,
}.Init(lt.SCtx(), expr.Group.Prop.Stats, lt.QueryBlockOffset(), resultProp)
switch expr.Group.EngineType {
case memo.EngineTiDB:
case pattern.EngineTiDB:
return []memo.Implementation{impl.NewTiDBTopNImpl(topN)}, nil
case memo.EngineTiKV:
case pattern.EngineTiKV:
return []memo.Implementation{impl.NewTiKVTopNImpl(topN)}, nil
default:
return nil, plannererrors.ErrInternal.GenWithStack("Unsupported EngineType '%s' for TopN.", expr.Group.EngineType.String())
Expand Down
9 changes: 5 additions & 4 deletions pkg/planner/cascades/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/pkg/expression"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/planner/property"
"github.com/pingcap/tidb/pkg/util/dbterror/plannererrors"
)
Expand All @@ -32,7 +33,7 @@ var DefaultOptimizer = NewOptimizer()
// Optimizer is the struct for cascades optimizer.
type Optimizer struct {
transformationRuleBatches []TransformationRuleBatch
implementationRuleMap map[memo.Operand][]ImplementationRule
implementationRuleMap map[pattern.Operand][]ImplementationRule
}

// NewOptimizer returns a cascades optimizer with default transformation
Expand All @@ -51,15 +52,15 @@ func (opt *Optimizer) ResetTransformationRules(ruleBatches ...TransformationRule
}

// ResetImplementationRules resets the implementationRuleMap of the optimizer, and returns the optimizer.
func (opt *Optimizer) ResetImplementationRules(rules map[memo.Operand][]ImplementationRule) *Optimizer {
func (opt *Optimizer) ResetImplementationRules(rules map[pattern.Operand][]ImplementationRule) *Optimizer {
opt.implementationRuleMap = rules
return opt
}

// GetImplementationRules gets all the candidate implementation rules of the optimizer
// for the logical plan node.
func (opt *Optimizer) GetImplementationRules(node plannercore.LogicalPlan) []ImplementationRule {
return opt.implementationRuleMap[memo.GetOperand(node)]
return opt.implementationRuleMap[pattern.GetOperand(node)]
}

// FindBestPlan is the optimization entrance of the cascades planner. The
Expand Down Expand Up @@ -172,7 +173,7 @@ func (opt *Optimizer) exploreGroup(g *memo.Group, round int, ruleBatch Transform
// findMoreEquiv finds and applies the matched transformation rules.
func (*Optimizer) findMoreEquiv(g *memo.Group, elem *list.Element, round int, ruleBatch TransformationRuleBatch) (eraseCur bool, err error) {
expr := elem.Value.(*memo.GroupExpr)
operand := memo.GetOperand(expr.ExprNode)
operand := pattern.GetOperand(expr.ExprNode)
for _, rule := range ruleBatch[operand] {
pattern := rule.GetPattern()
if !pattern.Operand.Match(operand) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/planner/cascades/optimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/pkg/parser/model"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/planner/property"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -116,8 +117,8 @@ func TestPreparePossibleProperties(t *testing.T) {
domain.GetDomain(ctx).MockInfoCacheAndLoadInfoSchema(is)
optimizer := NewOptimizer()

optimizer.ResetTransformationRules(map[memo.Operand][]Transformation{
memo.OperandDataSource: {
optimizer.ResetTransformationRules(map[pattern.Operand][]Transformation{
pattern.OperandDataSource: {
NewRuleEnumeratePaths(),
},
})
Expand Down Expand Up @@ -212,9 +213,9 @@ func TestAppliedRuleSet(t *testing.T) {
optimizer := NewOptimizer()

rule := fakeTransformation{}
rule.pattern = memo.NewPattern(memo.OperandProjection, memo.EngineAll)
optimizer.ResetTransformationRules(map[memo.Operand][]Transformation{
memo.OperandProjection: {
rule.pattern = pattern.NewPattern(pattern.OperandProjection, pattern.EngineAll)
optimizer.ResetTransformationRules(map[pattern.Operand][]Transformation{
pattern.OperandProjection: {
&rule,
},
})
Expand Down
7 changes: 4 additions & 3 deletions pkg/planner/cascades/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ import (
"github.com/pingcap/tidb/pkg/parser/model"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/testkit/testdata"
"github.com/stretchr/testify/require"
)

func TestGroupStringer(t *testing.T) {
optimizer := NewOptimizer()
optimizer.ResetTransformationRules(map[memo.Operand][]Transformation{
memo.OperandSelection: {
optimizer.ResetTransformationRules(map[pattern.Operand][]Transformation{
pattern.OperandSelection: {
NewRulePushSelDownTiKVSingleGather(),
NewRulePushSelDownTableScan(),
},
memo.OperandDataSource: {
pattern.OperandDataSource: {
NewRuleEnumeratePaths(),
},
})
Expand Down
Loading