github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/types/types.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 types 12 13 import ( 14 "bytes" 15 "fmt" 16 "regexp" 17 "runtime/debug" 18 "strings" 19 20 "github.com/cockroachdb/cockroachdb-parser/pkg/geo/geopb" 21 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lex" 22 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/oidext" 23 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgcode" 24 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgerror" 25 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/catid" 26 "github.com/cockroachdb/cockroachdb-parser/pkg/util/errorutil/unimplemented" 27 "github.com/cockroachdb/cockroachdb-parser/pkg/util/protoutil" 28 "github.com/cockroachdb/errors" 29 "github.com/cockroachdb/redact" 30 "github.com/gogo/protobuf/proto" 31 "github.com/lib/pq/oid" 32 ) 33 34 // T is an instance of a SQL scalar, array, or tuple type. It describes the 35 // domain of possible values which a column can return, or to which an 36 // expression can evaluate. The type system does not differentiate between 37 // nullable and non-nullable types. It is up to the caller to store that 38 // information separately if it is needed. Here are some example types: 39 // 40 // INT4 - any 32-bit integer 41 // DECIMAL(10, 3) - any base-10 value with at most 10 digits, with 42 // up to 3 to right of decimal point 43 // FLOAT[] - array of 64-bit IEEE 754 floating-point values 44 // TUPLE[TIME, VARCHAR(20)] - any pair of values where first value is a time 45 // of day and the second value is a string having 46 // up to 20 characters 47 // 48 // Fundamentally, a type consists of the following attributes, each of which has 49 // a corresponding accessor method. Some of these attributes are only defined 50 // for a subset of types. See the method comments for more details. 51 // 52 // Family - equivalence group of the type (enumeration) 53 // Oid - Postgres Object ID that describes the type (enumeration) 54 // Precision - maximum accuracy of the type (numeric) 55 // Width - maximum size or scale of the type (numeric) 56 // Locale - location which governs sorting, formatting, etc. (string) 57 // ArrayContents - array element type (T) 58 // TupleContents - slice of types of each tuple field ([]*T) 59 // TupleLabels - slice of labels of each tuple field ([]string) 60 // 61 // Some types are not currently allowed as the type of a column (e.g. nested 62 // arrays). Other usages of the types package may have similar restrictions. 63 // Each such caller is responsible for enforcing their own restrictions; it's 64 // not the concern of the types package. 65 // 66 // Implementation-wise, types.T wraps a protobuf-generated InternalType struct. 67 // The generated protobuf code defines the struct fields, marshals/unmarshals 68 // them, formats a string representation, etc. Meanwhile, the wrapper types.T 69 // struct overrides the Marshal/Unmarshal methods in order to map to/from older 70 // persisted InternalType representations. For example, older versions of 71 // InternalType (previously called ColumnType) used a VisibleType field to 72 // represent INT2, whereas newer versions use Width/Oid. Unmarshal upgrades from 73 // this old format to the new, and Marshal downgrades, thus preserving backwards 74 // compatibility. 75 // 76 // Simple (unary) scalars types 77 // ---------------------------- 78 // 79 // | SQL type | Family | Oid | Precision | Width | 80 // |-------------------|----------------|---------------|-----------|-------| 81 // | NULL (unknown) | UNKNOWN | T_unknown | 0 | 0 | 82 // | BOOL | BOOL | T_bool | 0 | 0 | 83 // | DATE | DATE | T_date | 0 | 0 | 84 // | TIMESTAMP | TIMESTAMP | T_timestamp | 0 | 0 | 85 // | INTERVAL | INTERVAL | T_interval | 0 | 0 | 86 // | TIMESTAMPTZ | TIMESTAMPTZ | T_timestamptz | 0 | 0 | 87 // | OID | OID | T_oid | 0 | 0 | 88 // | UUID | UUID | T_uuid | 0 | 0 | 89 // | INET | INET | T_inet | 0 | 0 | 90 // | TIME | TIME | T_time | 0 | 0 | 91 // | TIMETZ | TIMETZ | T_timetz | 0 | 0 | 92 // | JSON | JSONB | T_json | 0 | 0 | 93 // | JSONB | JSONB | T_jsonb | 0 | 0 | 94 // | | | | | | 95 // | BYTES | BYTES | T_bytea | 0 | 0 | 96 // | | | | | | 97 // | STRING | STRING | T_text | 0 | 0 | 98 // | STRING(N) | STRING | T_text | 0 | N | 99 // | VARCHAR | STRING | T_varchar | 0 | 0 | 100 // | VARCHAR(N) | STRING | T_varchar | 0 | N | 101 // | CHAR | STRING | T_bpchar | 0 | 1 | 102 // | CHAR(N) | STRING | T_bpchar | 0 | N | 103 // | "char" | STRING | T_char | 0 | 0 | 104 // | NAME | STRING | T_name | 0 | 0 | 105 // | | | | | | 106 // | STRING COLLATE en | COLLATEDSTRING | T_text | 0 | 0 | 107 // | STRING(N) COL... | COLLATEDSTRING | T_text | 0 | N | 108 // | VARCHAR COL... | COLLATEDSTRING | T_varchar | 0 | N | 109 // | VARCHAR(N) COL... | COLLATEDSTRING | T_varchar | 0 | N | 110 // | CHAR COL... | COLLATEDSTRING | T_bpchar | 0 | 1 | 111 // | CHAR(N) COL... | COLLATEDSTRING | T_bpchar | 0 | N | 112 // | "char" COL... | COLLATEDSTRING | T_char | 0 | 0 | 113 // | | | | | | 114 // | DECIMAL | DECIMAL | T_decimal | 0 | 0 | 115 // | DECIMAL(N) | DECIMAL | T_decimal | N | 0 | 116 // | DECIMAL(N,M) | DECIMAL | T_decimal | N | M | 117 // | | | | | | 118 // | FLOAT8 | FLOAT | T_float8 | 0 | 0 | 119 // | FLOAT4 | FLOAT | T_float4 | 0 | 0 | 120 // | | | | | | 121 // | BIT | BIT | T_bit | 0 | 1 | 122 // | BIT(N) | BIT | T_bit | 0 | N | 123 // | VARBIT | BIT | T_varbit | 0 | 0 | 124 // | VARBIT(N) | BIT | T_varbit | 0 | N | 125 // | | | | | | 126 // | INT,INTEGER | INT | T_int8 | 0 | 64 | 127 // | INT2,SMALLINT | INT | T_int2 | 0 | 16 | 128 // | INT4 | INT | T_int4 | 0 | 32 | 129 // | INT8,INT64,BIGINT | INT | T_int8 | 0 | 64 | 130 // 131 // Tuple types 132 // ----------- 133 // 134 // These cannot (yet) be used in tables but are used in DistSQL flow 135 // processors for queries that have tuple-typed intermediate results. 136 // 137 // | Field | Description | 138 // |-----------------|---------------------------------------------------------| 139 // | Family | TupleFamily | 140 // | Oid | T_record | 141 // | TupleContents | Contains tuple field types (can be recursively defined) | 142 // | TupleLabels | Contains labels for each tuple field | 143 // 144 // Array types 145 // ----------- 146 // 147 // | Field | Description | 148 // |-----------------|---------------------------------------------------------| 149 // | Family | ArrayFamily | 150 // | Oid | T__XXX (double underscores), where XXX is the Oid name | 151 // | | of a scalar type | 152 // | ArrayContents | Type of array elements (scalar, array, or tuple) | 153 // 154 // There are two special ARRAY types: 155 // 156 // | SQL type | Family | Oid | ArrayContents | 157 // |-------------------|----------------|---------------|---------------| 158 // | INT2VECTOR | ARRAY | T_int2vector | Int | 159 // | OIDVECTOR | ARRAY | T_oidvector | Oid | 160 // 161 // When these types are themselves made into arrays, the Oids become T__int2vector and 162 // T__oidvector, respectively. 163 // 164 // User defined types 165 // ------------------ 166 // 167 // * Enums 168 // | Field | Description | 169 // |---------------|--------------------------------------------| 170 // | Family | EnumFamily | 171 // | Oid | A unique OID generated upon enum creation | 172 // 173 // See types.proto for the corresponding proto definition. Its automatic 174 // type declaration is suppressed in the proto so that it is possible to 175 // add additional fields to T without serializing them. 176 type T struct { 177 // InternalType should never be directly referenced outside this package. The 178 // only reason it is exported is because gogoproto panics when printing the 179 // string representation of an unexported field. This is a problem when this 180 // struct is embedded in a larger struct (like a ColumnDescriptor). 181 InternalType InternalType 182 183 // Fields that are only populated when hydrating from a user defined 184 // type descriptor. It is assumed that a user defined type is only used 185 // once its metadata has been hydrated through the process of type resolution. 186 TypeMeta UserDefinedTypeMetadata 187 } 188 189 // UserDefinedTypeMetadata contains metadata needed for runtime 190 // operations on user defined types. The metadata must be read only. 191 type UserDefinedTypeMetadata struct { 192 // Name is the resolved name of this type. 193 Name *UserDefinedTypeName 194 195 // EnumData is non-nil iff the metadata is for an ENUM type. 196 EnumData *EnumMetadata 197 198 // Version is the descriptor version of the descriptor used to construct 199 // this version of the type metadata. 200 Version uint32 201 202 // ImplicitRecordType is true if the metadata is for an implicit record type 203 // for a table. Note: this can be deleted if we migrate implicit record types 204 // to ordinary persisted composite types. 205 ImplicitRecordType bool 206 } 207 208 // EnumMetadata is metadata about an ENUM needed for evaluation. 209 type EnumMetadata struct { 210 // PhysicalRepresentations is a slice of the byte array 211 // physical representations of enum members. 212 PhysicalRepresentations [][]byte 213 // LogicalRepresentations is a slice of the string logical 214 // representations of enum members. 215 LogicalRepresentations []string 216 // IsMemberReadOnly holds whether the enum member at index i is 217 // read only or not. 218 IsMemberReadOnly []bool 219 // TODO (rohany): For small enums, having a map would be slower 220 // than just an array. Investigate at what point the tradeoff 221 // should occur, if at all. 222 } 223 224 func (e *EnumMetadata) debugString() string { 225 return fmt.Sprintf( 226 "PhysicalReps: %v; LogicalReps: %s", 227 e.PhysicalRepresentations, 228 e.LogicalRepresentations, 229 ) 230 } 231 232 // UserDefinedTypeName is a struct representing a qualified user defined 233 // type name. We redefine a common struct from higher level packages. We 234 // do so because proto will panic if any members of a proto struct are 235 // private. Rather than expose private members of higher level packages, 236 // we define a separate type here to be safe. 237 type UserDefinedTypeName struct { 238 Catalog string 239 ExplicitSchema bool 240 Schema string 241 Name string 242 } 243 244 // Basename returns the unqualified name. 245 func (u UserDefinedTypeName) Basename() string { 246 return u.Name 247 } 248 249 // FQName returns the fully qualified name. 250 func (u UserDefinedTypeName) FQName() string { 251 return FormatTypeName(u) 252 } 253 254 // Convenience list of pre-constructed types. Caller code can use any of these 255 // types, or use the MakeXXX methods to construct a custom type that is not 256 // listed here (e.g. if a custom width is needed). 257 var ( 258 // Unknown is the type of an expression that statically evaluates to NULL. 259 // This type should never be returned for an expression that does not *always* 260 // evaluate to NULL. 261 Unknown = &T{InternalType: InternalType{ 262 Family: UnknownFamily, Oid: oid.T_unknown, Locale: &emptyLocale}} 263 264 // Bool is the type of a boolean true/false value. 265 Bool = &T{InternalType: InternalType{ 266 Family: BoolFamily, Oid: oid.T_bool, Locale: &emptyLocale}} 267 268 // VarBit is the type of an ordered list of bits (0 or 1 valued), with no 269 // specified limit on the count of bits. 270 VarBit = &T{InternalType: InternalType{ 271 Family: BitFamily, Oid: oid.T_varbit, Locale: &emptyLocale}} 272 273 // Int is the type of a 64-bit signed integer. This is the canonical type 274 // for IntFamily. 275 Int = &T{InternalType: InternalType{ 276 Family: IntFamily, Width: 64, Oid: oid.T_int8, Locale: &emptyLocale}} 277 278 // Int4 is the type of a 32-bit signed integer. 279 Int4 = &T{InternalType: InternalType{ 280 Family: IntFamily, Width: 32, Oid: oid.T_int4, Locale: &emptyLocale}} 281 282 // Int2 is the type of a 16-bit signed integer. 283 Int2 = &T{InternalType: InternalType{ 284 Family: IntFamily, Width: 16, Oid: oid.T_int2, Locale: &emptyLocale}} 285 286 // Float is the type of a 64-bit base-2 floating-point number (IEEE 754). 287 // This is the canonical type for FloatFamily. 288 Float = &T{InternalType: InternalType{ 289 Family: FloatFamily, Width: 64, Oid: oid.T_float8, Locale: &emptyLocale}} 290 291 // Float4 is the type of a 32-bit base-2 floating-point number (IEEE 754). 292 Float4 = &T{InternalType: InternalType{ 293 Family: FloatFamily, Width: 32, Oid: oid.T_float4, Locale: &emptyLocale}} 294 295 // Decimal is the type of a base-10 floating-point number, with no specified 296 // limit on precision (number of digits) or scale (digits to right of decimal 297 // point). 298 Decimal = &T{InternalType: InternalType{ 299 Family: DecimalFamily, Oid: oid.T_numeric, Locale: &emptyLocale}} 300 301 // String is the type of a Unicode string, with no specified limit on the 302 // count of characters. This is the canonical type for StringFamily. It is 303 // reported as STRING in SHOW CREATE but "text" in introspection for 304 // compatibility with PostgreSQL. 305 String = &T{InternalType: InternalType{ 306 Family: StringFamily, Oid: oid.T_text, Locale: &emptyLocale}} 307 308 // VarChar is equivalent to String, but has a differing OID (T_varchar), 309 // which makes it show up differently when displayed. It is reported as 310 // VARCHAR in SHOW CREATE and "character varying" in introspection for 311 // compatibility with PostgreSQL. 312 VarChar = &T{InternalType: InternalType{ 313 Family: StringFamily, Oid: oid.T_varchar, Locale: &emptyLocale}} 314 315 // QChar is the special "char" type that is a single-character column type. 316 // It's used by system tables. It is reported as "char" (with double quotes 317 // included) in SHOW CREATE and "char" in introspection for compatibility 318 // with PostgreSQL. 319 // 320 // See https://www.postgresql.org/docs/9.1/static/datatype-character.html 321 QChar = &T{InternalType: InternalType{ 322 Family: StringFamily, Width: 1, Oid: oid.T_char, Locale: &emptyLocale}} 323 324 // Name is a type-alias for String with a different OID (T_name). It is 325 // reported as NAME in SHOW CREATE and "name" in introspection for 326 // compatibility with PostgreSQL. 327 Name = &T{InternalType: InternalType{ 328 Family: StringFamily, Oid: oid.T_name, Locale: &emptyLocale}} 329 330 // Bytes is the type of a list of raw byte values. 331 Bytes = &T{InternalType: InternalType{ 332 Family: BytesFamily, Oid: oid.T_bytea, Locale: &emptyLocale}} 333 334 // Date is the type of a value specifying year, month, day (with no time 335 // component). There is no timezone associated with it. For example: 336 // 337 // YYYY-MM-DD 338 // 339 Date = &T{InternalType: InternalType{ 340 Family: DateFamily, Oid: oid.T_date, Locale: &emptyLocale}} 341 342 // Time is the type of a value specifying hour, minute, second (with no date 343 // component). By default, it has microsecond precision. There is no timezone 344 // associated with it. For example: 345 // 346 // HH:MM:SS.ssssss 347 // 348 Time = &T{InternalType: InternalType{ 349 Family: TimeFamily, 350 Precision: 0, 351 TimePrecisionIsSet: false, 352 Oid: oid.T_time, 353 Locale: &emptyLocale, 354 }} 355 356 // TimeTZ is the type specifying hour, minute, second and timezone with 357 // no date component. By default, it has microsecond precision. 358 // For example: 359 // 360 // HH:MM:SS.ssssss+-ZZ:ZZ 361 TimeTZ = &T{InternalType: InternalType{ 362 Family: TimeTZFamily, 363 Precision: 0, 364 TimePrecisionIsSet: false, 365 Oid: oid.T_timetz, 366 Locale: &emptyLocale, 367 }} 368 369 // Timestamp is the type of a value specifying year, month, day, hour, minute, 370 // and second, but with no associated timezone. By default, it has microsecond 371 // precision. For example: 372 // 373 // YYYY-MM-DD HH:MM:SS.ssssss 374 // 375 Timestamp = &T{InternalType: InternalType{ 376 Family: TimestampFamily, 377 Precision: 0, 378 TimePrecisionIsSet: false, 379 Oid: oid.T_timestamp, 380 Locale: &emptyLocale, 381 }} 382 383 // TimestampTZ is the type of a value specifying year, month, day, hour, 384 // minute, and second, as well as an associated timezone. By default, it has 385 // microsecond precision. For example: 386 // 387 // YYYY-MM-DD HH:MM:SS.ssssss+-ZZ:ZZ 388 // 389 TimestampTZ = &T{InternalType: InternalType{ 390 Family: TimestampTZFamily, 391 Precision: 0, 392 TimePrecisionIsSet: false, 393 Oid: oid.T_timestamptz, 394 Locale: &emptyLocale, 395 }} 396 397 // Interval is the type of a value describing a duration of time. By default, 398 // it has microsecond precision. 399 Interval = &T{InternalType: InternalType{ 400 Family: IntervalFamily, 401 Precision: 0, 402 TimePrecisionIsSet: false, 403 Oid: oid.T_interval, 404 Locale: &emptyLocale, 405 IntervalDurationField: &IntervalDurationField{}, 406 }} 407 408 // Jsonb is the type of a JavaScript Object Notation (JSON) value that is 409 // stored in a decomposed binary format (hence the "b" in jsonb). 410 Jsonb = &T{InternalType: InternalType{ 411 Family: JsonFamily, Oid: oid.T_jsonb, Locale: &emptyLocale}} 412 413 // Json is the type of a JavaScript Object Notation (JSON) value. At the time 414 // of writing, these are stored the same as Jsonb types. 415 Json = &T{InternalType: InternalType{ 416 Family: JsonFamily, Oid: oid.T_json, Locale: &emptyLocale}} 417 418 // Uuid is the type of a universally unique identifier (UUID), which is a 419 // 128-bit quantity that is very unlikely to ever be generated again, and so 420 // can be relied on to be distinct from all other UUID values. 421 Uuid = &T{InternalType: InternalType{ 422 Family: UuidFamily, Oid: oid.T_uuid, Locale: &emptyLocale}} 423 424 // INet is the type of an IPv4 or IPv6 network address. For example: 425 // 426 // 192.168.100.128/25 427 // FE80:CD00:0:CDE:1257:0:211E:729C 428 // 429 INet = &T{InternalType: InternalType{ 430 Family: INetFamily, Oid: oid.T_inet, Locale: &emptyLocale}} 431 432 // Geometry is the type of a geospatial Geometry object. 433 Geometry = &T{ 434 InternalType: InternalType{ 435 Family: GeometryFamily, 436 Oid: oidext.T_geometry, 437 Locale: &emptyLocale, 438 GeoMetadata: &GeoMetadata{}, 439 }, 440 } 441 442 // Geography is the type of a geospatial Geography object. 443 Geography = &T{ 444 InternalType: InternalType{ 445 Family: GeographyFamily, 446 Oid: oidext.T_geography, 447 Locale: &emptyLocale, 448 GeoMetadata: &GeoMetadata{}, 449 }, 450 } 451 452 // Box2D is the type of a geospatial box2d object. 453 Box2D = &T{ 454 InternalType: InternalType{ 455 Family: Box2DFamily, 456 Oid: oidext.T_box2d, 457 Locale: &emptyLocale, 458 }, 459 } 460 461 // PGLSN is the type representing a PostgreSQL LSN object. 462 PGLSN = &T{ 463 InternalType: InternalType{ 464 Family: PGLSNFamily, 465 Oid: oid.T_pg_lsn, 466 Locale: &emptyLocale, 467 }, 468 } 469 470 // Void is the type representing void. 471 Void = &T{ 472 InternalType: InternalType{ 473 Family: VoidFamily, 474 Oid: oid.T_void, 475 Locale: &emptyLocale, 476 }, 477 } 478 479 // EncodedKey is a special type used internally for passing encoded key data. 480 // It behaves similarly to Bytes in most circumstances, except 481 // encoding/decoding. It is currently used to pass around inverted index keys, 482 // which do not fully encode an object. 483 EncodedKey = &T{ 484 InternalType: InternalType{ 485 Family: EncodedKeyFamily, 486 Oid: oid.T_unknown, 487 Locale: &emptyLocale, 488 }, 489 } 490 491 // TSQuery is the tsquery type, which represents a full text search query. 492 TSQuery = &T{ 493 InternalType: InternalType{ 494 Family: TSQueryFamily, 495 Oid: oid.T_tsquery, 496 Locale: &emptyLocale, 497 }, 498 } 499 500 // TSVector is the tsvector type which represents a document compressed in 501 // a form that a tsquery query can operate on. 502 TSVector = &T{ 503 InternalType: InternalType{ 504 Family: TSVectorFamily, 505 Oid: oid.T_tsvector, 506 Locale: &emptyLocale, 507 }, 508 } 509 510 // RefCursor is the type for a variable representing the name of a cursor in a 511 // PLpgSQL routine. The underlying value is a string. 512 RefCursor = &T{ 513 InternalType: InternalType{ 514 Family: RefCursorFamily, 515 Oid: oid.T_refcursor, 516 Locale: &emptyLocale, 517 }, 518 } 519 520 // Scalar contains all types that meet this criteria: 521 // 522 // 1. Scalar type (no ArrayFamily or TupleFamily types). 523 // 2. Non-ambiguous type (no UnknownFamily or AnyFamily types). 524 // 3. Canonical type for one of the type families. 525 // 526 Scalar = []*T{ 527 Bool, 528 Box2D, 529 Int, 530 Float, 531 Decimal, 532 Date, 533 Timestamp, 534 Interval, 535 Geography, 536 Geometry, 537 String, 538 Bytes, 539 TimestampTZ, 540 Oid, 541 Uuid, 542 INet, 543 PGLSN, 544 RefCursor, 545 Time, 546 TimeTZ, 547 Jsonb, 548 VarBit, 549 } 550 551 // Any is a special type used only during static analysis as a wildcard type 552 // that matches any other type, including scalar, array, and tuple types. 553 // Execution-time values should never have this type. As an example of its 554 // use, many SQL builtin functions allow an input value to be of any type, 555 // and so use this type in their static definitions. 556 Any = &T{InternalType: InternalType{ 557 Family: AnyFamily, Oid: oid.T_anyelement, Locale: &emptyLocale}} 558 559 // AnyArray is a special type used only during static analysis as a wildcard 560 // type that matches an array having elements of any (uniform) type (including 561 // nested array types). Execution-time values should never have this type. 562 AnyArray = &T{InternalType: InternalType{ 563 Family: ArrayFamily, ArrayContents: Any, Oid: oid.T_anyarray, Locale: &emptyLocale}} 564 565 // AnyEnum is a special type only used during static analysis as a wildcard 566 // type that matches an possible enum value. Execution-time values should 567 // never have this type. 568 AnyEnum = &T{InternalType: InternalType{ 569 Family: EnumFamily, Locale: &emptyLocale, Oid: oid.T_anyenum}} 570 571 // AnyTuple is a special type used only during static analysis as a wildcard 572 // type that matches a tuple with any number of fields of any type (including 573 // tuple types). Execution-time values should never have this type. 574 AnyTuple = &T{InternalType: InternalType{ 575 Family: TupleFamily, TupleContents: []*T{Any}, Oid: oid.T_record, Locale: &emptyLocale}} 576 577 // AnyTupleArray is a special type used only during static analysis as a wildcard 578 // type that matches an array of tuples with any number of fields of any type (including 579 // tuple types). Execution-time values should never have this type. 580 AnyTupleArray = &T{InternalType: InternalType{ 581 Family: ArrayFamily, ArrayContents: AnyTuple, Oid: oid.T__record, Locale: &emptyLocale}} 582 583 // AnyCollatedString is a special type used only during static analysis as a 584 // wildcard type that matches a collated string with any locale. Execution- 585 // time values should never have this type. 586 AnyCollatedString = &T{InternalType: InternalType{ 587 Family: CollatedStringFamily, Oid: oid.T_text, Locale: &emptyLocale}} 588 589 // EmptyTuple is the tuple type with no fields. Note that this is different 590 // than AnyTuple, which is a wildcard type. 591 EmptyTuple = &T{InternalType: InternalType{ 592 Family: TupleFamily, Oid: oid.T_record, Locale: &emptyLocale}} 593 594 // StringArray is the type of an array value having String-typed elements. 595 StringArray = &T{InternalType: InternalType{ 596 Family: ArrayFamily, ArrayContents: String, Oid: oid.T__text, Locale: &emptyLocale}} 597 598 // BytesArray is the type of an array value having Byte-typed elements. 599 BytesArray = &T{InternalType: InternalType{ 600 Family: ArrayFamily, ArrayContents: Bytes, Oid: oid.T__bytea, Locale: &emptyLocale}} 601 602 // IntArray is the type of an array value having Int-typed elements. 603 IntArray = &T{InternalType: InternalType{ 604 Family: ArrayFamily, ArrayContents: Int, Oid: oid.T__int8, Locale: &emptyLocale}} 605 606 // FloatArray is the type of an array value having Float-typed elements. 607 FloatArray = &T{InternalType: InternalType{ 608 Family: ArrayFamily, ArrayContents: Float, Oid: oid.T__float8, Locale: &emptyLocale}} 609 610 // DecimalArray is the type of an array value having Decimal-typed elements. 611 DecimalArray = &T{InternalType: InternalType{ 612 Family: ArrayFamily, ArrayContents: Decimal, Oid: oid.T__numeric, Locale: &emptyLocale}} 613 614 // BoolArray is the type of an array value having Bool-typed elements. 615 BoolArray = &T{InternalType: InternalType{ 616 Family: ArrayFamily, ArrayContents: Bool, Oid: oid.T__bool, Locale: &emptyLocale}} 617 618 // UUIDArray is the type of an array value having UUID-typed elements. 619 UUIDArray = &T{InternalType: InternalType{ 620 Family: ArrayFamily, ArrayContents: Uuid, Oid: oid.T__uuid, Locale: &emptyLocale}} 621 622 // DateArray is the type of an array value having Date-typed elements. 623 DateArray = &T{InternalType: InternalType{ 624 Family: ArrayFamily, ArrayContents: Date, Oid: oid.T__date, Locale: &emptyLocale}} 625 626 // PGLSNArray is the type of an array value having PGLSN-typed elements. 627 PGLSNArray = &T{InternalType: InternalType{ 628 Family: ArrayFamily, ArrayContents: PGLSN, Oid: oid.T__pg_lsn, Locale: &emptyLocale}} 629 630 // RefCursorArray is the type of an array value having REFCURSOR-typed elements. 631 RefCursorArray = &T{InternalType: InternalType{ 632 Family: ArrayFamily, ArrayContents: RefCursor, Oid: oid.T__refcursor, Locale: &emptyLocale}} 633 634 // TimeArray is the type of an array value having Time-typed elements. 635 TimeArray = &T{InternalType: InternalType{ 636 Family: ArrayFamily, ArrayContents: Time, Oid: oid.T__time, Locale: &emptyLocale}} 637 638 // TimeTZArray is the type of an array value having TimeTZ-typed elements. 639 TimeTZArray = &T{InternalType: InternalType{ 640 Family: ArrayFamily, ArrayContents: TimeTZ, Oid: oid.T__timetz, Locale: &emptyLocale}} 641 642 // TimestampArray is the type of an array value having Timestamp-typed elements. 643 TimestampArray = &T{InternalType: InternalType{ 644 Family: ArrayFamily, ArrayContents: Timestamp, Oid: oid.T__timestamp, Locale: &emptyLocale}} 645 646 // TimestampTZArray is the type of an array value having TimestampTZ-typed elements. 647 TimestampTZArray = &T{InternalType: InternalType{ 648 Family: ArrayFamily, ArrayContents: TimestampTZ, Oid: oid.T__timestamptz, Locale: &emptyLocale}} 649 650 // IntervalArray is the type of an array value having Interval-typed elements. 651 IntervalArray = &T{InternalType: InternalType{ 652 Family: ArrayFamily, ArrayContents: Interval, Oid: oid.T__interval, Locale: &emptyLocale}} 653 654 // INetArray is the type of an array value having INet-typed elements. 655 INetArray = &T{InternalType: InternalType{ 656 Family: ArrayFamily, ArrayContents: INet, Oid: oid.T__inet, Locale: &emptyLocale}} 657 658 // VarBitArray is the type of an array value having VarBit-typed elements. 659 VarBitArray = &T{InternalType: InternalType{ 660 Family: ArrayFamily, ArrayContents: VarBit, Oid: oid.T__varbit, Locale: &emptyLocale}} 661 662 // AnyEnumArray is the type of an array value having AnyEnum-typed elements. 663 AnyEnumArray = &T{InternalType: InternalType{ 664 Family: ArrayFamily, ArrayContents: AnyEnum, Oid: oid.T_anyarray, Locale: &emptyLocale}} 665 666 // JSONBArray is the type of an array value having JSONB-typed elements. 667 JSONBArray = &T{InternalType: InternalType{ 668 Family: ArrayFamily, ArrayContents: Jsonb, Oid: oid.T__jsonb, Locale: &emptyLocale}} 669 670 // JSONArrayForDecodingOnly is the type of an array value having JSON-typed elements. 671 // Note that this struct can only used for decoding an input as we don't fully 672 // support the json array yet. 673 JSONArrayForDecodingOnly = &T{InternalType: InternalType{ 674 Family: ArrayFamily, ArrayContents: Json, Oid: oid.T__json, Locale: &emptyLocale}} 675 676 // Int2Vector is a type-alias for an array of Int2 values with a different 677 // OID (T_int2vector instead of T__int2). It is a special VECTOR type used 678 // by Postgres in system tables. Int2vectors are 0-indexed, unlike normal arrays. 679 Int2Vector = &T{InternalType: InternalType{ 680 Family: ArrayFamily, Oid: oid.T_int2vector, ArrayContents: Int2, Locale: &emptyLocale}} 681 ) 682 683 // Unexported wrapper types. 684 var ( 685 // typeBit is the SQL BIT type. It is not exported to avoid confusion with 686 // the VarBit type, and confusion over whether its default Width is 687 // unspecified or is 1. More commonly used instead is the VarBit type. 688 typeBit = &T{InternalType: InternalType{ 689 Family: BitFamily, Oid: oid.T_bit, Locale: &emptyLocale}} 690 691 // typeBpChar is a CHAR type with an unspecified width. "bp" stands for 692 // "blank padded". It is not exported to avoid confusion with QChar, as well 693 // as confusion over CHAR's default width of 1. 694 // 695 // It is reported as CHAR in SHOW CREATE and "character" in introspection for 696 // compatibility with PostgreSQL. 697 typeBpChar = &T{InternalType: InternalType{ 698 Family: StringFamily, Oid: oid.T_bpchar, Locale: &emptyLocale}} 699 700 // typeQChar is a "char" type with an unspecified width. It is not exported 701 // to avoid confusion with QChar. The "char" type should always have a width 702 // of one. A "char" type with an unspecified width is only used when the 703 // length of a "char" value cannot be determined, for example a placeholder 704 // typed as a "char" should have an unspecified width. 705 typeQChar = &T{InternalType: InternalType{ 706 Family: StringFamily, Oid: oid.T_char, Locale: &emptyLocale}} 707 ) 708 709 const ( 710 // Deprecated after 19.1, since it's now represented using the Oid field. 711 name Family = 11 712 713 // Deprecated after 19.1, since it's now represented using the Oid field. 714 int2vector Family = 200 715 716 // Deprecated after 19.1, since it's now represented using the Oid field. 717 oidvector Family = 201 718 719 visibleNONE = 0 720 721 // Deprecated after 2.1, since it's no longer used. 722 visibleINTEGER = 1 723 724 // Deprecated after 2.1, since it's now represented using the Width field. 725 visibleSMALLINT = 2 726 727 // Deprecated after 2.1, since it's now represented using the Width field. 728 visibleBIGINT = 3 729 730 // Deprecated after 2.0, since the original BIT representation was buggy. 731 visibleBIT = 4 732 733 // Deprecated after 19.1, since it's now represented using the Width field. 734 visibleREAL = 5 735 736 // Deprecated after 2.1, since it's now represented using the Width field. 737 visibleDOUBLE = 6 738 739 // Deprecated after 19.1, since it's now represented using the Oid field. 740 visibleVARCHAR = 7 741 742 // Deprecated after 19.1, since it's now represented using the Oid field. 743 visibleCHAR = 8 744 745 // Deprecated after 19.1, since it's now represented using the Oid field. 746 visibleQCHAR = 9 747 748 // Deprecated after 19.1, since it's now represented using the Oid field. 749 visibleVARBIT = 10 750 751 // OID returned for the unknown[] array type. PG has no OID for this case. 752 unknownArrayOid = 0 753 ) 754 755 const ( 756 // defaultTimePrecision is the default precision to return for time families 757 // if time is not set. 758 defaultTimePrecision = 6 759 ) 760 761 var ( 762 emptyLocale = "" 763 ) 764 765 // MakeScalar constructs a new instance of a scalar type (i.e. not array or 766 // tuple types) using the provided fields. 767 func MakeScalar(family Family, o oid.Oid, precision, width int32, locale string) *T { 768 t := OidToType[o] 769 if family != t.Family() { 770 if family != CollatedStringFamily || StringFamily != t.Family() { 771 panic(errors.AssertionFailedf("oid %d does not match %s", o, family)) 772 } 773 } 774 if family == ArrayFamily || family == TupleFamily { 775 panic(errors.AssertionFailedf("cannot make non-scalar type %s", family)) 776 } 777 if family != CollatedStringFamily && locale != "" { 778 panic(errors.AssertionFailedf("non-collation type cannot have locale %s", locale)) 779 } 780 781 timePrecisionIsSet := false 782 var intervalDurationField *IntervalDurationField 783 var geoMetadata *GeoMetadata 784 switch family { 785 case IntervalFamily: 786 intervalDurationField = &IntervalDurationField{} 787 if precision < 0 || precision > 6 { 788 panic(errors.AssertionFailedf("precision must be between 0 and 6 inclusive")) 789 } 790 timePrecisionIsSet = true 791 case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: 792 if precision < 0 || precision > 6 { 793 panic(errors.AssertionFailedf("precision must be between 0 and 6 inclusive")) 794 } 795 timePrecisionIsSet = true 796 case DecimalFamily: 797 if precision < 0 { 798 panic(errors.AssertionFailedf("negative precision is not allowed")) 799 } 800 default: 801 if precision != 0 { 802 panic(errors.AssertionFailedf("type %s cannot have precision", family)) 803 } 804 } 805 806 if width < 0 { 807 panic(errors.AssertionFailedf("negative width is not allowed")) 808 } 809 switch family { 810 case IntFamily: 811 switch width { 812 case 16, 32, 64: 813 default: 814 panic(errors.AssertionFailedf("invalid width %d for IntFamily type", width)) 815 } 816 case FloatFamily: 817 switch width { 818 case 32, 64: 819 default: 820 panic(errors.AssertionFailedf("invalid width %d for FloatFamily type", width)) 821 } 822 case DecimalFamily: 823 if width > precision { 824 panic(errors.AssertionFailedf( 825 "decimal scale %d cannot be larger than precision %d", width, precision)) 826 } 827 case StringFamily, BytesFamily, CollatedStringFamily, BitFamily: 828 // These types can have any width. 829 case GeometryFamily: 830 geoMetadata = &GeoMetadata{} 831 case GeographyFamily: 832 geoMetadata = &GeoMetadata{} 833 default: 834 if width != 0 { 835 panic(errors.AssertionFailedf("type %s cannot have width", family)) 836 } 837 } 838 839 return &T{InternalType: InternalType{ 840 Family: family, 841 Oid: o, 842 Precision: precision, 843 TimePrecisionIsSet: timePrecisionIsSet, 844 Width: width, 845 Locale: &locale, 846 IntervalDurationField: intervalDurationField, 847 GeoMetadata: geoMetadata, 848 }} 849 } 850 851 // MakeBit constructs a new instance of the BIT type (oid = T_bit) having the 852 // given max # bits (0 = unspecified number). 853 func MakeBit(width int32) *T { 854 if width == 0 { 855 return typeBit 856 } 857 if width < 0 { 858 panic(errors.AssertionFailedf("width %d cannot be negative", width)) 859 } 860 return &T{InternalType: InternalType{ 861 Family: BitFamily, Oid: oid.T_bit, Width: width, Locale: &emptyLocale}} 862 } 863 864 // MakeVarBit constructs a new instance of the BIT type (oid = T_varbit) having 865 // the given max # bits (0 = unspecified number). 866 func MakeVarBit(width int32) *T { 867 if width == 0 { 868 return VarBit 869 } 870 if width < 0 { 871 panic(errors.AssertionFailedf("width %d cannot be negative", width)) 872 } 873 return &T{InternalType: InternalType{ 874 Family: BitFamily, Width: width, Oid: oid.T_varbit, Locale: &emptyLocale}} 875 } 876 877 // MakeString constructs a new instance of the STRING type (oid = T_text) having 878 // the given max # characters (0 = unspecified number). 879 func MakeString(width int32) *T { 880 if width == 0 { 881 return String 882 } 883 if width < 0 { 884 panic(errors.AssertionFailedf("width %d cannot be negative", width)) 885 } 886 return &T{InternalType: InternalType{ 887 Family: StringFamily, Oid: oid.T_text, Width: width, Locale: &emptyLocale}} 888 } 889 890 // MakeVarChar constructs a new instance of the VARCHAR type (oid = T_varchar) 891 // having the given max # characters (0 = unspecified number). 892 func MakeVarChar(width int32) *T { 893 if width == 0 { 894 return VarChar 895 } 896 if width < 0 { 897 panic(errors.AssertionFailedf("width %d cannot be negative", width)) 898 } 899 return &T{InternalType: InternalType{ 900 Family: StringFamily, Oid: oid.T_varchar, Width: width, Locale: &emptyLocale}} 901 } 902 903 // MakeChar constructs a new instance of the CHAR type (oid = T_bpchar) having 904 // the given max # characters (0 = unspecified number). 905 func MakeChar(width int32) *T { 906 if width == 0 { 907 return typeBpChar 908 } 909 if width < 0 { 910 panic(errors.AssertionFailedf("width %d cannot be negative", width)) 911 } 912 return &T{InternalType: InternalType{ 913 Family: StringFamily, Oid: oid.T_bpchar, Width: width, Locale: &emptyLocale}} 914 } 915 916 // oidCanBeCollatedString returns true if the given oid is can be a CollatedString. 917 func oidCanBeCollatedString(o oid.Oid) bool { 918 switch o { 919 case oid.T_text, oid.T_varchar, oid.T_bpchar, oid.T_char, oid.T_name: 920 return true 921 } 922 return false 923 } 924 925 // MakeCollatedString constructs a new instance of a CollatedStringFamily type 926 // that is collated according to the given locale. The new type is based upon 927 // the given string type, having the same oid and width values. For example: 928 // 929 // STRING => STRING COLLATE EN 930 // VARCHAR(20) => VARCHAR(20) COLLATE EN 931 func MakeCollatedString(strType *T, locale string) *T { 932 if oidCanBeCollatedString(strType.Oid()) { 933 return &T{InternalType: InternalType{ 934 Family: CollatedStringFamily, Oid: strType.Oid(), Width: strType.Width(), Locale: &locale}} 935 } 936 panic(errors.AssertionFailedf("cannot apply collation to non-string type: %s", strType)) 937 } 938 939 // MakeDecimal constructs a new instance of a DECIMAL type (oid = T_numeric) 940 // that has at most "precision" # of decimal digits (0 = unspecified number of 941 // digits) and at most "scale" # of decimal digits after the decimal point 942 // (0 = unspecified number of digits). scale must be <= precision. 943 func MakeDecimal(precision, scale int32) *T { 944 if precision == 0 && scale == 0 { 945 return Decimal 946 } 947 if precision < 0 { 948 panic(errors.AssertionFailedf("precision %d cannot be negative", precision)) 949 } 950 if scale < 0 { 951 panic(errors.AssertionFailedf("scale %d cannot be negative", scale)) 952 } 953 if scale > precision { 954 panic(errors.AssertionFailedf( 955 "scale %d cannot be larger than precision %d", scale, precision)) 956 } 957 return &T{InternalType: InternalType{ 958 Family: DecimalFamily, 959 Oid: oid.T_numeric, 960 Precision: precision, 961 Width: scale, 962 Locale: &emptyLocale, 963 }} 964 } 965 966 // MakeTime constructs a new instance of a TIME type (oid = T_time) that has at 967 // most the given number of fractional second digits. 968 // 969 // To use the default precision, use the `Time` variable. 970 func MakeTime(precision int32) *T { 971 return &T{InternalType: InternalType{ 972 Family: TimeFamily, 973 Oid: oid.T_time, 974 Precision: precision, 975 TimePrecisionIsSet: true, 976 Locale: &emptyLocale, 977 }} 978 } 979 980 // MakeTimeTZ constructs a new instance of a TIMETZ type (oid = T_timetz) that 981 // has at most the given number of fractional second digits. 982 // 983 // To use the default precision, use the `TimeTZ` variable. 984 func MakeTimeTZ(precision int32) *T { 985 return &T{InternalType: InternalType{ 986 Family: TimeTZFamily, 987 Oid: oid.T_timetz, 988 Precision: precision, 989 TimePrecisionIsSet: true, 990 Locale: &emptyLocale, 991 }} 992 } 993 994 // MakeGeometry constructs a new instance of a GEOMETRY type (oid = T_geometry) 995 // that has the given shape and SRID. 996 func MakeGeometry(shape geopb.ShapeType, srid geopb.SRID) *T { 997 // Negative values are promoted to 0. 998 if srid < 0 { 999 srid = 0 1000 } 1001 return &T{InternalType: InternalType{ 1002 Family: GeometryFamily, 1003 Oid: oidext.T_geometry, 1004 Locale: &emptyLocale, 1005 GeoMetadata: &GeoMetadata{ 1006 ShapeType: shape, 1007 SRID: srid, 1008 }, 1009 }} 1010 } 1011 1012 // MakeGeography constructs a new instance of a geography-related type. 1013 func MakeGeography(shape geopb.ShapeType, srid geopb.SRID) *T { 1014 // Negative values are promoted to 0. 1015 if srid < 0 { 1016 srid = 0 1017 } 1018 return &T{InternalType: InternalType{ 1019 Family: GeographyFamily, 1020 Oid: oidext.T_geography, 1021 Locale: &emptyLocale, 1022 GeoMetadata: &GeoMetadata{ 1023 ShapeType: shape, 1024 SRID: srid, 1025 }, 1026 }} 1027 } 1028 1029 // GeoMetadata returns the GeoMetadata of the type object if it exists. 1030 // This should only exist on Geometry and Geography types. 1031 func (t *T) GeoMetadata() (*GeoMetadata, error) { 1032 if t.InternalType.GeoMetadata == nil { 1033 return nil, errors.Newf("GeoMetadata does not exist on type") 1034 } 1035 return t.InternalType.GeoMetadata, nil 1036 } 1037 1038 // GeoSRIDOrZero returns the geo SRID of the type object if it exists. 1039 // This should only exist on a subset of Geometry and Geography types. 1040 func (t *T) GeoSRIDOrZero() geopb.SRID { 1041 if t.InternalType.GeoMetadata != nil { 1042 return t.InternalType.GeoMetadata.SRID 1043 } 1044 return 0 1045 } 1046 1047 var ( 1048 // DefaultIntervalTypeMetadata returns a duration field that is unset, 1049 // using INTERVAL or INTERVAL ( iconst32 ) syntax instead of INTERVAL 1050 // with a qualifier afterwards. 1051 DefaultIntervalTypeMetadata = IntervalTypeMetadata{} 1052 ) 1053 1054 // IntervalTypeMetadata is metadata pertinent for intervals. 1055 type IntervalTypeMetadata struct { 1056 // DurationField represents the duration field definition. 1057 DurationField IntervalDurationField 1058 // Precision is the precision to use - note this matches InternalType rules. 1059 Precision int32 1060 // PrecisionIsSet indicates whether Precision is explicitly set. 1061 PrecisionIsSet bool 1062 } 1063 1064 // IsMinuteToSecond returns whether the IntervalDurationField represents 1065 // the MINUTE TO SECOND interval qualifier. 1066 func (m *IntervalDurationField) IsMinuteToSecond() bool { 1067 return m.FromDurationType == IntervalDurationType_MINUTE && 1068 m.DurationType == IntervalDurationType_SECOND 1069 } 1070 1071 // IsDayToHour returns whether the IntervalDurationField represents 1072 // the DAY TO HOUR interval qualifier. 1073 func (m *IntervalDurationField) IsDayToHour() bool { 1074 return m.FromDurationType == IntervalDurationType_DAY && 1075 m.DurationType == IntervalDurationType_HOUR 1076 } 1077 1078 // IntervalTypeMetadata returns the IntervalTypeMetadata for interval types. 1079 func (t *T) IntervalTypeMetadata() (IntervalTypeMetadata, error) { 1080 if t.Family() != IntervalFamily { 1081 return IntervalTypeMetadata{}, errors.Newf("cannot call IntervalTypeMetadata on non-intervals") 1082 } 1083 return IntervalTypeMetadata{ 1084 DurationField: *t.InternalType.IntervalDurationField, 1085 Precision: t.InternalType.Precision, 1086 PrecisionIsSet: t.InternalType.TimePrecisionIsSet, 1087 }, nil 1088 } 1089 1090 // MakeInterval constructs a new instance of a 1091 // INTERVAL type (oid = T_interval) with a duration field. 1092 // 1093 // To use the default precision and field, use the `Interval` variable. 1094 func MakeInterval(itm IntervalTypeMetadata) *T { 1095 switch itm.DurationField.DurationType { 1096 case IntervalDurationType_SECOND, IntervalDurationType_UNSET: 1097 default: 1098 if itm.PrecisionIsSet { 1099 panic(errors.Errorf("cannot set precision for duration type %s", itm.DurationField.DurationType)) 1100 } 1101 } 1102 if itm.Precision > 0 && !itm.PrecisionIsSet { 1103 panic(errors.Errorf("precision must be set if Precision > 0")) 1104 } 1105 1106 return &T{InternalType: InternalType{ 1107 Family: IntervalFamily, 1108 Oid: oid.T_interval, 1109 Locale: &emptyLocale, 1110 Precision: itm.Precision, 1111 TimePrecisionIsSet: itm.PrecisionIsSet, 1112 IntervalDurationField: &itm.DurationField, 1113 }} 1114 } 1115 1116 // MakeTimestamp constructs a new instance of a TIMESTAMP type that has at most 1117 // the given number of fractional second digits. 1118 // 1119 // To use the default precision, use the `Timestamp` variable. 1120 func MakeTimestamp(precision int32) *T { 1121 return &T{InternalType: InternalType{ 1122 Family: TimestampFamily, 1123 Oid: oid.T_timestamp, 1124 Precision: precision, 1125 TimePrecisionIsSet: true, 1126 Locale: &emptyLocale, 1127 }} 1128 } 1129 1130 // MakeTimestampTZ constructs a new instance of a TIMESTAMPTZ type that has at 1131 // most the given number of fractional second digits. 1132 // 1133 // To use the default precision, use the `TimestampTZ` variable. 1134 func MakeTimestampTZ(precision int32) *T { 1135 return &T{InternalType: InternalType{ 1136 Family: TimestampTZFamily, 1137 Oid: oid.T_timestamptz, 1138 Precision: precision, 1139 TimePrecisionIsSet: true, 1140 Locale: &emptyLocale, 1141 }} 1142 } 1143 1144 // MakeEnum constructs a new instance of an EnumFamily type with the given 1145 // stable type ID. Note that it does not hydrate cached fields on the type. 1146 func MakeEnum(typeOID, arrayTypeOID oid.Oid) *T { 1147 return &T{InternalType: InternalType{ 1148 Family: EnumFamily, 1149 Oid: typeOID, 1150 Locale: &emptyLocale, 1151 UDTMetadata: &PersistentUserDefinedTypeMetadata{ 1152 ArrayTypeOID: arrayTypeOID, 1153 }, 1154 }} 1155 } 1156 1157 // MakeArray constructs a new instance of an ArrayFamily type with the given 1158 // element type (which may itself be an ArrayFamily type). 1159 func MakeArray(typ *T) *T { 1160 // Do not make an array of type unknown[]. Follow Postgres' behavior and 1161 // convert this to type string[]. 1162 if typ.Family() == UnknownFamily { 1163 typ = String 1164 } 1165 arr := &T{InternalType: InternalType{ 1166 Family: ArrayFamily, 1167 Oid: CalcArrayOid(typ), 1168 ArrayContents: typ, 1169 Locale: &emptyLocale, 1170 }} 1171 return arr 1172 } 1173 1174 // MakeTuple constructs a new instance of a TupleFamily type with the given 1175 // field types (some/all of which may be other TupleFamily types). 1176 // 1177 // Warning: the contents slice is used directly; the caller should not modify it 1178 // after calling this function. 1179 func MakeTuple(contents []*T) *T { 1180 return &T{InternalType: InternalType{ 1181 Family: TupleFamily, Oid: oid.T_record, TupleContents: contents, Locale: &emptyLocale, 1182 }} 1183 } 1184 1185 // MakeLabeledTuple constructs a new instance of a TupleFamily type with the 1186 // given field types and labels. 1187 func MakeLabeledTuple(contents []*T, labels []string) *T { 1188 if len(contents) != len(labels) && labels != nil { 1189 panic(errors.AssertionFailedf( 1190 "tuple contents and labels must be of same length: %v, %v", contents, labels)) 1191 } 1192 return &T{InternalType: InternalType{ 1193 Family: TupleFamily, 1194 Oid: oid.T_record, 1195 TupleContents: contents, 1196 TupleLabels: labels, 1197 Locale: &emptyLocale, 1198 }} 1199 } 1200 1201 // NewCompositeType constructs a new instance of a TupleFamily type with the 1202 // given field types and labels, and the given user-defined type OIDs. 1203 func NewCompositeType(typeOID, arrayTypeOID oid.Oid, contents []*T, labels []string) *T { 1204 if len(contents) != len(labels) && labels != nil { 1205 panic(errors.AssertionFailedf( 1206 "tuple contents and labels must be of same length: %v, %v", contents, labels)) 1207 } 1208 return &T{InternalType: InternalType{ 1209 Family: TupleFamily, 1210 Oid: typeOID, 1211 TupleContents: contents, 1212 TupleLabels: labels, 1213 Locale: &emptyLocale, 1214 UDTMetadata: &PersistentUserDefinedTypeMetadata{ 1215 ArrayTypeOID: arrayTypeOID, 1216 }, 1217 }} 1218 } 1219 1220 // Family specifies a group of types that are compatible with one another. Types 1221 // in the same family can be compared, assigned, etc., but may differ from one 1222 // another in width, precision, locale, and other attributes. For example, it is 1223 // always an error to insert an INT value into a FLOAT column, because they are 1224 // not in the same family. However, values of different types within the same 1225 // family are "insert-compatible" with one another. Insertion may still result 1226 // in an error because of width overflow or other constraints, but it can at 1227 // least be attempted. 1228 // 1229 // Families are convenient for performing type switches on types, because in 1230 // most cases it is the type family that matters, not the specific type. For 1231 // example, when CRDB encodes values, it maintains routines for each family, 1232 // since types in the same family encode in very similar ways. 1233 // 1234 // Most type families have an associated "canonical type" that is the default 1235 // representative of that family, and which is a superset of all other types in 1236 // that family. Values with other types (in the same family) can always be 1237 // trivially converted to the canonical type with no loss of information. For 1238 // example, the canonical type for IntFamily is Int, which is a 64-bit integer. 1239 // Both 32-bit and 16-bit integers can be trivially converted to it. 1240 // 1241 // Execution operators and functions are permissive in terms of input (allow any 1242 // type within a given family), and typically return only values having 1243 // canonical types as output. For example, the IntFamily Plus operator allows 1244 // values having any IntFamily type as input. But then it will always convert 1245 // those values to 64-bit integers, and return a final 64-bit integer value 1246 // (types.Int). Doing this vastly reduces the required number of operator 1247 // overloads. 1248 func (t *T) Family() Family { 1249 return t.InternalType.Family 1250 } 1251 1252 // Oid returns the type's Postgres Object ID. The OID identifies the type more 1253 // specifically than the type family, and is used by the Postgres wire protocol 1254 // various Postgres catalog tables, functions like pg_typeof, etc. Maintaining 1255 // the OID is required for Postgres-compatibility. 1256 func (t *T) Oid() oid.Oid { 1257 return t.InternalType.Oid 1258 } 1259 1260 // Locale identifies a specific geographical, political, or cultural region that 1261 // impacts various character-based operations such as sorting, pattern matching, 1262 // and builtin functions like lower and upper. It is only defined for the 1263 // types in the CollatedStringFamily, and is the empty string for all other 1264 // types. 1265 func (t *T) Locale() string { 1266 return *t.InternalType.Locale 1267 } 1268 1269 // Width is the size or scale of the type, such as number of bits or characters. 1270 // 1271 // INT : # of bits (64, 32, 16) 1272 // FLOAT : # of bits (64, 32) 1273 // DECIMAL : max # of digits after decimal point (must be <= Precision) 1274 // STRING : max # of characters 1275 // COLLATEDSTRING: max # of characters 1276 // BIT : max # of bits 1277 // 1278 // Width is always 0 for other types. 1279 func (t *T) Width() int32 { 1280 return t.InternalType.Width 1281 } 1282 1283 // Precision is the accuracy of the data type. 1284 // 1285 // DECIMAL : max # digits (must be >= Width/Scale) 1286 // INTERVAL : max # fractional second digits 1287 // TIME : max # fractional second digits 1288 // TIMETZ : max # fractional second digits 1289 // TIMESTAMP : max # fractional second digits 1290 // TIMESTAMPTZ: max # fractional second digits 1291 // 1292 // Precision for time-related families has special rules for 0 -- see 1293 // `precision_is_set` on the `InternalType` proto. 1294 // 1295 // Precision is always 0 for other types. 1296 func (t *T) Precision() int32 { 1297 switch t.InternalType.Family { 1298 case IntervalFamily, TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: 1299 if t.InternalType.Precision == 0 && !t.InternalType.TimePrecisionIsSet { 1300 return defaultTimePrecision 1301 } 1302 } 1303 return t.InternalType.Precision 1304 } 1305 1306 // TypeModifier returns the type modifier of the type. This corresponds to the 1307 // pg_attribute.atttypmod column. atttypmod records type-specific data supplied 1308 // at table creation time (for example, the maximum length of a varchar column). 1309 // Array types have the same type modifier as the contents of the array. 1310 // The value will be -1 for types that do not need atttypmod. 1311 func (t *T) TypeModifier() int32 { 1312 if t.Family() == ArrayFamily { 1313 return t.ArrayContents().TypeModifier() 1314 } 1315 // The type modifier for "char" is always -1. 1316 if t.Oid() == oid.T_char { 1317 return int32(-1) 1318 } 1319 1320 switch t.Family() { 1321 case StringFamily, CollatedStringFamily: 1322 if width := t.Width(); width != 0 { 1323 // Postgres adds 4 to the attypmod for bounded string types, the 1324 // var header size. 1325 return width + 4 1326 } 1327 case BitFamily: 1328 if width := t.Width(); width != 0 { 1329 return width 1330 } 1331 case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily, IntervalFamily: 1332 // For timestamp the precision is the type modifier value. 1333 if !t.InternalType.TimePrecisionIsSet { 1334 return -1 1335 } 1336 return t.Precision() 1337 case DecimalFamily: 1338 // attTypMod is calculated by putting the precision in the upper 1339 // bits and the scale in the lower bits of a 32-bit int, and adding 1340 // 4 (the var header size). We mock this for clients' sake. See 1341 // https://github.com/postgres/postgres/blob/5a2832465fd8984d089e8c44c094e6900d987fcd/src/backend/utils/adt/numeric.c#L1242. 1342 if width, precision := t.Width(), t.Precision(); precision != 0 || width != 0 { 1343 return ((precision << 16) | width) + 4 1344 } 1345 } 1346 return int32(-1) 1347 } 1348 1349 // WithoutTypeModifiers returns a copy of the given type with the type modifiers 1350 // reset, if the type has modifiers. The returned type has arbitrary width and 1351 // precision, or for some types, like timestamps, the maximum allowed width and 1352 // precision. If the given type already has no type modifiers, it is returned 1353 // unchanged and the function does not allocate a new type. 1354 func (t *T) WithoutTypeModifiers() *T { 1355 switch t.Family() { 1356 case ArrayFamily: 1357 // Remove type modifiers of the array content type. 1358 newContents := t.ArrayContents().WithoutTypeModifiers() 1359 if newContents == t.ArrayContents() { 1360 return t 1361 } 1362 return MakeArray(newContents) 1363 case TupleFamily: 1364 // Remove type modifiers for each of the tuple content types. 1365 oldContents := t.TupleContents() 1366 newContents := make([]*T, len(oldContents)) 1367 changed := false 1368 for i := range newContents { 1369 newContents[i] = oldContents[i].WithoutTypeModifiers() 1370 if newContents[i] != oldContents[i] { 1371 changed = true 1372 } 1373 } 1374 if !changed { 1375 return t 1376 } 1377 return MakeTuple(newContents) 1378 case EnumFamily: 1379 // Enums have no type modifiers. 1380 return t 1381 } 1382 1383 // For types that can be a collated string, we copy the type and set the width 1384 // to 0 rather than returning the default OidToType type so that we retain the 1385 // locale value if the type is collated. 1386 if oidCanBeCollatedString(t.Oid()) { 1387 newT := *t 1388 newT.InternalType.Width = 0 1389 return &newT 1390 } 1391 1392 typ, ok := OidToType[t.Oid()] 1393 if !ok { 1394 // These special cases for json, json[] is here so we can 1395 // support decoding parameters with oid=json/json[] without 1396 // adding full support for these type. 1397 // TODO(sql-exp): Remove this if we support JSON. 1398 if t.Oid() == oid.T_json { 1399 return Jsonb 1400 } 1401 if t.Oid() == oid.T__json { 1402 return JSONArrayForDecodingOnly 1403 } 1404 panic(errors.AssertionFailedf("unexpected OID: %d", t.Oid())) 1405 } 1406 return typ 1407 } 1408 1409 // Scale is an alias method for Width, used for clarity for types in 1410 // DecimalFamily. 1411 func (t *T) Scale() int32 { 1412 return t.InternalType.Width 1413 } 1414 1415 // ArrayContents returns the type of array elements. This is nil for types that 1416 // are not in the ArrayFamily. 1417 func (t *T) ArrayContents() *T { 1418 return t.InternalType.ArrayContents 1419 } 1420 1421 // TupleContents returns a slice containing the type of each tuple field. This 1422 // is nil for non-TupleFamily types. 1423 func (t *T) TupleContents() []*T { 1424 return t.InternalType.TupleContents 1425 } 1426 1427 // TupleLabels returns a slice containing the labels of each tuple field. This 1428 // is nil for types not in the TupleFamily, or if the tuple type does not 1429 // specify labels. 1430 func (t *T) TupleLabels() []string { 1431 return t.InternalType.TupleLabels 1432 } 1433 1434 // UserDefinedArrayOID returns the OID of the array type that corresponds to 1435 // this user defined type. This function only can only be called on user 1436 // defined types and returns non-zero data only for user defined types that 1437 // aren't arrays. 1438 func (t *T) UserDefinedArrayOID() oid.Oid { 1439 if t.InternalType.UDTMetadata == nil { 1440 return 0 1441 } 1442 return t.InternalType.UDTMetadata.ArrayTypeOID 1443 } 1444 1445 // RemapUserDefinedTypeOIDs is used to remap OIDs stored within a types.T 1446 // that is a user defined type. The newArrayOID argument is ignored if the 1447 // input type is an Array type. It mutates the input types.T and should only 1448 // be used when type is known to not be shared. If the input oid values are 1449 // 0 then the RemapUserDefinedTypeOIDs has no effect. 1450 func RemapUserDefinedTypeOIDs(t *T, newOID, newArrayOID oid.Oid) { 1451 if newOID != 0 { 1452 t.InternalType.Oid = newOID 1453 } 1454 if t.Family() != ArrayFamily && newArrayOID != 0 { 1455 t.InternalType.UDTMetadata.ArrayTypeOID = newArrayOID 1456 } 1457 } 1458 1459 // UserDefined returns whether or not t is a user defined type. 1460 func (t *T) UserDefined() bool { 1461 return IsOIDUserDefinedType(t.Oid()) 1462 } 1463 1464 // IsOIDUserDefinedType returns whether or not o corresponds to a user 1465 // defined type. 1466 func IsOIDUserDefinedType(o oid.Oid) bool { 1467 return catid.IsOIDUserDefined(o) 1468 } 1469 1470 var familyNames = map[Family]redact.SafeString{ 1471 AnyFamily: "any", 1472 ArrayFamily: "array", 1473 BitFamily: "bit", 1474 BoolFamily: "bool", 1475 Box2DFamily: "box2d", 1476 BytesFamily: "bytes", 1477 CollatedStringFamily: "collatedstring", 1478 DateFamily: "date", 1479 DecimalFamily: "decimal", 1480 EnumFamily: "enum", 1481 FloatFamily: "float", 1482 GeographyFamily: "geography", 1483 GeometryFamily: "geometry", 1484 INetFamily: "inet", 1485 IntFamily: "int", 1486 IntervalFamily: "interval", 1487 JsonFamily: "jsonb", 1488 OidFamily: "oid", 1489 PGLSNFamily: "pg_lsn", 1490 RefCursorFamily: "refcursor", 1491 StringFamily: "string", 1492 TimeFamily: "time", 1493 TimestampFamily: "timestamp", 1494 TimestampTZFamily: "timestamptz", 1495 TimeTZFamily: "timetz", 1496 TSQueryFamily: "tsquery", 1497 TSVectorFamily: "tsvector", 1498 TupleFamily: "tuple", 1499 UnknownFamily: "unknown", 1500 UuidFamily: "uuid", 1501 VoidFamily: "void", 1502 EncodedKeyFamily: "encodedkey", 1503 } 1504 1505 // Name returns a user-friendly word indicating the family type. 1506 // 1507 // TODO(radu): investigate whether anything breaks if we use 1508 // enumvalue_customname and use String() instead. 1509 func (f Family) Name() redact.SafeString { 1510 ret, ok := familyNames[f] 1511 if !ok { 1512 panic(errors.AssertionFailedf("unexpected Family: %d", f)) 1513 } 1514 return ret 1515 } 1516 1517 // Name returns a single word description of the type that describes it 1518 // succinctly, but without all the details, such as width, locale, etc. The name 1519 // is sometimes the same as the name returned by SQLStandardName, but is more 1520 // CRDB friendly. 1521 // 1522 // TODO(andyk): Should these be changed to be the same as SQLStandardName? 1523 func (t *T) Name() string { 1524 switch fam := t.Family(); fam { 1525 case AnyFamily: 1526 return "anyelement" 1527 1528 case ArrayFamily: 1529 switch t.Oid() { 1530 case oid.T_oidvector: 1531 return "oidvector" 1532 case oid.T_int2vector: 1533 return "int2vector" 1534 } 1535 return t.ArrayContents().Name() + "[]" 1536 1537 case BitFamily: 1538 if t.Oid() == oid.T_varbit { 1539 return "varbit" 1540 } 1541 return "bit" 1542 1543 case FloatFamily: 1544 switch t.Width() { 1545 case 64: 1546 return "float" 1547 case 32: 1548 return "float4" 1549 default: 1550 panic(errors.AssertionFailedf("programming error: unknown float width: %d", t.Width())) 1551 } 1552 1553 case IntFamily: 1554 switch t.Width() { 1555 case 64: 1556 return "int" 1557 case 32: 1558 return "int4" 1559 case 16: 1560 return "int2" 1561 default: 1562 panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width())) 1563 } 1564 1565 case OidFamily: 1566 return t.SQLStandardName() 1567 1568 case StringFamily, CollatedStringFamily: 1569 switch t.Oid() { 1570 case oid.T_text: 1571 return "string" 1572 case oid.T_bpchar: 1573 return "char" 1574 case oid.T_char: 1575 // Yes, that's the name. The ways of PostgreSQL are inscrutable. 1576 return `"char"` 1577 case oid.T_varchar: 1578 return "varchar" 1579 case oid.T_name: 1580 return "name" 1581 } 1582 panic(errors.AssertionFailedf("unexpected OID: %d", t.Oid())) 1583 1584 case TupleFamily: 1585 return t.SQLStandardName() 1586 1587 case EnumFamily: 1588 if t.Oid() == oid.T_anyenum { 1589 return "anyenum" 1590 } 1591 // This can be nil during unit testing. 1592 if t.TypeMeta.Name == nil { 1593 return "unknown_enum" 1594 } 1595 return t.TypeMeta.Name.Basename() 1596 1597 default: 1598 return string(fam.Name()) 1599 } 1600 } 1601 1602 // PGName returns the Postgres name for the type. This is sometimes different 1603 // than the native CRDB name for it (i.e. the Name function). It is used when 1604 // compatibility with PG is important. Examples of differences: 1605 // 1606 // Name() PGName() 1607 // -------------------------- 1608 // char bpchar 1609 // "char" char 1610 // bytes bytea 1611 // int4[] _int4 1612 func (t *T) PGName() string { 1613 name, ok := oidext.TypeName(t.Oid()) 1614 if ok { 1615 return strings.ToLower(name) 1616 } 1617 1618 // For user defined types, return the basename. 1619 if t.UserDefined() { 1620 return t.TypeMeta.Name.Basename() 1621 } 1622 1623 // Postgres does not have an UNKNOWN[] type. However, CRDB does, so 1624 // manufacture a name for it. 1625 if t.Family() != ArrayFamily || t.ArrayContents().Family() != UnknownFamily { 1626 panic(errors.AssertionFailedf("unknown PG name for oid %d", t.Oid())) 1627 } 1628 return "_unknown" 1629 } 1630 1631 // SQLStandardName returns the type's name as it is specified in the SQL 1632 // standard (or by Postgres for any non-standard types). This can be looked up 1633 // for any type in Postgres using a query similar to this: 1634 // 1635 // SELECT format_type(pg_typeof(1::int)::regtype, NULL) 1636 func (t *T) SQLStandardName() string { 1637 return t.SQLStandardNameWithTypmod(false, 0) 1638 } 1639 1640 var telemetryNameReplaceRegex = regexp.MustCompile("[^a-zA-Z0-9]") 1641 1642 // TelemetryName returns a name that is friendly for telemetry. 1643 func (t *T) TelemetryName() string { 1644 return strings.ToLower(telemetryNameReplaceRegex.ReplaceAllString(t.SQLString(), "_")) 1645 } 1646 1647 // SQLStandardNameWithTypmod is like SQLStandardName but it also accepts a 1648 // typmod argument, and a boolean which indicates whether or not a typmod was 1649 // even specified. The expected results of this function should be, in Postgres: 1650 // 1651 // SELECT format_type('thetype'::regype, typmod) 1652 // 1653 // Generally, what this does with a non-0 typmod is append the scale, precision 1654 // or length of a datatype to the name of the datatype. For example, a 1655 // varchar(20) would appear as character varying(20) when provided the typmod 1656 // value for varchar(20), which happens to be 24. 1657 // 1658 // This function is full of special cases. See backend/utils/adt/format_type.c 1659 // in Postgres. 1660 func (t *T) SQLStandardNameWithTypmod(haveTypmod bool, typmod int) string { 1661 var buf strings.Builder 1662 switch t.Family() { 1663 case AnyFamily: 1664 return "anyelement" 1665 case ArrayFamily: 1666 switch t.Oid() { 1667 case oid.T_oidvector: 1668 return "oidvector" 1669 case oid.T_int2vector: 1670 return "int2vector" 1671 } 1672 // If we have a typemod specified then pass it down when 1673 // formatting the array type. 1674 if !haveTypmod { 1675 return t.ArrayContents().SQLStandardName() + "[]" 1676 } else { 1677 ac := t.ArrayContents() 1678 return ac.SQLStandardNameWithTypmod(haveTypmod, typmod) + "[]" 1679 } 1680 case BitFamily: 1681 if t.Oid() == oid.T_varbit { 1682 buf.WriteString("bit varying") 1683 } else { 1684 buf.WriteString("bit") 1685 } 1686 if !haveTypmod || typmod <= 0 { 1687 return buf.String() 1688 } 1689 buf.WriteString(fmt.Sprintf("(%d)", typmod)) 1690 return buf.String() 1691 case BoolFamily: 1692 return "boolean" 1693 case Box2DFamily: 1694 return "box2d" 1695 case BytesFamily: 1696 return "bytea" 1697 case DateFamily: 1698 return "date" 1699 case DecimalFamily: 1700 if !haveTypmod || typmod <= 0 { 1701 return "numeric" 1702 } 1703 // The typmod of a numeric has the precision in the upper bits and the 1704 // scale in the lower bits of a 32-bit int, after subtracting 4 (the var 1705 // header size). See numeric.c. 1706 typmod -= 4 1707 return fmt.Sprintf( 1708 "numeric(%d,%d)", 1709 (typmod>>16)&0xffff, 1710 typmod&0xffff, 1711 ) 1712 1713 case FloatFamily: 1714 switch t.Width() { 1715 case 32: 1716 return "real" 1717 case 64: 1718 return "double precision" 1719 default: 1720 panic(errors.AssertionFailedf("programming error: unknown float width: %d", t.Width())) 1721 } 1722 case GeometryFamily, GeographyFamily: 1723 return t.Name() + t.InternalType.GeoMetadata.SQLString() 1724 case INetFamily: 1725 return "inet" 1726 case IntFamily: 1727 switch t.Width() { 1728 case 16: 1729 return "smallint" 1730 case 32: 1731 // PG shows "integer" for int4. 1732 return "integer" 1733 case 64: 1734 return "bigint" 1735 default: 1736 panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width())) 1737 } 1738 case IntervalFamily: 1739 if !haveTypmod || typmod < 0 { 1740 return "interval" 1741 } 1742 return fmt.Sprintf("interval(%d)", typmod) 1743 case JsonFamily: 1744 // Only binary JSON is currently supported. 1745 return "jsonb" 1746 case OidFamily: 1747 switch t.Oid() { 1748 case oid.T_oid: 1749 return "oid" 1750 case oid.T_regclass: 1751 return "regclass" 1752 case oid.T_regnamespace: 1753 return "regnamespace" 1754 case oid.T_regproc: 1755 return "regproc" 1756 case oid.T_regprocedure: 1757 return "regprocedure" 1758 case oid.T_regrole: 1759 return "regrole" 1760 case oid.T_regtype: 1761 return "regtype" 1762 default: 1763 panic(errors.AssertionFailedf("unexpected Oid: %v", errors.Safe(t.Oid()))) 1764 } 1765 case PGLSNFamily: 1766 return "pg_lsn" 1767 case RefCursorFamily: 1768 return "refcursor" 1769 case StringFamily, CollatedStringFamily: 1770 switch t.Oid() { 1771 case oid.T_text: 1772 buf.WriteString("text") 1773 case oid.T_varchar: 1774 buf.WriteString("character varying") 1775 case oid.T_bpchar: 1776 if haveTypmod && typmod < 0 { 1777 // Special case. Run `select format_type('bpchar'::regtype, -1);` in pg. 1778 return "bpchar" 1779 } 1780 buf.WriteString("character") 1781 case oid.T_char: 1782 // Type modifiers not allowed for "char". 1783 return `"char"` 1784 case oid.T_name: 1785 // Type modifiers not allowed for name. 1786 return "name" 1787 default: 1788 panic(errors.AssertionFailedf("unexpected OID: %d", t.Oid())) 1789 } 1790 if !haveTypmod { 1791 return buf.String() 1792 } 1793 1794 // Typmod gets subtracted by 4 for all non-text string-like types to produce 1795 // the length. 1796 if t.Oid() != oid.T_text { 1797 typmod -= 4 1798 } 1799 if typmod <= 0 { 1800 // In this case, we don't print any modifier. 1801 return buf.String() 1802 } 1803 buf.WriteString(fmt.Sprintf("(%d)", typmod)) 1804 return buf.String() 1805 1806 case TimeFamily: 1807 if !haveTypmod || typmod < 0 { 1808 return "time without time zone" 1809 } 1810 return fmt.Sprintf("time(%d) without time zone", typmod) 1811 case TimeTZFamily: 1812 if !haveTypmod || typmod < 0 { 1813 return "time with time zone" 1814 } 1815 return fmt.Sprintf("time(%d) with time zone", typmod) 1816 case TimestampFamily: 1817 if !haveTypmod || typmod < 0 { 1818 return "timestamp without time zone" 1819 } 1820 return fmt.Sprintf("timestamp(%d) without time zone", typmod) 1821 case TimestampTZFamily: 1822 if !haveTypmod || typmod < 0 { 1823 return "timestamp with time zone" 1824 } 1825 return fmt.Sprintf("timestamp(%d) with time zone", typmod) 1826 case TSQueryFamily: 1827 return "tsquery" 1828 case TSVectorFamily: 1829 return "tsvector" 1830 case TupleFamily: 1831 if t.UserDefined() { 1832 // If we have a user-defined tuple type, use its user-defined name. 1833 return t.TypeMeta.Name.Basename() 1834 } 1835 return "record" 1836 case UnknownFamily: 1837 return "unknown" 1838 case UuidFamily: 1839 return "uuid" 1840 case VoidFamily: 1841 return "void" 1842 case EnumFamily: 1843 return t.TypeMeta.Name.Basename() 1844 default: 1845 panic(errors.AssertionFailedf("unexpected Family: %v", errors.Safe(t.Family()))) 1846 } 1847 } 1848 1849 // InformationSchemaName returns the string suitable to populate the data_type 1850 // column of information_schema.columns. 1851 // 1852 // This is different from SQLString() in that it must report SQL standard names 1853 // that are compatible with PostgreSQL client expectations. 1854 func (t *T) InformationSchemaName() string { 1855 // This is the same as SQLStandardName, except for the case of arrays. 1856 if t.Family() == ArrayFamily { 1857 return "ARRAY" 1858 } 1859 // TypeMeta attributes are populated only when it is user defined type. 1860 if t.TypeMeta.Name != nil { 1861 return "USER-DEFINED" 1862 } 1863 return t.SQLStandardName() 1864 } 1865 1866 // SQLString returns the CockroachDB native SQL string that can be used to 1867 // reproduce the type via parsing the string as a type. It is used in error 1868 // messages and also to produce the output of SHOW CREATE. 1869 func (t *T) SQLString() string { 1870 switch t.Family() { 1871 case BitFamily: 1872 o := t.Oid() 1873 typName := "BIT" 1874 if o == oid.T_varbit { 1875 typName = "VARBIT" 1876 } 1877 // BIT(1) pretty-prints as just BIT. 1878 if (o != oid.T_varbit && t.Width() > 1) || 1879 (o == oid.T_varbit && t.Width() > 0) { 1880 typName = fmt.Sprintf("%s(%d)", typName, t.Width()) 1881 } 1882 return typName 1883 case IntFamily: 1884 switch t.Width() { 1885 case 16: 1886 return "INT2" 1887 case 32: 1888 return "INT4" 1889 case 64: 1890 return "INT8" 1891 default: 1892 panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width())) 1893 } 1894 case StringFamily: 1895 return t.stringTypeSQL() 1896 case CollatedStringFamily: 1897 return t.collatedStringTypeSQL(false /* isArray */) 1898 case FloatFamily: 1899 const realName = "FLOAT4" 1900 const doubleName = "FLOAT8" 1901 if t.Width() == 32 { 1902 return realName 1903 } 1904 return doubleName 1905 case DecimalFamily: 1906 if t.Precision() > 0 { 1907 if t.Width() > 0 { 1908 return fmt.Sprintf("DECIMAL(%d,%d)", t.Precision(), t.Scale()) 1909 } 1910 return fmt.Sprintf("DECIMAL(%d)", t.Precision()) 1911 } 1912 case JsonFamily: 1913 // Only binary JSON is currently supported. 1914 return "JSONB" 1915 case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: 1916 if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { 1917 return fmt.Sprintf("%s(%d)", strings.ToUpper(t.Name()), t.Precision()) 1918 } 1919 case GeometryFamily, GeographyFamily: 1920 return strings.ToUpper(t.Name() + t.InternalType.GeoMetadata.SQLString()) 1921 case IntervalFamily: 1922 switch t.InternalType.IntervalDurationField.DurationType { 1923 case IntervalDurationType_UNSET: 1924 if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { 1925 return fmt.Sprintf("%s(%d)", strings.ToUpper(t.Name()), t.Precision()) 1926 } 1927 default: 1928 fromStr := "" 1929 if t.InternalType.IntervalDurationField.FromDurationType != IntervalDurationType_UNSET { 1930 fromStr = fmt.Sprintf("%s TO ", t.InternalType.IntervalDurationField.FromDurationType.String()) 1931 } 1932 precisionStr := "" 1933 if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { 1934 precisionStr = fmt.Sprintf("(%d)", t.Precision()) 1935 } 1936 return fmt.Sprintf( 1937 "%s %s%s%s", 1938 strings.ToUpper(t.Name()), 1939 fromStr, 1940 t.InternalType.IntervalDurationField.DurationType.String(), 1941 precisionStr, 1942 ) 1943 } 1944 case OidFamily: 1945 if name, ok := oidext.TypeName(t.Oid()); ok { 1946 return name 1947 } 1948 case ArrayFamily: 1949 switch t.Oid() { 1950 case oid.T_oidvector: 1951 return "OIDVECTOR" 1952 case oid.T_int2vector: 1953 return "INT2VECTOR" 1954 } 1955 if t.ArrayContents().Family() == CollatedStringFamily { 1956 return t.ArrayContents().collatedStringTypeSQL(true /* isArray */) 1957 } 1958 return t.ArrayContents().SQLString() + "[]" 1959 case EnumFamily: 1960 if t.Oid() == oid.T_anyenum { 1961 return "anyenum" 1962 } 1963 // We do not expect to be in a situation where we want to format a 1964 // user-defined type to a string and do not have the TypeMeta hydrated, 1965 // but there have been bugs in the past, and returning a less informative 1966 // string is better than a nil-pointer panic. 1967 if t.TypeMeta.Name == nil { 1968 return fmt.Sprintf("@%d", t.Oid()) 1969 } 1970 return t.TypeMeta.Name.FQName() 1971 } 1972 return strings.ToUpper(t.Name()) 1973 } 1974 1975 // SQLStringForError returns a version of SQLString that will preserve safe 1976 // information during redaction. It is suitable for usage in error messages. 1977 func (t *T) SQLStringForError() redact.RedactableString { 1978 if t == nil { 1979 return "<nil>" 1980 } 1981 if t.UserDefined() { 1982 // Show the redacted SQLString output with an un-redacted prefix to indicate 1983 // that the type is user defined (and possibly enum or record). 1984 prefix := "TYPE" 1985 switch t.Family() { 1986 case EnumFamily: 1987 prefix = "ENUM" 1988 case TupleFamily: 1989 prefix = "RECORD" 1990 case ArrayFamily: 1991 prefix = "ARRAY" 1992 } 1993 return redact.Sprintf("USER DEFINED %s: %s", redact.Safe(prefix), t.SQLString()) 1994 } 1995 switch t.Family() { 1996 case EnumFamily, TupleFamily, ArrayFamily: 1997 // These types can be or can contain user-defined types, but the SQLString 1998 // is safe when they are not user-defined. We filtered out the user-defined 1999 // case above. 2000 return redact.Sprint(redact.Safe(t.SQLString())) 2001 case BoolFamily, IntFamily, FloatFamily, DecimalFamily, DateFamily, TimestampFamily, 2002 IntervalFamily, StringFamily, BytesFamily, TimestampTZFamily, CollatedStringFamily, OidFamily, 2003 UnknownFamily, UuidFamily, INetFamily, TimeFamily, JsonFamily, TimeTZFamily, BitFamily, 2004 GeometryFamily, GeographyFamily, Box2DFamily, VoidFamily, EncodedKeyFamily, TSQueryFamily, 2005 TSVectorFamily, AnyFamily, PGLSNFamily, RefCursorFamily: 2006 // These types do not contain other types, and do not require redaction. 2007 return redact.Sprint(redact.SafeString(t.SQLString())) 2008 } 2009 // Default to redaction for unhandled types. 2010 return redact.Sprint(t.SQLString()) 2011 } 2012 2013 // FormatTypeName is an injected dependency from tree to properly format a 2014 // type name. The logic for proper formatting lives in the tree package. 2015 var FormatTypeName = fallbackFormatTypeName 2016 2017 func fallbackFormatTypeName(UserDefinedTypeName) string { 2018 return "formatting logic has not been injected from tree" 2019 } 2020 2021 // Equivalent returns true if this type is "equivalent" to the given type. 2022 // Equivalent types are compatible with one another: they can be compared, 2023 // assigned, and unioned. Equivalent types must always have the same type family 2024 // for the root type and any descendant types (i.e. in case of array or tuple 2025 // types). Types in the CollatedStringFamily must have the same locale. But 2026 // other attributes of equivalent types, such as width, precision, and oid, can 2027 // be different. 2028 // 2029 // Wildcard types (e.g. Any, AnyArray, AnyTuple, etc) have special equivalence 2030 // behavior. AnyFamily types match any other type, including other AnyFamily 2031 // types. And a wildcard collation (empty string) matches any other collation. 2032 func (t *T) Equivalent(other *T) bool { 2033 if t.Family() == AnyFamily || other.Family() == AnyFamily { 2034 return true 2035 } 2036 if t.Family() != other.Family() { 2037 return false 2038 } 2039 2040 switch t.Family() { 2041 case CollatedStringFamily: 2042 // CockroachDB differs from Postgres by comparing collation names 2043 // case-insensitively and equating hyphens/underscores. 2044 if t.Locale() != "" && other.Locale() != "" { 2045 if !lex.LocaleNamesAreEqual(t.Locale(), other.Locale()) { 2046 return false 2047 } 2048 } 2049 2050 case TupleFamily: 2051 // If either tuple is the wildcard tuple, it's equivalent to any other 2052 // tuple type. This allows overloads to specify that they take an arbitrary 2053 // tuple type. 2054 if IsWildcardTupleType(t) || IsWildcardTupleType(other) { 2055 return true 2056 } 2057 if len(t.TupleContents()) != len(other.TupleContents()) { 2058 return false 2059 } 2060 for i := range t.TupleContents() { 2061 if !t.TupleContents()[i].Equivalent(other.TupleContents()[i]) { 2062 return false 2063 } 2064 } 2065 2066 case ArrayFamily: 2067 if !t.ArrayContents().Equivalent(other.ArrayContents()) { 2068 return false 2069 } 2070 2071 case EnumFamily: 2072 // If one of the types is anyenum, then allow the comparison to 2073 // go through -- anyenum is used when matching overloads. 2074 if t.Oid() == oid.T_anyenum || other.Oid() == oid.T_anyenum { 2075 return true 2076 } 2077 if t.Oid() != other.Oid() { 2078 return false 2079 } 2080 } 2081 2082 return true 2083 } 2084 2085 // EquivalentOrNull is the same as Equivalent, except it returns true if: 2086 // * `t` is Unknown (i.e., NULL) AND (allowNullTupleEquivalence OR `other` is not a tuple), 2087 // * `t` is a tuple with all non-Unknown elements matching the types in `other`. 2088 func (t *T) EquivalentOrNull(other *T, allowNullTupleEquivalence bool) bool { 2089 // Check normal equivalency first, then check for Null 2090 normalEquivalency := t.Equivalent(other) 2091 if normalEquivalency { 2092 return true 2093 } 2094 2095 switch t.Family() { 2096 case UnknownFamily: 2097 return allowNullTupleEquivalence || other.Family() != TupleFamily 2098 2099 case TupleFamily: 2100 if other.Family() != TupleFamily { 2101 return false 2102 } 2103 // If either tuple is the wildcard tuple, it's equivalent to any other 2104 // tuple type. This allows overloads to specify that they take an arbitrary 2105 // tuple type. 2106 if IsWildcardTupleType(t) || IsWildcardTupleType(other) { 2107 return true 2108 } 2109 if len(t.TupleContents()) != len(other.TupleContents()) { 2110 return false 2111 } 2112 for i := range t.TupleContents() { 2113 if !t.TupleContents()[i].EquivalentOrNull(other.TupleContents()[i], allowNullTupleEquivalence) { 2114 return false 2115 } 2116 } 2117 return true 2118 2119 default: 2120 return normalEquivalency 2121 } 2122 } 2123 2124 // Identical returns true if every field in this ColumnType is exactly the same 2125 // as every corresponding field in the given ColumnType. Identical performs a 2126 // deep comparison, traversing any Tuple or Array contents. 2127 // 2128 // NOTE: Consider whether the desired semantics really require identical types, 2129 // or if Equivalent is the right method to call instead. 2130 func (t *T) Identical(other *T) bool { 2131 return t.InternalType.Identical(&other.InternalType) 2132 } 2133 2134 // Equal is for use in generated protocol buffer code only. 2135 func (t *T) Equal(other *T) bool { 2136 return t.Identical(other) 2137 } 2138 2139 // Size returns the size, in bytes, of this type once it has been marshaled to 2140 // a byte buffer. This is typically called to determine the size of the buffer 2141 // that needs to be allocated before calling Marshal. 2142 // 2143 // Marshal is part of the protoutil.Message interface. 2144 func (t *T) Size() (n int) { 2145 // Need to first downgrade the type before delegating to InternalType, 2146 // because Marshal will downgrade. 2147 temp := *t 2148 err := temp.downgradeType() 2149 if err != nil { 2150 panic(errors.NewAssertionErrorWithWrappedErrf(err, "error during Size call")) 2151 } 2152 return temp.InternalType.Size() 2153 } 2154 2155 // Identical is the internal implementation for T.Identical. See that comment 2156 // for details. 2157 func (t *InternalType) Identical(other *InternalType) bool { 2158 if t.Family != other.Family { 2159 return false 2160 } 2161 if t.Width != other.Width { 2162 return false 2163 } 2164 if t.Precision != other.Precision { 2165 return false 2166 } 2167 if t.TimePrecisionIsSet != other.TimePrecisionIsSet { 2168 return false 2169 } 2170 if t.IntervalDurationField != nil && other.IntervalDurationField != nil { 2171 if *t.IntervalDurationField != *other.IntervalDurationField { 2172 return false 2173 } 2174 } else if t.IntervalDurationField != nil { 2175 return false 2176 } else if other.IntervalDurationField != nil { 2177 return false 2178 } 2179 if t.GeoMetadata != nil && other.GeoMetadata != nil { 2180 if t.GeoMetadata.ShapeType != other.GeoMetadata.ShapeType { 2181 return false 2182 } 2183 if t.GeoMetadata.SRID != other.GeoMetadata.SRID { 2184 return false 2185 } 2186 } else if t.GeoMetadata != nil { 2187 return false 2188 } else if other.GeoMetadata != nil { 2189 return false 2190 } 2191 if t.Locale != nil && other.Locale != nil { 2192 if !lex.LocaleNamesAreEqual(*t.Locale, *other.Locale) { 2193 return false 2194 } 2195 } else if t.Locale != nil { 2196 return false 2197 } else if other.Locale != nil { 2198 return false 2199 } 2200 if t.ArrayContents != nil && other.ArrayContents != nil { 2201 if !t.ArrayContents.Identical(other.ArrayContents) { 2202 return false 2203 } 2204 } else if t.ArrayContents != nil { 2205 return false 2206 } else if other.ArrayContents != nil { 2207 return false 2208 } 2209 if len(t.TupleContents) != len(other.TupleContents) { 2210 return false 2211 } 2212 for i := range t.TupleContents { 2213 if !t.TupleContents[i].Identical(other.TupleContents[i]) { 2214 return false 2215 } 2216 } 2217 if len(t.TupleLabels) != len(other.TupleLabels) { 2218 return false 2219 } 2220 for i := range t.TupleLabels { 2221 if t.TupleLabels[i] != other.TupleLabels[i] { 2222 return false 2223 } 2224 } 2225 if t.UDTMetadata != nil && other.UDTMetadata != nil { 2226 if t.UDTMetadata.ArrayTypeOID != other.UDTMetadata.ArrayTypeOID { 2227 return false 2228 } 2229 } else if t.UDTMetadata != nil { 2230 return false 2231 } else if other.UDTMetadata != nil { 2232 return false 2233 } 2234 return t.Oid == other.Oid 2235 } 2236 2237 // Unmarshal deserializes a type from the given byte representation using gogo 2238 // protobuf serialization rules. It is backwards-compatible with formats used 2239 // by older versions of CRDB. 2240 // 2241 // var t T 2242 // err := protoutil.Unmarshal(data, &t) 2243 // 2244 // Unmarshal is part of the protoutil.Message interface. 2245 func (t *T) Unmarshal(data []byte) error { 2246 // Unmarshal the internal type, and then perform an upgrade step to convert 2247 // to the latest format. 2248 err := protoutil.Unmarshal(data, &t.InternalType) 2249 if err != nil { 2250 return err 2251 } 2252 return t.upgradeType() 2253 } 2254 2255 // upgradeType assumes its input was just unmarshaled from bytes that may have 2256 // been serialized by any previous version of CRDB. It upgrades the object 2257 // according to the requirements of the latest version by remapping fields and 2258 // setting required values. This is necessary to preserve backwards- 2259 // compatibility with older formats (e.g. restoring database from old backup). 2260 func (t *T) upgradeType() error { 2261 switch t.Family() { 2262 case IntFamily: 2263 // Check VisibleType field that was populated in previous versions. 2264 switch t.InternalType.VisibleType { 2265 case visibleSMALLINT: 2266 t.InternalType.Width = 16 2267 t.InternalType.Oid = oid.T_int2 2268 case visibleINTEGER: 2269 t.InternalType.Width = 32 2270 t.InternalType.Oid = oid.T_int4 2271 case visibleBIGINT: 2272 t.InternalType.Width = 64 2273 t.InternalType.Oid = oid.T_int8 2274 case visibleBIT, visibleNONE: 2275 // Pre-2.1 BIT was using IntFamily with arbitrary widths. Clamp them 2276 // to fixed/known widths. See #34161. 2277 switch t.Width() { 2278 case 16: 2279 t.InternalType.Oid = oid.T_int2 2280 case 32: 2281 t.InternalType.Oid = oid.T_int4 2282 default: 2283 // Assume INT8 if width is 0 or not valid. 2284 t.InternalType.Oid = oid.T_int8 2285 t.InternalType.Width = 64 2286 } 2287 default: 2288 return errors.AssertionFailedf("unexpected visible type: %d", t.InternalType.VisibleType) 2289 } 2290 2291 case FloatFamily: 2292 // Map visible REAL type to 32-bit width. 2293 switch t.InternalType.VisibleType { 2294 case visibleREAL: 2295 t.InternalType.Oid = oid.T_float4 2296 t.InternalType.Width = 32 2297 case visibleDOUBLE: 2298 t.InternalType.Oid = oid.T_float8 2299 t.InternalType.Width = 64 2300 case visibleNONE: 2301 switch t.Width() { 2302 case 32: 2303 t.InternalType.Oid = oid.T_float4 2304 case 64: 2305 t.InternalType.Oid = oid.T_float8 2306 default: 2307 // Pre-2.1 (before Width) there were 3 cases: 2308 // - VisibleType = DOUBLE PRECISION, Width = 0 -> now clearly FLOAT8 2309 // - VisibleType = NONE, Width = 0 -> now clearly FLOAT8 2310 // - VisibleType = NONE, Precision > 0 -> we need to derive the width. 2311 if t.Precision() >= 1 && t.Precision() <= 24 { 2312 t.InternalType.Oid = oid.T_float4 2313 t.InternalType.Width = 32 2314 } else { 2315 t.InternalType.Oid = oid.T_float8 2316 t.InternalType.Width = 64 2317 } 2318 } 2319 default: 2320 return errors.AssertionFailedf("unexpected visible type: %d", t.InternalType.VisibleType) 2321 } 2322 2323 // Precision should always be set to 0 going forward. 2324 t.InternalType.Precision = 0 2325 2326 case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: 2327 // Some bad/experimental versions of master had precision stored as `-1`. 2328 // This represented a default - so upgrade this to 0 with TimePrecisionIsSet = false. 2329 if t.InternalType.Precision == -1 { 2330 t.InternalType.Precision = 0 2331 t.InternalType.TimePrecisionIsSet = false 2332 } 2333 // Going forwards after 19.2, we want `TimePrecisionIsSet` to be explicitly set 2334 // if Precision is > 0. 2335 if t.InternalType.Precision > 0 { 2336 t.InternalType.TimePrecisionIsSet = true 2337 } 2338 case IntervalFamily: 2339 // Fill in the IntervalDurationField here. 2340 if t.InternalType.IntervalDurationField == nil { 2341 t.InternalType.IntervalDurationField = &IntervalDurationField{} 2342 } 2343 // Going forwards after 19.2, we want `TimePrecisionIsSet` to be explicitly set 2344 // if Precision is > 0. 2345 if t.InternalType.Precision > 0 { 2346 t.InternalType.TimePrecisionIsSet = true 2347 } 2348 case StringFamily, CollatedStringFamily: 2349 // Map string-related visible types to corresponding Oid values. 2350 switch t.InternalType.VisibleType { 2351 case visibleVARCHAR: 2352 t.InternalType.Oid = oid.T_varchar 2353 case visibleCHAR: 2354 t.InternalType.Oid = oid.T_bpchar 2355 case visibleQCHAR: 2356 t.InternalType.Oid = oid.T_char 2357 case visibleNONE: 2358 t.InternalType.Oid = oid.T_text 2359 default: 2360 return errors.AssertionFailedf("unexpected visible type: %d", t.InternalType.VisibleType) 2361 } 2362 if t.InternalType.Family == StringFamily { 2363 if t.InternalType.Locale != nil && len(*t.InternalType.Locale) != 0 { 2364 return errors.AssertionFailedf( 2365 "STRING type should not have locale: %s", *t.InternalType.Locale) 2366 } 2367 } 2368 2369 case BitFamily: 2370 // Map visible VARBIT type to T_varbit OID value. 2371 switch t.InternalType.VisibleType { 2372 case visibleVARBIT: 2373 t.InternalType.Oid = oid.T_varbit 2374 case visibleNONE: 2375 t.InternalType.Oid = oid.T_bit 2376 default: 2377 return errors.AssertionFailedf("unexpected visible type: %d", t.InternalType.VisibleType) 2378 } 2379 2380 case ArrayFamily: 2381 if t.ArrayContents() == nil { 2382 // This array type was serialized by a previous version of CRDB, 2383 // so construct the array contents from scratch. 2384 arrayContents := *t 2385 arrayContents.InternalType.Family = *t.InternalType.ArrayElemType 2386 arrayContents.InternalType.ArrayDimensions = nil 2387 arrayContents.InternalType.ArrayElemType = nil 2388 if err := arrayContents.upgradeType(); err != nil { 2389 return err 2390 } 2391 t.InternalType.ArrayContents = &arrayContents 2392 t.InternalType.Oid = CalcArrayOid(t.ArrayContents()) 2393 } 2394 2395 // Marshaling/unmarshaling nested arrays is not yet supported. 2396 if t.ArrayContents().Family() == ArrayFamily { 2397 return errors.AssertionFailedf("nested array should never be unmarshaled") 2398 } 2399 2400 // Zero out fields that may have been used to store information about 2401 // the array element type, or which are no longer in use. 2402 t.InternalType.Width = 0 2403 t.InternalType.Precision = 0 2404 t.InternalType.Locale = nil 2405 t.InternalType.VisibleType = 0 2406 t.InternalType.ArrayElemType = nil 2407 t.InternalType.ArrayDimensions = nil 2408 2409 case int2vector: 2410 t.InternalType.Family = ArrayFamily 2411 t.InternalType.Width = 0 2412 t.InternalType.Oid = oid.T_int2vector 2413 t.InternalType.ArrayContents = Int2 2414 2415 case oidvector: 2416 t.InternalType.Family = ArrayFamily 2417 t.InternalType.Oid = oid.T_oidvector 2418 t.InternalType.ArrayContents = Oid 2419 2420 case name: 2421 if t.InternalType.Locale != nil { 2422 t.InternalType.Family = CollatedStringFamily 2423 } else { 2424 t.InternalType.Family = StringFamily 2425 } 2426 t.InternalType.Oid = oid.T_name 2427 if t.Width() != 0 { 2428 return errors.AssertionFailedf("name type cannot have non-zero width: %d", t.Width()) 2429 } 2430 } 2431 2432 if t.InternalType.Oid == 0 { 2433 t.InternalType.Oid = familyToOid[t.Family()] 2434 } 2435 2436 // Clear the deprecated visible types, since they are now handled by the 2437 // Width or Oid fields. 2438 t.InternalType.VisibleType = 0 2439 2440 // If locale is not set, always set it to the empty string, in order to avoid 2441 // bothersome deref errors when the Locale method is called. 2442 if t.InternalType.Locale == nil { 2443 t.InternalType.Locale = &emptyLocale 2444 } 2445 2446 return nil 2447 } 2448 2449 // Marshal serializes a type into a byte representation using gogo protobuf 2450 // serialization rules. It returns the resulting bytes as a slice. The bytes 2451 // are serialized in a format that is backwards-compatible with the previous 2452 // version of CRDB so that clusters can run in mixed version mode during 2453 // upgrade. 2454 // 2455 // bytes, err := protoutil.Marshal(&typ) 2456 func (t *T) Marshal() (data []byte, err error) { 2457 // First downgrade to a struct that will be serialized in a backwards- 2458 // compatible bytes format. 2459 temp := *t 2460 if err := temp.downgradeType(); err != nil { 2461 return nil, err 2462 } 2463 return protoutil.Marshal(&temp.InternalType) 2464 } 2465 2466 // MarshalToSizedBuffer is like Mashal, except that it deserializes to 2467 // an existing byte slice with exactly enough remaining space for 2468 // Size(). 2469 // 2470 // Marshal is part of the protoutil.Message interface. 2471 func (t *T) MarshalToSizedBuffer(data []byte) (int, error) { 2472 temp := *t 2473 if err := temp.downgradeType(); err != nil { 2474 return 0, err 2475 } 2476 return temp.InternalType.MarshalToSizedBuffer(data) 2477 } 2478 2479 // MarshalTo behaves like Marshal, except that it deserializes to an existing 2480 // byte slice and returns the number of bytes written to it. The slice must 2481 // already have sufficient capacity. Callers can use the Size method to 2482 // determine how much capacity needs to be allocated. 2483 // 2484 // Marshal is part of the protoutil.Message interface. 2485 func (t *T) MarshalTo(data []byte) (int, error) { 2486 temp := *t 2487 if err := temp.downgradeType(); err != nil { 2488 return 0, err 2489 } 2490 return temp.InternalType.MarshalTo(data) 2491 } 2492 2493 // of the latest CRDB version. It updates the fields so that they will be 2494 // marshaled into a format that is compatible with the previous version of 2495 // CRDB. This is necessary to preserve backwards-compatibility in mixed-version 2496 // scenarios, such as during upgrade. 2497 func (t *T) downgradeType() error { 2498 // Set Family and VisibleType for 19.1 backwards-compatibility. 2499 switch t.Family() { 2500 case BitFamily: 2501 if t.Oid() == oid.T_varbit { 2502 t.InternalType.VisibleType = visibleVARBIT 2503 } 2504 2505 case FloatFamily: 2506 switch t.Width() { 2507 case 32: 2508 t.InternalType.VisibleType = visibleREAL 2509 } 2510 2511 case StringFamily, CollatedStringFamily: 2512 switch t.Oid() { 2513 case oid.T_text: 2514 // Nothing to do. 2515 case oid.T_varchar: 2516 t.InternalType.VisibleType = visibleVARCHAR 2517 case oid.T_bpchar: 2518 t.InternalType.VisibleType = visibleCHAR 2519 case oid.T_char: 2520 t.InternalType.VisibleType = visibleQCHAR 2521 case oid.T_name: 2522 t.InternalType.Family = name 2523 default: 2524 return errors.AssertionFailedf("unexpected Oid: %d", t.Oid()) 2525 } 2526 2527 case ArrayFamily: 2528 // Marshaling/unmarshaling nested arrays is not yet supported. 2529 if t.ArrayContents().Family() == ArrayFamily { 2530 return errors.AssertionFailedf("nested array should never be marshaled") 2531 } 2532 2533 // Downgrade to array representation used before 19.2, in which the array 2534 // type fields specified the width, locale, etc. of the element type. 2535 temp := *t.InternalType.ArrayContents 2536 if err := temp.downgradeType(); err != nil { 2537 return err 2538 } 2539 t.InternalType.Width = temp.InternalType.Width 2540 t.InternalType.Precision = temp.InternalType.Precision 2541 t.InternalType.Locale = temp.InternalType.Locale 2542 t.InternalType.VisibleType = temp.InternalType.VisibleType 2543 t.InternalType.ArrayElemType = &t.InternalType.ArrayContents.InternalType.Family 2544 2545 switch t.Oid() { 2546 case oid.T_int2vector: 2547 t.InternalType.Family = int2vector 2548 case oid.T_oidvector: 2549 t.InternalType.Family = oidvector 2550 } 2551 } 2552 2553 // Map empty locale to nil. 2554 if t.InternalType.Locale != nil && len(*t.InternalType.Locale) == 0 { 2555 t.InternalType.Locale = nil 2556 } 2557 2558 return nil 2559 } 2560 2561 // String returns the name of the type, similar to the Name method. However, it 2562 // expands CollatedStringFamily, ArrayFamily, and TupleFamily types to be more 2563 // descriptive. 2564 // 2565 // TODO(andyk): It'd be nice to have this return SqlString() method output, 2566 // since that is more descriptive. 2567 func (t *T) String() string { 2568 switch t.Family() { 2569 case CollatedStringFamily: 2570 if t.Locale() == "" { 2571 // Used in telemetry. 2572 return fmt.Sprintf("collated%s{*}", t.Name()) 2573 } 2574 return fmt.Sprintf("collated%s{%s}", t.Name(), t.Locale()) 2575 2576 case ArrayFamily: 2577 switch t.Oid() { 2578 case oid.T_oidvector, oid.T_int2vector: 2579 return t.Name() 2580 } 2581 return t.ArrayContents().String() + "[]" 2582 2583 case TupleFamily: 2584 var buf bytes.Buffer 2585 buf.WriteString("tuple") 2586 if len(t.TupleContents()) != 0 && !IsWildcardTupleType(t) { 2587 buf.WriteByte('{') 2588 for i, typ := range t.TupleContents() { 2589 if i != 0 { 2590 buf.WriteString(", ") 2591 } 2592 buf.WriteString(typ.String()) 2593 if t.TupleLabels() != nil { 2594 buf.WriteString(" AS ") 2595 buf.WriteString(t.InternalType.TupleLabels[i]) 2596 } 2597 } 2598 buf.WriteByte('}') 2599 } 2600 return buf.String() 2601 case IntervalFamily, TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily: 2602 if t.InternalType.Precision > 0 || t.InternalType.TimePrecisionIsSet { 2603 return fmt.Sprintf("%s(%d)", t.Name(), t.Precision()) 2604 } 2605 } 2606 return t.Name() 2607 } 2608 2609 // MarshalText is implemented here so that gogo/protobuf know how to text marshal 2610 // protobuf struct directly/indirectly depends on types.T without panic. 2611 func (t *T) MarshalText() (text []byte, err error) { 2612 var buf bytes.Buffer 2613 if err := proto.MarshalText(&buf, &t.InternalType); err != nil { 2614 return nil, err 2615 } 2616 return buf.Bytes(), nil 2617 } 2618 2619 // DebugString returns a detailed dump of the type protobuf struct, suitable for 2620 // debugging scenarios. 2621 func (t *T) DebugString() string { 2622 if t.Family() == ArrayFamily && t.ArrayContents().UserDefined() { 2623 // In the case that this type is an array that contains a user defined 2624 // type, the introspection that protobuf performs to generate a string 2625 // representation will panic if the UserDefinedTypeMetadata field of the 2626 // type is populated. It seems to not understand that fields in the element 2627 // T could be not generated by proto, and panics trying to operate on the 2628 // TypeMeta field of the T. To get around this, we just deep copy the T and 2629 // zero out the type metadata to placate proto. See the following issue for 2630 // details: https://github.com/gogo/protobuf/issues/693. 2631 internalTypeCopy := protoutil.Clone(&t.InternalType).(*InternalType) 2632 internalTypeCopy.ArrayContents.TypeMeta = UserDefinedTypeMetadata{} 2633 return internalTypeCopy.String() 2634 } 2635 return t.InternalType.String() 2636 } 2637 2638 // IsAmbiguous returns true if this type is in UnknownFamily or AnyFamily. 2639 // Instances of ambiguous types can be NULL or be in one of several different 2640 // type families. This is important for parameterized types to determine whether 2641 // they are fully concrete or not. 2642 func (t *T) IsAmbiguous() bool { 2643 switch t.Family() { 2644 case UnknownFamily, AnyFamily: 2645 return true 2646 case CollatedStringFamily: 2647 return t.Locale() == "" 2648 case TupleFamily: 2649 if len(t.TupleContents()) == 0 { 2650 return true 2651 } 2652 for i := range t.TupleContents() { 2653 if t.TupleContents()[i].IsAmbiguous() { 2654 return true 2655 } 2656 } 2657 return false 2658 case ArrayFamily: 2659 return t.ArrayContents().IsAmbiguous() 2660 case EnumFamily: 2661 return t.Oid() == oid.T_anyenum 2662 } 2663 return false 2664 } 2665 2666 // IsNumeric returns true iff this type is an integer, float, or decimal. 2667 func (t *T) IsNumeric() bool { 2668 switch t.Family() { 2669 case IntFamily, FloatFamily, DecimalFamily: 2670 return true 2671 default: 2672 return false 2673 } 2674 } 2675 2676 // EnumGetIdxOfPhysical returns the index within the TypeMeta's slice of 2677 // enum physical representations that matches the input byte slice. 2678 func (t *T) EnumGetIdxOfPhysical(phys []byte) (int, error) { 2679 t.ensureHydratedEnum() 2680 // TODO (rohany): We can either use a map or just binary search here 2681 // since the physical representations are sorted. 2682 reps := t.TypeMeta.EnumData.PhysicalRepresentations 2683 for i := range reps { 2684 if bytes.Equal(phys, reps[i]) { 2685 return i, nil 2686 } 2687 } 2688 err := errors.Newf( 2689 "could not find %v in enum %q representation %s %s", 2690 phys, 2691 t.TypeMeta.Name.FQName(), 2692 t.TypeMeta.EnumData.debugString(), 2693 debug.Stack(), 2694 ) 2695 return 0, err 2696 } 2697 2698 // EnumGetIdxOfLogical returns the index within the TypeMeta's slice of 2699 // enum logical representations that matches the input string. 2700 func (t *T) EnumGetIdxOfLogical(logical string) (int, error) { 2701 t.ensureHydratedEnum() 2702 reps := t.TypeMeta.EnumData.LogicalRepresentations 2703 for i := range reps { 2704 if reps[i] == logical { 2705 // If this enum member is read only, we cannot construct it from the 2706 // logical representation. This is to ensure that it will not be 2707 // written until all nodes in the cluster are able to decode the 2708 // physical representation. 2709 if t.TypeMeta.EnumData.IsMemberReadOnly[i] { 2710 return 0, errors.Newf("enum value %q is not yet public", logical) 2711 } 2712 return i, nil 2713 } 2714 } 2715 return 0, pgerror.Newf( 2716 pgcode.InvalidTextRepresentation, "invalid input value for enum %s: %q", t, logical) 2717 } 2718 2719 func (t *T) ensureHydratedEnum() { 2720 if t.TypeMeta.EnumData == nil { 2721 panic(errors.AssertionFailedf("use of enum metadata before hydration as an enum: %v %p", t, t)) 2722 } 2723 } 2724 2725 // IsStringType returns true iff the given type is String or a collated string 2726 // type. 2727 func IsStringType(t *T) bool { 2728 switch t.Family() { 2729 case StringFamily, CollatedStringFamily: 2730 return true 2731 default: 2732 return false 2733 } 2734 } 2735 2736 // IsValidArrayElementType returns true if the given type can be used as the 2737 // element type of an ArrayFamily-typed column. If the valid return is false, 2738 // the issue number should be included in the error report to inform the user. 2739 func IsValidArrayElementType(t *T) (valid bool, issueNum int) { 2740 switch t.Family() { 2741 case TSQueryFamily: 2742 return false, 90886 2743 case TSVectorFamily: 2744 return false, 90886 2745 default: 2746 return true, 0 2747 } 2748 } 2749 2750 // CheckArrayElementType ensures that the given type can be used as the element 2751 // type of an ArrayFamily-typed column. If not, it returns an error. 2752 func CheckArrayElementType(t *T) error { 2753 if ok, issueNum := IsValidArrayElementType(t); !ok { 2754 return unimplemented.NewWithIssueDetailf(issueNum, t.String(), 2755 "arrays of %s not allowed", t) 2756 } 2757 return nil 2758 } 2759 2760 // IsDateTimeType returns true if the given type is a date or time-related type. 2761 func IsDateTimeType(t *T) bool { 2762 switch t.Family() { 2763 case DateFamily: 2764 return true 2765 case TimeFamily: 2766 return true 2767 case TimeTZFamily: 2768 return true 2769 case TimestampFamily: 2770 return true 2771 case TimestampTZFamily: 2772 return true 2773 case IntervalFamily: 2774 return true 2775 default: 2776 return false 2777 } 2778 } 2779 2780 // IsAdditiveType returns true if the given type supports addition and 2781 // subtraction. 2782 func IsAdditiveType(t *T) bool { 2783 switch t.Family() { 2784 case IntFamily: 2785 return true 2786 case FloatFamily: 2787 return true 2788 case DecimalFamily: 2789 return true 2790 default: 2791 return IsDateTimeType(t) 2792 } 2793 } 2794 2795 // IsWildcardTupleType returns true if this is the wildcard AnyTuple type. The 2796 // wildcard type matches a tuple type having any number of fields (including 0). 2797 func IsWildcardTupleType(t *T) bool { 2798 return len(t.TupleContents()) == 1 && t.TupleContents()[0].Family() == AnyFamily 2799 } 2800 2801 // IsRecordType returns true if this is a RECORD type. This should only be used 2802 // when processing UDFs. A record differs from AnyTuple in that the tuple 2803 // contents may contain types other than Any. 2804 func IsRecordType(typ *T) bool { 2805 return typ.Family() == TupleFamily && typ.Oid() == oid.T_record 2806 } 2807 2808 // collatedStringTypeSQL returns the string representation of a COLLATEDSTRING 2809 // or []COLLATEDSTRING type. This is tricky in the case of an array of collated 2810 // string, since brackets must precede the COLLATE identifier: 2811 // 2812 // STRING COLLATE EN 2813 // VARCHAR(20)[] COLLATE DE 2814 func (t *T) collatedStringTypeSQL(isArray bool) string { 2815 var buf bytes.Buffer 2816 buf.WriteString(t.stringTypeSQL()) 2817 if isArray { 2818 buf.WriteString("[] COLLATE ") 2819 } else { 2820 buf.WriteString(" COLLATE ") 2821 } 2822 lex.EncodeLocaleName(&buf, t.Locale()) 2823 return buf.String() 2824 } 2825 2826 // stringTypeSQL returns the visible type name plus any width specifier for the 2827 // STRING/COLLATEDSTRING type. 2828 func (t *T) stringTypeSQL() string { 2829 typName := "STRING" 2830 switch t.Oid() { 2831 case oid.T_varchar: 2832 typName = "VARCHAR" 2833 case oid.T_bpchar: 2834 typName = "CHAR" 2835 case oid.T_char: 2836 // Yes, that's the name. The ways of PostgreSQL are inscrutable. 2837 typName = `"char"` 2838 case oid.T_name: 2839 typName = "NAME" 2840 } 2841 2842 // In general, if there is a specified width we want to print it next to the 2843 // type. However, in the specific case of CHAR and "char", the default is 1 2844 // and the width should be omitted in that case. 2845 if t.Width() > 0 { 2846 o := t.Oid() 2847 if t.Width() != 1 || (o != oid.T_bpchar && o != oid.T_char) { 2848 typName = fmt.Sprintf("%s(%d)", typName, t.Width()) 2849 } 2850 } 2851 2852 return typName 2853 } 2854 2855 // IsHydrated returns true if this is a user-defined type and the TypeMeta 2856 // is hydrated. 2857 func (t *T) IsHydrated() bool { 2858 return t.UserDefined() && t.TypeMeta != (UserDefinedTypeMetadata{}) 2859 } 2860 2861 var typNameLiterals map[string]*T 2862 2863 func init() { 2864 typNameLiterals = make(map[string]*T) 2865 for o, t := range OidToType { 2866 name, ok := oidext.TypeName(o) 2867 if !ok { 2868 panic(errors.AssertionFailedf("oid %d has no type name", o)) 2869 } 2870 name = strings.ToLower(name) 2871 if _, ok := typNameLiterals[name]; !ok { 2872 typNameLiterals[name] = t 2873 } 2874 } 2875 for name, t := range unreservedTypeTokens { 2876 if _, ok := typNameLiterals[name]; !ok { 2877 typNameLiterals[name] = t 2878 } 2879 } 2880 } 2881 2882 // TypeForNonKeywordTypeName returns the column type for the string name of a 2883 // type, if one exists. The third return value indicates: 2884 // 2885 // 0 if no error or the type is not known in postgres. 2886 // -1 if the type is known in postgres. 2887 // >0 for a github issue number. 2888 func TypeForNonKeywordTypeName(name string) (*T, bool, int) { 2889 t, ok := typNameLiterals[name] 2890 if ok { 2891 return t, ok, 0 2892 } 2893 return nil, false, postgresPredefinedTypeIssues[name] 2894 } 2895 2896 // The SERIAL types are pseudo-types that are only used during parsing. After 2897 // that, they should behave identically to INT columns. They are declared 2898 // as INT types, but using different instances than types.Int, types.Int2, etc. 2899 // so that they can be compared by pointer to differentiate them from the 2900 // singleton INT types. While the usual requirement is that == is never used to 2901 // compare types, this is one case where it's allowed. 2902 var ( 2903 Serial2Type = *Int2 2904 Serial4Type = *Int4 2905 Serial8Type = *Int 2906 ) 2907 2908 // IsSerialType returns whether or not the input type is a SERIAL type. 2909 // This function should only be used during parsing. 2910 func IsSerialType(typ *T) bool { 2911 // This is a special case where == is used to compare types, since the SERIAL 2912 // types are pseudo-types. 2913 return typ == &Serial2Type || typ == &Serial4Type || typ == &Serial8Type 2914 } 2915 2916 // unreservedTypeTokens contain type alias that we resolve during parsing. 2917 // Instead of adding a new token to the parser, add the type here. 2918 var unreservedTypeTokens = map[string]*T{ 2919 "blob": Bytes, 2920 "bool": Bool, 2921 "bytea": Bytes, 2922 "bytes": Bytes, 2923 "date": Date, 2924 "float4": Float, 2925 "float8": Float, 2926 "inet": INet, 2927 "int2": Int2, 2928 "int4": Int4, 2929 "int8": Int, 2930 "int64": Int, 2931 "int2vector": Int2Vector, 2932 // NOTE(sql-exp): Change the line below to Json if we support the JSON type. 2933 "json": Jsonb, 2934 "jsonb": Jsonb, 2935 "name": Name, 2936 "oid": Oid, 2937 "oidvector": OidVector, 2938 // Postgres OID pseudo-types. See https://www.postgresql.org/docs/9.4/static/datatype-oid.html. 2939 "regclass": RegClass, 2940 "regnamespace": RegNamespace, 2941 "regproc": RegProc, 2942 "regprocedure": RegProcedure, 2943 "regrole": RegRole, 2944 "regtype": RegType, 2945 2946 "serial2": &Serial2Type, 2947 "serial4": &Serial4Type, 2948 "serial8": &Serial8Type, 2949 "smallserial": &Serial2Type, 2950 "bigserial": &Serial8Type, 2951 2952 "string": String, 2953 "uuid": Uuid, 2954 } 2955 2956 // The following map must include all types predefined in PostgreSQL 2957 // that are also not yet defined in CockroachDB and link them to 2958 // github issues. It is also possible, but not necessary, to include 2959 // PostgreSQL types that are already implemented in CockroachDB. 2960 var postgresPredefinedTypeIssues = map[string]int{ 2961 "box": 21286, 2962 "cidr": 18846, 2963 "circle": 21286, 2964 "jsonpath": 22513, 2965 "line": 21286, 2966 "lseg": 21286, 2967 "macaddr": 45813, 2968 "macaddr8": 45813, 2969 "money": 41578, 2970 "path": 21286, 2971 "txid_snapshot": -1, 2972 "xml": 43355, 2973 } 2974 2975 // SQLString outputs the GeoMetadata in a SQL-compatible string. 2976 func (m *GeoMetadata) SQLString() string { 2977 // If SRID is available, display both shape and SRID. 2978 // If shape is available but not SRID, just display shape. 2979 if m.SRID != 0 { 2980 shapeName := strings.ToLower(m.ShapeType.String()) 2981 if m.ShapeType == geopb.ShapeType_Unset { 2982 shapeName = "geometry" 2983 } 2984 return fmt.Sprintf("(%s,%d)", shapeName, m.SRID) 2985 } else if m.ShapeType != geopb.ShapeType_Unset { 2986 return fmt.Sprintf("(%s)", m.ShapeType) 2987 } 2988 return "" 2989 } 2990 2991 // Delimiter selects the correct delimiter rune based on the datum type specified. 2992 func (t *T) Delimiter() string { 2993 switch t.Family() { 2994 case Geometry.Family(), Geography.Family(): 2995 return ":" 2996 default: 2997 return "," 2998 } 2999 }