-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatum.go
More file actions
511 lines (454 loc) · 11.7 KB
/
Copy pathdatum.go
File metadata and controls
511 lines (454 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !codes
package ast
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"
"github.com/sqlc-dev/marino/charset"
"github.com/sqlc-dev/marino/mysql"
"github.com/sqlc-dev/marino/types"
)
// Kind constants.
const (
KindNull byte = 0
KindInt64 byte = 1
KindUint64 byte = 2
KindFloat32 byte = 3
KindFloat64 byte = 4
KindString byte = 5
KindBytes byte = 6
KindBinaryLiteral byte = 7 // Used for BIT / HEX literals.
KindMysqlDecimal byte = 8
KindMysqlDuration byte = 9
KindMysqlEnum byte = 10
KindMysqlBit byte = 11 // Used for BIT table column values.
KindMysqlSet byte = 12
KindMysqlTime byte = 13
KindInterface byte = 14
KindMinNotNull byte = 15
KindMaxValue byte = 16
KindRaw byte = 17
KindMysqlJSON byte = 18
)
// Datum is a data box holds different kind of data.
// It has better performance and is easier to use than `interface{}`.
type Datum struct {
k byte // datum kind.
i int64 // i can hold int64 uint64 float64 values.
b []byte // b can hold string or []byte values.
x any // x hold all other types.
}
// Kind gets the kind of the datum.
func (d *Datum) Kind() byte {
return d.k
}
// GetInt64 gets int64 value.
func (d *Datum) GetInt64() int64 {
return d.i
}
// SetInt64 sets int64 value.
func (d *Datum) SetInt64(i int64) {
d.k = KindInt64
d.i = i
}
// GetUint64 gets uint64 value.
func (d *Datum) GetUint64() uint64 {
return uint64(d.i)
}
// SetUint64 sets uint64 value.
func (d *Datum) SetUint64(i uint64) {
d.k = KindUint64
d.i = int64(i)
}
// GetFloat64 gets float64 value.
func (d *Datum) GetFloat64() float64 {
return math.Float64frombits(uint64(d.i))
}
// SetFloat64 sets float64 value.
func (d *Datum) SetFloat64(f float64) {
d.k = KindFloat64
d.i = int64(math.Float64bits(f))
}
// GetFloat32 gets float32 value.
func (d *Datum) GetFloat32() float32 {
return float32(math.Float64frombits(uint64(d.i)))
}
// SetFloat32 sets float32 value.
func (d *Datum) SetFloat32(f float32) {
d.k = KindFloat32
d.i = int64(math.Float64bits(float64(f)))
}
// GetString gets string value.
func (d *Datum) GetString() string {
return string(d.b)
}
// SetString sets string value.
func (d *Datum) SetString(s string) {
d.k = KindString
d.b = []byte(s)
}
// GetBytes gets bytes value.
func (d *Datum) GetBytes() []byte {
return d.b
}
// SetBytes sets bytes value to datum.
func (d *Datum) SetBytes(b []byte) {
d.k = KindBytes
d.b = b
}
// SetBytesAsString sets bytes value to datum as string type.
func (d *Datum) SetBytesAsString(b []byte) {
d.k = KindString
d.b = b
}
// GetInterface gets interface value.
func (d *Datum) GetInterface() any {
return d.x
}
// SetInterface sets interface to datum.
func (d *Datum) SetInterface(x any) {
d.k = KindInterface
d.x = x
}
// SetNull sets datum to nil.
func (d *Datum) SetNull() {
d.k = KindNull
d.x = nil
}
// GetBinaryLiteral gets Bit value
func (d *Datum) GetBinaryLiteral() BinaryLit {
return d.b
}
// SetBinaryLiteral sets Bit value
func (d *Datum) SetBinaryLiteral(b BinaryLit) {
d.k = KindBinaryLiteral
d.b = b
}
// GetMysqlDecimal gets decimal value
func (d *Datum) GetMysqlDecimal() *MyDecimal {
return d.x.(*MyDecimal)
}
// SetMysqlDecimal sets decimal value
func (d *Datum) SetMysqlDecimal(b *MyDecimal) {
d.k = KindMysqlDecimal
d.x = b
}
// GetValue gets the value of the datum of any kind.
func (d *Datum) GetValue() any {
switch d.k {
case KindInt64:
return d.GetInt64()
case KindUint64:
return d.GetUint64()
case KindFloat32:
return d.GetFloat32()
case KindFloat64:
return d.GetFloat64()
case KindString:
return d.GetString()
case KindBytes:
return d.GetBytes()
case KindMysqlDecimal:
return d.GetMysqlDecimal()
case KindBinaryLiteral, KindMysqlBit:
return d.GetBinaryLiteral()
default:
return d.GetInterface()
}
}
// SetValue sets any kind of value.
func (d *Datum) SetValue(val any) {
switch x := val.(type) {
case nil:
d.SetNull()
case bool:
if x {
d.SetInt64(1)
} else {
d.SetInt64(0)
}
case int:
d.SetInt64(int64(x))
case int64:
d.SetInt64(x)
case uint64:
d.SetUint64(x)
case float32:
d.SetFloat32(x)
case float64:
d.SetFloat64(x)
case string:
d.SetString(x)
case []byte:
d.SetBytes(x)
case *MyDecimal:
d.SetMysqlDecimal(x)
case BinaryLit:
d.SetBinaryLiteral(x)
case BitLiteral: // Store as BinaryLit for Bit and Hex literals
d.SetBinaryLiteral(BinaryLit(x))
case HexLiteral:
d.SetBinaryLiteral(BinaryLit(x))
default:
d.SetInterface(x)
}
}
// NewDatum creates a new Datum from an interface{}.
func NewDatum(in any) (d Datum) {
switch x := in.(type) {
case []any:
d.SetValue(MakeDatums(x...))
default:
d.SetValue(in)
}
return d
}
// NewBytesDatum creates a new Datum from a byte slice.
func NewBytesDatum(b []byte) (d Datum) {
d.SetBytes(b)
return d
}
// NewStringDatum creates a new Datum from a string.
func NewStringDatum(s string) (d Datum) {
d.SetString(s)
return d
}
// MakeDatums creates datum slice from interfaces.
func MakeDatums(args ...any) []Datum {
datums := make([]Datum, len(args))
for i, v := range args {
datums[i] = NewDatum(v)
}
return datums
}
// BinaryLit is the internal type for storing bit / hex literal type.
type BinaryLit []byte
// BitLiteral is the bit literal type.
type BitLiteral BinaryLit
// HexLiteral is the hex literal type.
type HexLiteral BinaryLit
// ZeroBinaryLit is a BinaryLit literal with zero value.
var ZeroBinaryLit = BinaryLit{}
// String implements fmt.Stringer interface.
func (b BinaryLit) String() string {
if len(b) == 0 {
return ""
}
return "0x" + hex.EncodeToString(b)
}
// ToString returns the string representation for the literal.
func (b BinaryLit) ToString() string {
return string(b)
}
// ToBitLiteralString returns the bit literal representation for the literal.
func (b BinaryLit) ToBitLiteralString(trimLeadingZero bool) string {
if len(b) == 0 {
return "b''"
}
var buf bytes.Buffer
for _, data := range b {
fmt.Fprintf(&buf, "%08b", data)
}
ret := buf.Bytes()
if trimLeadingZero {
ret = bytes.TrimLeft(ret, "0")
if len(ret) == 0 {
ret = []byte{'0'}
}
}
return fmt.Sprintf("b'%s'", string(ret))
}
// ParseBitStr parses bit string.
// The string format can be b'val', B'val' or 0bval, val must be 0 or 1.
// See https://dev.mysql.com/doc/refman/5.7/en/bit-value-literals.html
func ParseBitStr(s string) (BinaryLit, error) {
if len(s) == 0 {
return nil, fmt.Errorf("invalid empty string for parsing bit type")
}
if s[0] == 'b' || s[0] == 'B' {
// format is b'val' or B'val'
s = strings.Trim(s[1:], "'")
} else if strings.HasPrefix(s, "0b") {
s = s[2:]
} else {
// here means format is not b'val', B'val' or 0bval.
return nil, fmt.Errorf("invalid bit type format %s", s)
}
if len(s) == 0 {
return ZeroBinaryLit, nil
}
alignedLength := (len(s) + 7) &^ 7
s = ("00000000" + s)[len(s)+8-alignedLength:] // Pad with zero (slice from `-alignedLength`)
byteLength := len(s) >> 3
buf := make([]byte, byteLength)
for i := range byteLength {
strPosition := i << 3
val, err := strconv.ParseUint(s[strPosition:strPosition+8], 2, 8)
if err != nil {
return nil, err
}
buf[i] = byte(val)
}
return buf, nil
}
// NewBitLiteral parses bit string as BitLiteral type.
func NewBitLiteral(s string) (BitLiteral, error) {
b, err := ParseBitStr(s)
if err != nil {
return BitLiteral{}, err
}
return BitLiteral(b), nil
}
// ToString implement BinaryLiteral interface
func (b BitLiteral) ToString() string {
return BinaryLit(b).ToString()
}
// ParseHexStr parses hexadecimal string literal.
// See https://dev.mysql.com/doc/refman/5.7/en/hexadecimal-literals.html
func ParseHexStr(s string) (BinaryLit, error) {
if len(s) == 0 {
return nil, fmt.Errorf("invalid empty string for parsing hexadecimal literal")
}
if s[0] == 'x' || s[0] == 'X' {
// format is x'val' or X'val'
s = strings.Trim(s[1:], "'")
if len(s)%2 != 0 {
return nil, fmt.Errorf("invalid hexadecimal format, must even numbers, but %d", len(s))
}
} else if strings.HasPrefix(s, "0x") {
s = s[2:]
} else {
// here means format is not x'val', X'val' or 0xval.
return nil, fmt.Errorf("invalid hexadecimal format %s", s)
}
if len(s) == 0 {
return ZeroBinaryLit, nil
}
if len(s)%2 != 0 {
s = "0" + s
}
buf, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return buf, nil
}
// NewHexLiteral parses hexadecimal string as HexLiteral type.
func NewHexLiteral(s string) (HexLiteral, error) {
h, err := ParseHexStr(s)
if err != nil {
return HexLiteral{}, err
}
return HexLiteral(h), nil
}
// ToString implement BinaryLiteral interface
func (b HexLiteral) ToString() string {
return BinaryLit(b).ToString()
}
// SetBinChsClnFlag sets charset, collation as 'binary' and adds binaryFlag to FieldType.
func SetBinChsClnFlag(ft *types.FieldType) {
ft.SetCharset(charset.CharsetBin)
ft.SetCollate(charset.CollationBin)
ft.AddFlag(mysql.BinaryFlag)
}
// DefaultFsp is the default digit of fractional seconds part.
// MySQL use 0 as the default Fsp.
const DefaultFsp = int8(0)
// DefaultTypeForValue returns the default FieldType for the value.
func DefaultTypeForValue(value any, tp *types.FieldType, charset string, collate string) {
switch x := value.(type) {
case nil:
tp.SetType(mysql.TypeNull)
tp.SetFlen(0)
tp.SetDecimal(0)
SetBinChsClnFlag(tp)
case bool:
tp.SetType(mysql.TypeLonglong)
tp.SetFlen(1)
tp.SetDecimal(0)
tp.AddFlag(mysql.IsBooleanFlag)
SetBinChsClnFlag(tp)
case int:
tp.SetType(mysql.TypeLonglong)
tp.SetFlen(StrLenOfInt64Fast(int64(x)))
tp.SetDecimal(0)
SetBinChsClnFlag(tp)
case int64:
tp.SetType(mysql.TypeLonglong)
tp.SetFlen(StrLenOfInt64Fast(x))
tp.SetDecimal(0)
SetBinChsClnFlag(tp)
case uint64:
tp.SetType(mysql.TypeLonglong)
tp.AddFlag(mysql.UnsignedFlag)
tp.SetFlen(StrLenOfUint64Fast(x))
tp.SetDecimal(0)
SetBinChsClnFlag(tp)
case string:
tp.SetType(mysql.TypeVarString)
// TODO: tp.flen should be len(x) * 3 (max bytes length of CharsetUTF8)
tp.SetFlen(len(x))
tp.SetDecimal(types.UnspecifiedLength)
tp.SetCharset(charset)
tp.SetCollate(collate)
case float32:
tp.SetType(mysql.TypeFloat)
s := strconv.FormatFloat(float64(x), 'f', -1, 32)
tp.SetFlen(len(s))
tp.SetDecimal(types.UnspecifiedLength)
SetBinChsClnFlag(tp)
case float64:
tp.SetType(mysql.TypeDouble)
s := strconv.FormatFloat(x, 'f', -1, 64)
tp.SetFlen(len(s))
tp.SetDecimal(types.UnspecifiedLength)
SetBinChsClnFlag(tp)
case []byte:
tp.SetType(mysql.TypeBlob)
tp.SetFlen(len(x))
tp.SetDecimal(types.UnspecifiedLength)
SetBinChsClnFlag(tp)
case BitLiteral:
tp.SetType(mysql.TypeVarString)
tp.SetFlen(len(x))
tp.SetDecimal(0)
SetBinChsClnFlag(tp)
case HexLiteral:
tp.SetType(mysql.TypeVarString)
tp.SetFlen(len(x) * 3)
tp.SetDecimal(0)
tp.AddFlag(mysql.UnsignedFlag)
SetBinChsClnFlag(tp)
case BinaryLit:
tp.SetType(mysql.TypeBit)
tp.SetFlen(len(x) * 8)
tp.SetDecimal(0)
SetBinChsClnFlag(tp)
tp.DelFlag(mysql.BinaryFlag)
tp.AddFlag(mysql.UnsignedFlag)
case *MyDecimal:
tp.SetType(mysql.TypeNewDecimal)
tp.SetFlen(len(x.ToString()))
tp.SetDecimal(int(x.digitsFrac))
SetBinChsClnFlag(tp)
default:
tp.SetType(mysql.TypeUnspecified)
tp.SetFlen(types.UnspecifiedLength)
tp.SetDecimal(types.UnspecifiedLength)
}
}