|
| 1 | +// Copyright 2017 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | + |
| 15 | +package coltypes |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/lib/pq/oid" |
| 21 | + |
| 22 | + "github.com/eandre/sqlparse/pkg/util/pgerror" |
| 23 | + "github.com/eandre/sqlparse/sem/types" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + // Bool is an immutable T instance. |
| 28 | + Bool = &TBool{} |
| 29 | + |
| 30 | + // Bit is an immutable T instance. |
| 31 | + Bit = &TBitArray{Width: 1} |
| 32 | + // VarBit is an immutable T instance. |
| 33 | + VarBit = &TBitArray{Width: 0, Variable: true} |
| 34 | + |
| 35 | + // Int is an immutable T instance. |
| 36 | + Int = &TInt{} |
| 37 | + // Int2 is an immutable T instance. |
| 38 | + Int2 = &TInt{Width: 16} |
| 39 | + // Int4 is an immutable T instance. |
| 40 | + Int4 = &TInt{Width: 32} |
| 41 | + // Int8 is an immutable T instance. |
| 42 | + Int8 = &TInt{Width: 64} |
| 43 | + |
| 44 | + // Serial is an immutable T instance. |
| 45 | + Serial = &TSerial{TInt: Int} |
| 46 | + // Serial2 is an immutable T instance. |
| 47 | + Serial2 = &TSerial{TInt: Int2} |
| 48 | + // Serial4 is an immutable T instance. |
| 49 | + Serial4 = &TSerial{TInt: Int4} |
| 50 | + // Serial8 is an immutable T instance. |
| 51 | + Serial8 = &TSerial{TInt: Int8} |
| 52 | + |
| 53 | + // Float4 is an immutable T instance. |
| 54 | + Float4 = &TFloat{Short: true} |
| 55 | + // Float8 is an immutable T instance. |
| 56 | + Float8 = &TFloat{} |
| 57 | + |
| 58 | + // Decimal is an immutable T instance. |
| 59 | + Decimal = &TDecimal{} |
| 60 | + |
| 61 | + // Date is an immutable T instance. |
| 62 | + Date = &TDate{} |
| 63 | + |
| 64 | + // Time is an immutable T instance. |
| 65 | + Time = &TTime{} |
| 66 | + |
| 67 | + // Timestamp is an immutable T instance. |
| 68 | + Timestamp = &TTimestamp{} |
| 69 | + // TimestampWithTZ is an immutable T instance. |
| 70 | + TimestampWithTZ = &TTimestampTZ{} |
| 71 | + |
| 72 | + // Interval is an immutable T instance. |
| 73 | + Interval = &TInterval{} |
| 74 | + |
| 75 | + // Char is an immutable T instance. See strings.go for details. |
| 76 | + Char = &TString{Variant: TStringVariantCHAR, N: 1} |
| 77 | + // VarChar is an immutable T instance. See strings.go for details. |
| 78 | + VarChar = &TString{Variant: TStringVariantVARCHAR} |
| 79 | + // String is an immutable T instance. See strings.go for details. |
| 80 | + String = &TString{Variant: TStringVariantSTRING} |
| 81 | + // QChar is an immutable T instance. See strings.go for details. |
| 82 | + QChar = &TString{Variant: TStringVariantQCHAR} |
| 83 | + |
| 84 | + // Name is an immutable T instance. |
| 85 | + Name = &TName{} |
| 86 | + |
| 87 | + // Bytes is an immutable T instance. |
| 88 | + Bytes = &TBytes{} |
| 89 | + |
| 90 | + // Int2vector is an immutable T instance. |
| 91 | + Int2vector = &TVector{Name: "INT2VECTOR", ParamType: Int} |
| 92 | + |
| 93 | + // UUID is an immutable T instance. |
| 94 | + UUID = &TUUID{} |
| 95 | + |
| 96 | + // INet is an immutable T instance. |
| 97 | + INet = &TIPAddr{} |
| 98 | + |
| 99 | + // JSON is an immutable T instance. |
| 100 | + JSON = &TJSON{} |
| 101 | + |
| 102 | + // Oid is an immutable T instance. |
| 103 | + Oid = &TOid{Name: "OID"} |
| 104 | + // RegClass is an immutable T instance. |
| 105 | + RegClass = &TOid{Name: "REGCLASS"} |
| 106 | + // RegNamespace is an immutable T instance. |
| 107 | + RegNamespace = &TOid{Name: "REGNAMESPACE"} |
| 108 | + // RegProc is an immutable T instance. |
| 109 | + RegProc = &TOid{Name: "REGPROC"} |
| 110 | + // RegProcedure is an immutable T instance. |
| 111 | + RegProcedure = &TOid{Name: "REGPROCEDURE"} |
| 112 | + // RegType is an immutable T instance. |
| 113 | + RegType = &TOid{Name: "REGTYPE"} |
| 114 | + |
| 115 | + // OidVector is an immutable T instance. |
| 116 | + OidVector = &TVector{Name: "OIDVECTOR", ParamType: Oid} |
| 117 | +) |
| 118 | + |
| 119 | +var errBitLengthNotPositive = pgerror.NewError(pgerror.CodeInvalidParameterValueError, |
| 120 | + "length for type bit must be at least 1") |
| 121 | + |
| 122 | +// NewBitArrayType creates a new BIT type with the given bit width. |
| 123 | +func NewBitArrayType(width int, varying bool) (*TBitArray, error) { |
| 124 | + if width < 1 { |
| 125 | + return nil, errBitLengthNotPositive |
| 126 | + } |
| 127 | + return &TBitArray{Width: uint(width), Variable: varying}, nil |
| 128 | +} |
| 129 | + |
| 130 | +var errFloatPrecAtLeast1 = pgerror.NewError(pgerror.CodeInvalidParameterValueError, |
| 131 | + "precision for type float must be at least 1 bit") |
| 132 | +var errFloatPrecMax54 = pgerror.NewError(pgerror.CodeInvalidParameterValueError, |
| 133 | + "precision for type float must be less than 54 bits") |
| 134 | + |
| 135 | +// NewFloat creates a type alias for FLOAT with the given precision. |
| 136 | +func NewFloat(prec int64) (*TFloat, error) { |
| 137 | + if prec < 1 { |
| 138 | + return nil, errFloatPrecAtLeast1 |
| 139 | + } |
| 140 | + if prec <= 24 { |
| 141 | + return Float4, nil |
| 142 | + } |
| 143 | + if prec <= 54 { |
| 144 | + return Float8, nil |
| 145 | + } |
| 146 | + return nil, errFloatPrecMax54 |
| 147 | +} |
| 148 | + |
| 149 | +// ArrayOf creates a type alias for an array of the given element type and fixed bounds. |
| 150 | +func ArrayOf(colType T, bounds []int32) (T, error) { |
| 151 | + if !canBeInArrayColType(colType) { |
| 152 | + return nil, pgerror.NewErrorf(pgerror.CodeFeatureNotSupportedError, "arrays of %s not allowed", colType) |
| 153 | + } |
| 154 | + return &TArray{ParamType: colType, Bounds: bounds}, nil |
| 155 | +} |
| 156 | + |
| 157 | +var typNameLiterals map[string]T |
| 158 | + |
| 159 | +func init() { |
| 160 | + typNameLiterals = make(map[string]T) |
| 161 | + for o, t := range types.OidToType { |
| 162 | + name := strings.ToLower(oid.TypeName[o]) |
| 163 | + if _, ok := typNameLiterals[name]; !ok { |
| 164 | + colTyp, err := DatumTypeToColumnType(t) |
| 165 | + if err != nil { |
| 166 | + continue |
| 167 | + } |
| 168 | + typNameLiterals[name] = colTyp |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// TypeForNonKeywordTypeName returns the column type for the string name of a |
| 174 | +// type, if one exists. The third return value indicates: |
| 175 | +// 0 if no error or the type is not known in postgres. |
| 176 | +// -1 if the type is known in postgres. |
| 177 | +// >0 for a github issue number. |
| 178 | +func TypeForNonKeywordTypeName(name string) (T, bool, int) { |
| 179 | + t, ok := typNameLiterals[name] |
| 180 | + if ok { |
| 181 | + return t, ok, 0 |
| 182 | + } |
| 183 | + return nil, false, postgresPredefinedTypeIssues[name] |
| 184 | +} |
| 185 | + |
| 186 | +// The following map must include all types predefined in PostgreSQL |
| 187 | +// that are also not yet defined in CockroachDB and link them to |
| 188 | +// github issues. It is also possible, but not necessary, to include |
| 189 | +// PostgreSQL types that are already implemented in CockroachDB. |
| 190 | +var postgresPredefinedTypeIssues = map[string]int{ |
| 191 | + "box": 21286, |
| 192 | + "cidr": 18846, |
| 193 | + "circle": 21286, |
| 194 | + "line": 21286, |
| 195 | + "lseg": 21286, |
| 196 | + "macaddr": -1, |
| 197 | + "macaddr8": -1, |
| 198 | + "money": -1, |
| 199 | + "path": 21286, |
| 200 | + "pg_lsn": -1, |
| 201 | + "point": 21286, |
| 202 | + "polygon": 21286, |
| 203 | + "tsquery": 7821, |
| 204 | + "tsvector": 7821, |
| 205 | + "txid_snapshot": -1, |
| 206 | + "xml": -1, |
| 207 | +} |
0 commit comments