Debian Codegraph @

dependency/sqltypes/type.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqltypes
18
19 import (
20 "fmt"
21
22 "github.com/xwb1989/sqlparser/dependency/querypb"
23 )
24
25 // This file provides wrappers and support
26 // functions for querypb.Type.
27
28 // These bit flags can be used to query on the
29 // common properties of types.
30 const (
31 flagIsIntegral = int(querypb.Flag_ISINTEGRAL)
32 flagIsUnsigned = int(querypb.Flag_ISUNSIGNED)
33 flagIsFloat = int(querypb.Flag_ISFLOAT)
34 flagIsQuoted = int(querypb.Flag_ISQUOTED)
35 flagIsText = int(querypb.Flag_ISTEXT)
36 flagIsBinary = int(querypb.Flag_ISBINARY)
37 )
38
39 // IsIntegral returns true if querypb.Type is an integral
40 // (signed/unsigned) that can be represented using
41 // up to 64 binary bits.
42 // If you have a Value object, use its member function.
43 func IsIntegral(t querypb.Type) bool {
44 return int(t)&flagIsIntegral == flagIsIntegral
45 }
46
47 // IsSigned returns true if querypb.Type is a signed integral.
48 // If you have a Value object, use its member function.
49 func IsSigned(t querypb.Type) bool {
50 return int(t)&(flagIsIntegral|flagIsUnsigned) == flagIsIntegral
51 }
52
53 // IsUnsigned returns true if querypb.Type is an unsigned integral.
54 // Caution: this is not the same as !IsSigned.
55 // If you have a Value object, use its member function.
56 func IsUnsigned(t querypb.Type) bool {
57 return int(t)&(flagIsIntegral|flagIsUnsigned) == flagIsIntegral|flagIsUnsigned
58 }
59
60 // IsFloat returns true is querypb.Type is a floating point.
61 // If you have a Value object, use its member function.
62 func IsFloat(t querypb.Type) bool {
63 return int(t)&flagIsFloat == flagIsFloat
64 }
65
66 // IsQuoted returns true if querypb.Type is a quoted text or binary.
67 // If you have a Value object, use its member function.
68 func IsQuoted(t querypb.Type) bool {
69 return int(t)&flagIsQuoted == flagIsQuoted
70 }
71
72 // IsText returns true if querypb.Type is a text.
73 // If you have a Value object, use its member function.
74 func IsText(t querypb.Type) bool {
75 return int(t)&flagIsText == flagIsText
76 }
77
78 // IsBinary returns true if querypb.Type is a binary.
79 // If you have a Value object, use its member function.
80 func IsBinary(t querypb.Type) bool {
81 return int(t)&flagIsBinary == flagIsBinary
82 }
83
84 // isNumber returns true if the type is any type of number.
85 func isNumber(t querypb.Type) bool {
86 return IsIntegral(t) || IsFloat(t) || t == Decimal
87 }
88
89 // Vitess data types. These are idiomatically
90 // named synonyms for the querypb.Type values.
91 // Although these constants are interchangeable,
92 // they should be treated as different from querypb.Type.
93 // Use the synonyms only to refer to the type in Value.
94 // For proto variables, use the querypb.Type constants
95 // instead.
96 // The following conditions are non-overlapping
97 // and cover all types: IsSigned(), IsUnsigned(),
98 // IsFloat(), IsQuoted(), Null, Decimal, Expression.
99 // Also, IsIntegral() == (IsSigned()||IsUnsigned()).
100 // TestCategory needs to be updated accordingly if
101 // you add a new type.
102 // If IsBinary or IsText is true, then IsQuoted is
103 // also true. But there are IsQuoted types that are
104 // neither binary or text.
105 // querypb.Type_TUPLE is not included in this list
106 // because it's not a valid Value type.
107 // TODO(sougou): provide a categorization function
108 // that returns enums, which will allow for cleaner
109 // switch statements for those who want to cover types
110 // by their category.
111 const (
112 Null = querypb.Type_NULL_TYPE
113 Int8 = querypb.Type_INT8
114 Uint8 = querypb.Type_UINT8
115 Int16 = querypb.Type_INT16
116 Uint16 = querypb.Type_UINT16
117 Int24 = querypb.Type_INT24
118 Uint24 = querypb.Type_UINT24
119 Int32 = querypb.Type_INT32
120 Uint32 = querypb.Type_UINT32
121 Int64 = querypb.Type_INT64
122 Uint64 = querypb.Type_UINT64
123 Float32 = querypb.Type_FLOAT32
124 Float64 = querypb.Type_FLOAT64
125 Timestamp = querypb.Type_TIMESTAMP
126 Date = querypb.Type_DATE
127 Time = querypb.Type_TIME
128 Datetime = querypb.Type_DATETIME
129 Year = querypb.Type_YEAR
130 Decimal = querypb.Type_DECIMAL
131 Text = querypb.Type_TEXT
132 Blob = querypb.Type_BLOB
133 VarChar = querypb.Type_VARCHAR
134 VarBinary = querypb.Type_VARBINARY
135 Char = querypb.Type_CHAR
136 Binary = querypb.Type_BINARY
137 Bit = querypb.Type_BIT
138 Enum = querypb.Type_ENUM
139 Set = querypb.Type_SET
140 Geometry = querypb.Type_GEOMETRY
141 TypeJSON = querypb.Type_JSON
142 Expression = querypb.Type_EXPRESSION
143 )
144
145 // bit-shift the mysql flags by two byte so we
146 // can merge them with the mysql or vitess types.
147 const (
148 mysqlUnsigned = 32
149 mysqlBinary = 128
150 mysqlEnum = 256
151 mysqlSet = 2048
152 )
153
154 // If you add to this map, make sure you add a test case
155 // in tabletserver/endtoend.
156 var mysqlToType = map[int64]querypb.Type{
157 1: Int8,
158 2: Int16,
159 3: Int32,
160 4: Float32,
161 5: Float64,
162 6: Null,
163 7: Timestamp,
164 8: Int64,
165 9: Int24,
166 10: Date,
167 11: Time,
168 12: Datetime,
169 13: Year,
170 16: Bit,
171 245: TypeJSON,
172 246: Decimal,
173 249: Text,
174 250: Text,
175 251: Text,
176 252: Text,
177 253: VarChar,
178 254: Char,
179 255: Geometry,
180 }
181
182 // modifyType modifies the vitess type based on the
183 // mysql flag. The function checks specific flags based
184 // on the type. This allows us to ignore stray flags
185 // that MySQL occasionally sets.
186 func modifyType(typ querypb.Type, flags int64) querypb.Type {
187 switch typ {
188 case Int8:
189 if flags&mysqlUnsigned != 0 {
190 return Uint8
191 }
192 return Int8
193 case Int16:
194 if flags&mysqlUnsigned != 0 {
195 return Uint16
196 }
197 return Int16
198 case Int32:
199 if flags&mysqlUnsigned != 0 {
200 return Uint32
201 }
202 return Int32
203 case Int64:
204 if flags&mysqlUnsigned != 0 {
205 return Uint64
206 }
207 return Int64
208 case Int24:
209 if flags&mysqlUnsigned != 0 {
210 return Uint24
211 }
212 return Int24
213 case Text:
214 if flags&mysqlBinary != 0 {
215 return Blob
216 }
217 return Text
218 case VarChar:
219 if flags&mysqlBinary != 0 {
220 return VarBinary
221 }
222 return VarChar
223 case Char:
224 if flags&mysqlBinary != 0 {
225 return Binary
226 }
227 if flags&mysqlEnum != 0 {
228 return Enum
229 }
230 if flags&mysqlSet != 0 {
231 return Set
232 }
233 return Char
234 }
235 return typ
236 }
237
238 // MySQLToType computes the vitess type from mysql type and flags.
239 func MySQLToType(mysqlType, flags int64) (typ querypb.Type, err error) {
240 result, ok := mysqlToType[mysqlType]
241 if !ok {
242 return 0, fmt.Errorf("unsupported type: %d", mysqlType)
243 }
244 return modifyType(result, flags), nil
245 }
246
247 // typeToMySQL is the reverse of mysqlToType.
248 var typeToMySQL = map[querypb.Type]struct {
249 typ int64
250 flags int64
251 }{
252 Int8: {typ: 1},
253 Uint8: {typ: 1, flags: mysqlUnsigned},
254 Int16: {typ: 2},
255 Uint16: {typ: 2, flags: mysqlUnsigned},
256 Int32: {typ: 3},
257 Uint32: {typ: 3, flags: mysqlUnsigned},
258 Float32: {typ: 4},
259 Float64: {typ: 5},
260 Null: {typ: 6, flags: mysqlBinary},
261 Timestamp: {typ: 7},
262 Int64: {typ: 8},
263 Uint64: {typ: 8, flags: mysqlUnsigned},
264 Int24: {typ: 9},
265 Uint24: {typ: 9, flags: mysqlUnsigned},
266 Date: {typ: 10, flags: mysqlBinary},
267 Time: {typ: 11, flags: mysqlBinary},
268 Datetime: {typ: 12, flags: mysqlBinary},
269 Year: {typ: 13, flags: mysqlUnsigned},
270 Bit: {typ: 16, flags: mysqlUnsigned},
271 TypeJSON: {typ: 245},
272 Decimal: {typ: 246},
273 Text: {typ: 252},
274 Blob: {typ: 252, flags: mysqlBinary},
275 VarChar: {typ: 253},
276 VarBinary: {typ: 253, flags: mysqlBinary},
277 Char: {typ: 254},
278 Binary: {typ: 254, flags: mysqlBinary},
279 Enum: {typ: 254, flags: mysqlEnum},
280 Set: {typ: 254, flags: mysqlSet},
281 Geometry: {typ: 255},
282 }
283
284 // TypeToMySQL returns the equivalent mysql type and flag for a vitess type.
285 func TypeToMySQL(typ querypb.Type) (mysqlType, flags int64) {
286 val := typeToMySQL[typ]
287 return val.typ, val.flags
288 }