forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtypes.go
More file actions
643 lines (556 loc) · 16 KB
/
types.go
File metadata and controls
643 lines (556 loc) · 16 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package duckdb
import (
"database/sql/driver"
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"reflect"
"strings"
"time"
"github.com/go-viper/mapstructure/v2"
"github.com/google/uuid"
"github.com/duckdb/duckdb-go/v2/mapping"
)
// duckdb-go exports the following type wrappers:
// UUID, Map, Interval, Decimal, Union, Composite (optional, used to scan LIST and STRUCT).
// Pre-computed reflect type values to avoid repeated allocations.
var (
reflectTypeBool = reflect.TypeFor[bool]()
reflectTypeInt8 = reflect.TypeFor[int8]()
reflectTypeInt16 = reflect.TypeFor[int16]()
reflectTypeInt32 = reflect.TypeFor[int32]()
reflectTypeInt64 = reflect.TypeFor[int64]()
reflectTypeUint8 = reflect.TypeFor[uint8]()
reflectTypeUint16 = reflect.TypeFor[uint16]()
reflectTypeUint32 = reflect.TypeFor[uint32]()
reflectTypeUint64 = reflect.TypeFor[uint64]()
reflectTypeFloat32 = reflect.TypeFor[float32]()
reflectTypeFloat64 = reflect.TypeFor[float64]()
reflectTypeTime = reflect.TypeFor[time.Time]()
reflectTypeInterval = reflect.TypeFor[Interval]()
reflectTypeBigInt = reflect.TypeFor[*big.Int]()
reflectTypeString = reflect.TypeFor[string]()
reflectTypeBytes = reflect.TypeFor[[]byte]()
reflectTypeDecimal = reflect.TypeFor[Decimal]()
reflectTypeSliceAny = reflect.TypeFor[[]any]()
reflectTypeMapString = reflect.TypeFor[map[string]any]()
reflectTypeMap = reflect.TypeFor[OrderedMap]()
reflectTypeUnion = reflect.TypeFor[Union]()
reflectTypeAny = reflect.TypeFor[any]()
reflectTypeUUID = reflect.TypeFor[UUID]()
)
type numericType interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
const uuidLength = 16
type UUID [uuidLength]byte
// Scan implements the sql.Scanner interface.
func (u *UUID) Scan(v any) error {
switch val := v.(type) {
case []byte:
if len(val) != uuidLength {
return u.Scan(string(val))
}
copy(u[:], val)
case string:
id, err := uuid.Parse(val)
if err != nil {
return err
}
copy(u[:], id[:])
default:
return fmt.Errorf("invalid UUID value type: %T", val)
}
return nil
}
// String implements the fmt.Stringer interface.
func (u *UUID) String() string {
buf := make([]byte, 36)
hex.Encode(buf, u[:4])
buf[8] = '-'
hex.Encode(buf[9:13], u[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], u[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], u[8:10])
buf[23] = '-'
hex.Encode(buf[24:], u[10:])
return string(buf)
}
// Value implements the driver.Valuer interface.
func (u *UUID) Value() (driver.Value, error) {
return u.String(), nil
}
func inferUUID(val any) (mapping.HugeInt, error) {
var id UUID
switch v := val.(type) {
case UUID:
id = v
case *UUID:
id = *v
case []uint8:
if len(v) != uuidLength {
return mapping.HugeInt{}, castError(reflect.TypeOf(val).String(), reflectTypeUUID.String())
}
for i := range uuidLength {
id[i] = v[i]
}
default:
return mapping.HugeInt{}, castError(reflect.TypeOf(val).String(), reflectTypeUUID.String())
}
hi := uuidToHugeInt(id)
return hi, nil
}
// duckdb_hugeint is composed of (lower, upper) components.
// The value is computed as: upper * 2^64 + lower
func hugeIntToUUID(hugeInt *mapping.HugeInt) []byte {
// Flip the sign bit of the signed hugeint to transform it to UUID bytes.
var val [uuidLength]byte
lower, upper := mapping.HugeIntMembers(hugeInt)
binary.BigEndian.PutUint64(val[:8], uint64(upper)^1<<63)
binary.BigEndian.PutUint64(val[8:], lower)
return val[:]
}
func uuidToHugeInt(uuid UUID) mapping.HugeInt {
// Flip the sign bit.
lower := binary.BigEndian.Uint64(uuid[8:])
upper := binary.BigEndian.Uint64(uuid[:8])
return mapping.NewHugeInt(lower, int64(upper^(1<<63)))
}
func hugeIntToNative(hugeInt *mapping.HugeInt) *big.Int {
lower, upper := mapping.HugeIntMembers(hugeInt)
i := big.NewInt(upper)
i.Lsh(i, 64)
i.Add(i, new(big.Int).SetUint64(lower))
return i
}
func numToBigInt(val any) (*big.Int, error) {
switch v := val.(type) {
case uint8:
return big.NewInt(int64(v)), nil
case int8:
return big.NewInt(int64(v)), nil
case uint16:
return big.NewInt(int64(v)), nil
case int16:
return big.NewInt(int64(v)), nil
case uint32:
return big.NewInt(int64(v)), nil
case int32:
return big.NewInt(int64(v)), nil
case uint64:
return new(big.Int).SetUint64(v), nil
case int64:
return big.NewInt(v), nil
case uint:
return new(big.Int).SetUint64(uint64(v)), nil
case int:
return big.NewInt(int64(v)), nil
case float32:
bigFloat := new(big.Float).SetFloat64(float64(v))
bigInt, _ := bigFloat.Int(nil)
return bigInt, nil
case float64:
bigFloat := new(big.Float).SetFloat64(v)
bigInt, _ := bigFloat.Int(nil)
return bigInt, nil
case *big.Int:
if v == nil {
return nil, castError("nil *big.Int", "*big.Int")
}
return v, nil
case Decimal:
if v.Value == nil {
return nil, castError("nil Decimal.Value", "*big.Int")
}
return v.Value, nil
default:
return nil, castError(reflect.TypeOf(val).String(), "*big.Int")
}
}
func hugeIntFromNative(i *big.Int) (mapping.HugeInt, error) {
d := big.NewInt(1)
d.Lsh(d, 64)
q := new(big.Int)
r := new(big.Int)
q.DivMod(i, d, r)
if !q.IsInt64() {
return mapping.HugeInt{}, fmt.Errorf("big.Int(%s) is too big for HUGEINT", i.String())
}
return mapping.NewHugeInt(r.Uint64(), q.Int64()), nil
}
func inferHugeInt(val any) (mapping.HugeInt, error) {
var err error
var hi mapping.HugeInt
switch v := val.(type) {
case uint8:
hi = mapping.NewHugeInt(uint64(v), 0)
case int8:
hi = mapping.NewHugeInt(uint64(v), int64(v)>>63)
case uint16:
hi = mapping.NewHugeInt(uint64(v), 0)
case int16:
hi = mapping.NewHugeInt(uint64(v), int64(v)>>63)
case uint32:
hi = mapping.NewHugeInt(uint64(v), 0)
case int32:
hi = mapping.NewHugeInt(uint64(v), int64(v)>>63)
case uint64:
hi = mapping.NewHugeInt(v, 0)
case int64:
hi = mapping.NewHugeInt(uint64(v), v>>63)
case uint:
hi = mapping.NewHugeInt(uint64(v), 0)
case int:
hi = mapping.NewHugeInt(uint64(v), int64(v)>>63)
default:
var i *big.Int
if i, err = numToBigInt(val); err != nil {
return mapping.HugeInt{}, err
}
hi, err = hugeIntFromNative(i)
}
return hi, err
}
func uhugeIntToNative(uhi *mapping.UHugeInt) *big.Int {
lower, upper := mapping.UHugeIntMembers(uhi)
i := new(big.Int).SetUint64(upper)
i.Lsh(i, 64)
i.Add(i, new(big.Int).SetUint64(lower))
return i
}
func uhugeIntFromNative(i *big.Int) (mapping.UHugeInt, error) {
if i.Sign() < 0 {
return mapping.UHugeInt{}, fmt.Errorf("big.Int(%s) is negative, cannot convert to UHUGEINT", i.String())
}
d := big.NewInt(1)
d.Lsh(d, 64)
q := new(big.Int)
r := new(big.Int)
q.DivMod(i, d, r)
if !q.IsUint64() {
return mapping.UHugeInt{}, fmt.Errorf("big.Int(%s) is too big for UHUGEINT", i.String())
}
return mapping.NewUHugeInt(r.Uint64(), q.Uint64()), nil
}
func bigNumToNative(bn *mapping.BigNum) *big.Int {
data, isNegative := mapping.BigNumMembers(bn)
// Data is in big-endian format, which is what big.Int.SetBytes expects.
i := new(big.Int).SetBytes(data)
if isNegative {
i.Neg(i)
}
return i
}
func bigNumFromNative(i *big.Int) mapping.BigNum {
isNegative := i.Sign() < 0
// Get absolute value bytes in big-endian format (which is what NewBigNum expects).
absVal := new(big.Int).Abs(i)
bigEndian := absVal.Bytes()
// Handle zero case - ensure at least one byte
if len(bigEndian) == 0 {
bigEndian = []byte{0}
}
return mapping.NewBigNum(bigEndian, isNegative)
}
func inferBigNum(val any) (mapping.BigNum, error) {
i, err := numToBigInt(val)
if err != nil {
return mapping.BigNum{}, err
}
return bigNumFromNative(i), nil
}
func inferUHugeInt(val any) (mapping.UHugeInt, error) {
var err error
var uhi mapping.UHugeInt
switch v := val.(type) {
case uint8:
uhi = mapping.NewUHugeInt(uint64(v), 0)
case int8:
if v < 0 {
return mapping.UHugeInt{}, fmt.Errorf("negative value %d cannot be converted to UHUGEINT", v)
}
uhi = mapping.NewUHugeInt(uint64(v), 0)
case uint16:
uhi = mapping.NewUHugeInt(uint64(v), 0)
case int16:
if v < 0 {
return mapping.UHugeInt{}, fmt.Errorf("negative value %d cannot be converted to UHUGEINT", v)
}
uhi = mapping.NewUHugeInt(uint64(v), 0)
case uint32:
uhi = mapping.NewUHugeInt(uint64(v), 0)
case int32:
if v < 0 {
return mapping.UHugeInt{}, fmt.Errorf("negative value %d cannot be converted to UHUGEINT", v)
}
uhi = mapping.NewUHugeInt(uint64(v), 0)
case uint64:
uhi = mapping.NewUHugeInt(v, 0)
case int64:
if v < 0 {
return mapping.UHugeInt{}, fmt.Errorf("negative value %d cannot be converted to UHUGEINT", v)
}
uhi = mapping.NewUHugeInt(uint64(v), 0)
case uint:
uhi = mapping.NewUHugeInt(uint64(v), 0)
case int:
if v < 0 {
return mapping.UHugeInt{}, fmt.Errorf("negative value %d cannot be converted to UHUGEINT", v)
}
uhi = mapping.NewUHugeInt(uint64(v), 0)
default:
var i *big.Int
if i, err = numToBigInt(val); err != nil {
return mapping.UHugeInt{}, err
}
uhi, err = uhugeIntFromNative(i)
}
return uhi, err
}
// Map is used to represent DuckDB maps as Go maps.
// Note that Go maps do not preserve key order, so direct comparison operations
// on DuckDB maps may not behave as expected when using this type. Use OrderedMap as an alternative.
// Deprecated: Use OrderedMap instead to preserve key order.
type Map map[any]any
func (m *Map) Scan(v any) error {
data, ok := v.(OrderedMap)
if !ok {
return fmt.Errorf("invalid type `%T` for scanning `Map`, expected `OrderedMap`", v)
}
nm := make(map[any]any, data.Len())
keys := data.Keys()
vals := data.Values()
for i, key := range keys {
nm[key] = vals[i]
}
*m = nm
return nil
}
// OrderedMap is used to represent DuckDB maps while preserving key order.
// Key order is significant in DuckDB maps for direct comparison operations.
//
// NOTE: only supports keys of comparable types (no slices, maps, or functions).
// NOTE: Set and Get use linear search, so performance may degrade with large maps.
//
//nolint:recvcheck
type OrderedMap struct {
keys []any
values []any
}
func (om *OrderedMap) Keys() []any {
return append([]any(nil), om.keys...)
}
func (om *OrderedMap) Values() []any {
return append([]any(nil), om.values...)
}
func (om *OrderedMap) Len() int {
return len(om.keys)
}
// String implements the fmt.Stringer interface for debugging purposes.
func (om OrderedMap) String() string {
var sb strings.Builder
sb.WriteString("OrderedMap{")
for i, key := range om.keys {
if i > 0 {
sb.WriteString(", ")
}
fmt.Fprintf(&sb, "%v: %v", key, om.values[i])
}
sb.WriteString("}")
return sb.String()
}
// Set adds or updates a key-value pair, always inserting the key to the end of the map.
// Previous entries with the same key will be removed to ensure only the last value is retained.
func (om *OrderedMap) Set(k, v any) {
om.Delete(k)
om.keys = append(om.keys, k)
om.values = append(om.values, v)
}
func (om *OrderedMap) Get(k any) (any, bool) {
for i, key := range om.keys {
if key == k {
return om.values[i], true
}
}
return nil, false
}
func (om *OrderedMap) Delete(k any) {
for i, key := range om.keys {
if key == k {
om.keys = append(om.keys[:i], om.keys[i+1:]...)
om.values = append(om.values[:i], om.values[i+1:]...)
return
}
}
}
func (om *OrderedMap) Scan(v any) error {
data, ok := v.(OrderedMap)
if !ok {
return fmt.Errorf("invalid type `%T` for scanning `OrderedMap`, expected `OrderedMap`", v)
}
om.keys = data.Keys()
om.values = data.Values()
return nil
}
func mapKeysField() string {
return "key"
}
func mapValuesField() string {
return "value"
}
type Interval struct {
Days int32 `json:"days"`
Months int32 `json:"months"`
Micros int64 `json:"micros"`
}
func inferInterval(val any) (mapping.Interval, error) {
var i Interval
switch v := val.(type) {
case Interval:
i = v
default:
return mapping.Interval{}, castError(reflect.TypeOf(val).String(), reflectTypeInterval.String())
}
return mapping.NewInterval(i.Months, i.Days, i.Micros), nil
}
// Composite can be used as the `Scanner` type for any composite types (maps, lists, structs).
type Composite[T any] struct {
t T
}
func (s Composite[T]) Get() T {
return s.t
}
func (s *Composite[T]) Scan(v any) error {
return mapstructure.Decode(v, &s.t)
}
const max_decimal_width = 38
type Decimal struct {
Width uint8
Scale uint8
Value *big.Int
}
func (d Decimal) Float64() float64 {
scale := big.NewInt(int64(d.Scale))
factor := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), scale, nil))
value := new(big.Float).SetInt(d.Value)
value.Quo(value, factor)
f, _ := value.Float64()
return f
}
func (d Decimal) String() string {
// Get the sign, and return early, if zero.
if d.Value.Sign() == 0 {
return "0"
}
// Remove the sign from the string integer value
var signStr string
scaleless := d.Value.String()
if d.Value.Sign() < 0 {
signStr = "-"
scaleless = scaleless[1:]
}
// Remove all zeros from the right side
zeroTrimmed := strings.TrimRightFunc(scaleless, func(r rune) bool { return r == '0' })
scale := int(d.Scale) - (len(scaleless) - len(zeroTrimmed))
// If the string is still bigger than the scale factor, output it without a decimal point
if scale <= 0 {
return signStr + zeroTrimmed + strings.Repeat("0", -1*scale)
}
// Pad a number with 0.0's if needed
if len(zeroTrimmed) <= scale {
return fmt.Sprintf("%s0.%s%s", signStr, strings.Repeat("0", scale-len(zeroTrimmed)), zeroTrimmed)
}
return signStr + zeroTrimmed[:len(zeroTrimmed)-scale] + "." + zeroTrimmed[len(zeroTrimmed)-scale:]
}
type Union struct {
Value driver.Value `json:"value"`
Tag string `json:"tag"`
}
func castToTime(val any) (time.Time, error) {
var ti time.Time
switch v := val.(type) {
case time.Time:
ti = v
default:
return ti, castError(reflect.TypeOf(val).String(), reflectTypeTime.String())
}
return ti, nil
}
func getTSTicks(t Type, val any) (int64, error) {
ti, err := castToTime(val)
if err != nil {
return 0, err
}
if t == TYPE_TIMESTAMP_S {
return ti.Unix(), nil
}
if t == TYPE_TIMESTAMP_MS {
return ti.UnixMilli(), nil
}
year := ti.Year()
if t == TYPE_TIMESTAMP || t == TYPE_TIMESTAMP_TZ {
if year < -290307 || year > 294246 {
return 0, conversionError(year, -290307, 294246)
}
return ti.UnixMicro(), nil
}
// TYPE_TIMESTAMP_NS:
if year < 1678 || year > 2262 {
return 0, conversionError(year, -290307, 294246)
}
return ti.UnixNano(), nil
}
func inferTimestamp(t Type, val any) (mapping.Timestamp, error) {
ticks, err := getTSTicks(t, val)
return mapping.NewTimestamp(ticks), err
}
func inferTimestampS(val any) (mapping.TimestampS, error) {
ticks, err := getTSTicks(TYPE_TIMESTAMP_S, val)
return mapping.NewTimestampS(ticks), err
}
func inferTimestampMS(val any) (mapping.TimestampMS, error) {
ticks, err := getTSTicks(TYPE_TIMESTAMP_MS, val)
return mapping.NewTimestampMS(ticks), err
}
func inferTimestampNS(val any) (mapping.TimestampNS, error) {
ticks, err := getTSTicks(TYPE_TIMESTAMP_NS, val)
return mapping.NewTimestampNS(ticks), err
}
func inferDate[T any](val T) (mapping.Date, error) {
ti, err := castToTime(val)
if err != nil {
return mapping.Date{}, err
}
date := mapping.NewDate(int32(ti.Unix() / secondsPerDay))
return date, err
}
func inferTime(val any) (mapping.Time, error) {
ticks, err := getTimeTicks(val)
if err != nil {
return mapping.Time{}, err
}
return mapping.NewTime(ticks), nil
}
func inferTimeTZ(val any) (mapping.TimeTZ, error) {
ti, err := castToTime(val)
if err != nil {
return mapping.TimeTZ{}, err
}
// DuckDB stores time as microseconds since 00:00:00.
base := time.Date(1970, time.January, 1, ti.Hour(), ti.Minute(), ti.Second(), ti.Nanosecond(), time.UTC)
ticks := base.UnixMicro()
// Preserve the UTC offset from the input time.
_, offset := ti.Zone()
return mapping.CreateTimeTZ(ticks, int32(offset)), nil
}
func getTimeTicks[T any](val T) (int64, error) {
ti, err := castToTime(val)
if err != nil {
return 0, err
}
// DuckDB stores time as microseconds since 00:00:00.
base := time.Date(1970, time.January, 1, ti.Hour(), ti.Minute(), ti.Second(), ti.Nanosecond(), time.UTC)
return base.UnixMicro(), err
}