-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathparquet_nested.go
More file actions
682 lines (612 loc) · 19 KB
/
Copy pathparquet_nested.go
File metadata and controls
682 lines (612 loc) · 19 KB
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2024 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package external
import (
"context"
"errors"
"fmt"
"io"
"time"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/container/batch"
"github.com/matrixorigin/matrixone/pkg/container/bytejson"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/container/vector"
"github.com/matrixorigin/matrixone/pkg/sql/plan"
"github.com/matrixorigin/matrixone/pkg/util/trace"
"github.com/matrixorigin/matrixone/pkg/vm/process"
"github.com/parquet-go/parquet-go"
)
// isNestedTargetTypeSupported checks if nested type can map to target type
func isNestedTargetTypeSupported(t types.T) bool {
switch t {
case types.T_json, types.T_text, types.T_varchar, types.T_char:
return true
default:
return false
}
}
// getNestedMapper creates mapper for nested type column
func (h *ParquetHandler) getNestedMapper(col *parquet.Column, dt plan.Type) *columnMapper {
return &columnMapper{
srcNull: col.Optional(),
dstNull: !dt.NotNullable,
maxDefinitionLevel: byte(col.MaxDefinitionLevel()),
mapper: nil, // nested columns use row mode
}
}
// getDataByRow reads data row by row (used when has nested columns)
func (h *ParquetHandler) getDataByRow(bat *batch.Batch, param *ExternalParam, proc *process.Process) error {
_, span := trace.Start(proc.Ctx, "ParquetHandler.getDataByRow")
defer span.End()
rowModeStart := time.Now()
defer func() {
param.addParquetProfile(process.ParquetProfileStats{
RowModeTime: time.Since(rowModeStart).Nanoseconds(),
})
}()
if h.offset > 0 {
if err := h.rowReader.SeekToRow(h.offset); err != nil {
return moerr.ConvertGoError(param.Ctx, err)
}
}
rowBuf := make([]parquet.Row, int(h.batchCnt))
n, err := h.rowReader.ReadRows(rowBuf)
if err != nil && !errors.Is(err, io.EOF) {
return moerr.ConvertGoError(param.Ctx, err)
}
rowBuf = rowBuf[:n]
for _, row := range rowBuf {
if err := h.processRow(row, bat, param, proc); err != nil {
return err
}
}
bat.SetRowCount(n)
h.offset += int64(n)
finish := n == 0 || h.isFinished()
if finish {
h.cleanup()
// File completion (FileFin/End) is now handled by Call's finishCurrentFile
}
return nil
}
// cleanup releases resources
func (h *ParquetHandler) cleanup() {
if h.rowReader != nil {
h.rowReader.Close()
h.rowReader = nil
}
}
// processRow processes a single row
func (h *ParquetHandler) processRow(row parquet.Row, bat *batch.Batch, param *ExternalParam, proc *process.Process) error {
for colIdx, col := range h.cols {
if col == nil || param.Cols[colIdx].Hidden || h.mappers[colIdx] == nil {
continue
}
vec := bat.Vecs[colIdx]
def := param.Cols[colIdx]
if !col.Leaf() {
if err := h.processNestedValue(row, col, vec, def, proc); err != nil {
return err
}
} else {
if err := h.processLeafValue(row, col, vec, def, proc); err != nil {
return err
}
}
}
return nil
}
// processLeafValue processes a leaf column value
func (h *ParquetHandler) processLeafValue(
row parquet.Row,
col *parquet.Column,
vec *vector.Vector,
def *plan.ColDef,
proc *process.Process,
) error {
colIndex := col.Index()
var value parquet.Value
found := false
for _, v := range row {
if v.Column() == colIndex {
value = v
found = true
break
}
}
if !found || value.IsNull() {
return appendNull(vec, def, proc)
}
return appendLeafValue(value, col, vec, def, proc)
}
// appendNull appends a NULL value to vector
func appendNull(vec *vector.Vector, def *plan.ColDef, proc *process.Process) error {
if def.NotNull {
return moerr.NewConstraintViolation(proc.Ctx, "NULL value not allowed")
}
return vector.AppendAny(vec, nil, true, proc.Mp())
}
// appendLeafValue appends a leaf value to vector
func appendLeafValue(
v parquet.Value,
col *parquet.Column,
vec *vector.Vector,
def *plan.ColDef,
proc *process.Process,
) error {
targetType := types.T(def.Typ.Id)
st := col.Type()
switch targetType {
case types.T_bool:
return vector.AppendFixed(vec, v.Boolean(), false, proc.Mp())
case types.T_int8:
return vector.AppendFixed(vec, int8(v.Int32()), false, proc.Mp())
case types.T_int16:
return vector.AppendFixed(vec, int16(v.Int32()), false, proc.Mp())
case types.T_int32:
return vector.AppendFixed(vec, v.Int32(), false, proc.Mp())
case types.T_int64:
if st.Kind() == parquet.Int32 {
return vector.AppendFixed(vec, int64(v.Int32()), false, proc.Mp())
}
return vector.AppendFixed(vec, v.Int64(), false, proc.Mp())
case types.T_uint8:
return vector.AppendFixed(vec, uint8(v.Int32()), false, proc.Mp())
case types.T_uint16:
return vector.AppendFixed(vec, uint16(v.Int32()), false, proc.Mp())
case types.T_uint32:
if st.Kind() == parquet.Int32 {
return vector.AppendFixed(vec, uint32(v.Int32()), false, proc.Mp())
}
return vector.AppendFixed(vec, uint32(v.Int64()), false, proc.Mp())
case types.T_uint64:
return vector.AppendFixed(vec, uint64(v.Int64()), false, proc.Mp())
case types.T_float32:
return vector.AppendFixed(vec, v.Float(), false, proc.Mp())
case types.T_float64:
if st.Kind() == parquet.Float {
return vector.AppendFixed(vec, float64(v.Float()), false, proc.Mp())
}
return vector.AppendFixed(vec, v.Double(), false, proc.Mp())
case types.T_char, types.T_varchar, types.T_text, types.T_blob,
types.T_binary, types.T_varbinary:
return vector.AppendBytes(vec, v.ByteArray(), false, proc.Mp())
default:
return moerr.NewNYIf(proc.Ctx, "row mode convert to %s", targetType.String())
}
}
// processNestedValue processes a nested column value
func (h *ParquetHandler) processNestedValue(
row parquet.Row,
col *parquet.Column,
vec *vector.Vector,
def *plan.ColDef,
proc *process.Process,
) error {
colValues := extractNestedColumnValues(row, col)
if isNestedColumnNull(colValues, col) {
return appendNull(vec, def, proc)
}
nested, err := reconstructNestedValue(proc.Ctx, col, colValues)
if err != nil {
return moerr.NewInternalErrorf(proc.Ctx,
"failed to reconstruct nested column %s: %v", col.Name(), err)
}
targetType := types.T(def.Typ.Id)
return writeNestedToVector(nested, targetType, vec, proc)
}
// extractNestedColumnValues extracts all values for a nested column from row
func extractNestedColumnValues(row parquet.Row, col *parquet.Column) []parquet.Value {
startIdx, endIdx := getNestedColumnIndexRange(col)
var values []parquet.Value
for _, v := range row {
colIdx := v.Column()
if colIdx >= startIdx && colIdx < endIdx {
values = append(values, v)
}
}
return values
}
// getNestedColumnIndexRange gets leaf column index range for nested column
func getNestedColumnIndexRange(col *parquet.Column) (start, end int) {
leaves := collectLeafColumns(col)
if len(leaves) == 0 {
return -1, -1
}
start = leaves[0].Index()
end = leaves[len(leaves)-1].Index() + 1
return
}
// collectLeafColumns recursively collects all leaf columns
func collectLeafColumns(col *parquet.Column) []*parquet.Column {
if col.Leaf() {
return []*parquet.Column{col}
}
var leaves []*parquet.Column
for _, child := range col.Columns() {
leaves = append(leaves, collectLeafColumns(child)...)
}
return leaves
}
// isNestedColumnNull checks if nested column is NULL
func isNestedColumnNull(values []parquet.Value, col *parquet.Column) bool {
if len(values) == 0 {
return true
}
if col != nil && col.Optional() && len(values) > 0 {
return values[0].DefinitionLevel() == 0
}
return false
}
// reconstructNestedValue reconstructs nested structure to Go type
func reconstructNestedValue(ctx context.Context, col *parquet.Column, values []parquet.Value) (any, error) {
// Check LogicalType first
logicalType := col.Type().LogicalType()
if logicalType != nil {
if logicalType.List != nil {
return reconstructList(ctx, col, values)
}
if logicalType.Map != nil {
// Map with LogicalType annotation has structure:
// metadata (Map) -> key_value (repeated) -> key, value
// We need to pass the key_value child to reconstructMap
children := col.Columns()
if len(children) == 1 {
return reconstructMap(ctx, children[0], values)
}
return reconstructMap(ctx, col, values)
}
}
// Check for Parquet 3-level List pattern (without LogicalType annotation)
// Pattern: group (optional) -> repeated group "list" -> element
if isParquetListPattern(col) {
return reconstructListFromPattern(ctx, col, values)
}
// Check for Parquet Map pattern (without LogicalType annotation)
// Pattern: group (optional) -> repeated group "key_value" -> key, value
if isParquetMapPattern(col) {
return reconstructMapFromPattern(ctx, col, values)
}
return reconstructStruct(ctx, col, values)
}
// isParquetListPattern checks if column matches Parquet 3-level List pattern
// Pattern: group (optional) -> repeated group "list" -> element
func isParquetListPattern(col *parquet.Column) bool {
if col.Leaf() {
return false
}
children := col.Columns()
if len(children) != 1 {
return false
}
listChild := children[0]
// Check if child is named "list" and is repeated
return listChild.Name() == "list" && listChild.Repeated()
}
// isParquetMapPattern checks if column matches Parquet Map pattern
// Pattern: group (optional) -> repeated group "key_value" -> key, value
func isParquetMapPattern(col *parquet.Column) bool {
if col.Leaf() {
return false
}
children := col.Columns()
if len(children) != 1 {
return false
}
kvChild := children[0]
// Check if child is named "key_value" and is repeated
return kvChild.Name() == "key_value" && kvChild.Repeated()
}
// reconstructListFromPattern reconstructs List from 3-level pattern
func reconstructListFromPattern(ctx context.Context, col *parquet.Column, values []parquet.Value) ([]any, error) {
// Extract values from the "list.element" path
listChild := col.Columns()[0]
if len(listChild.Columns()) == 0 {
return nil, moerr.NewInternalErrorf(ctx, "list child has no element column")
}
elementCol := listChild.Columns()[0]
// Check if element is a leaf or nested structure
if elementCol.Leaf() {
// Simple case: list of primitive values
return reconstructList(ctx, listChild, values)
} else {
// Complex case: list of structs/maps/lists
return reconstructListOfNested(ctx, elementCol, values)
}
}
// reconstructMapFromPattern reconstructs Map from pattern
func reconstructMapFromPattern(ctx context.Context, col *parquet.Column, values []parquet.Value) (map[string]any, error) {
// Extract values from the "key_value.key" and "key_value.value" paths
kvChild := col.Columns()[0]
return reconstructMap(ctx, kvChild, values)
}
// reconstructListOfNested reconstructs List of nested structures (Struct/Map/List)
func reconstructListOfNested(ctx context.Context, elementCol *parquet.Column, values []parquet.Value) ([]any, error) {
result := make([]any, 0)
if len(values) == 0 {
return result, nil
}
leafCols := collectLeafColumns(elementCol)
if len(leafCols) == 0 {
return result, nil
}
expectedCols := make(map[int]bool)
for _, leaf := range leafCols {
expectedCols[leaf.Index()] = true
}
// Group values by column repetition - when we see a column again, new element starts
var groups [][]parquet.Value
currentGroup := make([]parquet.Value, 0, len(leafCols))
seenCols := make(map[int]bool)
for _, v := range values {
colIdx := v.Column()
if !expectedCols[colIdx] {
continue
}
if seenCols[colIdx] {
if len(currentGroup) > 0 {
groups = append(groups, currentGroup)
}
currentGroup = make([]parquet.Value, 0, len(leafCols))
seenCols = make(map[int]bool)
}
currentGroup = append(currentGroup, v)
seenCols[colIdx] = true
}
if len(currentGroup) > 0 {
groups = append(groups, currentGroup)
}
for _, group := range groups {
nested, err := reconstructNestedByType(ctx, elementCol, group)
if err != nil {
return nil, err
}
result = append(result, nested)
}
return result, nil
}
func reconstructList(ctx context.Context, col *parquet.Column, values []parquet.Value) ([]any, error) {
result := make([]any, 0)
// Empty list case: no values at all
if len(values) == 0 {
return result, nil
}
// Empty list case: single NULL value with low definition level
// This indicates an empty list, not a list with a NULL element
if len(values) == 1 && values[0].IsNull() && values[0].RepetitionLevel() == 0 {
return result, nil
}
for _, v := range values {
if v.IsNull() {
result = append(result, nil)
} else {
result = append(result, parquetValueToGo(v))
}
}
return result, nil
}
// reconstructMap reconstructs Map type
func reconstructMap(ctx context.Context, col *parquet.Column, values []parquet.Value) (map[string]any, error) {
result := make(map[string]any)
// Find key and value columns
var keyCol, valueCol *parquet.Column
for _, child := range col.Columns() {
if child.Name() == "key" {
keyCol = child
} else if child.Name() == "value" {
valueCol = child
}
}
if keyCol == nil {
return result, nil
}
keyColIdx := keyCol.Index()
// Simple case: both key and value are leaf columns
if keyCol.Leaf() && (valueCol == nil || valueCol.Leaf()) {
valColIdx := -1
if valueCol != nil {
valColIdx = valueCol.Index()
}
var keys, vals []parquet.Value
for _, v := range values {
switch v.Column() {
case keyColIdx:
keys = append(keys, v)
case valColIdx:
vals = append(vals, v)
}
}
for i := 0; i < len(keys); i++ {
keyStr := stringifyMapKey(keys[i])
if _, exists := result[keyStr]; exists {
return nil, moerr.NewInternalErrorf(ctx, "duplicate map key: %s", keyStr)
}
if i < len(vals) {
if vals[i].IsNull() {
result[keyStr] = nil
} else {
result[keyStr] = parquetValueToGo(vals[i])
}
} else {
result[keyStr] = nil
}
}
return result, nil
}
// Complex case: value is nested (List/Struct/Map)
if valueCol != nil && !valueCol.Leaf() {
valueStartIdx, valueEndIdx := getNestedColumnIndexRange(valueCol)
groupCap := valueEndIdx - valueStartIdx
if groupCap < 0 {
groupCap = 0
}
// Collect all keys
var keys []parquet.Value
for _, v := range values {
if v.Column() == keyColIdx {
keys = append(keys, v)
}
}
// Group values by RepetitionLevel
// Rep=0 or Rep=1 starts a new map entry, Rep>=2 continues current entry
valueGroups := make([][]parquet.Value, 0)
currentGroup := make([]parquet.Value, 0, groupCap)
for _, v := range values {
colIdx := v.Column()
if colIdx >= valueStartIdx && colIdx < valueEndIdx {
rep := v.RepetitionLevel()
// Rep <= 1 means new map entry (0=new row, 1=new key_value)
if rep <= 1 && len(currentGroup) > 0 {
valueGroups = append(valueGroups, currentGroup)
currentGroup = make([]parquet.Value, 0, groupCap)
}
currentGroup = append(currentGroup, v)
}
}
if len(currentGroup) > 0 {
valueGroups = append(valueGroups, currentGroup)
}
for i, key := range keys {
keyStr := stringifyMapKey(key)
if _, exists := result[keyStr]; exists {
return nil, moerr.NewInternalErrorf(ctx, "duplicate map key: %s", keyStr)
}
if i >= len(valueGroups) || len(valueGroups[i]) == 0 {
result[keyStr] = nil
} else {
nested, err := reconstructNestedByType(ctx, valueCol, valueGroups[i])
if err != nil {
return nil, err
}
result[keyStr] = nested
}
}
}
return result, nil
}
// stringifyMapKey converts map key to string
func stringifyMapKey(v parquet.Value) string {
if v.IsNull() {
return "null"
}
switch v.Kind() {
case parquet.ByteArray, parquet.FixedLenByteArray:
return string(v.ByteArray())
default:
return fmt.Sprintf("%v", parquetValueToGo(v))
}
}
// reconstructStruct reconstructs Struct type
func reconstructStruct(ctx context.Context, col *parquet.Column, values []parquet.Value) (map[string]any, error) {
result := make(map[string]any)
for _, child := range col.Columns() {
fieldName := child.Name()
if child.Leaf() {
for _, v := range values {
if v.Column() == child.Index() {
if v.IsNull() {
result[fieldName] = nil
} else {
result[fieldName] = parquetValueToGo(v)
}
break
}
}
} else {
childValues := filterValuesByColumn(values, child)
childResult, err := reconstructNestedByType(ctx, child, childValues)
if err != nil {
return nil, err
}
result[fieldName] = childResult
}
}
return result, nil
}
// filterValuesByColumn filters values belonging to nested column
func filterValuesByColumn(values []parquet.Value, col *parquet.Column) []parquet.Value {
startIdx, endIdx := getNestedColumnIndexRange(col)
var result []parquet.Value
for _, v := range values {
colIdx := v.Column()
if colIdx >= startIdx && colIdx < endIdx {
result = append(result, v)
}
}
return result
}
// reconstructNestedByType reconstructs nested structure by type
func reconstructNestedByType(ctx context.Context, col *parquet.Column, values []parquet.Value) (any, error) {
logicalType := col.Type().LogicalType()
if logicalType != nil {
if logicalType.List != nil {
return reconstructList(ctx, col, values)
}
if logicalType.Map != nil {
return reconstructMap(ctx, col, values)
}
}
// Check for Parquet 3-level List pattern (without LogicalType annotation)
if isParquetListPattern(col) {
return reconstructListFromPattern(ctx, col, values)
}
// Check for Parquet Map pattern (without LogicalType annotation)
if isParquetMapPattern(col) {
return reconstructMapFromPattern(ctx, col, values)
}
return reconstructStruct(ctx, col, values)
}
// parquetValueToGo converts parquet.Value to Go type
func parquetValueToGo(v parquet.Value) any {
if v.IsNull() {
return nil
}
switch v.Kind() {
case parquet.Boolean:
return v.Boolean()
case parquet.Int32:
return int64(v.Int32())
case parquet.Int64:
return v.Int64()
case parquet.Float:
return float64(v.Float())
case parquet.Double:
return v.Double()
case parquet.ByteArray, parquet.FixedLenByteArray:
return string(v.ByteArray())
default:
return nil
}
}
// writeNestedToVector writes nested structure to vector
func writeNestedToVector(nested any, targetType types.T, vec *vector.Vector, proc *process.Process) error {
bj, err := bytejson.CreateByteJSON(nested)
if err != nil {
return moerr.NewInternalErrorf(proc.Ctx, "failed to create JSON: %v", err)
}
switch targetType {
case types.T_json:
jsonBytes, err := types.EncodeJson(bj)
if err != nil {
return moerr.NewInternalErrorf(proc.Ctx, "failed to encode JSON: %v", err)
}
return vector.AppendBytes(vec, jsonBytes, false, proc.Mp())
case types.T_text, types.T_varchar, types.T_char:
jsonStr := bj.String()
return vector.AppendBytes(vec, []byte(jsonStr), false, proc.Mp())
default:
return moerr.NewInternalErrorf(proc.Ctx, "unsupported target type: %s", targetType.String())
}
}