This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
column_buffer_go18.go
481 lines (407 loc) · 12.3 KB
/
column_buffer_go18.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
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
//go:build go1.18
package parquet
import (
"encoding/json"
"math/bits"
"reflect"
"time"
"unsafe"
"github.com/segmentio/parquet-go/deprecated"
"github.com/segmentio/parquet-go/internal/unsafecast"
"github.com/segmentio/parquet-go/sparse"
)
// writeRowsFunc is the type of functions that apply rows to a set of column
// buffers.
//
// - columns is the array of column buffer where the rows are written.
//
// - rows is the array of Go values to write to the column buffers.
//
// - levels is used to track the column index, repetition and definition levels
// of values when writing optional or repeated columns.
type writeRowsFunc func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error
// writeRowsFuncOf generates a writeRowsFunc function for the given Go type and
// parquet schema. The column path indicates the column that the function is
// being generated for in the parquet schema.
func writeRowsFuncOf(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
if leaf, exists := schema.Lookup(path...); exists && leaf.Node.Type().LogicalType() != nil && leaf.Node.Type().LogicalType().Json != nil {
return writeRowsFuncOfJSON(t, schema, path)
}
switch t {
case reflect.TypeOf(deprecated.Int96{}):
return writeRowsFuncOfRequired(t, schema, path)
case reflect.TypeOf(time.Time{}):
return writeRowsFuncOfTime(t, schema, path)
}
switch t.Kind() {
case reflect.Bool,
reflect.Int,
reflect.Uint,
reflect.Int32,
reflect.Uint32,
reflect.Int64,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.String:
return writeRowsFuncOfRequired(t, schema, path)
case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
return writeRowsFuncOfRequired(t, schema, path)
} else {
return writeRowsFuncOfSlice(t, schema, path)
}
case reflect.Array:
if t.Elem().Kind() == reflect.Uint8 {
return writeRowsFuncOfRequired(t, schema, path)
}
case reflect.Pointer:
return writeRowsFuncOfPointer(t, schema, path)
case reflect.Struct:
return writeRowsFuncOfStruct(t, schema, path)
case reflect.Map:
return writeRowsFuncOfMap(t, schema, path)
}
panic("cannot convert Go values of type " + typeNameOf(t) + " to parquet value")
}
func writeRowsFuncOfRequired(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
column := schema.mapping.lookup(path)
columnIndex := column.columnIndex
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
columns[columnIndex].writeValues(rows, levels)
return nil
}
}
func writeRowsFuncOfOptional(t reflect.Type, schema *Schema, path columnPath, writeRows writeRowsFunc) writeRowsFunc {
nullIndex := nullIndexFuncOf(t)
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeRows(columns, rows, levels)
}
nulls := acquireBitmap(rows.Len())
defer releaseBitmap(nulls)
nullIndex(nulls.bits, rows)
nullLevels := levels
levels.definitionLevel++
// In this function, we are dealing with optional values which are
// neither pointers nor slices; for example, a int32 field marked
// "optional" in its parent struct.
//
// We need to find zero values, which should be represented as nulls
// in the parquet column. In order to minimize the calls to writeRows
// and maximize throughput, we use the nullIndex and nonNullIndex
// functions, which are type-specific implementations of the algorithm.
//
// Sections of the input that are contiguous nulls or non-nulls can be
// sent to a single call to writeRows to be written to the underlying
// buffer since they share the same definition level.
//
// This optimization is defeated by inputs alternating null and non-null
// sequences of single values, we do not expect this condition to be a
// common case.
for i := 0; i < rows.Len(); {
j := 0
x := i / 64
y := i % 64
if y != 0 {
if b := nulls.bits[x] >> uint(y); b == 0 {
x++
y = 0
} else {
y += bits.TrailingZeros64(b)
goto writeNulls
}
}
for x < len(nulls.bits) && nulls.bits[x] == 0 {
x++
}
if x < len(nulls.bits) {
y = bits.TrailingZeros64(nulls.bits[x]) % 64
}
writeNulls:
if j = x*64 + y; j > rows.Len() {
j = rows.Len()
}
if i < j {
if err := writeRows(columns, rows.Slice(i, j), nullLevels); err != nil {
return err
}
i = j
}
if y != 0 {
if b := nulls.bits[x] >> uint(y); b == (1<<uint64(y))-1 {
x++
y = 0
} else {
y += bits.TrailingZeros64(^b)
goto writeNonNulls
}
}
for x < len(nulls.bits) && nulls.bits[x] == ^uint64(0) {
x++
}
if x < len(nulls.bits) {
y = bits.TrailingZeros64(^nulls.bits[x]) % 64
}
writeNonNulls:
if j = x*64 + y; j > rows.Len() {
j = rows.Len()
}
if i < j {
if err := writeRows(columns, rows.Slice(i, j), levels); err != nil {
return err
}
i = j
}
}
return nil
}
}
func writeRowsFuncOfPointer(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
elemType := t.Elem()
elemSize := uintptr(elemType.Size())
writeRows := writeRowsFuncOf(elemType, schema, path)
if len(path) == 0 {
// This code path is taken when generating a writeRowsFunc for a pointer
// type. In this case, we do not need to increase the definition level
// since we are not deailng with an optional field but a pointer to the
// row type.
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeRows(columns, rows, levels)
}
for i := 0; i < rows.Len(); i++ {
p := *(*unsafe.Pointer)(rows.Index(i))
a := sparse.Array{}
if p != nil {
a = makeArray(p, 1, elemSize)
}
if err := writeRows(columns, a, levels); err != nil {
return err
}
}
return nil
}
}
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeRows(columns, rows, levels)
}
for i := 0; i < rows.Len(); i++ {
p := *(*unsafe.Pointer)(rows.Index(i))
a := sparse.Array{}
elemLevels := levels
if p != nil {
a = makeArray(p, 1, elemSize)
elemLevels.definitionLevel++
}
if err := writeRows(columns, a, elemLevels); err != nil {
return err
}
}
return nil
}
}
func writeRowsFuncOfSlice(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
elemType := t.Elem()
elemSize := uintptr(elemType.Size())
writeRows := writeRowsFuncOf(elemType, schema, path)
// When the element is a pointer type, the writeRows function will be an
// instance returned by writeRowsFuncOfPointer, which handles incrementing
// the definition level if the pointer value is not nil.
definitionLevelIncrement := byte(0)
if elemType.Kind() != reflect.Ptr {
definitionLevelIncrement = 1
}
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeRows(columns, rows, levels)
}
levels.repetitionDepth++
for i := 0; i < rows.Len(); i++ {
p := (*sliceHeader)(rows.Index(i))
a := makeArray(p.base, p.len, elemSize)
b := sparse.Array{}
elemLevels := levels
if a.Len() > 0 {
b = a.Slice(0, 1)
elemLevels.definitionLevel += definitionLevelIncrement
}
if err := writeRows(columns, b, elemLevels); err != nil {
return err
}
if a.Len() > 1 {
elemLevels.repetitionLevel = elemLevels.repetitionDepth
if err := writeRows(columns, a.Slice(1, a.Len()), elemLevels); err != nil {
return err
}
}
}
return nil
}
}
func writeRowsFuncOfStruct(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
type column struct {
offset uintptr
writeRows writeRowsFunc
}
fields := structFieldsOf(t)
columns := make([]column, len(fields))
for i, f := range fields {
optional := false
columnPath := path.append(f.Name)
forEachStructTagOption(f, func(_ reflect.Type, option, _ string) {
switch option {
case "list":
columnPath = columnPath.append("list", "element")
case "optional":
optional = true
}
})
writeRows := writeRowsFuncOf(f.Type, schema, columnPath)
if optional {
switch f.Type.Kind() {
case reflect.Pointer, reflect.Slice:
default:
writeRows = writeRowsFuncOfOptional(f.Type, schema, columnPath, writeRows)
}
}
columns[i] = column{
offset: f.Offset,
writeRows: writeRows,
}
}
return func(buffers []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
for _, column := range columns {
if err := column.writeRows(buffers, rows, levels); err != nil {
return err
}
}
} else {
for _, column := range columns {
if err := column.writeRows(buffers, rows.Offset(column.offset), levels); err != nil {
return err
}
}
}
return nil
}
}
func writeRowsFuncOfMap(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
keyPath := path.append("key_value", "key")
keyType := t.Key()
keySize := uintptr(keyType.Size())
writeKeys := writeRowsFuncOf(keyType, schema, keyPath)
valuePath := path.append("key_value", "value")
valueType := t.Elem()
valueSize := uintptr(valueType.Size())
writeValues := writeRowsFuncOf(valueType, schema, valuePath)
writeKeyValues := func(columns []ColumnBuffer, keys, values sparse.Array, levels columnLevels) error {
if err := writeKeys(columns, keys, levels); err != nil {
return err
}
if err := writeValues(columns, values, levels); err != nil {
return err
}
return nil
}
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeKeyValues(columns, rows, rows, levels)
}
levels.repetitionDepth++
mapKey := reflect.New(keyType).Elem()
mapValue := reflect.New(valueType).Elem()
for i := 0; i < rows.Len(); i++ {
m := reflect.NewAt(t, rows.Index(i)).Elem()
if m.Len() == 0 {
empty := sparse.Array{}
if err := writeKeyValues(columns, empty, empty, levels); err != nil {
return err
}
} else {
elemLevels := levels
elemLevels.definitionLevel++
for it := m.MapRange(); it.Next(); {
mapKey.SetIterKey(it)
mapValue.SetIterValue(it)
k := makeArray(unsafecast.PointerOfValue(mapKey), 1, keySize)
v := makeArray(unsafecast.PointerOfValue(mapValue), 1, valueSize)
if err := writeKeyValues(columns, k, v, elemLevels); err != nil {
return err
}
elemLevels.repetitionLevel = elemLevels.repetitionDepth
}
}
}
return nil
}
}
func writeRowsFuncOfJSON(t reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
// If this is a string or a byte array write directly.
switch t.Kind() {
case reflect.String:
return writeRowsFuncOfRequired(t, schema, path)
case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
return writeRowsFuncOfRequired(t, schema, path)
}
}
// Otherwise handle with a json.Marshal
asStrT := reflect.TypeOf(string(""))
writer := writeRowsFuncOfRequired(asStrT, schema, path)
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writer(columns, rows, levels)
}
for i := 0; i < rows.Len(); i++ {
val := reflect.NewAt(t, rows.Index(i))
asI := val.Interface()
b, err := json.Marshal(asI)
if err != nil {
return err
}
asStr := string(b)
a := sparse.MakeStringArray([]string{asStr})
if err := writer(columns, a.UnsafeArray(), levels); err != nil {
return err
}
}
return nil
}
}
func writeRowsFuncOfTime(_ reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
t := reflect.TypeOf(int64(0))
elemSize := uintptr(t.Size())
writeRows := writeRowsFuncOf(t, schema, path)
col, _ := schema.Lookup(path...)
unit := Nanosecond.TimeUnit()
lt := col.Node.Type().LogicalType()
if lt != nil && lt.Timestamp != nil {
unit = lt.Timestamp.Unit
}
return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeRows(columns, rows, levels)
}
times := rows.TimeArray()
for i := 0; i < times.Len(); i++ {
t := times.Index(i)
var val int64
switch {
case unit.Millis != nil:
val = t.UnixMilli()
case unit.Micros != nil:
val = t.UnixMicro()
default:
val = t.UnixNano()
}
a := makeArray(unsafecast.PointerOfValue(reflect.ValueOf(val)), 1, elemSize)
if err := writeRows(columns, a, levels); err != nil {
return err
}
}
return nil
}
}