github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/types/oid.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package types 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/sql/oidext" 15 "github.com/cockroachdb/errors" 16 "github.com/lib/pq/oid" 17 ) 18 19 // Convenience list of pre-constructed OID-related types. 20 var ( 21 // Oid is the type of a Postgres Object ID value. 22 Oid = &T{InternalType: InternalType{ 23 Family: OidFamily, Oid: oid.T_oid, Locale: &emptyLocale}} 24 25 // Regclass is the type of a Postgres regclass OID variant (T_regclass). 26 RegClass = &T{InternalType: InternalType{ 27 Family: OidFamily, Oid: oid.T_regclass, Locale: &emptyLocale}} 28 29 // RegNamespace is the type of a Postgres regnamespace OID variant 30 // (T_regnamespace). 31 RegNamespace = &T{InternalType: InternalType{ 32 Family: OidFamily, Oid: oid.T_regnamespace, Locale: &emptyLocale}} 33 34 // RegProc is the type of a Postgres regproc OID variant (T_regproc). 35 RegProc = &T{InternalType: InternalType{ 36 Family: OidFamily, Oid: oid.T_regproc, Locale: &emptyLocale}} 37 38 // RegProcedure is the type of a Postgres regprocedure OID variant 39 // (T_regprocedure). 40 RegProcedure = &T{InternalType: InternalType{ 41 Family: OidFamily, Oid: oid.T_regprocedure, Locale: &emptyLocale}} 42 43 // RegType is the type of of a Postgres regtype OID variant (T_regtype). 44 RegType = &T{InternalType: InternalType{ 45 Family: OidFamily, Oid: oid.T_regtype, Locale: &emptyLocale}} 46 47 // OidVector is a type-alias for an array of Oid values, but with a different 48 // OID (T_oidvector instead of T__oid). It is a special VECTOR type used by 49 // Postgres in system tables. OidVectors are 0-indexed, unlike normal arrays. 50 OidVector = &T{InternalType: InternalType{ 51 Family: ArrayFamily, Oid: oid.T_oidvector, ArrayContents: Oid, Locale: &emptyLocale}} 52 ) 53 54 // OidToType maps Postgres object IDs to CockroachDB types. We export the map 55 // instead of a method so that other packages can iterate over the map directly. 56 // Note that additional elements for the array Oid types are added in init(). 57 var OidToType = map[oid.Oid]*T{ 58 oid.T_anyelement: Any, 59 oid.T_bit: typeBit, 60 oid.T_bool: Bool, 61 oid.T_bpchar: typeBpChar, 62 oid.T_bytea: Bytes, 63 oid.T_char: typeQChar, 64 oid.T_date: Date, 65 oid.T_float4: Float4, 66 oid.T_float8: Float, 67 oid.T_int2: Int2, 68 oid.T_int2vector: Int2Vector, 69 oid.T_int4: Int4, 70 oid.T_int8: Int, 71 oid.T_inet: INet, 72 oid.T_interval: Interval, 73 oid.T_jsonb: Jsonb, 74 oid.T_name: Name, 75 oid.T_numeric: Decimal, 76 oid.T_oid: Oid, 77 oid.T_oidvector: OidVector, 78 oid.T_record: AnyTuple, 79 oid.T_regclass: RegClass, 80 oid.T_regnamespace: RegNamespace, 81 oid.T_regproc: RegProc, 82 oid.T_regprocedure: RegProcedure, 83 oid.T_regtype: RegType, 84 oid.T_text: String, 85 oid.T_time: Time, 86 oid.T_timetz: TimeTZ, 87 oid.T_timestamp: Timestamp, 88 oid.T_timestamptz: TimestampTZ, 89 oid.T_unknown: Unknown, 90 oid.T_uuid: Uuid, 91 oid.T_varbit: VarBit, 92 oid.T_varchar: VarChar, 93 94 oidext.T_geometry: Geometry, 95 oidext.T_geography: Geography, 96 } 97 98 // oidToArrayOid maps scalar type Oids to their corresponding array type Oid. 99 var oidToArrayOid = map[oid.Oid]oid.Oid{ 100 oid.T_anyelement: oid.T_anyarray, 101 oid.T_bit: oid.T__bit, 102 oid.T_bool: oid.T__bool, 103 oid.T_bpchar: oid.T__bpchar, 104 oid.T_bytea: oid.T__bytea, 105 oid.T_char: oid.T__char, 106 oid.T_date: oid.T__date, 107 oid.T_float4: oid.T__float4, 108 oid.T_float8: oid.T__float8, 109 oid.T_inet: oid.T__inet, 110 oid.T_int2: oid.T__int2, 111 oid.T_int2vector: oid.T__int2vector, 112 oid.T_int4: oid.T__int4, 113 oid.T_int8: oid.T__int8, 114 oid.T_interval: oid.T__interval, 115 oid.T_jsonb: oid.T__jsonb, 116 oid.T_name: oid.T__name, 117 oid.T_numeric: oid.T__numeric, 118 oid.T_oid: oid.T__oid, 119 oid.T_oidvector: oid.T__oidvector, 120 oid.T_record: oid.T__record, 121 oid.T_regclass: oid.T__regclass, 122 oid.T_regnamespace: oid.T__regnamespace, 123 oid.T_regproc: oid.T__regproc, 124 oid.T_regprocedure: oid.T__regprocedure, 125 oid.T_regtype: oid.T__regtype, 126 oid.T_text: oid.T__text, 127 oid.T_time: oid.T__time, 128 oid.T_timetz: oid.T__timetz, 129 oid.T_timestamp: oid.T__timestamp, 130 oid.T_timestamptz: oid.T__timestamptz, 131 oid.T_uuid: oid.T__uuid, 132 oid.T_varbit: oid.T__varbit, 133 oid.T_varchar: oid.T__varchar, 134 135 oidext.T_geometry: oidext.T__geometry, 136 oidext.T_geography: oidext.T__geography, 137 } 138 139 // familyToOid maps each type family to a default OID value that is used when 140 // another Oid is not present (e.g. when deserializing a type saved by a 141 // previous version of CRDB). 142 var familyToOid = map[Family]oid.Oid{ 143 BoolFamily: oid.T_bool, 144 IntFamily: oid.T_int8, 145 FloatFamily: oid.T_float8, 146 DecimalFamily: oid.T_numeric, 147 DateFamily: oid.T_date, 148 TimestampFamily: oid.T_timestamp, 149 IntervalFamily: oid.T_interval, 150 StringFamily: oid.T_text, 151 BytesFamily: oid.T_bytea, 152 TimestampTZFamily: oid.T_timestamptz, 153 CollatedStringFamily: oid.T_text, 154 OidFamily: oid.T_oid, 155 UnknownFamily: oid.T_unknown, 156 UuidFamily: oid.T_uuid, 157 ArrayFamily: oid.T_anyarray, 158 INetFamily: oid.T_inet, 159 TimeFamily: oid.T_time, 160 TimeTZFamily: oid.T_timetz, 161 JsonFamily: oid.T_jsonb, 162 TupleFamily: oid.T_record, 163 BitFamily: oid.T_bit, 164 AnyFamily: oid.T_anyelement, 165 166 GeometryFamily: oidext.T_geometry, 167 GeographyFamily: oidext.T_geography, 168 } 169 170 // ArrayOids is a set of all oids which correspond to an array type. 171 var ArrayOids = map[oid.Oid]struct{}{} 172 173 func init() { 174 for o, ao := range oidToArrayOid { 175 ArrayOids[ao] = struct{}{} 176 OidToType[ao] = MakeArray(OidToType[o]) 177 } 178 } 179 180 // calcArrayOid returns the OID of the array type having elements of the given 181 // type. 182 func calcArrayOid(elemTyp *T) oid.Oid { 183 o := elemTyp.Oid() 184 switch elemTyp.Family() { 185 case ArrayFamily: 186 // Postgres nested arrays return the OID of the nested array (i.e. the 187 // OID doesn't change no matter how many levels of nesting there are), 188 // except in the special-case of the vector types. 189 switch o { 190 case oid.T_int2vector, oid.T_oidvector: 191 // Vector types have their own array OID types. 192 default: 193 return o 194 } 195 196 case UnknownFamily: 197 // Postgres doesn't have an OID for an array of unknown values, since 198 // it's not possible to create that in Postgres. But CRDB does allow that, 199 // so return 0 for that case (since there's no T__unknown). This is what 200 // previous versions of CRDB returned for this case. 201 return unknownArrayOid 202 203 case EnumFamily: 204 return StableTypeIDToOID(elemTyp.StableArrayTypeID()) 205 } 206 207 // Map the OID of the array element type to the corresponding array OID. 208 // This should always be possible for all other OIDs (checked in oid.go 209 // init method). 210 o = oidToArrayOid[o] 211 if o == 0 { 212 panic(errors.AssertionFailedf("oid %d couldn't be mapped to array oid", o)) 213 } 214 return o 215 }