github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sqlbase/result_columns.go (about) 1 // Copyright 2017 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 sqlbase 12 13 import ( 14 "fmt" 15 16 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 17 "github.com/cockroachdb/cockroach/pkg/sql/types" 18 ) 19 20 // ResultColumn contains the name and type of a SQL "cell". 21 type ResultColumn struct { 22 Name string 23 Typ *types.T 24 25 // If set, this is an implicit column; used internally. 26 Hidden bool 27 28 // TableID/PGAttributeNum identify the source of the column, if it is a simple 29 // reference to a column of a base table (or view). If it is not a simple 30 // reference, these fields are zeroes. 31 TableID ID // OID of column's source table (pg_attribute.attrelid). 32 PGAttributeNum ColumnID // Column's number in source table (pg_attribute.attnum). 33 } 34 35 // ResultColumns is the type used throughout the sql module to 36 // describe the column types of a table. 37 type ResultColumns []ResultColumn 38 39 // ResultColumnsFromColDescs converts ColumnDescriptors to ResultColumns. 40 func ResultColumnsFromColDescs(tableID ID, colDescs []ColumnDescriptor) ResultColumns { 41 cols := make(ResultColumns, 0, len(colDescs)) 42 for i := range colDescs { 43 // Convert the ColumnDescriptor to ResultColumn. 44 colDesc := &colDescs[i] 45 typ := colDesc.Type 46 if typ == nil { 47 panic(fmt.Sprintf("unsupported column type: %s", colDesc.Type.Family())) 48 } 49 50 hidden := colDesc.Hidden 51 cols = append( 52 cols, 53 ResultColumn{ 54 Name: colDesc.Name, 55 Typ: typ, 56 Hidden: hidden, 57 TableID: tableID, 58 PGAttributeNum: colDesc.GetLogicalColumnID(), 59 }, 60 ) 61 } 62 return cols 63 } 64 65 // GetTypeModifier returns the type modifier for this column. If it is not set, 66 // it defaults to returning -1. 67 func (r ResultColumn) GetTypeModifier() int32 { 68 return r.Typ.TypeModifier() 69 } 70 71 // TypesEqual returns whether the length and types of r matches other. If 72 // a type in other is NULL, it is considered equal. 73 func (r ResultColumns) TypesEqual(other ResultColumns) bool { 74 if len(r) != len(other) { 75 return false 76 } 77 for i, c := range r { 78 // NULLs are considered equal because some types of queries (SELECT CASE, 79 // for example) can change their output types between a type and NULL based 80 // on input. 81 if other[i].Typ.Family() == types.UnknownFamily { 82 continue 83 } 84 if !c.Typ.Equivalent(other[i].Typ) { 85 return false 86 } 87 } 88 return true 89 } 90 91 // NodeFormatter returns a tree.NodeFormatter that, when formatted, 92 // represents the column at the input column index. 93 func (r ResultColumns) NodeFormatter(colIdx int) tree.NodeFormatter { 94 return &varFormatter{ColumnName: tree.Name(r[colIdx].Name)} 95 } 96 97 // ExplainPlanColumns are the result columns of an EXPLAIN (PLAN) ... 98 // statement. 99 var ExplainPlanColumns = ResultColumns{ 100 // Tree shows the node type with the tree structure. 101 {Name: "tree", Typ: types.String}, 102 // Field is the part of the node that a row of output pertains to. 103 {Name: "field", Typ: types.String}, 104 // Description contains details about the field. 105 {Name: "description", Typ: types.String}, 106 } 107 108 // ExplainPlanVerboseColumns are the result columns of an 109 // EXPLAIN (PLAN, ...) ... 110 // statement when a flag like VERBOSE or TYPES is passed. 111 var ExplainPlanVerboseColumns = ResultColumns{ 112 // Tree shows the node type with the tree structure. 113 {Name: "tree", Typ: types.String}, 114 // Level is the depth of the node in the tree. Hidden by default; can be 115 // retrieved using: 116 // SELECT level FROM [ EXPLAIN (VERBOSE) ... ]. 117 {Name: "level", Typ: types.Int, Hidden: true}, 118 // Type is the node type. Hidden by default. 119 {Name: "node_type", Typ: types.String, Hidden: true}, 120 // Field is the part of the node that a row of output pertains to. 121 {Name: "field", Typ: types.String}, 122 // Description contains details about the field. 123 {Name: "description", Typ: types.String}, 124 // Columns is the type signature of the data source. 125 {Name: "columns", Typ: types.String}, 126 // Ordering indicates the known ordering of the data from this source. 127 {Name: "ordering", Typ: types.String}, 128 } 129 130 // ExplainDistSQLColumns are the result columns of an 131 // EXPLAIN (DISTSQL) statement. 132 var ExplainDistSQLColumns = ResultColumns{ 133 {Name: "automatic", Typ: types.Bool}, 134 {Name: "url", Typ: types.String}, 135 {Name: "json", Typ: types.String, Hidden: true}, 136 } 137 138 // ExplainOptColumns are the result columns of an 139 // EXPLAIN (OPT) statement. 140 var ExplainOptColumns = ResultColumns{ 141 {Name: "text", Typ: types.String}, 142 } 143 144 // ExplainVecColumns are the result columns of an 145 // EXPLAIN (VEC) statement. 146 var ExplainVecColumns = ResultColumns{ 147 {Name: "text", Typ: types.String}, 148 } 149 150 // ExplainAnalyzeDebugColumns are the result columns of an 151 // EXPLAIN ANALYZE (DEBUG) statement. 152 var ExplainAnalyzeDebugColumns = ResultColumns{ 153 {Name: "text", Typ: types.String}, 154 } 155 156 // ShowTraceColumns are the result columns of a SHOW [KV] TRACE statement. 157 var ShowTraceColumns = ResultColumns{ 158 {Name: "timestamp", Typ: types.TimestampTZ}, 159 {Name: "age", Typ: types.Interval}, // Note GetTraceAgeColumnIdx below. 160 {Name: "message", Typ: types.String}, 161 {Name: "tag", Typ: types.String}, 162 {Name: "location", Typ: types.String}, 163 {Name: "operation", Typ: types.String}, 164 {Name: "span", Typ: types.Int}, 165 } 166 167 // ShowCompactTraceColumns are the result columns of a 168 // SHOW COMPACT [KV] TRACE statement. 169 var ShowCompactTraceColumns = ResultColumns{ 170 {Name: "age", Typ: types.Interval}, // Note GetTraceAgeColumnIdx below. 171 {Name: "message", Typ: types.String}, 172 {Name: "tag", Typ: types.String}, 173 {Name: "operation", Typ: types.String}, 174 } 175 176 // GetTraceAgeColumnIdx retrieves the index of the age column 177 // depending on whether the compact format is used. 178 func GetTraceAgeColumnIdx(compact bool) int { 179 if compact { 180 return 0 181 } 182 return 1 183 } 184 185 // ShowReplicaTraceColumns are the result columns of a 186 // SHOW EXPERIMENTAL_REPLICA TRACE statement. 187 var ShowReplicaTraceColumns = ResultColumns{ 188 {Name: "timestamp", Typ: types.TimestampTZ}, 189 {Name: "node_id", Typ: types.Int}, 190 {Name: "store_id", Typ: types.Int}, 191 {Name: "replica_id", Typ: types.Int}, 192 } 193 194 // ShowSyntaxColumns are the columns of a SHOW SYNTAX statement. 195 var ShowSyntaxColumns = ResultColumns{ 196 {Name: "field", Typ: types.String}, 197 {Name: "message", Typ: types.String}, 198 } 199 200 // ShowFingerprintsColumns are the result columns of a 201 // SHOW EXPERIMENTAL_FINGERPRINTS statement. 202 var ShowFingerprintsColumns = ResultColumns{ 203 {Name: "index_name", Typ: types.String}, 204 {Name: "fingerprint", Typ: types.String}, 205 } 206 207 // AlterTableSplitColumns are the result columns of an 208 // ALTER TABLE/INDEX .. SPLIT AT statement. 209 var AlterTableSplitColumns = ResultColumns{ 210 {Name: "key", Typ: types.Bytes}, 211 {Name: "pretty", Typ: types.String}, 212 {Name: "split_enforced_until", Typ: types.Timestamp}, 213 } 214 215 // AlterTableUnsplitColumns are the result columns of an 216 // ALTER TABLE/INDEX .. UNSPLIT statement. 217 var AlterTableUnsplitColumns = ResultColumns{ 218 {Name: "key", Typ: types.Bytes}, 219 {Name: "pretty", Typ: types.String}, 220 } 221 222 // AlterTableRelocateColumns are the result columns of an 223 // ALTER TABLE/INDEX .. EXPERIMENTAL_RELOCATE statement. 224 var AlterTableRelocateColumns = ResultColumns{ 225 {Name: "key", Typ: types.Bytes}, 226 {Name: "pretty", Typ: types.String}, 227 } 228 229 // AlterTableScatterColumns are the result columns of an 230 // ALTER TABLE/INDEX .. SCATTER statement. 231 var AlterTableScatterColumns = ResultColumns{ 232 {Name: "key", Typ: types.Bytes}, 233 {Name: "pretty", Typ: types.String}, 234 } 235 236 // ScrubColumns are the result columns of a SCRUB statement. 237 var ScrubColumns = ResultColumns{ 238 {Name: "job_uuid", Typ: types.Uuid}, 239 {Name: "error_type", Typ: types.String}, 240 {Name: "database", Typ: types.String}, 241 {Name: "table", Typ: types.String}, 242 {Name: "primary_key", Typ: types.String}, 243 {Name: "timestamp", Typ: types.Timestamp}, 244 {Name: "repaired", Typ: types.Bool}, 245 {Name: "details", Typ: types.Jsonb}, 246 } 247 248 // SequenceSelectColumns are the result columns of a sequence data source. 249 var SequenceSelectColumns = ResultColumns{ 250 {Name: `last_value`, Typ: types.Int}, 251 {Name: `log_cnt`, Typ: types.Int}, 252 {Name: `is_called`, Typ: types.Bool}, 253 } 254 255 // ExportColumns are the result columns of an EXPORT statement. 256 var ExportColumns = ResultColumns{ 257 {Name: "filename", Typ: types.String}, 258 {Name: "rows", Typ: types.Int}, 259 {Name: "bytes", Typ: types.Int}, 260 }