/
arrow.go
636 lines (558 loc) 路 15.3 KB
/
arrow.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
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
package pqarrow
import (
"context"
"errors"
"fmt"
"io"
"github.com/apache/arrow/go/v8/arrow"
"github.com/apache/arrow/go/v8/arrow/array"
"github.com/apache/arrow/go/v8/arrow/memory"
"github.com/segmentio/parquet-go"
"github.com/polarsignals/arcticdb/dynparquet"
"github.com/polarsignals/arcticdb/query/logicalplan"
)
// ParquetRowGroupToArrowRecord converts a parquet row group to an arrow record.
func ParquetRowGroupToArrowRecord(
ctx context.Context,
pool memory.Allocator,
rg parquet.RowGroup,
projections []logicalplan.ColumnMatcher,
filterExpr logicalplan.Expr,
distinctColumns []logicalplan.ColumnMatcher,
) (arrow.Record, error) {
switch rg.(type) {
case *dynparquet.MergedRowGroup:
return rowBasedParquetRowGroupToArrowRecord(ctx, pool, rg)
default:
return contiguousParquetRowGroupToArrowRecord(
ctx,
pool,
rg,
projections,
filterExpr,
distinctColumns,
)
}
}
// rowBasedParquetRowGroupToArrowRecord converts a parquet row group to an arrow record row by row.
func rowBasedParquetRowGroupToArrowRecord(
ctx context.Context,
pool memory.Allocator,
rg parquet.RowGroup,
) (arrow.Record, error) {
s := rg.Schema()
parquetFields := s.Fields()
fields := make([]arrow.Field, 0, len(parquetFields))
newWriterFuncs := make([]func(array.Builder, int) valueWriter, 0, len(parquetFields))
for _, parquetField := range parquetFields {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
name := parquetField.Name()
node := dynparquet.FieldByName(s, name)
typ, newValueWriter, err := parquetNodeToType(node)
if err != nil {
return nil, err
}
nullable := false
if node.Optional() {
nullable = true
}
if node.Repeated() {
typ = arrow.ListOf(typ)
newValueWriter = newListValueWriter(newValueWriter)
}
newWriterFuncs = append(newWriterFuncs, newValueWriter)
fields = append(fields, arrow.Field{
Name: name,
Type: typ,
Nullable: nullable,
})
}
}
writers := make([]valueWriter, len(parquetFields))
b := array.NewRecordBuilder(pool, arrow.NewSchema(fields, nil))
for i, column := range b.Fields() {
writers[i] = newWriterFuncs[i](column, 0)
}
rows := rg.Rows()
defer rows.Close()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
rowBuf := make([]parquet.Row, 64) // Random guess.
n, err := rows.ReadRows(rowBuf)
if err == io.EOF && n == 0 {
break
}
if err != nil && err != io.EOF {
return nil, fmt.Errorf("read row: %w", err)
}
rowBuf = rowBuf[:n]
for i, writer := range writers {
for _, row := range rowBuf {
values := dynparquet.ValuesForIndex(row, i)
writer.Write(values)
}
}
if err == io.EOF {
break
}
}
return b.NewRecord(), nil
}
// contiguousParquetRowGroupToArrowRecord converts a parquet row group to an arrow record.
func contiguousParquetRowGroupToArrowRecord(
ctx context.Context,
pool memory.Allocator,
rg parquet.RowGroup,
projections []logicalplan.ColumnMatcher,
filterExpr logicalplan.Expr,
distinctColumns []logicalplan.ColumnMatcher,
) (arrow.Record, error) {
s := rg.Schema()
parquetColumns := rg.ColumnChunks()
parquetFields := s.Fields()
if len(distinctColumns) == 1 && filterExpr == nil {
// We can use the faster path for a single distinct column by just
// returning its dictionary.
fields := make([]arrow.Field, 0, 1)
cols := make([]arrow.Array, 0, 1)
rows := int64(0)
for i, field := range parquetFields {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
name := field.Name()
if distinctColumns[0].Match(name) {
typ, nullable, array, err := parquetColumnToArrowArray(
pool,
field,
parquetColumns[i],
true,
)
if err != nil {
return nil, fmt.Errorf("convert parquet column to arrow array: %w", err)
}
fields = append(fields, arrow.Field{
Name: name,
Type: typ,
Nullable: nullable,
})
cols = append(cols, array)
rows = int64(array.Len())
}
}
}
schema := arrow.NewSchema(fields, nil)
return array.NewRecord(schema, cols, rows), nil
}
fields := make([]arrow.Field, 0, len(parquetFields))
cols := make([]arrow.Array, 0, len(parquetFields))
for i, parquetField := range parquetFields {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
if includedProjection(projections, parquetField.Name()) {
typ, nullable, array, err := parquetColumnToArrowArray(
pool,
parquetField,
parquetColumns[i],
false,
)
if err != nil {
return nil, fmt.Errorf("convert parquet column to arrow array: %w", err)
}
fields = append(fields, arrow.Field{
Name: parquetField.Name(),
Type: typ,
Nullable: nullable,
})
cols = append(cols, array)
}
}
}
schema := arrow.NewSchema(fields, nil)
return array.NewRecord(schema, cols, rg.NumRows()), nil
}
func includedProjection(projections []logicalplan.ColumnMatcher, name string) bool {
if len(projections) == 0 {
return true
}
for _, p := range projections {
if p.Match(name) {
return true
}
}
return false
}
// parquetColumnToArrowArray converts a single parquet column to an arrow array
// and returns the type, nullability, and the actual resulting arrow array. If
// a column is a repeated type, it automatically boxes it into the appropriate
// arrow equivalent.
func parquetColumnToArrowArray(
pool memory.Allocator,
n parquet.Node,
c parquet.ColumnChunk,
dictionaryOnly bool,
) (
arrow.DataType,
bool,
arrow.Array,
error,
) {
at, newValueWriter, err := parquetNodeToType(n)
if err != nil {
return nil, false, nil, err
}
var (
w valueWriter
b array.Builder
lb *array.ListBuilder
repeated = false
nullable = false
)
optional := n.Optional()
if optional {
nullable = true
}
// Using the retrieved arrow type and whether the type is repeated we can
// build a type-safe `writeValues` function that only casts the resulting
// builder once and can perform optimized transfers of the page values to
// the target array.
if n.Repeated() {
// If the column is repeated, we need to box it into a list.
lt := arrow.ListOf(at)
lt.SetElemNullable(optional)
at = lt
nullable = true
repeated = true
lb = array.NewBuilder(pool, at).(*array.ListBuilder)
// A list builder actually expects all values of all sublists to be
// written contiguously, the offsets where litsts start are recorded in
// the offsets array below.
w = newValueWriter(lb.ValueBuilder(), int(c.NumValues()))
b = lb
} else {
b = array.NewBuilder(pool, at)
w = newValueWriter(b, int(c.NumValues()))
}
defer b.Release()
err = writePagesToArray(
c.Pages(),
optional,
repeated,
lb,
w,
dictionaryOnly,
)
if err != nil {
return nil, false, nil, err
}
arr := b.NewArray()
// Is this a bug in arrow? We already set the nullability above, but it
// doesn't appear to transfer into the resulting array's type. Needs to be
// investigated.
switch t := arr.DataType().(type) {
case *arrow.ListType:
t.SetElemNullable(optional)
}
return at, nullable, arr, nil
}
// writePagesToArray reads all pages of a page iterator and writes the values
// to an array builder. If the type is a repeated type it will also write the
// starting offsets of lists to the list builder.
func writePagesToArray(
pages parquet.Pages,
optional bool,
repeated bool,
lb *array.ListBuilder,
w valueWriter,
dictionaryOnly bool,
) error {
defer pages.Close()
// We are potentially writing multiple pages to the same array, so we need
// to keep track of the index of the offsets in case this is a List-type.
i := 0
for {
p, err := pages.ReadPage()
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("read page: %w", err)
}
dict := p.Dictionary()
switch {
case !repeated && dictionaryOnly && dict != nil:
// If we are only writing the dictionary, we don't need to read
// the values.
err = w.WritePage(dict.Page())
if err != nil {
return fmt.Errorf("write dictionary page: %w", err)
}
case !repeated && !optional && dict == nil:
// If the column is not optional, we can read all values at once
// consecutively without worrying about null values.
err = w.WritePage(p)
if err != nil {
return fmt.Errorf("write page: %w", err)
}
default:
values := make([]parquet.Value, p.NumValues())
reader := p.Values()
_, err = reader.ReadValues(values)
// We're reading all values in the page so we always expect an io.EOF.
if err != nil && err != io.EOF {
return fmt.Errorf("read values: %w", err)
}
w.Write(values)
if repeated {
offsets := []int32{}
validity := []bool{}
for _, v := range values {
rep := v.RepetitionLevel()
def := v.DefinitionLevel()
if rep == 0 && def == 1 {
offsets = append(offsets, int32(i))
validity = append(validity, true)
}
if rep == 0 && def == 0 {
offsets = append(offsets, int32(i))
validity = append(validity, false)
}
// rep == 1 && def == 1 means the item in the list is not null which is handled by the value writer
// rep == 1 && def == 0 means the item in the list is null which is handled by the value writer
i++
}
lb.AppendValues(offsets, validity)
}
}
}
return nil
}
// ParquetNodeToType converts a parquet node to an arrow type.
func ParquetNodeToType(n parquet.Node) (arrow.DataType, error) {
typ, _, err := parquetNodeToType(n)
if err != nil {
return nil, err
}
return typ, nil
}
// parquetNodeToType converts a parquet node to an arrow type and a function to
// create a value writer.
func parquetNodeToType(n parquet.Node) (arrow.DataType, func(b array.Builder, numValues int) valueWriter, error) {
t := n.Type()
lt := t.LogicalType()
if lt == nil {
return nil, nil, errors.New("unsupported type")
}
switch {
case lt.UTF8 != nil:
return &arrow.BinaryType{}, newBinaryValueWriter, nil
case lt.Integer != nil:
switch lt.Integer.BitWidth {
case 64:
if lt.Integer.IsSigned {
return &arrow.Int64Type{}, newInt64ValueWriter, nil
}
return &arrow.Uint64Type{}, newUint64ValueWriter, nil
default:
return nil, nil, errors.New("unsupported int bit width")
}
default:
return nil, nil, errors.New("unsupported type")
}
}
var ErrPageTypeMismatch = errors.New("page type mismatch")
type valueWriter interface {
WritePage(p parquet.Page) error
Write([]parquet.Value)
}
type binaryValueWriter struct {
b *array.BinaryBuilder
numValues int
firstWrite bool
}
func newBinaryValueWriter(b array.Builder, numValues int) valueWriter {
return &binaryValueWriter{
b: b.(*array.BinaryBuilder),
numValues: numValues,
firstWrite: true,
}
}
func (w *binaryValueWriter) Write(values []parquet.Value) {
if w.firstWrite {
w.firstWrite = false
// Depending on the nullability of the column this could be optimized
// further by reading strings directly and adding all of them at once
// to the array builder.
vs := make([][]byte, len(values))
validity := make([]bool, len(values))
largest := 0
for i, v := range values {
if !v.IsNull() {
vs[i] = v.ByteArray()
if len(vs[i]) > largest {
largest = len(vs[i])
}
validity[i] = true
}
}
w.b.ReserveData(w.numValues * largest)
w.b.AppendValues(vs, validity)
} else {
// Depending on the nullability of the column this could be optimized
// further by reading strings directly and adding all of them at once
// to the array builder.
vs := make([][]byte, len(values))
validity := make([]bool, len(values))
for i, v := range values {
if !v.IsNull() {
vs[i] = v.ByteArray()
validity[i] = true
}
}
w.b.AppendValues(vs, validity)
}
}
// TODO: implement fast path of writing the whole page directly.
func (w *binaryValueWriter) WritePage(p parquet.Page) error {
reader := p.Values()
values := make([]parquet.Value, p.NumValues())
_, err := reader.ReadValues(values)
// We're reading all values in the page so we always expect an io.EOF.
if err != nil && err != io.EOF {
return fmt.Errorf("read values: %w", err)
}
w.Write(values)
return nil
}
type int64ValueWriter struct {
b *array.Int64Builder
buf []int64
}
func newInt64ValueWriter(b array.Builder, numValues int) valueWriter {
res := &int64ValueWriter{
b: b.(*array.Int64Builder),
}
res.b.Reserve(numValues)
return res
}
func (w *int64ValueWriter) Write(values []parquet.Value) {
// Depending on the nullability of the column this could be optimized
// further by reading int64s directly and adding all of them at once to
// the array builder.
for _, v := range values {
if v.IsNull() {
w.b.AppendNull()
} else {
w.b.Append(v.Int64())
}
}
}
func (w *int64ValueWriter) WritePage(p parquet.Page) error {
reader := p.Values()
ireader, ok := reader.(parquet.Int64Reader)
if ok {
// fast path
if w.buf == nil {
w.buf = make([]int64, p.NumValues())
}
values := w.buf
for {
n, err := ireader.ReadInt64s(values)
if err != nil && err != io.EOF {
return fmt.Errorf("read values: %w", err)
}
w.b.AppendValues(values[:n], nil)
if err == io.EOF {
break
}
}
return nil
}
values := make([]parquet.Value, p.NumValues())
_, err := reader.ReadValues(values)
// We're reading all values in the page so we always expect an io.EOF.
if err != nil && err != io.EOF {
return fmt.Errorf("read values: %w", err)
}
w.Write(values)
return nil
}
type uint64ValueWriter struct {
b *array.Uint64Builder
}
func newUint64ValueWriter(b array.Builder, numValues int) valueWriter {
res := &uint64ValueWriter{
b: b.(*array.Uint64Builder),
}
res.b.Reserve(numValues)
return res
}
func (w *uint64ValueWriter) Write(values []parquet.Value) {
// Depending on the nullability of the column this could be optimized
// further by reading uint64s directly and adding all of them at once
// to the array builder.
for _, v := range values {
if v.IsNull() {
w.b.AppendNull()
} else {
w.b.Append(uint64(v.Int64()))
}
}
}
// TODO: implement fast path of writing the whole page directly.
func (w *uint64ValueWriter) WritePage(p parquet.Page) error {
reader := p.Values()
values := make([]parquet.Value, p.NumValues())
_, err := reader.ReadValues(values)
// We're reading all values in the page so we always expect an io.EOF.
if err != nil && err != io.EOF {
return fmt.Errorf("read values: %w", err)
}
w.Write(values)
return nil
}
type repeatedValueWriter struct {
b *array.ListBuilder
values valueWriter
}
func newListValueWriter(newValueWriter func(b array.Builder, numValues int) valueWriter) func(b array.Builder, numValues int) valueWriter {
return func(b array.Builder, numValues int) valueWriter {
builder := b.(*array.ListBuilder)
return &repeatedValueWriter{
b: builder,
values: newValueWriter(builder.ValueBuilder(), numValues),
}
}
}
func (w *repeatedValueWriter) Write(values []parquet.Value) {
v0 := values[0]
rep := v0.RepetitionLevel()
def := v0.DefinitionLevel()
if rep == 0 && def == 0 {
w.b.AppendNull()
}
w.b.Append(true)
w.values.Write(values)
}
// TODO: implement fast path of writing the whole page directly.
func (w *repeatedValueWriter) WritePage(p parquet.Page) error {
reader := p.Values()
values := make([]parquet.Value, p.NumValues())
_, err := reader.ReadValues(values)
// We're reading all values in the page so we always expect an io.EOF.
if err != nil && err != io.EOF {
return fmt.Errorf("read values: %w", err)
}
w.Write(values)
return nil
}