forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector_getters.go
225 lines (193 loc) · 5.81 KB
/
vector_getters.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
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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"encoding/json"
"math/big"
"time"
"unsafe"
)
// fnGetVectorValue is the getter callback function for any (nested) vector.
type fnGetVectorValue func(vec *vector, rowIdx C.idx_t) any
func (vec *vector) getNull(rowIdx C.idx_t) bool {
mask := unsafe.Pointer(vec.mask)
if mask == nil {
return false
}
entryIdx := rowIdx / 64
idxInEntry := rowIdx % 64
maskPtr := (*[1 << 31]C.uint64_t)(mask)
isValid := maskPtr[entryIdx] & (C.uint64_t(1) << idxInEntry)
return uint64(isValid) == 0
}
func getPrimitive[T any](vec *vector, rowIdx C.idx_t) T {
xs := (*[1 << 31]T)(vec.ptr)
return xs[rowIdx]
}
func (vec *vector) getTS(t Type, rowIdx C.idx_t) time.Time {
val := getPrimitive[C.duckdb_timestamp](vec, rowIdx)
return getTS(t, val)
}
func getTS(t Type, ts C.duckdb_timestamp) time.Time {
switch t {
case TYPE_TIMESTAMP:
return time.UnixMicro(int64(ts.micros)).UTC()
case TYPE_TIMESTAMP_S:
return time.Unix(int64(ts.micros), 0).UTC()
case TYPE_TIMESTAMP_MS:
return time.UnixMilli(int64(ts.micros)).UTC()
case TYPE_TIMESTAMP_NS:
return time.Unix(0, int64(ts.micros)).UTC()
case TYPE_TIMESTAMP_TZ:
return time.UnixMicro(int64(ts.micros)).UTC()
}
return time.Time{}
}
func (vec *vector) getDate(rowIdx C.idx_t) time.Time {
date := getPrimitive[C.duckdb_date](vec, rowIdx)
return getDate(date)
}
func getDate(date C.duckdb_date) time.Time {
d := C.duckdb_from_date(date)
return time.Date(int(d.year), time.Month(d.month), int(d.day), 0, 0, 0, 0, time.UTC)
}
func (vec *vector) getTime(rowIdx C.idx_t) time.Time {
switch vec.Type {
case TYPE_TIME:
val := getPrimitive[C.duckdb_time](vec, rowIdx)
return getTime(val)
case TYPE_TIME_TZ:
ti := getPrimitive[C.duckdb_time_tz](vec, rowIdx)
return getTimeTZ(ti)
}
return time.Time{}
}
func getTime(ti C.duckdb_time) time.Time {
unix := time.UnixMicro(int64(ti.micros)).UTC()
return time.Date(1, time.January, 1, unix.Hour(), unix.Minute(), unix.Second(), unix.Nanosecond(), time.UTC)
}
func getTimeTZ(ti C.duckdb_time_tz) time.Time {
timeTZ := C.duckdb_from_time_tz(ti)
hour := int(timeTZ.time.hour)
minute := int(timeTZ.time.min)
sec := int(timeTZ.time.sec)
// TIMETZ has microsecond precision.
nanos := int(timeTZ.time.micros) * 1000
loc := time.FixedZone("", int(timeTZ.offset))
return time.Date(1, time.January, 1, hour, minute, sec, nanos, loc).UTC()
}
func (vec *vector) getInterval(rowIdx C.idx_t) Interval {
interval := getPrimitive[C.duckdb_interval](vec, rowIdx)
return getInterval(interval)
}
func getInterval(interval C.duckdb_interval) Interval {
return Interval{
Days: int32(interval.days),
Months: int32(interval.months),
Micros: int64(interval.micros),
}
}
func (vec *vector) getHugeint(rowIdx C.idx_t) *big.Int {
hugeInt := getPrimitive[C.duckdb_hugeint](vec, rowIdx)
return hugeIntToNative(hugeInt)
}
func (vec *vector) getBytes(rowIdx C.idx_t) any {
cStr := getPrimitive[duckdb_string_t](vec, rowIdx)
var blob []byte
if cStr.length <= stringInlineLength {
// Inlined data is stored from byte 4 to stringInlineLength + 4.
blob = C.GoBytes(unsafe.Pointer(&cStr.prefix), C.int(cStr.length))
} else {
// Any strings exceeding stringInlineLength are stored as a pointer in `ptr`.
blob = C.GoBytes(unsafe.Pointer(cStr.ptr), C.int(cStr.length))
}
if vec.Type == TYPE_VARCHAR {
return string(blob)
}
return blob
}
func (vec *vector) getJSON(rowIdx C.idx_t) any {
bytes := vec.getBytes(rowIdx).(string)
var value any
_ = json.Unmarshal([]byte(bytes), &value)
return value
}
func (vec *vector) getDecimal(rowIdx C.idx_t) Decimal {
var val *big.Int
switch vec.internalType {
case TYPE_SMALLINT:
v := getPrimitive[int16](vec, rowIdx)
val = big.NewInt(int64(v))
case TYPE_INTEGER:
v := getPrimitive[int32](vec, rowIdx)
val = big.NewInt(int64(v))
case TYPE_BIGINT:
v := getPrimitive[int64](vec, rowIdx)
val = big.NewInt(v)
case TYPE_HUGEINT:
v := getPrimitive[C.duckdb_hugeint](vec, rowIdx)
val = hugeIntToNative(C.duckdb_hugeint{
lower: v.lower,
upper: v.upper,
})
}
return Decimal{Width: vec.decimalWidth, Scale: vec.decimalScale, Value: val}
}
func (vec *vector) getEnum(rowIdx C.idx_t) string {
var idx uint64
switch vec.internalType {
case TYPE_UTINYINT:
idx = uint64(getPrimitive[uint8](vec, rowIdx))
case TYPE_USMALLINT:
idx = uint64(getPrimitive[uint16](vec, rowIdx))
case TYPE_UINTEGER:
idx = uint64(getPrimitive[uint32](vec, rowIdx))
case TYPE_UBIGINT:
idx = getPrimitive[uint64](vec, rowIdx)
}
logicalType := C.duckdb_vector_get_column_type(vec.duckdbVector)
defer C.duckdb_destroy_logical_type(&logicalType)
val := C.duckdb_enum_dictionary_value(logicalType, (C.idx_t)(idx))
defer C.duckdb_free(unsafe.Pointer(val))
return C.GoString(val)
}
func (vec *vector) getList(rowIdx C.idx_t) []any {
entry := getPrimitive[duckdb_list_entry_t](vec, rowIdx)
return vec.getSliceChild(entry.offset, entry.length)
}
func (vec *vector) getStruct(rowIdx C.idx_t) map[string]any {
m := map[string]any{}
for i := 0; i < len(vec.childVectors); i++ {
child := &vec.childVectors[i]
val := child.getFn(child, rowIdx)
m[vec.structEntries[i].Name()] = val
}
return m
}
func (vec *vector) getMap(rowIdx C.idx_t) Map {
list := vec.getList(rowIdx)
m := Map{}
for i := 0; i < len(list); i++ {
mapItem := list[i].(map[string]any)
key := mapItem[mapKeysField()]
val := mapItem[mapValuesField()]
m[key] = val
}
return m
}
func (vec *vector) getArray(rowIdx C.idx_t) []any {
length := C.idx_t(vec.arrayLength)
return vec.getSliceChild(rowIdx*length, length)
}
func (vec *vector) getSliceChild(offset C.idx_t, length C.idx_t) []any {
slice := make([]any, 0, length)
child := &vec.childVectors[0]
// Fill the slice with all child values.
for i := C.idx_t(0); i < length; i++ {
val := child.getFn(child, i+offset)
slice = append(slice, val)
}
return slice
}