forked from parquet-go/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_buffer_test.go
More file actions
1936 lines (1649 loc) · 47.5 KB
/
Copy pathcolumn_buffer_test.go
File metadata and controls
1936 lines (1649 loc) · 47.5 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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package parquet
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
func TestBroadcastValueInt32(t *testing.T) {
buf := make([]int32, 123)
broadcastValueInt32(buf, 0x0A)
for i, v := range buf {
if v != 0x0A0A0A0A {
t.Fatalf("wrong value at index %d: %v", i, v)
}
}
}
func TestBroadcastRangeInt32(t *testing.T) {
buf := make([]int32, 123)
broadcastRangeInt32(buf, 1)
for i, v := range buf {
if v != int32(1+i) {
t.Fatalf("wrong value at index %d: %v", i, v)
}
}
}
func BenchmarkBroadcastValueInt32(b *testing.B) {
buf := make([]int32, 1000)
for b.Loop() {
broadcastValueInt32(buf, -1)
}
b.SetBytes(4 * int64(len(buf)))
}
func BenchmarkBroadcastRangeInt32(b *testing.B) {
buf := make([]int32, 1000)
for b.Loop() {
broadcastRangeInt32(buf, 0)
}
b.SetBytes(4 * int64(len(buf)))
}
func TestWriteAndReadOptionalList(t *testing.T) {
type record struct {
Values []float64 `parquet:"values,list,optional"`
}
records := []record{
{Values: []float64{1.0, 2.0, 3.0}},
{Values: []float64{}},
{Values: []float64{4.0, 5.0}},
}
buffer := new(bytes.Buffer)
if err := Write(buffer, records); err != nil {
t.Fatal(err)
}
found, err := Read[record](bytes.NewReader(buffer.Bytes()), int64(buffer.Len()))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(records, found) {
t.Fatalf("expected %v, got %v", records, found)
}
}
func TestWriteAndReadOptionalPointer(t *testing.T) {
type record struct {
Value float64 `parquet:"values,optional"`
}
records := []record{
{Value: 1.0},
{Value: 0.0},
{Value: 2.0},
{Value: 0.0},
}
buffer := new(bytes.Buffer)
if err := Write(buffer, records); err != nil {
t.Fatal(err)
}
found, err := Read[record](bytes.NewReader(buffer.Bytes()), int64(buffer.Len()))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(records, found) {
t.Fatalf("expected %v, got %v", records, found)
}
}
// https://github.com/segmentio/parquet-go/issues/501
func TestIssueSegmentio501(t *testing.T) {
col := newBooleanColumnBuffer(BooleanType, 0, 2055208)
// write all trues and then flush the buffer
_, err := col.WriteBooleans([]bool{true, true, true, true, true, true, true, true})
if err != nil {
t.Fatal(err)
}
col.Reset()
// write a single false, we are trying to trip a certain line of code in WriteBooleans
_, err = col.WriteBooleans([]bool{false})
if err != nil {
t.Fatal(err)
}
// now write 7 booleans at once, this will cause WriteBooleans to attempt its "alignment" logic
_, err = col.WriteBooleans([]bool{false, false, false, false, false, false, false})
if err != nil {
panic(err)
}
for i := range 8 {
read := make([]Value, 1)
_, err = col.ReadValuesAt(read, int64(i))
if err != nil {
t.Fatal(err)
}
if read[0].Boolean() {
t.Fatalf("expected false at index %d", i)
}
}
}
func TestWriteRowsFuncOfRequiredColumnNotFound(t *testing.T) {
schema := NewSchema("test", Group{
"name": String(),
"age": Int(32),
})
defer func() {
if r := recover(); r != nil {
expected := "parquet: column not found: nonexistent"
if r != expected {
t.Fatalf("expected panic message %q, got %q", expected, r)
}
} else {
t.Fatal("expected panic but none occurred")
}
}()
writeRowsFuncOfRequired(reflect.TypeOf(""), schema, columnPath{"nonexistent"})
}
// TestMapFieldToGroupSchema tests writing a Go struct with a map[string]string
// field to a schema where that field is defined as a GROUP with named optional fields.
// This verifies that NewWriter supports this functionality.
func TestMapFieldToGroupSchema(t *testing.T) {
// The user's Go type
type RecordWithMap struct {
Nested map[string]string
}
// The desired schema structure (GROUP with named fields)
type RecordWithStruct struct {
Nested struct {
A string `parquet:",optional"`
B string `parquet:",optional"`
C string `parquet:",optional"`
}
}
// Create schema from the struct (this gives us the desired GROUP schema)
desiredSchema := SchemaOf(RecordWithStruct{})
// Try to write using the map type
buf := new(bytes.Buffer)
writer := NewWriter(buf, desiredSchema)
// Attempt to write a value with map[string]string
record := RecordWithMap{
Nested: map[string]string{
"A": "value_a",
"B": "value_b",
"C": "value_c",
},
}
err := writer.Write(record)
if err != nil {
t.Fatalf("failed to write row: %v", err)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Try to read it back
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
var result RecordWithStruct
if err := reader.Read(&result); err != nil {
t.Fatalf("failed to read row: %v", err)
}
}
// TestWhatActuallyHappensWithMapField tests what schema is generated when
// you use a map[string]string field - it creates a MAP logical type, not a GROUP.
func TestWhatActuallyHappensWithMapField(t *testing.T) {
type RecordWithMap struct {
Nested map[string]string
}
// Get the schema that's naturally generated from the map type
naturalSchema := SchemaOf(RecordWithMap{})
// Verify that the natural schema is a MAP type, not a GROUP
nestedField := naturalSchema.Fields()[0]
if nestedField.Type().LogicalType() == nil || nestedField.Type().LogicalType().Map == nil {
t.Fatalf("expected Nested field to have MAP logical type, got: %v", nestedField.Type().LogicalType())
}
// Write some data
buf := new(bytes.Buffer)
writer := NewWriter(buf, naturalSchema)
record := RecordWithMap{
Nested: map[string]string{
"A": "value_a",
"B": "value_b",
},
}
if err := writer.Write(record); err != nil {
t.Fatalf("failed to write: %v", err)
}
writer.Close()
// Read it back and check the schema matches
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
if !EqualNodes(naturalSchema, reader.Schema()) {
t.Errorf("reader schema doesn't match natural schema:\nexpected: %s\ngot: %s", naturalSchema, reader.Schema())
}
}
// TestGenericWriterMapToGroupSchema tests that NewGenericWriter supports
// writing map[string]string fields to a GROUP schema with named optional fields.
func TestGenericWriterMapToGroupSchema(t *testing.T) {
// The user's Go type
type RecordWithMap struct {
Nested map[string]string
}
// The desired schema structure (GROUP with named fields)
type RecordWithStruct struct {
Nested struct {
A string `parquet:",optional"`
B string `parquet:",optional"`
C string `parquet:",optional"`
}
}
// Create schema from the struct (this gives us the desired GROUP schema)
desiredSchema := SchemaOf(RecordWithStruct{})
// Try to write using NewGenericWriter with the map type
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, desiredSchema)
// Attempt to write values with map[string]string
records := []RecordWithMap{
{
Nested: map[string]string{
"A": "value_a1",
"B": "value_b1",
"C": "value_c1",
},
},
{
Nested: map[string]string{
"A": "value_a2",
"B": "value_b2",
// C is omitted - should be null
},
},
}
n, err := writer.Write(records)
if err != nil {
t.Fatalf("failed to write rows: %v", err)
}
if n != 2 {
t.Fatalf("expected to write 2 rows, wrote %d", n)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Try to read it back
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
var result1 RecordWithStruct
if err := reader.Read(&result1); err != nil {
t.Fatalf("failed to read row 1: %v", err)
}
if result1.Nested.A != "value_a1" || result1.Nested.B != "value_b1" || result1.Nested.C != "value_c1" {
t.Errorf("row 1 values incorrect: %+v", result1.Nested)
}
var result2 RecordWithStruct
if err := reader.Read(&result2); err != nil {
t.Fatalf("failed to read row 2: %v", err)
}
if result2.Nested.A != "value_a2" || result2.Nested.B != "value_b2" || result2.Nested.C != "" {
t.Errorf("row 2 values incorrect: %+v", result2.Nested)
}
}
// TestGenericWriterMapToOptionalGroupSchema tests that NewGenericWriter supports
// writing map[string]string fields to an OPTIONAL GROUP schema.
// This is a regression test for the case where the GROUP itself is optional,
// which requires incrementing the definition level when the GROUP is present.
func TestGenericWriterMapToOptionalGroupSchema(t *testing.T) {
// The user's Go type
type RecordWithMap struct {
Nested map[string]string
}
// The desired schema structure (OPTIONAL GROUP with named fields)
type RecordWithStruct struct {
Nested struct {
A string `parquet:",optional"`
B string `parquet:",optional"`
} `parquet:",optional"` // <-- GROUP itself is optional
}
// Create schema from the struct
desiredSchema := SchemaOf(RecordWithStruct{})
// Verify the schema has optional GROUP
nestedField := desiredSchema.Fields()[0]
if !nestedField.Optional() {
t.Fatalf("expected Nested field to be optional, but it's required")
}
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, desiredSchema)
// Write records: one with values, one with nil map (absent GROUP)
records := []RecordWithMap{
{
Nested: map[string]string{
"A": "value_a",
"B": "value_b",
},
},
{
Nested: nil, // GROUP is absent
},
{
Nested: map[string]string{
"A": "value_a3",
// B is omitted - should be null within present GROUP
},
},
}
n, err := writer.Write(records)
if err != nil {
t.Fatalf("failed to write rows: %v", err)
}
if n != 3 {
t.Fatalf("expected to write 3 rows, wrote %d", n)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Read back and verify
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
// Row 1: GROUP present, both fields present
var result1 RecordWithStruct
if err := reader.Read(&result1); err != nil {
t.Fatalf("failed to read row 1: %v", err)
}
if result1.Nested.A != "value_a" {
t.Errorf("row 1 A incorrect: got %q, want %q", result1.Nested.A, "value_a")
}
if result1.Nested.B != "value_b" {
t.Errorf("row 1 B incorrect: got %q, want %q", result1.Nested.B, "value_b")
}
// Row 2: GROUP absent (nil map)
var result2 RecordWithStruct
if err := reader.Read(&result2); err != nil {
t.Fatalf("failed to read row 2: %v", err)
}
// When GROUP is absent, fields should be zero values
if result2.Nested.A != "" {
t.Errorf("row 2 A incorrect: got %q, want empty", result2.Nested.A)
}
if result2.Nested.B != "" {
t.Errorf("row 2 B incorrect: got %q, want empty", result2.Nested.B)
}
// Row 3: GROUP present, only A present
var result3 RecordWithStruct
if err := reader.Read(&result3); err != nil {
t.Fatalf("failed to read row 3: %v", err)
}
if result3.Nested.A != "value_a3" {
t.Errorf("row 3 A incorrect: got %q, want %q", result3.Nested.A, "value_a3")
}
if result3.Nested.B != "" {
t.Errorf("row 3 B incorrect: got %q, want empty", result3.Nested.B)
}
}
// TestGenericWriterMapToNestedStructWithRequiredGroup tests writing a map
// inside a nested struct where the inner GROUP is required.
func TestGenericWriterMapToNestedStructWithRequiredGroup(t *testing.T) {
// Go type with nested struct containing map
type RecordWithNestedMap struct {
Resource struct {
Type string
Labels map[string]string
}
}
// Schema type with nested struct containing GROUP
type RecordWithNestedStruct struct {
Resource struct {
Type string
Labels struct {
ProjectID string `parquet:"project_id,optional"`
Zone string `parquet:"zone,optional"`
}
}
}
desiredSchema := SchemaOf(RecordWithNestedStruct{})
// Verify Resource.Labels is required (not optional)
resourceField := desiredSchema.Fields()[0]
labelsField := resourceField.Fields()[1]
if labelsField.Optional() {
t.Fatalf("expected Resource.Labels to be required, but it's optional")
}
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithNestedMap](buf, desiredSchema)
records := []RecordWithNestedMap{
{
Resource: struct {
Type string
Labels map[string]string
}{
Type: "gce_instance",
Labels: map[string]string{
"project_id": "test-project",
"zone": "us-central1-a",
},
},
},
}
n, err := writer.Write(records)
if err != nil {
t.Fatalf("failed to write rows: %v", err)
}
if n != 1 {
t.Fatalf("expected to write 1 row, wrote %d", n)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Read back and verify
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
var result RecordWithNestedStruct
if err := reader.Read(&result); err != nil {
t.Fatalf("failed to read row: %v", err)
}
if result.Resource.Type != "gce_instance" {
t.Errorf("Resource.Type incorrect: got %q, want %q", result.Resource.Type, "gce_instance")
}
if result.Resource.Labels.ProjectID != "test-project" {
t.Errorf("Resource.Labels.ProjectID incorrect: got %q, want %q", result.Resource.Labels.ProjectID, "test-project")
}
if result.Resource.Labels.Zone != "us-central1-a" {
t.Errorf("Resource.Labels.Zone incorrect: got %q, want %q", result.Resource.Labels.Zone, "us-central1-a")
}
}
// TestGenericWriterMapAnyToNestedGroupSchema tests that NewGenericWriter supports
// writing map[string]any fields to a nested GROUP schema with multiple levels.
// This corresponds to the example:
//
// message record {
// group nested {
// group coordinates {
// double x;
// double y;
// }
// string id;
// }
// }
func TestGenericWriterMapAnyToNestedGroupSchema(t *testing.T) {
// The user's Go type with map[string]any
type RecordWithMap struct {
Nested map[string]any
}
// The desired schema structure (nested GROUPs)
type RecordWithStruct struct {
Nested struct {
Coordinates struct {
X float64 `parquet:",optional"`
Y float64 `parquet:",optional"`
} `parquet:",optional"`
ID string `parquet:",optional"`
}
}
// Create schema from the struct (this gives us the desired nested GROUP schema)
desiredSchema := SchemaOf(RecordWithStruct{})
// Try to write using NewGenericWriter with the map type
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, desiredSchema)
// Attempt to write values with map[string]any
records := []RecordWithMap{
{
Nested: map[string]any{
"Coordinates": map[string]float64{
"X": 0.1,
"Y": 0.2,
},
"ID": "1234567890",
},
},
{
Nested: map[string]any{
"Coordinates": map[string]any{
"X": 1.5,
"Y": 2.5,
},
"ID": "abc",
},
},
{
Nested: map[string]any{
// Coordinates omitted - should be null
"ID": "xyz",
},
},
}
n, err := writer.Write(records)
if err != nil {
t.Fatalf("failed to write rows: %v", err)
}
if n != 3 {
t.Fatalf("expected to write 3 rows, wrote %d", n)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Try to read it back
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
var result1 RecordWithStruct
if err := reader.Read(&result1); err != nil {
t.Fatalf("failed to read row 1: %v", err)
}
if result1.Nested.Coordinates.X != 0.1 || result1.Nested.Coordinates.Y != 0.2 {
t.Errorf("row 1 coordinates incorrect: %+v", result1.Nested.Coordinates)
}
if result1.Nested.ID != "1234567890" {
t.Errorf("row 1 ID incorrect: %s", result1.Nested.ID)
}
var result2 RecordWithStruct
if err := reader.Read(&result2); err != nil {
t.Fatalf("failed to read row 2: %v", err)
}
if result2.Nested.Coordinates.X != 1.5 || result2.Nested.Coordinates.Y != 2.5 {
t.Errorf("row 2 coordinates incorrect: %+v", result2.Nested.Coordinates)
}
if result2.Nested.ID != "abc" {
t.Errorf("row 2 ID incorrect: %s", result2.Nested.ID)
}
var result3 RecordWithStruct
if err := reader.Read(&result3); err != nil {
t.Fatalf("failed to read row 3: %v", err)
}
if result3.Nested.Coordinates.X != 0 || result3.Nested.Coordinates.Y != 0 {
t.Errorf("row 3 coordinates should be zero: %+v", result3.Nested.Coordinates)
}
if result3.Nested.ID != "xyz" {
t.Errorf("row 3 ID incorrect: %s", result3.Nested.ID)
}
}
// TestGenericWriterMapAnyToGroupWithMixedTypes tests map[string]any mapping
// to a GROUP schema with various primitive types.
func TestGenericWriterMapAnyToGroupWithMixedTypes(t *testing.T) {
// The user's Go type with map[string]any
type RecordWithMap struct {
Data map[string]any
}
// The desired schema structure with various types
type RecordWithStruct struct {
Data struct {
Name string `parquet:",optional"`
Age int32 `parquet:",optional"`
Score float64 `parquet:",optional"`
Active bool `parquet:",optional"`
}
}
// Create schema from the struct
desiredSchema := SchemaOf(RecordWithStruct{})
// Try to write using NewGenericWriter with the map type
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, desiredSchema)
// Attempt to write values with map[string]any containing different types
records := []RecordWithMap{
{
Data: map[string]any{
"Name": "Alice",
"Age": int32(30),
"Score": 95.5,
"Active": true,
},
},
{
Data: map[string]any{
"Name": "Bob",
"Age": int32(25),
"Score": 87.3,
// Active omitted
},
},
}
n, err := writer.Write(records)
if err != nil {
t.Fatalf("failed to write rows: %v", err)
}
if n != 2 {
t.Fatalf("expected to write 2 rows, wrote %d", n)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Try to read it back
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
var result1 RecordWithStruct
if err := reader.Read(&result1); err != nil {
t.Fatalf("failed to read row 1: %v", err)
}
if result1.Data.Name != "Alice" {
t.Errorf("row 1 Name incorrect: %s", result1.Data.Name)
}
if result1.Data.Age != 30 {
t.Errorf("row 1 Age incorrect: %d", result1.Data.Age)
}
if result1.Data.Score != 95.5 {
t.Errorf("row 1 Score incorrect: %f", result1.Data.Score)
}
if result1.Data.Active != true {
t.Errorf("row 1 Active incorrect: %v", result1.Data.Active)
}
var result2 RecordWithStruct
if err := reader.Read(&result2); err != nil {
t.Fatalf("failed to read row 2: %v", err)
}
if result2.Data.Name != "Bob" {
t.Errorf("row 2 Name incorrect: %s", result2.Data.Name)
}
if result2.Data.Age != 25 {
t.Errorf("row 2 Age incorrect: %d", result2.Data.Age)
}
if result2.Data.Score != 87.3 {
t.Errorf("row 2 Score incorrect: %f", result2.Data.Score)
}
if result2.Data.Active != false {
t.Errorf("row 2 Active should be false: %v", result2.Data.Active)
}
}
// TestGenericWriterMapAnyToDeeplyNestedGroups tests map[string]any mapping
// to a deeply nested GROUP schema (3+ levels).
func TestGenericWriterMapAnyToDeeplyNestedGroups(t *testing.T) {
// The user's Go type with map[string]any
type RecordWithMap struct {
Root map[string]any
}
// The desired schema structure with deep nesting
type RecordWithStruct struct {
Root struct {
Level1 struct {
Level2 struct {
Value string `parquet:",optional"`
} `parquet:",optional"`
Name *string `parquet:",optional"`
} `parquet:",optional"`
}
}
// Create schema from the struct
desiredSchema := SchemaOf(RecordWithStruct{})
// Try to write using NewGenericWriter with the map type
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, desiredSchema)
strPtr := func(s string) *string { return &s }
// Attempt to write values with deeply nested maps
records := []RecordWithMap{
{
Root: map[string]any{
"Level1": map[string]any{
"Level2": map[string]any{
"Value": strPtr("deep_value"),
},
"Name": "level1_name",
},
},
},
{
Root: map[string]any{
"Level1": map[string]any{
// Level2 omitted
"Name": "another_name",
},
},
},
}
n, err := writer.Write(records)
if err != nil {
t.Fatalf("failed to write rows: %v", err)
}
if n != 2 {
t.Fatalf("expected to write 2 rows, wrote %d", n)
}
if err := writer.Close(); err != nil {
t.Fatalf("failed to close writer: %v", err)
}
// Try to read it back
reader := NewReader(bytes.NewReader(buf.Bytes()))
defer reader.Close()
var result1 RecordWithStruct
if err := reader.Read(&result1); err != nil {
t.Fatalf("failed to read row 1: %v", err)
}
if result1.Root.Level1.Level2.Value != "deep_value" {
t.Errorf("row 1 deep value incorrect: %s", result1.Root.Level1.Level2.Value)
}
if *result1.Root.Level1.Name != "level1_name" {
t.Errorf("row 1 level1 name incorrect: %s", *result1.Root.Level1.Name)
}
var result2 RecordWithStruct
if err := reader.Read(&result2); err != nil {
t.Fatalf("failed to read row 2: %v", err)
}
if result2.Root.Level1.Level2.Value != "" {
t.Errorf("row 2 deep value should be empty: %s", result2.Root.Level1.Level2.Value)
}
if *result2.Root.Level1.Name != "another_name" {
t.Errorf("row 2 level1 name incorrect: %s", *result2.Root.Level1.Name)
}
}
// BenchmarkMapToGroup benchmarks writing maps to GROUP schemas
func BenchmarkMapToGroup(b *testing.B) {
b.Run("map[string]string", func(b *testing.B) {
benchmarkMapToGroupStringString(b)
})
b.Run("map[string]any", func(b *testing.B) {
benchmarkMapToGroupStringAny(b)
})
b.Run("map[string]map[string]string", func(b *testing.B) {
benchmarkMapToGroupNested(b)
})
}
func benchmarkMapToGroupStringString(b *testing.B) {
// Go type with map
type RecordWithMap struct {
ID int64
Data map[string]string
}
// Schema type with GROUP
type RecordWithStruct struct {
ID int64
Data struct {
Field1 string `parquet:",optional"`
Field2 string `parquet:",optional"`
Field3 string `parquet:",optional"`
Field4 string `parquet:",optional"`
Field5 string `parquet:",optional"`
}
}
schema := SchemaOf(RecordWithStruct{})
// Create test data
records := make([]RecordWithMap, 1000)
for i := range records {
records[i] = RecordWithMap{
ID: int64(i),
Data: map[string]string{
"Field1": "value1_" + string(rune('a'+i%26)),
"Field2": "value2_" + string(rune('a'+i%26)),
"Field3": "value3_" + string(rune('a'+i%26)),
"Field4": "value4_" + string(rune('a'+i%26)),
"Field5": "value5_" + string(rune('a'+i%26)),
},
}
}
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, schema)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
buf.Reset()
writer.Reset(buf)
_, err := writer.Write(records)
if err != nil {
b.Fatal(err)
}
if err := writer.Close(); err != nil {
b.Fatal(err)
}
}
}
func benchmarkMapToGroupStringAny(b *testing.B) {
// Go type with map[string]any
type RecordWithMap struct {
ID int64
Data map[string]any
}
// Schema type with GROUP containing various types
type RecordWithStruct struct {
ID int64
Data struct {
Name string `parquet:",optional"`
Age int32 `parquet:",optional"`
Score float64 `parquet:",optional"`
Active bool `parquet:",optional"`
Count int64 `parquet:",optional"`
}
}
schema := SchemaOf(RecordWithStruct{})
// Create test data
records := make([]RecordWithMap, 1000)
for i := range records {
records[i] = RecordWithMap{
ID: int64(i),
Data: map[string]any{
"Name": "name_" + string(rune('a'+i%26)),
"Age": int32(20 + i%50),
"Score": float64(i%100) + 0.5,
"Active": i%2 == 0,
"Count": int64(i * 10),
},
}
}
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, schema)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
buf.Reset()
writer.Reset(buf)
_, err := writer.Write(records)
if err != nil {
b.Fatal(err)
}
if err := writer.Close(); err != nil {
b.Fatal(err)
}
}
}
func benchmarkMapToGroupNested(b *testing.B) {
// Go type with nested maps - using map[string]any for outer map
// Note: map[string]map[string]string would be more natural but currently has a bug
// that causes a nil pointer panic (see TestGenericWriterMapMapToNestedGroupSchema)
type RecordWithMap struct {
ID int64
Nested map[string]map[string]string
}
// Schema type with nested GROUPs
type RecordWithStruct struct {
ID int64
Nested struct {
Group1 struct {
A string `parquet:",optional"`
B string `parquet:",optional"`
C string `parquet:",optional"`
} `parquet:",optional"`
Group2 struct {
X string `parquet:",optional"`
Y string `parquet:",optional"`
Z string `parquet:",optional"`
} `parquet:",optional"`
}
}
schema := SchemaOf(RecordWithStruct{})
// Create test data
records := make([]RecordWithMap, 1000)
for i := range records {
records[i] = RecordWithMap{
ID: int64(i),
Nested: map[string]map[string]string{
"Group1": {
"A": "value_a_" + string(rune('a'+i%26)),
"B": "value_b_" + string(rune('a'+i%26)),
"C": "value_c_" + string(rune('a'+i%26)),
},
"Group2": {
"X": "value_x_" + string(rune('a'+i%26)),
"Y": "value_y_" + string(rune('a'+i%26)),
"Z": "value_z_" + string(rune('a'+i%26)),
},
},
}
}
buf := new(bytes.Buffer)
writer := NewGenericWriter[RecordWithMap](buf, schema)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
buf.Reset()
writer.Reset(buf)
_, err := writer.Write(records)
if err != nil {
b.Fatal(err)
}