github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/column_meta.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 opt
    12  
    13  import (
    14  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    15  	"github.com/cockroachdb/cockroach/pkg/util"
    16  )
    17  
    18  // ColumnID uniquely identifies the usage of a column within the scope of a
    19  // query. ColumnID 0 is reserved to mean "unknown column". See the comment for
    20  // Metadata for more details.
    21  type ColumnID int32
    22  
    23  // index returns the index of the column in Metadata.cols. It's biased by 1, so
    24  // that ColumnID 0 can be be reserved to mean "unknown column".
    25  func (c ColumnID) index() int {
    26  	return int(c - 1)
    27  }
    28  
    29  // ColList is a list of column ids.
    30  //
    31  // TODO(radu): perhaps implement a FastIntList with the same "small"
    32  // representation as FastIntMap but with a slice for large cases.
    33  type ColList []ColumnID
    34  
    35  // ColumnMeta stores information about one of the columns stored in the
    36  // metadata.
    37  type ColumnMeta struct {
    38  	// MetaID is the identifier for this column that is unique within the query
    39  	// metadata.
    40  	MetaID ColumnID
    41  
    42  	// Alias is the best-effort name of this column. Since the same column in a
    43  	// query can have multiple names (using aliasing), one of those is chosen to
    44  	// be used for pretty-printing and debugging. This might be different than
    45  	// what is stored in the physical properties and is presented to end users.
    46  	Alias string
    47  
    48  	// Type is the scalar SQL type of this column.
    49  	Type *types.T
    50  
    51  	// Table is the base table to which this column belongs.
    52  	// If the column was synthesized (i.e. no base table), then it is 0.
    53  	Table TableID
    54  }
    55  
    56  // ToSet converts a column id list to a column id set.
    57  func (cl ColList) ToSet() ColSet {
    58  	var r ColSet
    59  	for _, col := range cl {
    60  		r.Add(col)
    61  	}
    62  	return r
    63  }
    64  
    65  // Find searches for a column in the list and returns its index in the list (if
    66  // successful).
    67  func (cl ColList) Find(col ColumnID) (idx int, ok bool) {
    68  	for i := range cl {
    69  		if cl[i] == col {
    70  			return i, true
    71  		}
    72  	}
    73  	return -1, false
    74  }
    75  
    76  // Equals returns true if this column list has the same columns as the given
    77  // column list, in the same order.
    78  func (cl ColList) Equals(other ColList) bool {
    79  	if len(cl) != len(other) {
    80  		return false
    81  	}
    82  	for i := range cl {
    83  		if cl[i] != other[i] {
    84  			return false
    85  		}
    86  	}
    87  	return true
    88  }
    89  
    90  // ColSetToList converts a column id set to a list, in column id order.
    91  func ColSetToList(set ColSet) ColList {
    92  	res := make(ColList, 0, set.Len())
    93  	set.ForEach(func(x ColumnID) {
    94  		res = append(res, x)
    95  	})
    96  	return res
    97  }
    98  
    99  // ColMap provides a 1:1 mapping from one column id to another. It is used by
   100  // operators that need to match columns from its inputs.
   101  type ColMap = util.FastIntMap
   102  
   103  // AliasedColumn specifies the label and id of a column.
   104  type AliasedColumn struct {
   105  	Alias string
   106  	ID    ColumnID
   107  }