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

     1  // Copyright 2015 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/util"
    18  )
    19  
    20  // distinctNode de-duplicates rows returned by a wrapped planNode.
    21  type distinctNode struct {
    22  	plan planNode
    23  
    24  	// distinctOnColIdxs are the column indices of the child planNode and
    25  	// is what defines the distinct key.
    26  	// For a normal DISTINCT (without the ON clause), distinctOnColIdxs
    27  	// contains all the column indices of the child planNode.
    28  	// Otherwise, distinctOnColIdxs is a strict subset of the child
    29  	// planNode's column indices indicating which columns are specified in
    30  	// the DISTINCT ON (<exprs>) clause.
    31  	distinctOnColIdxs util.FastIntSet
    32  
    33  	// Subset of distinctOnColIdxs on which the input guarantees an ordering.
    34  	// All rows that are equal on these columns appear contiguously in the input.
    35  	columnsInOrder util.FastIntSet
    36  
    37  	reqOrdering ReqOrdering
    38  
    39  	// nullsAreDistinct, if true, causes the distinct operation to treat NULL
    40  	// values as not equal to one another. Each NULL value will cause a new row
    41  	// group to be created. For example:
    42  	//
    43  	//   c
    44  	//   ----
    45  	//   NULL
    46  	//   NULL
    47  	//
    48  	// A distinct operation on column "c" will result in one output row if
    49  	// nullsAreDistinct is false, or two output rows if true. This is set to true
    50  	// for UPSERT and INSERT..ON CONFLICT statements, since they must treat NULL
    51  	// values as distinct.
    52  	nullsAreDistinct bool
    53  
    54  	// errorOnDup, if non-empty, is the text of the error that will be raised if
    55  	// the distinct operation finds two rows with duplicate grouping column
    56  	// values. This is used to implement the UPSERT and INSERT..ON CONFLICT
    57  	// statements, both of which prohibit the same row from being changed twice.
    58  	errorOnDup string
    59  }
    60  
    61  func (n *distinctNode) startExec(params runParams) error {
    62  	panic("distinctNode can't be called in local mode")
    63  }
    64  
    65  func (n *distinctNode) Next(params runParams) (bool, error) {
    66  	panic("distinctNode can't be called in local mode")
    67  }
    68  
    69  func (n *distinctNode) Values() tree.Datums {
    70  	panic("distinctNode can't be called in local mode")
    71  }
    72  
    73  func (n *distinctNode) Close(ctx context.Context) {
    74  	n.plan.Close(ctx)
    75  }