github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/causet/memo/pattern.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package memo 15 16 import ( 17 causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded" 18 ) 19 20 // Operand is the node of a pattern tree, it represents a logical memex operator. 21 // Different from logical plan operator which holds the full information about an memex 22 // operator, Operand only stores the type information. 23 // An Operand may correspond to a concrete logical plan operator, or it can has special meaning, 24 // e.g, a placeholder for any logical plan operator. 25 type Operand int 26 27 const ( 28 // OperandAny is a placeholder for any Operand. 29 OperandAny Operand = iota 30 // OperandJoin is the operand for LogicalJoin. 31 OperandJoin 32 // OperanPosetDaggregation is the operand for LogicalAggregation. 33 OperanPosetDaggregation 34 // OperandProjection is the operand for LogicalProjection. 35 OperandProjection 36 // OperandSelection is the operand for LogicalSelection. 37 OperandSelection 38 // OperandApply is the operand for LogicalApply. 39 OperandApply 40 // OperandMaxOneRow is the operand for LogicalMaxOneRow. 41 OperandMaxOneRow 42 // OperandBlockDual is the operand for LogicalBlockDual. 43 OperandBlockDual 44 // OperandDataSource is the operand for DataSource. 45 OperandDataSource 46 // OperandUnionScan is the operand for LogicalUnionScan. 47 OperandUnionScan 48 // OperandUnionAll is the operand for LogicalUnionAll. 49 OperandUnionAll 50 // OperandSort is the operand for LogicalSort. 51 OperandSort 52 // OperandTopN is the operand for LogicalTopN. 53 OperandTopN 54 // OperandLock is the operand for LogicalLock. 55 OperandLock 56 // OperandLimit is the operand for LogicalLimit. 57 OperandLimit 58 // OperandEinsteinDBSingleGather is the operand for EinsteinDBSingleGather. 59 OperandEinsteinDBSingleGather 60 // OperandMemBlockScan is the operand for MemBlockScan. 61 OperandMemBlockScan 62 // OperandBlockScan is the operand for BlockScan. 63 OperandBlockScan 64 // OperandIndexScan is the operand for IndexScan. 65 OperandIndexScan 66 // OperandShow is the operand for Show. 67 OperandShow 68 // OperandWindow is the operand for window function. 69 OperandWindow 70 // OperandUnsupported is the operand for unsupported operators. 71 OperandUnsupported 72 ) 73 74 // GetOperand maps logical plan operator to Operand. 75 func GetOperand(p causetembedded.LogicalCauset) Operand { 76 switch p.(type) { 77 case *causetembedded.LogicalApply: 78 return OperandApply 79 case *causetembedded.LogicalJoin: 80 return OperandJoin 81 case *causetembedded.LogicalAggregation: 82 return OperanPosetDaggregation 83 case *causetembedded.LogicalProjection: 84 return OperandProjection 85 case *causetembedded.LogicalSelection: 86 return OperandSelection 87 case *causetembedded.LogicalMaxOneRow: 88 return OperandMaxOneRow 89 case *causetembedded.LogicalBlockDual: 90 return OperandBlockDual 91 case *causetembedded.DataSource: 92 return OperandDataSource 93 case *causetembedded.LogicalUnionScan: 94 return OperandUnionScan 95 case *causetembedded.LogicalUnionAll: 96 return OperandUnionAll 97 case *causetembedded.LogicalSort: 98 return OperandSort 99 case *causetembedded.LogicalTopN: 100 return OperandTopN 101 case *causetembedded.LogicalLock: 102 return OperandLock 103 case *causetembedded.LogicalLimit: 104 return OperandLimit 105 case *causetembedded.EinsteinDBSingleGather: 106 return OperandEinsteinDBSingleGather 107 case *causetembedded.LogicalBlockScan: 108 return OperandBlockScan 109 case *causetembedded.LogicalMemBlock: 110 return OperandMemBlockScan 111 case *causetembedded.LogicalIndexScan: 112 return OperandIndexScan 113 case *causetembedded.LogicalShow: 114 return OperandShow 115 case *causetembedded.LogicalWindow: 116 return OperandWindow 117 default: 118 return OperandUnsupported 119 } 120 } 121 122 // Match checks if current Operand matches specified one. 123 func (o Operand) Match(t Operand) bool { 124 if o == OperandAny || t == OperandAny { 125 return true 126 } 127 if o == t { 128 return true 129 } 130 return false 131 } 132 133 // Pattern defines the match pattern for a rule. It's a tree-like structure 134 // which is a piece of a logical memex. Each node in the Pattern tree is 135 // defined by an Operand and EngineType pair. 136 type Pattern struct { 137 Operand 138 EngineTypeSet 139 Children []*Pattern 140 } 141 142 // Match checks whether the EngineTypeSet contains the given EngineType 143 // and whether the two Operands match. 144 func (p *Pattern) Match(o Operand, e EngineType) bool { 145 return p.EngineTypeSet.Contains(e) && p.Operand.Match(o) 146 } 147 148 // MatchOperandAny checks whether the pattern's Operand is OperandAny 149 // and the EngineTypeSet contains the given EngineType. 150 func (p *Pattern) MatchOperandAny(e EngineType) bool { 151 return p.EngineTypeSet.Contains(e) && p.Operand == OperandAny 152 } 153 154 // NewPattern creates a pattern node according to the Operand and EngineType. 155 func NewPattern(operand Operand, engineTypeSet EngineTypeSet) *Pattern { 156 return &Pattern{Operand: operand, EngineTypeSet: engineTypeSet} 157 } 158 159 // SetChildren sets the Children information for a pattern node. 160 func (p *Pattern) SetChildren(children ...*Pattern) { 161 p.Children = children 162 } 163 164 // BuildPattern builds a Pattern from Operand, EngineType and child Patterns. 165 // Used in GetPattern() of Transformation interface to generate a Pattern. 166 func BuildPattern(operand Operand, engineTypeSet EngineTypeSet, children ...*Pattern) *Pattern { 167 p := &Pattern{Operand: operand, EngineTypeSet: engineTypeSet} 168 p.Children = children 169 return p 170 }