github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/exec/factory.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package exec 12 13 import ( 14 "context" 15 16 "github.com/cockroachdb/cockroach/pkg/geo/geoindex" 17 "github.com/cockroachdb/cockroach/pkg/sql/opt" 18 "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" 19 "github.com/cockroachdb/cockroach/pkg/sql/opt/constraint" 20 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 21 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 22 "github.com/cockroachdb/cockroach/pkg/sql/types" 23 "github.com/cockroachdb/cockroach/pkg/util" 24 ) 25 26 // Node represents a node in the execution tree 27 // (currently maps to sql.planNode). 28 type Node interface{} 29 30 // BufferNode is a node returned by ConstructBuffer. 31 type BufferNode interface { 32 Node 33 34 BufferNodeMarker() 35 } 36 37 // Plan represents the plan for a query (currently maps to sql.planTop). 38 // For simple queries, the plan is associated with a single Node tree. 39 // For queries containing subqueries, the plan is associated with multiple Node 40 // trees (see ConstructPlan). 41 type Plan interface{} 42 43 // Factory defines the interface for building an execution plan, which consists 44 // of a tree of execution nodes (currently a sql.planNode tree). 45 // 46 // The tree is always built bottom-up. The Construct methods either construct 47 // leaf nodes, or they take other nodes previously constructed by this same 48 // factory as children. 49 // 50 // The TypedExprs passed to these functions refer to columns of the input node 51 // via IndexedVars. 52 type Factory interface { 53 // ConstructValues returns a node that outputs the given rows as results. 54 ConstructValues(rows [][]tree.TypedExpr, cols sqlbase.ResultColumns) (Node, error) 55 56 // ConstructScan returns a node that represents a scan of the given index on 57 // the given table. 58 // - Only the given set of needed columns are part of the result. 59 // - If indexConstraint is not nil, the scan is restricted to the spans in 60 // in the constraint. 61 // - If hardLimit > 0, then the scan returns only up to hardLimit rows. 62 // - If softLimit > 0, then the scan may be required to return up to all 63 // of its rows (or up to the hardLimit if it is set), but can be optimized 64 // under the assumption that only softLimit rows will be needed. 65 // - If maxResults > 0, the scan is guaranteed to return at most maxResults 66 // rows. 67 // - If locking is provided, the scan should use the specified row-level 68 // locking mode. 69 ConstructScan( 70 table cat.Table, 71 index cat.Index, 72 needed TableColumnOrdinalSet, 73 indexConstraint *constraint.Constraint, 74 hardLimit int64, 75 softLimit int64, 76 reverse bool, 77 maxResults uint64, 78 reqOrdering OutputOrdering, 79 rowCount float64, 80 locking *tree.LockingItem, 81 ) (Node, error) 82 83 // ConstructFilter returns a node that applies a filter on the results of 84 // the given input node. 85 ConstructFilter(n Node, filter tree.TypedExpr, reqOrdering OutputOrdering) (Node, error) 86 87 // ConstructSimpleProject returns a node that applies a "simple" projection on the 88 // results of the given input node. A simple projection is one that does not 89 // involve new expressions; it's just a reshuffling of columns. This is a 90 // more efficient version of ConstructRender. 91 // The colNames argument is optional; if it is nil, the names of the 92 // corresponding input columns are kept. 93 ConstructSimpleProject( 94 n Node, cols []NodeColumnOrdinal, colNames []string, reqOrdering OutputOrdering, 95 ) (Node, error) 96 97 // ConstructRender returns a node that applies a projection on the results of 98 // the given input node. The projection can contain new expressions. The input 99 // expression slice will be modified. 100 ConstructRender( 101 n Node, columns sqlbase.ResultColumns, exprs tree.TypedExprs, reqOrdering OutputOrdering, 102 ) (Node, error) 103 104 // ConstructApplyJoin returns a node that runs an apply join between an input 105 // node (the left side of the join) and a RelExpr that has outer columns (the 106 // right side of the join) by replacing the outer columns of the right side 107 // RelExpr with data from each row of the left side of the join according to 108 // the data in leftBoundColMap. The apply join can be any kind of join except 109 // for right outer and full outer. 110 // 111 // To plan the right-hand side, planRightSideFn must be called for each left 112 // row. This function generates a plan (using the same factory) that produces 113 // the rightColumns (in order). 114 // 115 // onCond is the join condition. 116 ConstructApplyJoin( 117 joinType sqlbase.JoinType, 118 left Node, 119 rightColumns sqlbase.ResultColumns, 120 onCond tree.TypedExpr, 121 planRightSideFn ApplyJoinPlanRightSideFn, 122 ) (Node, error) 123 124 // ConstructHashJoin returns a node that runs a hash-join between the results 125 // of two input nodes. 126 // 127 // The leftEqColsAreKey/rightEqColsAreKey flags, if set, indicate that the 128 // equality columns form a key in the left/right input. 129 // 130 // The extraOnCond expression can refer to columns from both inputs using 131 // IndexedVars (first the left columns, then the right columns). 132 ConstructHashJoin( 133 joinType sqlbase.JoinType, 134 left, right Node, 135 leftEqCols, rightEqCols []NodeColumnOrdinal, 136 leftEqColsAreKey, rightEqColsAreKey bool, 137 extraOnCond tree.TypedExpr, 138 ) (Node, error) 139 140 // ConstructMergeJoin returns a node that (under distsql) runs a merge join. 141 // The ON expression can refer to columns from both inputs using IndexedVars 142 // (first the left columns, then the right columns). In addition, the i-th 143 // column in leftOrdering is constrained to equal the i-th column in 144 // rightOrdering. The directions must match between the two orderings. 145 ConstructMergeJoin( 146 joinType sqlbase.JoinType, 147 left, right Node, 148 onCond tree.TypedExpr, 149 leftOrdering, rightOrdering sqlbase.ColumnOrdering, 150 reqOrdering OutputOrdering, 151 leftEqColsAreKey, rightEqColsAreKey bool, 152 ) (Node, error) 153 154 // ConstructGroupBy returns a node that runs an aggregation. A set of 155 // aggregations is performed for each group of values on the groupCols. 156 // 157 // If the input is guaranteed to have an ordering on grouping columns, a 158 // "streaming" aggregation is performed (i.e. aggregation happens separately 159 // for each distinct set of values on the set of columns in the ordering). 160 ConstructGroupBy( 161 input Node, 162 groupCols []NodeColumnOrdinal, 163 groupColOrdering sqlbase.ColumnOrdering, 164 aggregations []AggInfo, 165 reqOrdering OutputOrdering, 166 ) (Node, error) 167 168 // ConstructScalarGroupBy returns a node that runs a scalar aggregation, i.e. 169 // one which performs a set of aggregations on all the input rows (as a single 170 // group) and has exactly one result row (even when there are no input rows). 171 ConstructScalarGroupBy(input Node, aggregations []AggInfo) (Node, error) 172 173 // ConstructDistinct returns a node that filters out rows such that only the 174 // first row is kept for each set of values along the distinct columns. 175 // The orderedCols are a subset of distinctCols; the input is required to be 176 // ordered along these columns (i.e. all rows with the same values on these 177 // columns are a contiguous part of the input). 178 ConstructDistinct( 179 input Node, 180 distinctCols, orderedCols NodeColumnOrdinalSet, 181 reqOrdering OutputOrdering, 182 nullsAreDistinct bool, 183 errorOnDup string, 184 ) (Node, error) 185 186 // ConstructSetOp returns a node that performs a UNION / INTERSECT / EXCEPT 187 // operation (either the ALL or the DISTINCT version). The left and right 188 // nodes must have the same number of columns. 189 ConstructSetOp(typ tree.UnionType, all bool, left, right Node) (Node, error) 190 191 // ConstructSort returns a node that performs a resorting of the rows produced 192 // by the input node. 193 // 194 // When the input is partially sorted we can execute a "segmented" sort. In 195 // this case alreadyOrderedPrefix is non-zero and the input is ordered by 196 // ordering[:alreadyOrderedPrefix]. 197 ConstructSort(input Node, ordering sqlbase.ColumnOrdering, alreadyOrderedPrefix int) (Node, error) 198 199 // ConstructOrdinality returns a node that appends an ordinality column to 200 // each row in the input node. 201 ConstructOrdinality(input Node, colName string) (Node, error) 202 203 // ConstructIndexJoin returns a node that performs an index join. The input 204 // contains the primary key (on the columns identified as keyCols). 205 // 206 // The index join produces the given table columns (in ordinal order). 207 ConstructIndexJoin( 208 input Node, 209 table cat.Table, 210 keyCols []NodeColumnOrdinal, 211 tableCols TableColumnOrdinalSet, 212 reqOrdering OutputOrdering, 213 ) (Node, error) 214 215 // ConstructLookupJoin returns a node that performs a lookup join. 216 // The eqCols are columns from the input used as keys for the columns of the 217 // index (or a prefix of them); lookupCols are ordinals for the table columns 218 // we are retrieving. 219 // 220 // The node produces the columns in the input and (unless join type is 221 // LeftSemiJoin or LeftAntiJoin) the lookupCols, ordered by ordinal. The ON 222 // condition can refer to these using IndexedVars. 223 ConstructLookupJoin( 224 joinType sqlbase.JoinType, 225 input Node, 226 table cat.Table, 227 index cat.Index, 228 eqCols []NodeColumnOrdinal, 229 eqColsAreKey bool, 230 lookupCols TableColumnOrdinalSet, 231 onCond tree.TypedExpr, 232 reqOrdering OutputOrdering, 233 ) (Node, error) 234 235 // ConstructGeoLookupJoin returns a node that performs a geospatial lookup 236 // join. geoRelationshipType describes the type of geospatial relationship 237 // represented by the join. geoCol is the geospatial column from the input 238 // that will be used to look up into the index; lookupCols are ordinals for 239 // the table columns we are retrieving. 240 // 241 // The node produces the columns in the input and (unless join type is 242 // LeftSemiJoin or LeftAntiJoin) the lookupCols, ordered by ordinal. The ON 243 // condition can refer to these using IndexedVars. 244 ConstructGeoLookupJoin( 245 joinType sqlbase.JoinType, 246 geoRelationshipType geoindex.RelationshipType, 247 input Node, 248 table cat.Table, 249 index cat.Index, 250 geoCol NodeColumnOrdinal, 251 lookupCols TableColumnOrdinalSet, 252 onCond tree.TypedExpr, 253 reqOrdering OutputOrdering, 254 ) (Node, error) 255 256 // ConstructZigzagJoin returns a node that performs a zigzag join. 257 // Each side of the join has two kinds of columns that form a prefix 258 // of the specified index: fixed columns (with values specified in 259 // fixedVals), and equal columns (with column ordinals specified in 260 // {left,right}EqCols). The lengths of leftEqCols and rightEqCols 261 // must match. 262 ConstructZigzagJoin( 263 leftTable cat.Table, 264 leftIndex cat.Index, 265 rightTable cat.Table, 266 rightIndex cat.Index, 267 leftEqCols []NodeColumnOrdinal, 268 rightEqCols []NodeColumnOrdinal, 269 leftCols NodeColumnOrdinalSet, 270 rightCols NodeColumnOrdinalSet, 271 onCond tree.TypedExpr, 272 fixedVals []Node, 273 reqOrdering OutputOrdering, 274 ) (Node, error) 275 276 // ConstructLimit returns a node that implements LIMIT and/or OFFSET on the 277 // results of the given node. If one or the other is not needed, then it is 278 // set to nil. 279 ConstructLimit(input Node, limit, offset tree.TypedExpr) (Node, error) 280 281 // ConstructMax1Row returns a node that permits at most one row from the 282 // given input node, returning an error with the given text at runtime if 283 // the node tries to return more than one row. 284 ConstructMax1Row(input Node, errorText string) (Node, error) 285 286 // ConstructProjectSet returns a node that performs a lateral cross join 287 // between the output of the given node and the functional zip of the given 288 // expressions. 289 ConstructProjectSet( 290 n Node, exprs tree.TypedExprs, zipCols sqlbase.ResultColumns, numColsPerGen []int, 291 ) (Node, error) 292 293 // ConstructWindow returns a node that executes a window function over the 294 // given node. 295 ConstructWindow(input Node, window WindowInfo) (Node, error) 296 297 // RenameColumns modifies the column names of a node. 298 RenameColumns(input Node, colNames []string) (Node, error) 299 300 // ConstructPlan creates a plan enclosing the given plan and (optionally) 301 // subqueries, cascades, and checks. 302 // 303 // Subqueries are executed before the root tree, which can refer to subquery 304 // results using tree.Subquery nodes. 305 // 306 // Cascades are executed after the root tree. They can return more cascades 307 // and checks which should also be executed. 308 // 309 // Checks are executed after all cascades have been executed. They don't 310 // return results but can generate errors (e.g. foreign key check failures). 311 ConstructPlan( 312 root Node, subqueries []Subquery, cascades []Cascade, checks []Node, 313 ) (Plan, error) 314 315 // ConstructExplain returns a node that implements EXPLAIN (OPT), showing 316 // information about the given plan. 317 ConstructExplainOpt(plan string, envOpts ExplainEnvData) (Node, error) 318 319 // ConstructExplain returns a node that implements EXPLAIN, showing 320 // information about the given plan. 321 ConstructExplain( 322 options *tree.ExplainOptions, stmtType tree.StatementType, plan Plan, 323 ) (Node, error) 324 325 // ConstructShowTrace returns a node that implements a SHOW TRACE 326 // FOR SESSION statement. 327 ConstructShowTrace(typ tree.ShowTraceType, compact bool) (Node, error) 328 329 // ConstructInsert creates a node that implements an INSERT statement. The 330 // input columns are inserted into a subset of columns in the table, in the 331 // same order they're defined. The insertCols set contains the ordinal 332 // positions of columns in the table into which values are inserted. All 333 // columns are expected to be present except delete-only mutation columns, 334 // since those do not need to participate in an insert operation. 335 // 336 // If allowAutoCommit is set, the operator is allowed to commit the 337 // transaction (if appropriate, i.e. if it is in an implicit transaction). 338 // This is false if there are multiple mutations in a statement, or the output 339 // of the mutation is processed through side-effecting expressions. 340 // 341 // If skipFKChecks is set, foreign keys are not checked as part of the 342 // execution of the insertion. This is used when the FK checks are planned by 343 // the optimizer and are run separately as plan checks. 344 ConstructInsert( 345 input Node, 346 table cat.Table, 347 insertCols TableColumnOrdinalSet, 348 returnCols TableColumnOrdinalSet, 349 checkCols CheckOrdinalSet, 350 allowAutoCommit bool, 351 skipFKChecks bool, 352 ) (Node, error) 353 354 // ConstructInsertFastPath creates a node that implements a special (but very 355 // common) case of insert, satisfying the following conditions: 356 // - the input is Values with at most InsertFastPathMaxRows, and there are no 357 // subqueries; 358 // - there are no other mutations in the statement, and the output of the 359 // insert is not processed through side-effecting expressions (see 360 // allowAutoCommit flag for ConstructInsert); 361 // - there are no self-referencing foreign keys; 362 // - all FK checks can be performed using direct lookups into unique indexes. 363 // 364 // In this case, the foreign-key checks can run before (or even concurrently 365 // with) the insert. If they are run before, the insert is allowed to 366 // auto-commit. 367 ConstructInsertFastPath( 368 rows [][]tree.TypedExpr, 369 table cat.Table, 370 insertCols TableColumnOrdinalSet, 371 returnCols TableColumnOrdinalSet, 372 checkCols CheckOrdinalSet, 373 fkChecks []InsertFastPathFKCheck, 374 ) (Node, error) 375 376 // ConstructUpdate creates a node that implements an UPDATE statement. The 377 // input contains columns that were fetched from the target table, and that 378 // provide existing values that can be used to formulate the new encoded 379 // value that will be written back to the table (updating any column in a 380 // family requires having the values of all other columns). The input also 381 // contains computed columns that provide new values for any updated columns. 382 // 383 // The fetchCols and updateCols sets contain the ordinal positions of the 384 // fetch and update columns in the target table. The input must contain those 385 // columns in the same order as they appear in the table schema, with the 386 // fetch columns first and the update columns second. 387 // 388 // The passthrough parameter contains all the result columns that are part of 389 // the input node that the update node needs to return (passing through from 390 // the input). The pass through columns are used to return any column from the 391 // FROM tables that are referenced in the RETURNING clause. 392 // 393 // If allowAutoCommit is set, the operator is allowed to commit the 394 // transaction (if appropriate, i.e. if it is in an implicit transaction). 395 // This is false if there are multiple mutations in a statement, or the output 396 // of the mutation is processed through side-effecting expressions. 397 // 398 // If skipFKChecks is set, foreign keys are not checked as part of the 399 // execution of the insertion. This is used when the FK checks are planned by 400 // the optimizer and are run separately as plan checks. 401 ConstructUpdate( 402 input Node, 403 table cat.Table, 404 fetchCols TableColumnOrdinalSet, 405 updateCols TableColumnOrdinalSet, 406 returnCols TableColumnOrdinalSet, 407 checks CheckOrdinalSet, 408 passthrough sqlbase.ResultColumns, 409 allowAutoCommit bool, 410 skipFKChecks bool, 411 ) (Node, error) 412 413 // ConstructUpsert creates a node that implements an INSERT..ON CONFLICT or 414 // UPSERT statement. For each input row, Upsert will test the canaryCol. If 415 // it is null, then it will insert a new row. If not-null, then Upsert will 416 // update an existing row. The input is expected to contain the columns to be 417 // inserted, followed by the columns containing existing values, and finally 418 // the columns containing new values. 419 // 420 // The length of each group of input columns can be up to the number of 421 // columns in the given table. The insertCols, fetchCols, and updateCols sets 422 // contain the ordinal positions of the table columns that are involved in 423 // the Upsert. For example: 424 // 425 // CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT) 426 // INSERT INTO abc VALUES (10, 20, 30) ON CONFLICT (a) DO UPDATE SET b=25 427 // 428 // insertCols = {0, 1, 2} 429 // fetchCols = {0, 1, 2} 430 // updateCols = {1} 431 // 432 // The input is expected to first have 3 columns that will be inserted into 433 // columns {0, 1, 2} of the table. The next 3 columns contain the existing 434 // values of columns {0, 1, 2} of the table. The last column contains the 435 // new value for column {1} of the table. 436 // 437 // If allowAutoCommit is set, the operator is allowed to commit the 438 // transaction (if appropriate, i.e. if it is in an implicit transaction). 439 // This is false if there are multiple mutations in a statement, or the output 440 // of the mutation is processed through side-effecting expressions. 441 // 442 // If skipFKChecks is set, foreign keys are not checked as part of the 443 // execution of the upsert for the insert half. This is used when the FK 444 // checks are planned by the optimizer and are run separately as plan 445 // checks. 446 ConstructUpsert( 447 input Node, 448 table cat.Table, 449 canaryCol NodeColumnOrdinal, 450 insertCols TableColumnOrdinalSet, 451 fetchCols TableColumnOrdinalSet, 452 updateCols TableColumnOrdinalSet, 453 returnCols TableColumnOrdinalSet, 454 checks CheckOrdinalSet, 455 allowAutoCommit bool, 456 skipFKChecks bool, 457 ) (Node, error) 458 459 // ConstructDelete creates a node that implements a DELETE statement. The 460 // input contains columns that were fetched from the target table, and that 461 // will be deleted. 462 // 463 // The fetchCols set contains the ordinal positions of the fetch columns in 464 // the target table. The input must contain those columns in the same order 465 // as they appear in the table schema. 466 // 467 // If allowAutoCommit is set, the operator is allowed to commit the 468 // transaction (if appropriate, i.e. if it is in an implicit transaction). 469 // This is false if there are multiple mutations in a statement, or the output 470 // of the mutation is processed through side-effecting expressions. 471 // 472 // If skipFKChecks is set, foreign keys are not checked as part of the 473 // execution of the delete. This is used when the FK checks are planned 474 // by the optimizer and are run separately as plan checks. 475 ConstructDelete( 476 input Node, 477 table cat.Table, 478 fetchCols TableColumnOrdinalSet, 479 returnCols TableColumnOrdinalSet, 480 allowAutoCommit bool, 481 skipFKChecks bool, 482 ) (Node, error) 483 484 // ConstructDeleteRange creates a node that efficiently deletes contiguous 485 // rows stored in the given table's primary index. This fast path is only 486 // possible when certain conditions hold true: 487 // - there are no secondary indexes; 488 // - the input to the delete is a scan (without limits); 489 // - the table is not involved in interleaving, or it is at the root of an 490 // interleaving hierarchy with cascading FKs such that a delete of a row 491 // cascades and deletes all interleaved rows corresponding to that row; 492 // - there are no inbound FKs to the table (other than within the 493 // interleaving as described above). 494 // 495 // See the comment for ConstructScan for descriptions of the needed and 496 // indexConstraint parameters, since DeleteRange combines Delete + Scan into a 497 // single operator. 498 // 499 // If any interleavedTables are passed, they are all the descendant tables in 500 // an interleaving hierarchy we are deleting from. 501 ConstructDeleteRange( 502 table cat.Table, 503 needed TableColumnOrdinalSet, 504 indexConstraint *constraint.Constraint, 505 interleavedTables []cat.Table, 506 maxReturnedKeys int, 507 allowAutoCommit bool, 508 ) (Node, error) 509 510 // ConstructCreateTable returns a node that implements a CREATE TABLE 511 // statement. 512 ConstructCreateTable(input Node, schema cat.Schema, ct *tree.CreateTable) (Node, error) 513 514 // ConstructCreateView returns a node that implements a CREATE VIEW 515 // statement. 516 ConstructCreateView( 517 schema cat.Schema, 518 viewName string, 519 ifNotExists bool, 520 replace bool, 521 temporary bool, 522 viewQuery string, 523 columns sqlbase.ResultColumns, 524 deps opt.ViewDeps, 525 ) (Node, error) 526 527 // ConstructSequenceSelect creates a node that implements a scan of a sequence 528 // as a data source. 529 ConstructSequenceSelect(sequence cat.Sequence) (Node, error) 530 531 // ConstructSaveTable wraps the input into a node that passes through all the 532 // rows, but also creates a table and inserts all the rows into it. 533 ConstructSaveTable(input Node, table *cat.DataSourceName, colNames []string) (Node, error) 534 535 // ConstructErrorIfRows wraps the input into a node which itself returns no 536 // results, but errors out if the input returns any rows. The mkErr function 537 // is used to create the error. 538 ConstructErrorIfRows(input Node, mkErr func(tree.Datums) error) (Node, error) 539 540 // ConstructOpaque creates a node for an opaque operator. 541 ConstructOpaque(metadata opt.OpaqueMetadata) (Node, error) 542 543 // ConstructAlterTableSplit creates a node that implements ALTER TABLE/INDEX 544 // SPLIT AT. 545 ConstructAlterTableSplit(index cat.Index, input Node, expiration tree.TypedExpr) (Node, error) 546 547 // ConstructAlterTableUnsplit creates a node that implements ALTER TABLE/INDEX 548 // UNSPLIT AT. 549 ConstructAlterTableUnsplit(index cat.Index, input Node) (Node, error) 550 551 // ConstructAlterTableUnsplitAll creates a node that implements ALTER TABLE/INDEX 552 // UNSPLIT ALL. 553 ConstructAlterTableUnsplitAll(index cat.Index) (Node, error) 554 555 // ConstructAlterTableRelocate creates a node that implements ALTER TABLE/INDEX 556 // UNSPLIT AT. 557 ConstructAlterTableRelocate(index cat.Index, input Node, relocateLease bool) (Node, error) 558 559 // ConstructBuffer constructs a node whose input can be referenced from 560 // elsewhere in the query. 561 ConstructBuffer(input Node, label string) (BufferNode, error) 562 563 // ConstructScanBuffer constructs a node which refers to a node constructed by 564 // ConstructBuffer or passed to RecursiveCTEIterationFn. 565 ConstructScanBuffer(ref BufferNode, label string) (Node, error) 566 567 // ConstructRecursiveCTE constructs a node that executes a recursive CTE: 568 // * the initial plan is run first; the results are emitted and also saved 569 // in a buffer. 570 // * so long as the last buffer is not empty: 571 // - the RecursiveCTEIterationFn is used to create a plan for the 572 // recursive side; a reference to the last buffer is passed to this 573 // function. The returned plan uses this reference with a 574 // ConstructScanBuffer call. 575 // - the plan is executed; the results are emitted and also saved in a new 576 // buffer for the next iteration. 577 ConstructRecursiveCTE(initial Node, fn RecursiveCTEIterationFn, label string) (Node, error) 578 579 // ConstructControlJobs creates a node that implements PAUSE/CANCEL/RESUME 580 // JOBS. 581 ConstructControlJobs(command tree.JobCommand, input Node) (Node, error) 582 583 // ConstructCancelQueries creates a node that implements CANCEL QUERIES. 584 ConstructCancelQueries(input Node, ifExists bool) (Node, error) 585 586 // ConstructCancelSessions creates a node that implements CANCEL SESSIONS. 587 ConstructCancelSessions(input Node, ifExists bool) (Node, error) 588 589 // ConstructExport creates a node that implements EXPORT. 590 ConstructExport( 591 input Node, 592 fileName tree.TypedExpr, 593 fileFormat string, 594 options []KVOption, 595 ) (Node, error) 596 } 597 598 // OutputOrdering indicates the required output ordering on a Node that is being 599 // created. It refers to the output columns of the node by ordinal. 600 // 601 // This ordering is used for distributed execution planning, to know how to 602 // merge results from different nodes. For example, scanning a table can be 603 // executed as multiple hosts scanning different pieces of the table. When the 604 // results from the nodes get merged, we they are merged according to the output 605 // ordering. 606 // 607 // The node must be able to support this output ordering given its other 608 // configuration parameters. 609 type OutputOrdering sqlbase.ColumnOrdering 610 611 // Subquery encapsulates information about a subquery that is part of a plan. 612 type Subquery struct { 613 // ExprNode is a reference to a AST node that can be used for printing the SQL 614 // of the subquery (for EXPLAIN). 615 ExprNode tree.NodeFormatter 616 Mode SubqueryMode 617 // Root is the root Node of the plan for this subquery. This Node returns 618 // results as required for the specific Type. 619 Root Node 620 } 621 622 // SubqueryMode indicates how the results of the subquery are to be processed. 623 type SubqueryMode int 624 625 const ( 626 // SubqueryExists - the value of the subquery is a boolean: true if the 627 // subquery returns any rows, false otherwise. 628 SubqueryExists SubqueryMode = iota 629 // SubqueryOneRow - the subquery expects at most one row; the result is that 630 // row (as a single value or a tuple), or NULL if there were no rows. 631 SubqueryOneRow 632 // SubqueryAnyRows - the subquery is an argument to ANY. Any number of rows 633 // expected; the result is a sorted, distinct tuple of rows (i.e. it has been 634 // normalized). As a special case, if there is only one column selected, the 635 // result is a tuple of the selected values (instead of a tuple of 1-tuples). 636 SubqueryAnyRows 637 // SubqueryAllRows - the subquery is an argument to ARRAY. The result is a 638 // tuple of rows. 639 SubqueryAllRows 640 ) 641 642 // TableColumnOrdinal is the 0-based ordinal index of a cat.Table column. 643 // It is used when operations involve a table directly (e.g. scans, index/lookup 644 // joins, mutations). 645 type TableColumnOrdinal int32 646 647 // TableColumnOrdinalSet contains a set of TableColumnOrdinal values. 648 type TableColumnOrdinalSet = util.FastIntSet 649 650 // NodeColumnOrdinal is the 0-based ordinal index of a column produced by a 651 // Node. It is used when referring to a column in an input to an operator. 652 type NodeColumnOrdinal int32 653 654 // NodeColumnOrdinalSet contains a set of NodeColumnOrdinal values. 655 type NodeColumnOrdinalSet = util.FastIntSet 656 657 // CheckOrdinalSet contains the ordinal positions of a set of check constraints 658 // taken from the opt.Table.Check collection. 659 type CheckOrdinalSet = util.FastIntSet 660 661 // AggInfo represents an aggregation (see ConstructGroupBy). 662 type AggInfo struct { 663 FuncName string 664 Builtin *tree.Overload 665 Distinct bool 666 ResultType *types.T 667 ArgCols []NodeColumnOrdinal 668 669 // ConstArgs is the list of any constant arguments to the aggregate, 670 // for instance, the separator in string_agg. 671 ConstArgs []tree.Datum 672 673 // Filter is the index of the column, if any, which should be used as the 674 // FILTER condition for the aggregate. If there is no filter, Filter is -1. 675 Filter NodeColumnOrdinal 676 } 677 678 // WindowInfo represents the information about a window function that must be 679 // passed through to the execution engine. 680 type WindowInfo struct { 681 // Cols is the set of columns that are returned from the windowing operator. 682 Cols sqlbase.ResultColumns 683 684 // TODO(justin): refactor this to be a single array of structs. 685 686 // Exprs is the list of window function expressions. 687 Exprs []*tree.FuncExpr 688 689 // OutputIdxs are the indexes that the various window functions being computed 690 // should put their output in. 691 OutputIdxs []int 692 693 // ArgIdxs is the list of column ordinals each function takes as arguments, 694 // in the same order as Exprs. 695 ArgIdxs [][]NodeColumnOrdinal 696 697 // FilterIdxs is the list of column indices to use as filters. 698 FilterIdxs []int 699 700 // Partition is the set of input columns to partition on. 701 Partition []NodeColumnOrdinal 702 703 // Ordering is the set of input columns to order on. 704 Ordering sqlbase.ColumnOrdering 705 706 // RangeOffsetColumn is the column ID of a single column from ORDER BY clause 707 // when window frame has RANGE mode of framing and at least one 'offset' 708 // boundary. We store it separately because the ordering might be simplified 709 // (when that single column is in Partition), but the execution still needs 710 // to know the original ordering. 711 RangeOffsetColumn NodeColumnOrdinal 712 } 713 714 // ExplainEnvData represents the data that's going to be displayed in EXPLAIN (env). 715 type ExplainEnvData struct { 716 ShowEnv bool 717 Tables []tree.TableName 718 Sequences []tree.TableName 719 Views []tree.TableName 720 } 721 722 // KVOption represents information about a statement option 723 // (see tree.KVOptions). 724 type KVOption struct { 725 Key string 726 // If there is no value, Value is DNull. 727 Value tree.TypedExpr 728 } 729 730 // RecursiveCTEIterationFn creates a plan for an iteration of WITH RECURSIVE, 731 // given the result of the last iteration (as a BufferNode). 732 type RecursiveCTEIterationFn func(bufferRef BufferNode) (Plan, error) 733 734 // ApplyJoinPlanRightSideFn creates a plan for an iteration of ApplyJoin, given 735 // a row produced from the left side. The plan is guaranteed to produce the 736 // rightColumns passed to ConstructApplyJoin (in order). 737 type ApplyJoinPlanRightSideFn func(leftRow tree.Datums) (Plan, error) 738 739 // Cascade describes a cascading query. The query uses a BufferNode as an input; 740 // it should only be triggered if this buffer is not empty. 741 type Cascade struct { 742 // FKName is the name of the foreign key constraint. 743 FKName string 744 745 // Buffer is the Node returned by ConstructBuffer which stores the input to 746 // the mutation. 747 Buffer BufferNode 748 749 // PlanFn builds the cascade query and creates the plan for it. 750 // Note that the generated Plan can in turn contain more cascades (as well as 751 // checks, which should run after all cascades are executed). 752 // 753 // The bufferRef is a reference that can be used with ConstructWithBuffer to 754 // read the mutation input. It is conceptually the same as the Buffer field; 755 // however, we allow the execution engine to provide a different copy or 756 // implementation of the node (e.g. to facilitate early cleanup of the 757 // original plan). 758 // 759 // This method does not mutate any captured state; it is ok to call PlanFn 760 // methods concurrently (provided that they don't use a single non-thread-safe 761 // execFactory). 762 PlanFn func( 763 ctx context.Context, 764 semaCtx *tree.SemaContext, 765 evalCtx *tree.EvalContext, 766 execFactory Factory, 767 bufferRef BufferNode, 768 numBufferedRows int, 769 ) (Plan, error) 770 } 771 772 // InsertFastPathMaxRows is the maximum number of rows for which we can use the 773 // insert fast path. 774 const InsertFastPathMaxRows = 10000 775 776 // InsertFastPathFKCheck contains information about a foreign key check to be 777 // performed by the insert fast-path (see ConstructInsertFastPath). It 778 // identifies the index into which we can perform the lookup. 779 type InsertFastPathFKCheck struct { 780 ReferencedTable cat.Table 781 ReferencedIndex cat.Index 782 783 // InsertCols contains the FK columns from the origin table, in the order of 784 // the ReferencedIndex columns. For each, the value in the array indicates the 785 // index of the column in the input table. 786 InsertCols []TableColumnOrdinal 787 788 MatchMethod tree.CompositeKeyMatchMethod 789 790 // MkErr is called when a violation is detected (i.e. the index has no entries 791 // for a given inserted row). The values passed correspond to InsertCols 792 // above. 793 MkErr func(tree.Datums) error 794 }