forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
81 lines (64 loc) · 1.6 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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"encoding/binary"
"fmt"
"math/big"
"github.com/mitchellh/mapstructure"
)
// 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 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)
}