github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/optimizer/plan/plans.go (about) 1 // Copyright 2015 PingCAP, 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 plan 15 16 import ( 17 "fmt" 18 "github.com/insionng/yougam/libraries/pingcap/tidb/ast" 19 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/util/types" 21 ) 22 23 // TableRange represents a range of row handle. 24 type TableRange struct { 25 LowVal int64 26 HighVal int64 27 } 28 29 // TableDual represents a dual table plan. 30 type TableDual struct { 31 basePlan 32 33 HasAgg bool 34 // FilterConditions can be used to filter result. 35 FilterConditions []ast.ExprNode 36 } 37 38 // TableScan represents a table scan plan. 39 type TableScan struct { 40 basePlan 41 42 Table *model.TableInfo 43 Desc bool 44 Ranges []TableRange 45 46 // RefAccess indicates it references a previous joined table, used in explain. 47 RefAccess bool 48 49 // AccessConditions can be used to build index range. 50 AccessConditions []ast.ExprNode 51 52 // FilterConditions can be used to filter result. 53 FilterConditions []ast.ExprNode 54 55 // TableName is used to distinguish the same table selected multiple times in different place, 56 // like 'select * from t where exists(select 1 from t as x where t.c < x.c)' 57 TableName *ast.TableName 58 59 TableAsName *model.CIStr 60 61 LimitCount *int64 62 } 63 64 // ShowDDL is for showing DDL information. 65 type ShowDDL struct { 66 basePlan 67 } 68 69 // CheckTable is for checking table data. 70 type CheckTable struct { 71 basePlan 72 73 Tables []*ast.TableName 74 } 75 76 // IndexRange represents an index range to be scanned. 77 type IndexRange struct { 78 LowVal []types.Datum 79 LowExclude bool 80 HighVal []types.Datum 81 HighExclude bool 82 } 83 84 // IsPoint returns if the index range is a point. 85 func (ir *IndexRange) IsPoint() bool { 86 if len(ir.LowVal) != len(ir.HighVal) { 87 return false 88 } 89 for i := range ir.LowVal { 90 a := ir.LowVal[i] 91 b := ir.HighVal[i] 92 if a.Kind() == types.KindMinNotNull || b.Kind() == types.KindMaxValue { 93 return false 94 } 95 cmp, err := a.CompareDatum(b) 96 if err != nil { 97 return false 98 } 99 if cmp != 0 { 100 return false 101 } 102 } 103 return !ir.LowExclude && !ir.HighExclude 104 } 105 106 // IndexScan represents an index scan plan. 107 type IndexScan struct { 108 basePlan 109 110 // The index used. 111 Index *model.IndexInfo 112 113 // The table to lookup. 114 Table *model.TableInfo 115 116 // Ordered and non-overlapping ranges to be scanned. 117 Ranges []*IndexRange 118 119 // Desc indicates whether the index should be scanned in descending order. 120 Desc bool 121 122 // RefAccess indicates it references a previous joined table, used in explain. 123 RefAccess bool 124 125 // AccessConditions can be used to build index range. 126 AccessConditions []ast.ExprNode 127 128 // Number of leading equal access condition. 129 // The offset of each equal condition correspond to the offset of index column. 130 // For example, an index has column (a, b, c), condition is 'a = 0 and b = 0 and c > 0' 131 // AccessEqualCount would be 2. 132 AccessEqualCount int 133 134 // FilterConditions can be used to filter result. 135 FilterConditions []ast.ExprNode 136 137 // OutOfOrder indicates if the index scan can return out of order. 138 OutOfOrder bool 139 140 // NoLimit indicates that this plan need fetch all the rows. 141 NoLimit bool 142 143 // TableName is used to distinguish the same table selected multiple times in different place, 144 // like 'select * from t where exists(select 1 from t as x where t.c < x.c)' 145 TableName *ast.TableName 146 147 TableAsName *model.CIStr 148 149 LimitCount *int64 150 } 151 152 // JoinOuter represents outer join plan. 153 type JoinOuter struct { 154 basePlan 155 156 Outer Plan 157 Inner Plan 158 } 159 160 // JoinInner represents inner join plan. 161 type JoinInner struct { 162 basePlan 163 164 Inners []Plan 165 Conditions []ast.ExprNode 166 } 167 168 func (p *JoinInner) String() string { 169 return fmt.Sprintf("JoinInner()") 170 } 171 172 // SelectLock represents a select lock plan. 173 type SelectLock struct { 174 basePlan 175 176 Lock ast.SelectLockType 177 } 178 179 // SetLimit implements Plan SetLimit interface. 180 func (p *SelectLock) SetLimit(limit float64) { 181 p.limit = limit 182 p.GetChildByIndex(0).SetLimit(p.limit) 183 } 184 185 // SelectFields represents a select fields plan. 186 type SelectFields struct { 187 basePlan 188 } 189 190 // SetLimit implements Plan SetLimit interface. 191 func (p *SelectFields) SetLimit(limit float64) { 192 p.limit = limit 193 if p.GetChildByIndex(0) != nil { 194 p.GetChildByIndex(0).SetLimit(limit) 195 } 196 } 197 198 // Sort represents a sorting plan. 199 type Sort struct { 200 basePlan 201 202 ByItems []*ast.ByItem 203 204 ExecLimit *Limit 205 } 206 207 // SetLimit implements Plan SetLimit interface. 208 // It set the Src limit only if it is bypassed. 209 // Bypass has to be determined before this get called. 210 func (p *Sort) SetLimit(limit float64) { 211 p.limit = limit 212 } 213 214 // Limit represents offset and limit plan. 215 type Limit struct { 216 basePlan 217 218 Offset uint64 219 Count uint64 220 } 221 222 // SetLimit implements Plan SetLimit interface. 223 // As Limit itself determine the real limit, 224 // We just ignore the input, and set the real limit. 225 func (p *Limit) SetLimit(limit float64) { 226 p.limit = float64(p.Offset + p.Count) 227 p.GetChildByIndex(0).SetLimit(p.limit) 228 } 229 230 // Union represents Union plan. 231 type Union struct { 232 basePlan 233 234 Selects []Plan 235 } 236 237 // Distinct represents Distinct plan. 238 type Distinct struct { 239 basePlan 240 } 241 242 // SetLimit implements Plan SetLimit interface. 243 func (p *Distinct) SetLimit(limit float64) { 244 p.limit = limit 245 if p.GetChildByIndex(0) != nil { 246 p.GetChildByIndex(0).SetLimit(limit) 247 } 248 } 249 250 // Prepare represents prepare plan. 251 type Prepare struct { 252 basePlan 253 254 Name string 255 SQLText string 256 } 257 258 // Execute represents prepare plan. 259 type Execute struct { 260 basePlan 261 262 Name string 263 UsingVars []ast.ExprNode 264 ID uint32 265 } 266 267 // Deallocate represents deallocate plan. 268 type Deallocate struct { 269 basePlan 270 271 Name string 272 } 273 274 // Aggregate represents a select fields plan. 275 type Aggregate struct { 276 basePlan 277 AggFuncs []*ast.AggregateFuncExpr 278 GroupByItems []*ast.ByItem 279 } 280 281 // SetLimit implements Plan SetLimit interface. 282 func (p *Aggregate) SetLimit(limit float64) { 283 p.limit = limit 284 if p.GetChildByIndex(0) != nil { 285 p.GetChildByIndex(0).SetLimit(limit) 286 } 287 } 288 289 // Having represents a having plan. 290 // The having plan should after aggregate plan. 291 type Having struct { 292 basePlan 293 294 // Originally the WHERE or ON condition is parsed into a single expression, 295 // but after we converted to CNF(Conjunctive normal form), it can be 296 // split into a list of AND conditions. 297 Conditions []ast.ExprNode 298 } 299 300 // SetLimit implements Plan SetLimit interface. 301 func (p *Having) SetLimit(limit float64) { 302 p.limit = limit 303 // We assume 50% of the GetChildByIndex(0) row is filtered out. 304 p.GetChildByIndex(0).SetLimit(limit * 2) 305 } 306 307 // Update represents an update plan. 308 type Update struct { 309 basePlan 310 311 OrderedList []*ast.Assignment // OrderedList has the same offset as TablePlan's result fields. 312 SelectPlan Plan 313 } 314 315 // Delete represents a delete plan. 316 type Delete struct { 317 basePlan 318 319 SelectPlan Plan 320 Tables []*ast.TableName 321 IsMultiTable bool 322 } 323 324 // Filter represents a plan that filter GetChildByIndex(0)plan result. 325 type Filter struct { 326 basePlan 327 328 // Originally the WHERE or ON condition is parsed into a single expression, 329 // but after we converted to CNF(Conjunctive normal form), it can be 330 // split into a list of AND conditions. 331 Conditions []ast.ExprNode 332 } 333 334 // SetLimit implements Plan SetLimit interface. 335 func (p *Filter) SetLimit(limit float64) { 336 p.limit = limit 337 // We assume 50% of the GetChildByIndex(0) row is filtered out. 338 p.GetChildByIndex(0).SetLimit(limit * 2) 339 } 340 341 // Show represents a show plan. 342 type Show struct { 343 basePlan 344 345 Tp ast.ShowStmtType // Databases/Tables/Columns/.... 346 DBName string 347 Table *ast.TableName // Used for showing columns. 348 Column *ast.ColumnName // Used for `desc table column`. 349 Flag int // Some flag parsed from sql, such as FULL. 350 Full bool 351 User string // Used for show grants. 352 353 // Used by show variables 354 GlobalScope bool 355 } 356 357 // Simple represents a simple statement plan which doesn't need any optimization. 358 type Simple struct { 359 basePlan 360 361 Statement ast.StmtNode 362 } 363 364 // Insert represents an insert plan. 365 type Insert struct { 366 basePlan 367 368 Table *ast.TableRefsClause 369 Columns []*ast.ColumnName 370 Lists [][]ast.ExprNode 371 Setlist []*ast.Assignment 372 OnDuplicate []*ast.Assignment 373 SelectPlan Plan 374 375 IsReplace bool 376 Priority int 377 } 378 379 // DDL represents a DDL statement plan. 380 type DDL struct { 381 basePlan 382 383 Statement ast.DDLNode 384 } 385 386 // Explain represents a explain plan. 387 type Explain struct { 388 basePlan 389 390 StmtPlan Plan 391 }