github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/cat/column.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 cat 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 15 "github.com/cockroachdb/cockroach/pkg/sql/types" 16 ) 17 18 // Column is an interface to a table column, exposing only the information 19 // needed by the query optimizer. 20 type Column interface { 21 // ColID is the unique, stable identifier for this column within its table. 22 // Each new column in the table will be assigned a new ID that is different 23 // than every column allocated before or after. This is true even if a column 24 // is dropped and then re-added with the same name; the new column will have 25 // a different ID. See the comment for StableID for more detail. 26 ColID() StableID 27 28 // ColName returns the name of the column. 29 ColName() tree.Name 30 31 // DatumType returns the data type of the column. 32 DatumType() *types.T 33 34 // ColTypePrecision returns the precision of the column's SQL data type. This 35 // is only defined for the Decimal data type and represents the max number of 36 // decimal digits in the decimal (including fractional digits). If precision 37 // is 0, then the decimal has no max precision. 38 ColTypePrecision() int 39 40 // ColTypeWidth returns the width of the column's SQL data type. This has 41 // different meanings depending on the data type: 42 // 43 // Decimal : scale 44 // Int : # bits (16, 32, 64, etc) 45 // Bit Array: # bits 46 // String : rune count 47 // 48 // TODO(andyk): Switch calling code to use DatumType. 49 ColTypeWidth() int 50 51 // ColTypeStr returns the SQL data type of the column, as a string. Note that 52 // this is sometimes different than DatumType().String(), since datum types 53 // are a subset of column types. 54 ColTypeStr() string 55 56 // IsNullable returns true if the column is nullable. 57 IsNullable() bool 58 59 // IsHidden returns true if the column is hidden (e.g., there is always a 60 // hidden column called rowid if there is no primary key on the table). 61 IsHidden() bool 62 63 // HasDefault returns true if the column has a default value. DefaultExprStr 64 // will be set to the SQL expression string in that case. 65 HasDefault() bool 66 67 // DefaultExprStr is set to the SQL expression string that describes the 68 // column's default value. It is used when the user does not provide a value 69 // for the column when inserting a row. Default values cannot depend on other 70 // columns. 71 DefaultExprStr() string 72 73 // IsComputed returns true if the column is a computed value. ComputedExprStr 74 // will be set to the SQL expression string in that case. 75 IsComputed() bool 76 77 // ComputedExprStr is set to the SQL expression string that describes the 78 // column's computed value. It is always used to provide the column's value 79 // when inserting or updating a row. Computed values cannot depend on other 80 // computed columns, but they can depend on all other columns, including 81 // columns with default values. 82 ComputedExprStr() string 83 } 84 85 // IsMutationColumn is a convenience function that returns true if the column at 86 // the given ordinal position is a mutation column. 87 func IsMutationColumn(table Table, ord int) bool { 88 return ord >= table.ColumnCount() 89 }