From 104a215f07c1f44cf2b9b7017dbd4a4244aa8690 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 12 Oct 2018 15:37:35 +0800 Subject: [PATCH 1/6] plan: implement Operand and Pattern of cascades planner. --- planner/cascades/pattern.go | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 planner/cascades/pattern.go diff --git a/planner/cascades/pattern.go b/planner/cascades/pattern.go new file mode 100644 index 0000000000000..b6391f6226394 --- /dev/null +++ b/planner/cascades/pattern.go @@ -0,0 +1,106 @@ +// Copyright 2018 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package cascades + +import ( + plannercore "github.com/pingcap/tidb/planner/core" +) + +// Operand is the node of a pattern tree, it represents a logical expression operator. +// Different from logical plan operator which holds the full information about an expression +// operator, Operand only stores the type information. +// An Operand may correspond to a concrete logical plan operator, or it can has special meaning, +// e.g, a placeholder for any logical plan operator. +type Operand int + +const ( + OperandAny Operand = iota + OperandJoin + OperandAggregation + OperandProjection + OperandSelection + OperandApply + OperandMaxOneRow + OperandTableDual + OperandDataSource + OperandUnionScan + OperandUnionAll + OperandSort + OperandTopN + OperandLock + OperandLimit + OperandUnsupported +) + +// GetOperand maps logical plan operator to Operand. +func GetOperand(p plannercore.LogicalPlan) (Operand, error) { + switch x := p.(type) { + case *plannercore.LogicalJoin: + return OperandJoin, nil + case *plannercore.LogicalAggregation: + return OperandAggregation, nil + case *plannercore.LogicalProjection: + return OperandProjection, nil + case *plannercore.LogicalSelection: + return OperandSelection, nil + case *plannercore.LogicalApply: + return OperandApply, nil + case *plannercore.LogicalMaxOneRow: + return OperandMaxOneRow, nil + case *plannercore.LogicalTableDual: + return OperandTableDual, nil + case *plannercore.DataSource: + return OperandDataSource, nil + case *plannercore.LogicalUnionScan: + return OperandUnionScan, nil + case *plannercore.LogicalUnionAll: + return OperandUnionAll, nil + case *plannercore.LogicalSort: + return OperandSort, nil + case *plannercore.LogicalTopN: + return OperandTopN, nil + case *plannercore.LogicalLock: + return OperandLock, nil + case *plannercore.LogicalLimit: + return OperandLimit, nil + } + return OperandUnsupported, plannercore.ErrUnsupportedType.GenWithStack("Unsupported LogicalPlan(%T) for GetOperand", p) +} + +// match checks if current Operand matches specified one. +func (o Operand) match(t Operand) bool { + if o == OperandAny || t == OperandAny { + return true + } + if o == t { + return true + } + return false +} + +// Pattern defines the match pattern for a rule. +// It describes a piece of logical expression. +// It's a tree-like structure and each node in the tree is an Operand. +type Pattern struct { + operand Operand + children []*Pattern +} + +// BuildPattern builds a Pattern from Operand and child Patterns. +// Used in GetPattern() of Transformation interface to generate a Pattern. +func BuildPattern(operand Operand, children ...*Pattern) *Pattern { + p := &Pattern{operand: operand} + p.children = children + return p +} From 21d7a7b49f3b8acd2c7b00e844e567e360e3f348 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 12 Oct 2018 15:51:23 +0800 Subject: [PATCH 2/6] shutup compiler --- planner/cascades/pattern.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/cascades/pattern.go b/planner/cascades/pattern.go index b6391f6226394..f815d320b0d80 100644 --- a/planner/cascades/pattern.go +++ b/planner/cascades/pattern.go @@ -75,7 +75,7 @@ func GetOperand(p plannercore.LogicalPlan) (Operand, error) { case *plannercore.LogicalLimit: return OperandLimit, nil } - return OperandUnsupported, plannercore.ErrUnsupportedType.GenWithStack("Unsupported LogicalPlan(%T) for GetOperand", p) + return OperandUnsupported, plannercore.ErrUnsupportedType.GenWithStack("Unsupported LogicalPlan(%T) for GetOperand", x) } // match checks if current Operand matches specified one. From 4f57a887e5cde9a62b254b4e44ea8063b6c03596 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 12 Oct 2018 16:15:03 +0800 Subject: [PATCH 3/6] shutup compiler --- planner/cascades/pattern.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/planner/cascades/pattern.go b/planner/cascades/pattern.go index f815d320b0d80..a51753ee3115b 100644 --- a/planner/cascades/pattern.go +++ b/planner/cascades/pattern.go @@ -45,7 +45,8 @@ const ( // GetOperand maps logical plan operator to Operand. func GetOperand(p plannercore.LogicalPlan) (Operand, error) { - switch x := p.(type) { + x := p.(type) + switch x { case *plannercore.LogicalJoin: return OperandJoin, nil case *plannercore.LogicalAggregation: From 67cff09179b08edd80618978ce79d3bd08d4f9f2 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 12 Oct 2018 16:35:59 +0800 Subject: [PATCH 4/6] shutup compiler !!! --- planner/cascades/pattern.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/planner/cascades/pattern.go b/planner/cascades/pattern.go index a51753ee3115b..9b69630cf300d 100644 --- a/planner/cascades/pattern.go +++ b/planner/cascades/pattern.go @@ -45,8 +45,7 @@ const ( // GetOperand maps logical plan operator to Operand. func GetOperand(p plannercore.LogicalPlan) (Operand, error) { - x := p.(type) - switch x { + switch x := p.(type) { case *plannercore.LogicalJoin: return OperandJoin, nil case *plannercore.LogicalAggregation: @@ -75,8 +74,9 @@ func GetOperand(p plannercore.LogicalPlan) (Operand, error) { return OperandLock, nil case *plannercore.LogicalLimit: return OperandLimit, nil + default: + return OperandUnsupported, plannercore.ErrUnsupportedType.GenWithStack("Unsupported LogicalPlan(%T) for GetOperand", x) } - return OperandUnsupported, plannercore.ErrUnsupportedType.GenWithStack("Unsupported LogicalPlan(%T) for GetOperand", x) } // match checks if current Operand matches specified one. From e794fcee3d33b6031eba7ae6fd6a040e07eea918 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 12 Oct 2018 16:50:59 +0800 Subject: [PATCH 5/6] add comments for exported variables. --- planner/cascades/pattern.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/planner/cascades/pattern.go b/planner/cascades/pattern.go index 9b69630cf300d..ce4bcbf24dbd2 100644 --- a/planner/cascades/pattern.go +++ b/planner/cascades/pattern.go @@ -25,21 +25,37 @@ import ( type Operand int const ( + // Placeholder for any Operand. OperandAny Operand = iota + // Operand for LogicalJoin. OperandJoin + // Operand for LogicalAggregation. OperandAggregation + // Operand for LogicalProjection. OperandProjection + // Operand for LogicalSelection. OperandSelection + // Operand for LogicalApply. OperandApply + // Operand for LogicalMaxOneRow. OperandMaxOneRow + // Operand for LogicalTableDual. OperandTableDual + // Operand for DataSource. OperandDataSource + // Operand for LogicalUnionScan. OperandUnionScan + // Operand for LogicalUnionAll. OperandUnionAll + // Operand for LogicalSort. OperandSort + // Operand for LogicalTopN. OperandTopN + // Operand for LogicalLock. OperandLock + // Operand for LogicalLimit. OperandLimit + // Upper bound of defined Operand. OperandUnsupported ) From 6993ac8af757c075f888cea2279cc888a736015b Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 12 Oct 2018 17:01:35 +0800 Subject: [PATCH 6/6] adjust comments for exported variable --- planner/cascades/pattern.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/planner/cascades/pattern.go b/planner/cascades/pattern.go index ce4bcbf24dbd2..82d703853a786 100644 --- a/planner/cascades/pattern.go +++ b/planner/cascades/pattern.go @@ -25,37 +25,37 @@ import ( type Operand int const ( - // Placeholder for any Operand. + // OperandAny is a placeholder for any Operand. OperandAny Operand = iota - // Operand for LogicalJoin. + // OperandJoin for LogicalJoin. OperandJoin - // Operand for LogicalAggregation. + // OperandAggregation for LogicalAggregation. OperandAggregation - // Operand for LogicalProjection. + // OperandProjection for LogicalProjection. OperandProjection - // Operand for LogicalSelection. + // OperandSelection for LogicalSelection. OperandSelection - // Operand for LogicalApply. + // OperandApply for LogicalApply. OperandApply - // Operand for LogicalMaxOneRow. + // OperandMaxOneRow for LogicalMaxOneRow. OperandMaxOneRow - // Operand for LogicalTableDual. + // OperandTableDual for LogicalTableDual. OperandTableDual - // Operand for DataSource. + // OperandDataSource for DataSource. OperandDataSource - // Operand for LogicalUnionScan. + // OperandUnionScan for LogicalUnionScan. OperandUnionScan - // Operand for LogicalUnionAll. + // OperandUnionAll for LogicalUnionAll. OperandUnionAll - // Operand for LogicalSort. + // OperandSort for LogicalSort. OperandSort - // Operand for LogicalTopN. + // OperandTopN for LogicalTopN. OperandTopN - // Operand for LogicalLock. + // OperandLock for LogicalLock. OperandLock - // Operand for LogicalLimit. + // OperandLimit for LogicalLimit. OperandLimit - // Upper bound of defined Operand. + // OperandUnsupported is upper bound of defined Operand yet. OperandUnsupported )