github.com/matrixorigin/matrixone@v1.2.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/pb/plan" 22 pb "github.com/matrixorigin/matrixone/pkg/pb/statsinfo" 23 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 24 "github.com/matrixorigin/matrixone/pkg/sql/plan/function" 25 "github.com/matrixorigin/matrixone/pkg/vm/process" 26 ) 27 28 const ( 29 JoinSideNone int8 = 0 30 JoinSideLeft = 1 << 1 31 JoinSideRight = 1 << 2 32 JoinSideBoth = JoinSideLeft | JoinSideRight 33 JoinSideMark = 1 << 3 34 JoinSideCorrelated = 1 << 4 35 ) 36 37 type ExpandAliasMode int8 38 39 const ( 40 NoAlias ExpandAliasMode = iota 41 AliasBeforeColumn 42 AliasAfterColumn 43 ) 44 45 type TableDefType = plan.TableDef_DefType 46 type TableDef = plan.TableDef 47 type ColDef = plan.ColDef 48 type ObjectRef = plan.ObjectRef 49 type ColRef = plan.ColRef 50 type Stats = plan.Stats 51 type Const = plan.Literal 52 type MaxValue = plan.MaxValue 53 type Expr = plan.Expr 54 type Node = plan.Node 55 type RowsetData = plan.RowsetData 56 type Query = plan.Query 57 type Plan = plan.Plan 58 type Type = plan.Type 59 type Plan_Query = plan.Plan_Query 60 type Property = plan.Property 61 type TableDef_DefType_Properties = plan.TableDef_DefType_Properties 62 type PropertiesDef = plan.PropertiesDef 63 type ViewDef = plan.ViewDef 64 type PartitionByDef = plan.PartitionByDef 65 type ClusterByDef = plan.ClusterByDef 66 type OrderBySpec = plan.OrderBySpec 67 type FkColName = plan.FkColName 68 type ForeignKeyDef = plan.ForeignKeyDef 69 type ClusterTable = plan.ClusterTable 70 type PrimaryKeyDef = plan.PrimaryKeyDef 71 type IndexDef = plan.IndexDef 72 type SubscriptionMeta = plan.SubscriptionMeta 73 type Snapshot = plan.Snapshot 74 type SnapshotTenant = plan.SnapshotTenant 75 76 type CompilerContext interface { 77 // Default database/schema in context 78 DefaultDatabase() string 79 // check if database exist 80 DatabaseExists(name string, snapshot Snapshot) bool 81 // get table definition by database/schema 82 Resolve(schemaName string, tableName string, snapshot Snapshot) (*ObjectRef, *TableDef) 83 // get table definition by table id 84 ResolveById(tableId uint64, snapshot Snapshot) (*ObjectRef, *TableDef) 85 // get the value of variable 86 ResolveVariable(varName string, isSystemVar, isGlobalVar bool) (interface{}, error) 87 // get the list of the account id 88 ResolveAccountIds(accountNames []string) ([]uint32, error) 89 // get the relevant information of udf 90 ResolveUdf(name string, args []*Expr) (*function.Udf, error) 91 // get the definition of primary key 92 GetPrimaryKeyDef(dbName string, tableName string, snapshot Snapshot) []*ColDef 93 // get needed info for stats by table 94 Stats(obj *ObjectRef, snapshot Snapshot) (*pb.StatsInfo, error) 95 // get origin sql string of the root 96 GetRootSql() string 97 // get username of current session 98 GetUserName() string 99 GetAccountId() (uint32, error) 100 // GetContext get raw context.Context 101 GetContext() context.Context 102 // GetDatabaseId Get database id 103 GetDatabaseId(dbName string, snapshot Snapshot) (uint64, error) 104 105 GetProcess() *process.Process 106 107 GetQueryResultMeta(uuid string) ([]*ColDef, string, error) 108 SetBuildingAlterView(yesOrNo bool, dbName, viewName string) 109 // is building the alter view or not 110 // return: yes or no, dbName, viewName 111 GetBuildingAlterView() (bool, string, string) 112 GetStatsCache() *StatsCache 113 GetSubscriptionMeta(dbName string, snapshot Snapshot) (*SubscriptionMeta, error) 114 CheckSubscriptionValid(subName, accName string, pubName string) error 115 SetQueryingSubscription(meta *SubscriptionMeta) 116 GetQueryingSubscription() *SubscriptionMeta 117 IsPublishing(dbName string) (bool, error) 118 ResolveSubscriptionTableById(tableId uint64, pubmeta *SubscriptionMeta) (*ObjectRef, *TableDef) 119 120 ResolveSnapshotWithSnapshotName(snapshotName string) (*Snapshot, error) 121 CheckTimeStampValid(ts int64) (bool, error) 122 //ReplacePlan replaces the plan of the EXECUTE by the plan generated by the PREPARE. 123 //return 124 // the plan generated by the PREPARE 125 // the statement generated by the PREPARE 126 ReplacePlan(execPlan *plan.Execute) (*plan.Plan, tree.Statement, error) 127 128 GetSnapshot() *Snapshot 129 SetSnapshot(snapshot *Snapshot) 130 GetViews() []string 131 SetViews(views []string) 132 } 133 134 type Optimizer interface { 135 Optimize(stmt tree.Statement) (*Query, error) 136 CurrentContext() CompilerContext 137 } 138 139 type Rule interface { 140 Match(*Node) bool // rule match? 141 Apply(*Node, *Query, *process.Process) // apply the rule 142 } 143 144 // BaseOptimizer is base optimizer, capable of handling only a few simple rules 145 type BaseOptimizer struct { 146 qry *Query 147 rules []Rule 148 ctx CompilerContext 149 } 150 151 type ViewData struct { 152 Stmt string 153 DefaultDatabase string 154 } 155 156 type ExecType int 157 158 const ( 159 ExecTypeAP ExecType = iota 160 ExecTypeTP 161 ) 162 163 type ExecInfo struct { 164 Typ ExecType 165 WithGPU bool 166 WithBigMem bool 167 CnNumbers int 168 } 169 170 type QueryBuilder struct { 171 qry *plan.Query 172 compCtx CompilerContext 173 174 ctxByNode []*BindContext 175 nameByColRef map[[2]int32]string 176 177 tag2Table map[int32]*TableDef 178 179 nextTag int32 180 nextMsgTag int32 181 182 isPrepareStatement bool 183 mysqlCompatible bool 184 haveOnDuplicateKey bool // if it's a plan contain onduplicate key node, we can not use some optmize rule 185 isForUpdate bool // if it's a query plan for update 186 187 deleteNode map[uint64]int32 //delete node in this query. key is tableId, value is the nodeId of sinkScan node in the delete plan 188 skipStats bool 189 optimizerHints *OptimizerHints 190 } 191 192 type OptimizerHints struct { 193 pushDownLimitToScan int 194 pushDownTopThroughLeftJoin int 195 pushDownSemiAntiJoins int 196 aggPushDown int 197 aggPullUp int 198 removeEffectLessLeftJoins int 199 removeRedundantJoinCond int 200 optimizeLikeExpr int 201 optimizeDateFormatExpr int 202 determineHashOnPK int 203 sendMessageFromTopToScan int 204 determineShuffle int 205 blockFilter int 206 applyIndices int 207 runtimeFilter int 208 joinOrdering int 209 } 210 211 type CTERef struct { 212 defaultDatabase string 213 isRecursive bool 214 ast *tree.CTE 215 maskedCTEs map[string]bool 216 snapshot *Snapshot 217 } 218 219 type aliasItem struct { 220 idx int32 221 astExpr tree.Expr 222 } 223 224 type BindContext struct { 225 binder Binder 226 227 cteByName map[string]*CTERef 228 maskedCTEs map[string]bool 229 normalCTE bool 230 initSelect bool 231 recSelect bool 232 finalSelect bool 233 unionSelect bool 234 recRecursiveScanNodeId int32 235 isTryBindingCTE bool 236 237 cteName string 238 headings []string 239 240 groupTag int32 241 aggregateTag int32 242 projectTag int32 243 resultTag int32 244 sinkTag int32 245 windowTag int32 246 timeTag int32 247 sampleTag int32 248 249 groups []*plan.Expr 250 aggregates []*plan.Expr 251 projects []*plan.Expr 252 results []*plan.Expr 253 windows []*plan.Expr 254 times []*plan.Expr 255 256 groupByAst map[string]int32 257 aggregateByAst map[string]int32 258 sampleByAst map[string]int32 259 windowByAst map[string]int32 260 projectByExpr map[string]int32 261 timeByAst map[string]int32 262 263 timeAsts []tree.Expr 264 265 aliasMap map[string]*aliasItem 266 267 bindings []*Binding 268 bindingByTag map[int32]*Binding //rel_pos 269 bindingByTable map[string]*Binding 270 bindingByCol map[string]*Binding 271 272 // for join tables 273 bindingTree *BindingTreeNode 274 275 isDistinct bool 276 isCorrelated bool 277 hasSingleRow bool 278 279 parent *BindContext 280 leftChild *BindContext 281 rightChild *BindContext 282 283 defaultDatabase string 284 285 forceWindows bool 286 287 // sample function related. 288 sampleFunc SampleFuncCtx 289 290 tmpGroups []*plan.Expr 291 292 snapshot *Snapshot 293 // all view keys(dbName#viewName) 294 views []string 295 } 296 297 type NameTuple struct { 298 table string 299 col string 300 } 301 302 type BindingTreeNode struct { 303 using []NameTuple 304 305 binding *Binding 306 307 left *BindingTreeNode 308 right *BindingTreeNode 309 } 310 311 type Binder interface { 312 BindExpr(tree.Expr, int32, bool) (*plan.Expr, error) 313 BindColRef(*tree.UnresolvedName, int32, bool) (*plan.Expr, error) 314 BindAggFunc(string, *tree.FuncExpr, int32, bool) (*plan.Expr, error) 315 BindWinFunc(string, *tree.FuncExpr, int32, bool) (*plan.Expr, error) 316 BindSubquery(*tree.Subquery, bool) (*plan.Expr, error) 317 BindTimeWindowFunc(string, *tree.FuncExpr, int32, bool) (*plan.Expr, error) 318 GetContext() context.Context 319 } 320 321 type baseBinder struct { 322 sysCtx context.Context 323 builder *QueryBuilder 324 ctx *BindContext 325 impl Binder 326 boundCols []string 327 } 328 329 type DefaultBinder struct { 330 baseBinder 331 typ Type 332 cols []string 333 } 334 335 type UpdateBinder struct { 336 baseBinder 337 cols []*ColDef 338 } 339 340 type TableBinder struct { 341 baseBinder 342 } 343 344 type WhereBinder struct { 345 baseBinder 346 } 347 348 type GroupBinder struct { 349 baseBinder 350 } 351 352 type HavingBinder struct { 353 baseBinder 354 insideAgg bool 355 } 356 357 type ProjectionBinder struct { 358 baseBinder 359 havingBinder *HavingBinder 360 } 361 362 type OrderBinder struct { 363 *ProjectionBinder 364 selectList tree.SelectExprs 365 } 366 367 type LimitBinder struct { 368 baseBinder 369 } 370 371 type PartitionBinder struct { 372 baseBinder 373 } 374 375 // SetBinder for 'set @var = expr' 376 type SetBinder struct { 377 baseBinder 378 } 379 380 var _ Binder = (*TableBinder)(nil) 381 var _ Binder = (*WhereBinder)(nil) 382 var _ Binder = (*GroupBinder)(nil) 383 var _ Binder = (*HavingBinder)(nil) 384 var _ Binder = (*ProjectionBinder)(nil) 385 var _ Binder = (*LimitBinder)(nil) 386 var _ Binder = (*PartitionBinder)(nil) 387 var _ Binder = (*UpdateBinder)(nil) 388 389 var Sequence_cols_name = []string{"last_seq_num", "min_value", "max_value", "start_value", "increment_value", "cycle", "is_called"} 390 391 const ( 392 NotFound int32 = math.MaxInt32 393 AmbiguousName int32 = math.MinInt32 394 ) 395 396 type Binding struct { 397 tag int32 398 nodeId int32 399 db string 400 table string 401 tableID uint64 402 cols []string 403 colIsHidden []bool 404 types []*plan.Type 405 refCnts []uint 406 colIdByName map[string]int32 407 isClusterTable bool 408 defaults []string 409 } 410 411 const ( 412 maxLengthOfTableComment int = 2048 413 maxLengthOfColumnComment int = 1024 414 ) 415 416 // fuzzy filter need to get partial unique key attrs name and its origin table name 417 // for Decimal type, we need colDef to get the scale 418 type OriginTableMessageForFuzzy struct { 419 ParentTableName string 420 ParentUniqueCols []*ColDef 421 } 422 423 type MultiTableIndex struct { 424 IndexAlgo string 425 IndexDefs map[string]*plan.IndexDef 426 }