github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/types.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plan 16 17 import ( 18 "context" 19 "math" 20 21 "github.com/matrixorigin/matrixone/pkg/vm/process" 22 23 "github.com/matrixorigin/matrixone/pkg/pb/plan" 24 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 25 ) 26 27 const ( 28 JoinSideNone int8 = 0 29 JoinSideLeft = 1 << iota 30 JoinSideRight = 1 << iota 31 JoinSideBoth = JoinSideLeft | JoinSideRight 32 JoinSideMark = 1 << iota 33 JoinSideCorrelated = 1 << iota 34 ) 35 36 type TableDefType = plan.TableDef_DefType 37 type TableDef = plan.TableDef 38 type ColDef = plan.ColDef 39 type ObjectRef = plan.ObjectRef 40 type ColRef = plan.ColRef 41 type Stats = plan.Stats 42 type Const = plan.Const 43 type MaxValue = plan.MaxValue 44 type Expr = plan.Expr 45 type Node = plan.Node 46 type RowsetData = plan.RowsetData 47 type Query = plan.Query 48 type Plan = plan.Plan 49 type Type = plan.Type 50 type Plan_Query = plan.Plan_Query 51 type Property = plan.Property 52 type TableDef_DefType_Properties = plan.TableDef_DefType_Properties 53 type PropertiesDef = plan.PropertiesDef 54 type ViewDef = plan.ViewDef 55 type PartitionByDef = plan.PartitionByDef 56 type ClusterByDef = plan.ClusterByDef 57 type OrderBySpec = plan.OrderBySpec 58 type CreateTable_FkColName = plan.CreateTable_FkColName 59 type ForeignKeyDef = plan.ForeignKeyDef 60 type ClusterTable = plan.ClusterTable 61 type PrimaryKeyDef = plan.PrimaryKeyDef 62 type IndexDef = plan.IndexDef 63 64 type CompilerContext interface { 65 // Default database/schema in context 66 DefaultDatabase() string 67 // check if database exist 68 DatabaseExists(name string) bool 69 // get table definition by database/schema 70 Resolve(schemaName string, tableName string) (*ObjectRef, *TableDef) 71 // get table definition by table id 72 ResolveById(tableId uint64) (*ObjectRef, *TableDef) 73 // get the value of variable 74 ResolveVariable(varName string, isSystemVar, isGlobalVar bool) (interface{}, error) 75 // get the list of the account id 76 ResolveAccountIds(accountNames []string) ([]uint32, error) 77 // get the definition of primary key 78 GetPrimaryKeyDef(dbName string, tableName string) []*ColDef 79 // get the definition of hide key 80 GetHideKeyDef(dbName string, tableName string) *ColDef 81 // get estimated stats by table & expr 82 Stats(obj *ObjectRef, e *Expr) *Stats 83 // get origin sql string of the root 84 GetRootSql() string 85 // get username of current session 86 GetUserName() string 87 GetAccountId() uint32 88 // GetContext get raw context.Context 89 GetContext() context.Context 90 91 GetProcess() *process.Process 92 93 GetQueryResultMeta(uuid string) ([]*ColDef, string, error) 94 SetBuildingAlterView(yesOrNo bool, dbName, viewName string) 95 // is building the alter view or not 96 // return: yes or no, dbName, viewName 97 GetBuildingAlterView() (bool, string, string) 98 } 99 100 type Optimizer interface { 101 Optimize(stmt tree.Statement) (*Query, error) 102 CurrentContext() CompilerContext 103 } 104 105 type Rule interface { 106 Match(*Node) bool // rule match? 107 Apply(*Node, *Query, *process.Process) // apply the rule 108 } 109 110 // BaseOptimizer is base optimizer, capable of handling only a few simple rules 111 type BaseOptimizer struct { 112 qry *Query 113 rules []Rule 114 ctx CompilerContext 115 } 116 117 type ViewData struct { 118 Stmt string 119 DefaultDatabase string 120 } 121 122 type ExecType int 123 124 const ( 125 ExecTypeAP ExecType = iota 126 ExecTypeTP 127 ) 128 129 type ExecInfo struct { 130 Typ ExecType 131 WithGPU bool 132 WithBigMem bool 133 CnNumbers int 134 } 135 136 /////////////////////////////// 137 // Data structures for refactor 138 /////////////////////////////// 139 140 type QueryBuilder struct { 141 qry *plan.Query 142 compCtx CompilerContext 143 144 ctxByNode []*BindContext 145 nameByColRef map[[2]int32]string 146 147 nextTag int32 148 149 mysqlCompatible bool 150 } 151 152 type CTERef struct { 153 defaultDatabase string 154 ast *tree.CTE 155 maskedCTEs map[string]any 156 } 157 158 type BindContext struct { 159 binder Binder 160 161 cteByName map[string]*CTERef 162 maskedCTEs map[string]any 163 164 cteName string 165 headings []string 166 167 groupTag int32 168 aggregateTag int32 169 projectTag int32 170 resultTag int32 171 172 groups []*plan.Expr 173 aggregates []*plan.Expr 174 projects []*plan.Expr 175 results []*plan.Expr 176 177 groupByAst map[string]int32 178 aggregateByAst map[string]int32 179 projectByExpr map[string]int32 180 181 aliasMap map[string]int32 182 183 bindings []*Binding 184 bindingByTag map[int32]*Binding //rel_pos 185 bindingByTable map[string]*Binding 186 bindingByCol map[string]*Binding 187 188 // for join tables 189 bindingTree *BindingTreeNode 190 191 isDistinct bool 192 isCorrelated bool 193 hasSingleRow bool 194 195 parent *BindContext 196 leftChild *BindContext 197 rightChild *BindContext 198 199 defaultDatabase string 200 } 201 202 type NameTuple struct { 203 table string 204 col string 205 } 206 207 type BindingTreeNode struct { 208 using []NameTuple 209 210 binding *Binding 211 212 left *BindingTreeNode 213 right *BindingTreeNode 214 } 215 216 type Binder interface { 217 BindExpr(tree.Expr, int32, bool) (*plan.Expr, error) 218 BindColRef(*tree.UnresolvedName, int32, bool) (*plan.Expr, error) 219 BindAggFunc(string, *tree.FuncExpr, int32, bool) (*plan.Expr, error) 220 BindWinFunc(string, *tree.FuncExpr, int32, bool) (*plan.Expr, error) 221 BindSubquery(*tree.Subquery, bool) (*plan.Expr, error) 222 GetContext() context.Context 223 } 224 225 type baseBinder struct { 226 sysCtx context.Context 227 builder *QueryBuilder 228 ctx *BindContext 229 impl Binder 230 boundCols []string 231 } 232 233 type DefaultBinder struct { 234 baseBinder 235 typ *Type 236 cols []string 237 } 238 239 type TableBinder struct { 240 baseBinder 241 } 242 243 type WhereBinder struct { 244 baseBinder 245 } 246 247 type GroupBinder struct { 248 baseBinder 249 } 250 251 type HavingBinder struct { 252 baseBinder 253 insideAgg bool 254 } 255 256 type ProjectionBinder struct { 257 baseBinder 258 havingBinder *HavingBinder 259 } 260 261 type OrderBinder struct { 262 *ProjectionBinder 263 selectList tree.SelectExprs 264 } 265 266 type LimitBinder struct { 267 baseBinder 268 } 269 270 type PartitionBinder struct { 271 baseBinder 272 } 273 274 var _ Binder = (*TableBinder)(nil) 275 var _ Binder = (*WhereBinder)(nil) 276 var _ Binder = (*GroupBinder)(nil) 277 var _ Binder = (*HavingBinder)(nil) 278 var _ Binder = (*ProjectionBinder)(nil) 279 var _ Binder = (*LimitBinder)(nil) 280 var _ Binder = (*PartitionBinder)(nil) 281 282 const ( 283 NotFound int32 = math.MaxInt32 284 AmbiguousName int32 = math.MinInt32 285 ) 286 287 type Binding struct { 288 tag int32 289 nodeId int32 290 table string 291 cols []string 292 types []*plan.Type 293 refCnts []uint 294 colIdByName map[string]int32 295 isClusterTable bool 296 } 297 298 const ( 299 maxLengthOfTableComment int = 2048 300 maxLengthOfColumnComment int = 1024 301 )