github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/ordinality.go (about)

     1  // Copyright 2016 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 sql
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    18  )
    19  
    20  // ordinalityNode represents a node that adds an "ordinality" column
    21  // to its child node which numbers the rows it produces. Used to
    22  // support WITH ORDINALITY.
    23  //
    24  // Note that the ordinalityNode produces results that number the *full
    25  // set of original values*, as defined by the upstream data source
    26  // specification. In particular, applying a filter before or after
    27  // an intermediate ordinalityNode will produce different results.
    28  //
    29  // It is inserted in the logical plan between the renderNode and its
    30  // source node, thus earlier than the WHERE filters.
    31  //
    32  // In other words, *ordinalityNode establishes a barrier to many
    33  // common SQL optimizations*. Its use should be limited in clients to
    34  // situations where the corresponding performance cost is affordable.
    35  type ordinalityNode struct {
    36  	source      planNode
    37  	columns     sqlbase.ResultColumns
    38  	reqOrdering ReqOrdering
    39  
    40  	run ordinalityRun
    41  }
    42  
    43  // ordinalityRun contains the run-time state of ordinalityNode during local execution.
    44  type ordinalityRun struct {
    45  	row    tree.Datums
    46  	curCnt int64
    47  }
    48  
    49  func (o *ordinalityNode) startExec(runParams) error {
    50  	panic("ordinalityNode can't be run in local mode")
    51  }
    52  
    53  func (o *ordinalityNode) Next(params runParams) (bool, error) {
    54  	panic("ordinalityNode can't be run in local mode")
    55  }
    56  
    57  func (o *ordinalityNode) Values() tree.Datums {
    58  	panic("ordinalityNode can't be run in local mode")
    59  }
    60  
    61  func (o *ordinalityNode) Close(ctx context.Context) { o.source.Close(ctx) }