forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
137 lines (112 loc) · 3 KB
/
types.go
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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"encoding/binary"
"fmt"
"math/big"
"github.com/mitchellh/mapstructure"
)
var unsupportedAppenderTypeMap = map[C.duckdb_type]string{
C.DUCKDB_TYPE_INVALID: "INVALID",
C.DUCKDB_TYPE_TIME: "TIME",
C.DUCKDB_TYPE_INTERVAL: "INTERVAL",
C.DUCKDB_TYPE_HUGEINT: "HUGEINT",
C.DUCKDB_TYPE_UHUGEINT: "UHUGEINT",
C.DUCKDB_TYPE_DECIMAL: "DECIMAL",
C.DUCKDB_TYPE_ENUM: "ENUM",
C.DUCKDB_TYPE_MAP: "MAP",
C.DUCKDB_TYPE_UNION: "UNION",
C.DUCKDB_TYPE_BIT: "BIT",
C.DUCKDB_TYPE_TIME_TZ: "TIME_TZ",
}
type numericType interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
func convertNumericType[srcT numericType, destT numericType](val srcT) destT {
return destT(val)
}
type UUID [16]byte
func (u *UUID) Scan(v any) error {
if n := copy(u[:], v.([]byte)); n != 16 {
return fmt.Errorf("invalid UUID length: %d", n)
}
return nil
}
// duckdb_hugeint is composed of (lower, upper) components.
// The value is computed as: upper * 2^64 + lower
func hugeIntToUUID(hi C.duckdb_hugeint) []byte {
var uuid [16]byte
// We need to flip the sign bit of the signed hugeint to transform it to UUID bytes
binary.BigEndian.PutUint64(uuid[:8], uint64(hi.upper)^1<<63)
binary.BigEndian.PutUint64(uuid[8:], uint64(hi.lower))
return uuid[:]
}
func uuidToHugeInt(uuid UUID) C.duckdb_hugeint {
var dt C.duckdb_hugeint
upper := binary.BigEndian.Uint64(uuid[:8])
// flip the sign bit
upper = upper ^ (1 << 63)
dt.upper = C.int64_t(upper)
dt.lower = C.uint64_t(binary.BigEndian.Uint64(uuid[8:]))
return dt
}
func hugeIntToNative(hi C.duckdb_hugeint) *big.Int {
i := big.NewInt(int64(hi.upper))
i.Lsh(i, 64)
i.Add(i, new(big.Int).SetUint64(uint64(hi.lower)))
return i
}
func hugeIntFromNative(i *big.Int) (C.duckdb_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 C.duckdb_hugeint{}, fmt.Errorf("big.Int(%s) is too big for HUGEINT", i.String())
}
return C.duckdb_hugeint{
lower: C.uint64_t(r.Uint64()),
upper: C.int64_t(q.Int64()),
}, nil
}
type Map map[any]any
func (m *Map) Scan(v any) error {
data, ok := v.(Map)
if !ok {
return fmt.Errorf("invalid type `%T` for scanning `Map`, expected `Map`", data)
}
*m = data
return nil
}
type Interval struct {
Days int32 `json:"days"`
Months int32 `json:"months"`
Micros int64 `json:"micros"`
}
// Use 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)
}
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
}