-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
TestComplexWriter.java
1895 lines (1746 loc) · 74.8 KB
/
TestComplexWriter.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.arrow.vector.complex.writer;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.util.AutoCloseables;
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.DecimalVector;
import org.apache.arrow.vector.Float4Vector;
import org.apache.arrow.vector.Float8Vector;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.LargeVarBinaryVector;
import org.apache.arrow.vector.LargeVarCharVector;
import org.apache.arrow.vector.SchemaChangeCallBack;
import org.apache.arrow.vector.VarBinaryVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.MapVector;
import org.apache.arrow.vector.complex.NonNullableStructVector;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.complex.impl.ComplexWriterImpl;
import org.apache.arrow.vector.complex.impl.NullableStructReaderImpl;
import org.apache.arrow.vector.complex.impl.NullableStructWriter;
import org.apache.arrow.vector.complex.impl.SingleStructReaderImpl;
import org.apache.arrow.vector.complex.impl.SingleStructWriter;
import org.apache.arrow.vector.complex.impl.UnionListReader;
import org.apache.arrow.vector.complex.impl.UnionListWriter;
import org.apache.arrow.vector.complex.impl.UnionMapReader;
import org.apache.arrow.vector.complex.impl.UnionReader;
import org.apache.arrow.vector.complex.impl.UnionWriter;
import org.apache.arrow.vector.complex.reader.BaseReader.StructReader;
import org.apache.arrow.vector.complex.reader.BigIntReader;
import org.apache.arrow.vector.complex.reader.FieldReader;
import org.apache.arrow.vector.complex.reader.Float4Reader;
import org.apache.arrow.vector.complex.reader.Float8Reader;
import org.apache.arrow.vector.complex.reader.IntReader;
import org.apache.arrow.vector.complex.writer.BaseWriter.ComplexWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.ListWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.MapWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.StructWriter;
import org.apache.arrow.vector.holders.DecimalHolder;
import org.apache.arrow.vector.holders.DurationHolder;
import org.apache.arrow.vector.holders.FixedSizeBinaryHolder;
import org.apache.arrow.vector.holders.IntHolder;
import org.apache.arrow.vector.holders.NullableDurationHolder;
import org.apache.arrow.vector.holders.NullableFixedSizeBinaryHolder;
import org.apache.arrow.vector.holders.NullableTimeStampMilliTZHolder;
import org.apache.arrow.vector.holders.NullableTimeStampNanoTZHolder;
import org.apache.arrow.vector.holders.TimeStampMilliTZHolder;
import org.apache.arrow.vector.types.TimeUnit;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.ArrowType.ArrowTypeID;
import org.apache.arrow.vector.types.pojo.ArrowType.Int;
import org.apache.arrow.vector.types.pojo.ArrowType.Struct;
import org.apache.arrow.vector.types.pojo.ArrowType.Timestamp;
import org.apache.arrow.vector.types.pojo.ArrowType.Union;
import org.apache.arrow.vector.types.pojo.ArrowType.Utf8;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.util.CallBack;
import org.apache.arrow.vector.util.DecimalUtility;
import org.apache.arrow.vector.util.JsonStringArrayList;
import org.apache.arrow.vector.util.JsonStringHashMap;
import org.apache.arrow.vector.util.Text;
import org.apache.arrow.vector.util.TransferPair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestComplexWriter {
private BufferAllocator allocator;
private static final int COUNT = 100;
@BeforeEach
public void init() {
allocator = new RootAllocator(Integer.MAX_VALUE);
}
@AfterEach
public void terminate() throws Exception {
allocator.close();
}
@Test
public void simpleNestedTypes() {
NonNullableStructVector parent = populateStructVector(null);
StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
for (int i = 0; i < COUNT; i++) {
rootReader.setPosition(i);
assertEquals(i, rootReader.reader("int").readInteger().intValue());
assertEquals(i, rootReader.reader("bigInt").readLong().longValue());
}
parent.close();
}
@Test
public void transferPairSchemaChange() {
SchemaChangeCallBack callBack1 = new SchemaChangeCallBack();
try (NonNullableStructVector parent = populateStructVector(callBack1)) {
ComplexWriter writer = new ComplexWriterImpl("newWriter", parent);
StructWriter rootWriter = writer.rootAsStruct();
IntWriter intWriter = rootWriter.integer("newInt");
intWriter.writeInt(1);
writer.setValueCount(1);
assertTrue(callBack1.getSchemaChangedAndReset());
// The second vector should not have registered a schema change
assertFalse(callBack1.getSchemaChangedAndReset());
}
}
private NonNullableStructVector populateStructVector(CallBack callBack) {
NonNullableStructVector parent =
new NonNullableStructVector("parent", allocator, new FieldType(false, Struct.INSTANCE, null, null), callBack);
ComplexWriter writer = new ComplexWriterImpl("root", parent);
StructWriter rootWriter = writer.rootAsStruct();
IntWriter intWriter = rootWriter.integer("int");
BigIntWriter bigIntWriter = rootWriter.bigInt("bigInt");
for (int i = 0; i < COUNT; i++) {
rootWriter.start();
intWriter.writeInt(i);
bigIntWriter.writeBigInt(i);
rootWriter.end();
}
writer.setValueCount(COUNT);
return parent;
}
@Test
public void nullableStruct() {
try (NonNullableStructVector structVector = NonNullableStructVector.empty("parent", allocator)) {
ComplexWriter writer = new ComplexWriterImpl("root", structVector);
StructWriter rootWriter = writer.rootAsStruct();
for (int i = 0; i < COUNT; i++) {
rootWriter.start();
if (i % 2 == 0) {
StructWriter structWriter = rootWriter.struct("struct");
structWriter.setPosition(i);
structWriter.start();
structWriter.bigInt("nested").writeBigInt(i);
structWriter.end();
}
rootWriter.end();
}
writer.setValueCount(COUNT);
checkNullableStruct(structVector);
}
}
/**
* This test is similar to {@link #nullableStruct()} ()} but we get the inner struct writer once at the beginning.
*/
@Test
public void nullableStruct2() {
try (NonNullableStructVector structVector = NonNullableStructVector.empty("parent", allocator)) {
ComplexWriter writer = new ComplexWriterImpl("root", structVector);
StructWriter rootWriter = writer.rootAsStruct();
StructWriter structWriter = rootWriter.struct("struct");
for (int i = 0; i < COUNT; i++) {
rootWriter.start();
if (i % 2 == 0) {
structWriter.setPosition(i);
structWriter.start();
structWriter.bigInt("nested").writeBigInt(i);
structWriter.end();
}
rootWriter.end();
}
writer.setValueCount(COUNT);
checkNullableStruct(structVector);
}
}
private void checkNullableStruct(NonNullableStructVector structVector) {
StructReader rootReader = new SingleStructReaderImpl(structVector).reader("root");
for (int i = 0; i < COUNT; i++) {
rootReader.setPosition(i);
assertTrue(rootReader.isSet(), "index is set: " + i);
FieldReader struct = rootReader.reader("struct");
if (i % 2 == 0) {
assertTrue(struct.isSet(), "index is set: " + i);
assertNotNull(struct.readObject(), "index is set: " + i);
assertEquals(i, struct.reader("nested").readLong().longValue());
} else {
assertFalse(struct.isSet(), "index is not set: " + i);
assertNull(struct.readObject(), "index is not set: " + i);
}
}
}
@Test
public void testList() {
try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) {
ComplexWriter writer = new ComplexWriterImpl("root", parent);
StructWriter rootWriter = writer.rootAsStruct();
rootWriter.start();
rootWriter.bigInt("int").writeBigInt(0);
rootWriter.list("list").startList();
rootWriter.list("list").bigInt().writeBigInt(0);
rootWriter.list("list").endList();
rootWriter.end();
rootWriter.start();
rootWriter.bigInt("int").writeBigInt(1);
rootWriter.end();
writer.setValueCount(2);
StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
rootReader.setPosition(0);
assertTrue(rootReader.reader("list").isSet(), "row 0 list is not set");
assertEquals(Long.valueOf(0), rootReader.reader("list").reader().readLong());
rootReader.setPosition(1);
assertFalse(rootReader.reader("list").isSet(), "row 1 list is set");
}
}
@Test
public void listScalarType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 2 == 0) {
listWriter.writeInt(j);
} else {
IntHolder holder = new IntHolder();
holder.value = j;
listWriter.write(holder);
}
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
assertEquals(j, listReader.reader().readInteger().intValue());
}
}
}
}
@Test
public void testListScalarNull() {
/* Write to a integer list vector
* each list of size 8 and having it's data values alternating between null and a non-null.
* Read and verify
*/
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 2 == 0) {
listWriter.writeNull();
} else {
IntHolder holder = new IntHolder();
holder.value = j;
listWriter.write(holder);
}
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
if (j % 2 == 0) {
assertFalse(listReader.reader().isSet(), "index is set: " + j);
} else {
assertTrue(listReader.reader().isSet(), "index is not set: " + j);
assertEquals(j, listReader.reader().readInteger().intValue());
}
}
}
}
}
@Test
public void listDecimalType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
DecimalHolder holder = new DecimalHolder();
holder.buffer = allocator.buffer(DecimalVector.TYPE_WIDTH);
ArrowType arrowType = new ArrowType.Decimal(10, 0, 128);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 4 == 0) {
listWriter.writeDecimal(new BigDecimal(j));
} else if (j % 4 == 1) {
DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0, DecimalVector.TYPE_WIDTH);
holder.start = 0;
holder.scale = 0;
holder.precision = 10;
listWriter.write(holder);
} else if (j % 4 == 2) {
DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0, DecimalVector.TYPE_WIDTH);
listWriter.writeDecimal(0, holder.buffer, arrowType);
} else {
byte[] value = BigDecimal.valueOf(j).unscaledValue().toByteArray();
listWriter.writeBigEndianBytesToDecimal(value, arrowType);
}
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
Object expected = new BigDecimal(j);
Object actual = listReader.reader().readBigDecimal();
assertEquals(expected, actual);
}
}
holder.buffer.close();
}
}
@Test
public void listTimeStampMilliTZType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 2 == 0) {
listWriter.writeNull();
} else {
TimeStampMilliTZHolder holder = new TimeStampMilliTZHolder();
holder.timezone = "FakeTimeZone";
holder.value = j;
listWriter.timeStampMilliTZ().write(holder);
}
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
if (j % 2 == 0) {
assertFalse(listReader.reader().isSet(), "index is set: " + j);
} else {
NullableTimeStampMilliTZHolder actual = new NullableTimeStampMilliTZHolder();
listReader.reader().read(actual);
assertEquals(j, actual.value);
assertEquals("FakeTimeZone", actual.timezone);
}
}
}
}
}
@Test
public void listDurationType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 2 == 0) {
listWriter.writeNull();
} else {
DurationHolder holder = new DurationHolder();
holder.unit = TimeUnit.MICROSECOND;
holder.value = j;
listWriter.duration().write(holder);
}
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
if (j % 2 == 0) {
assertFalse(listReader.reader().isSet(), "index is set: " + j);
} else {
NullableDurationHolder actual = new NullableDurationHolder();
listReader.reader().read(actual);
assertEquals(TimeUnit.MICROSECOND, actual.unit);
assertEquals(j, actual.value);
}
}
}
}
}
@Test
public void listFixedSizeBinaryType() throws Exception {
List<ArrowBuf> bufs = new ArrayList<ArrowBuf>();
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 2 == 0) {
listWriter.writeNull();
} else {
ArrowBuf buf = allocator.buffer(4);
buf.setInt(0, j);
FixedSizeBinaryHolder holder = new FixedSizeBinaryHolder();
holder.byteWidth = 4;
holder.buffer = buf;
listWriter.fixedSizeBinary().write(holder);
bufs.add(buf);
}
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
if (j % 2 == 0) {
assertFalse(listReader.reader().isSet(), "index is set: " + j);
} else {
NullableFixedSizeBinaryHolder actual = new NullableFixedSizeBinaryHolder();
listReader.reader().read(actual);
assertEquals(j, actual.buffer.getInt(0));
assertEquals(4, actual.byteWidth);
}
}
}
}
AutoCloseables.close(bufs);
}
@Test
public void listScalarTypeNullable() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
if (i % 2 == 0) {
listWriter.setPosition(i);
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
listWriter.writeInt(j);
}
listWriter.endList();
}
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
if (i % 2 == 0) {
assertTrue(listReader.isSet(), "index is set: " + i);
assertEquals(i % 7, ((List<?>) listReader.readObject()).size(), "correct length at: " + i);
} else {
assertFalse(listReader.isSet(), "index is not set: " + i);
assertNull(listReader.readObject(), "index is not set: " + i);
}
}
}
}
@Test
public void listStructType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
StructWriter structWriter = listWriter.struct();
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
structWriter.start();
structWriter.integer("int").writeInt(j);
structWriter.bigInt("bigInt").writeBigInt(j);
structWriter.end();
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
assertEquals(j, listReader.reader().reader("int").readInteger().intValue(), "record: " + i);
assertEquals(j, listReader.reader().reader("bigInt").readLong().longValue());
}
}
}
}
@Test
public void listListType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
ListWriter innerListWriter = listWriter.list();
innerListWriter.startList();
for (int k = 0; k < i % 13; k++) {
innerListWriter.integer().writeInt(k);
}
innerListWriter.endList();
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
checkListOfLists(listVector);
}
}
/**
* This test is similar to {@link #listListType()} but we get the inner list writer once at the beginning.
*/
@Test
public void listListType2() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
ListWriter innerListWriter = listWriter.list();
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
innerListWriter.startList();
for (int k = 0; k < i % 13; k++) {
innerListWriter.integer().writeInt(k);
}
innerListWriter.endList();
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
checkListOfLists(listVector);
}
}
private void checkListOfLists(final ListVector listVector) {
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
FieldReader innerListReader = listReader.reader();
for (int k = 0; k < i % 13; k++) {
innerListReader.next();
assertEquals(k, innerListReader.reader().readInteger().intValue(), "record: " + i);
}
}
}
}
@Test
public void unionListListType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
ListWriter innerListWriter = listWriter.list();
innerListWriter.startList();
for (int k = 0; k < i % 13; k++) {
if (k % 2 == 0) {
innerListWriter.integer().writeInt(k);
} else {
innerListWriter.bigInt().writeBigInt(k);
}
}
innerListWriter.endList();
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
checkUnionList(listVector);
}
}
/**
* This test is similar to {@link #unionListListType()} but we get the inner list writer once at the beginning.
*/
@Test
public void unionListListType2() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
ListWriter innerListWriter = listWriter.list();
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
innerListWriter.startList();
for (int k = 0; k < i % 13; k++) {
if (k % 2 == 0) {
innerListWriter.integer().writeInt(k);
} else {
innerListWriter.bigInt().writeBigInt(k);
}
}
innerListWriter.endList();
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
checkUnionList(listVector);
}
}
private void checkUnionList(ListVector listVector) {
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
FieldReader innerListReader = listReader.reader();
for (int k = 0; k < i % 13; k++) {
innerListReader.next();
if (k % 2 == 0) {
assertEquals(k, innerListReader.reader().readInteger().intValue(), "record: " + i);
} else {
assertEquals(k, innerListReader.reader().readLong().longValue(), "record: " + i);
}
}
}
}
}
@Test
public void testListMapType() {
try (ListVector listVector = ListVector.empty("list", allocator)) {
listVector.allocateNew();
UnionListWriter listWriter = new UnionListWriter(listVector);
MapWriter innerMapWriter = listWriter.map(true);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
innerMapWriter.startMap();
for (int k = 0; k < i % 13; k++) {
innerMapWriter.startEntry();
innerMapWriter.key().integer().writeInt(k);
if (k % 2 == 0) {
innerMapWriter.value().bigInt().writeBigInt(k);
}
innerMapWriter.endEntry();
}
innerMapWriter.endMap();
}
listWriter.endList();
}
listWriter.setValueCount(COUNT);
checkListMap(listVector);
// Verify that the map vector has keysSorted = true
MapVector mapVector = (MapVector) listVector.getDataVector();
ArrowType arrowType = mapVector.getField().getFieldType().getType();
assertTrue(((ArrowType.Map) arrowType).getKeysSorted());
}
}
private void checkListMap(ListVector listVector) {
UnionListReader listReader = new UnionListReader(listVector);
for (int i = 0; i < COUNT; i++) {
listReader.setPosition(i);
for (int j = 0; j < i % 7; j++) {
listReader.next();
UnionMapReader mapReader = (UnionMapReader) listReader.reader();
for (int k = 0; k < i % 13; k++) {
mapReader.next();
assertEquals(k, mapReader.key().readInteger().intValue(), "record key: " + i);
if (k % 2 == 0) {
assertEquals(k, mapReader.value().readLong().longValue(), "record value: " + i);
} else {
assertNull(mapReader.value().readLong(), "record value: " + i);
}
}
}
}
}
@Test
public void simpleUnion() throws Exception {
List<ArrowBuf> bufs = new ArrayList<ArrowBuf>();
UnionVector vector = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null);
UnionWriter unionWriter = new UnionWriter(vector);
unionWriter.allocate();
for (int i = 0; i < COUNT; i++) {
unionWriter.setPosition(i);
if (i % 5 == 0) {
unionWriter.writeInt(i);
} else if (i % 5 == 1) {
TimeStampMilliTZHolder holder = new TimeStampMilliTZHolder();
holder.value = (long) i;
holder.timezone = "AsdfTimeZone";
unionWriter.write(holder);
} else if (i % 5 == 2) {
DurationHolder holder = new DurationHolder();
holder.value = (long) i;
holder.unit = TimeUnit.NANOSECOND;
unionWriter.write(holder);
} else if (i % 5 == 3) {
FixedSizeBinaryHolder holder = new FixedSizeBinaryHolder();
ArrowBuf buf = allocator.buffer(4);
buf.setInt(0, i);
holder.byteWidth = 4;
holder.buffer = buf;
unionWriter.write(holder);
bufs.add(buf);
} else {
unionWriter.writeFloat4((float) i);
}
}
vector.setValueCount(COUNT);
UnionReader unionReader = new UnionReader(vector);
for (int i = 0; i < COUNT; i++) {
unionReader.setPosition(i);
if (i % 5 == 0) {
assertEquals(i, unionReader.readInteger().intValue());
} else if (i % 5 == 1) {
NullableTimeStampMilliTZHolder holder = new NullableTimeStampMilliTZHolder();
unionReader.read(holder);
assertEquals(i, holder.value);
assertEquals("AsdfTimeZone", holder.timezone);
} else if (i % 5 == 2) {
NullableDurationHolder holder = new NullableDurationHolder();
unionReader.read(holder);
assertEquals(i, holder.value);
assertEquals(TimeUnit.NANOSECOND, holder.unit);
} else if (i % 5 == 3) {
NullableFixedSizeBinaryHolder holder = new NullableFixedSizeBinaryHolder();
unionReader.read(holder);
assertEquals(i, holder.buffer.getInt(0));
assertEquals(4, holder.byteWidth);
} else {
assertEquals((float) i, unionReader.readFloat(), 1e-12);
}
}
vector.close();
AutoCloseables.close(bufs);
}
@Test
public void promotableWriter() {
try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) {
ComplexWriter writer = new ComplexWriterImpl("root", parent);
StructWriter rootWriter = writer.rootAsStruct();
for (int i = 0; i < 100; i++) {
BigIntWriter bigIntWriter = rootWriter.bigInt("a");
bigIntWriter.setPosition(i);
bigIntWriter.writeBigInt(i);
}
Field field = parent.getField().getChildren().get(0).getChildren().get(0);
assertEquals("a", field.getName());
assertEquals(Int.TYPE_TYPE, field.getType().getTypeID());
Int intType = (Int) field.getType();
assertEquals(64, intType.getBitWidth());
assertTrue(intType.getIsSigned());
for (int i = 100; i < 200; i++) {
VarCharWriter varCharWriter = rootWriter.varChar("a");
varCharWriter.setPosition(i);
byte[] bytes = Integer.toString(i).getBytes(StandardCharsets.UTF_8);
ArrowBuf tempBuf = allocator.buffer(bytes.length);
tempBuf.setBytes(0, bytes);
varCharWriter.writeVarChar(0, bytes.length, tempBuf);
tempBuf.close();
}
field = parent.getField().getChildren().get(0).getChildren().get(0);
assertEquals("a", field.getName());
assertEquals(Union.TYPE_TYPE, field.getType().getTypeID());
assertEquals(Int.TYPE_TYPE, field.getChildren().get(0).getType().getTypeID());
assertEquals(Utf8.TYPE_TYPE, field.getChildren().get(1).getType().getTypeID());
StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
for (int i = 0; i < 100; i++) {
rootReader.setPosition(i);
FieldReader reader = rootReader.reader("a");
Long value = reader.readLong();
assertNotNull(value, "index: " + i);
assertEquals(i, value.intValue());
}
for (int i = 100; i < 200; i++) {
rootReader.setPosition(i);
FieldReader reader = rootReader.reader("a");
Text value = reader.readText();
assertEquals(Integer.toString(i), value.toString());
}
}
}
/**
* Even without writing to the writer, the union schema is created correctly.
*/
@Test
public void promotableWriterSchema() {
try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) {
ComplexWriter writer = new ComplexWriterImpl("root", parent);
StructWriter rootWriter = writer.rootAsStruct();
rootWriter.bigInt("a");
rootWriter.varChar("a");
Field field = parent.getField().getChildren().get(0).getChildren().get(0);
assertEquals("a", field.getName());
assertEquals(ArrowTypeID.Union, field.getType().getTypeID());
assertEquals(ArrowTypeID.Int, field.getChildren().get(0).getType().getTypeID());
Int intType = (Int) field.getChildren().get(0).getType();
assertEquals(64, intType.getBitWidth());
assertTrue(intType.getIsSigned());
assertEquals(ArrowTypeID.Utf8, field.getChildren().get(1).getType().getTypeID());
}
}
private Set<String> getFieldNames(List<Field> fields) {
Set<String> fieldNames = new HashSet<>();
for (Field field : fields) {
fieldNames.add(field.getName());
if (!field.getChildren().isEmpty()) {
for (String name : getFieldNames(field.getChildren())) {
fieldNames.add(field.getName() + "::" + name);
}
}
}
return fieldNames;
}
@Test
public void structWriterMixedCaseFieldNames() {
try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) {
// test case-sensitive StructWriter
ComplexWriter writer = new ComplexWriterImpl("rootCaseSensitive", parent, false, true);
StructWriter rootWriterCaseSensitive = writer.rootAsStruct();
rootWriterCaseSensitive.bigInt("int_field");
rootWriterCaseSensitive.bigInt("Int_Field");
rootWriterCaseSensitive.float4("float_field");
rootWriterCaseSensitive.float4("Float_Field");
StructWriter structFieldWriterCaseSensitive = rootWriterCaseSensitive.struct("struct_field");
structFieldWriterCaseSensitive.varChar("char_field");
structFieldWriterCaseSensitive.varChar("Char_Field");
ListWriter listFieldWriterCaseSensitive = rootWriterCaseSensitive.list("list_field");
StructWriter listStructFieldWriterCaseSensitive = listFieldWriterCaseSensitive.struct();
listStructFieldWriterCaseSensitive.bit("bit_field");
listStructFieldWriterCaseSensitive.bit("Bit_Field");
List<Field> fieldsCaseSensitive = parent.getField().getChildren().get(0).getChildren();
Set<String> fieldNamesCaseSensitive = getFieldNames(fieldsCaseSensitive);
assertEquals(11, fieldNamesCaseSensitive.size());
assertTrue(fieldNamesCaseSensitive.contains("int_field"));
assertTrue(fieldNamesCaseSensitive.contains("Int_Field"));
assertTrue(fieldNamesCaseSensitive.contains("float_field"));
assertTrue(fieldNamesCaseSensitive.contains("Float_Field"));
assertTrue(fieldNamesCaseSensitive.contains("struct_field"));
assertTrue(fieldNamesCaseSensitive.contains("struct_field::char_field"));
assertTrue(fieldNamesCaseSensitive.contains("struct_field::Char_Field"));
assertTrue(fieldNamesCaseSensitive.contains("list_field"));
assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$"));
assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::bit_field"));
assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::Bit_Field"));
// test case-insensitive StructWriter
ComplexWriter writerCaseInsensitive = new ComplexWriterImpl("rootCaseInsensitive", parent, false, false);
StructWriter rootWriterCaseInsensitive = writerCaseInsensitive.rootAsStruct();
rootWriterCaseInsensitive.bigInt("int_field");
rootWriterCaseInsensitive.bigInt("Int_Field");
rootWriterCaseInsensitive.float4("float_field");
rootWriterCaseInsensitive.float4("Float_Field");
StructWriter structFieldWriterCaseInsensitive = rootWriterCaseInsensitive.struct("struct_field");
structFieldWriterCaseInsensitive.varChar("char_field");
structFieldWriterCaseInsensitive.varChar("Char_Field");
ListWriter listFieldWriterCaseInsensitive = rootWriterCaseInsensitive.list("list_field");
StructWriter listStructFieldWriterCaseInsensitive = listFieldWriterCaseInsensitive.struct();
listStructFieldWriterCaseInsensitive.bit("bit_field");
listStructFieldWriterCaseInsensitive.bit("Bit_Field");
List<Field> fieldsCaseInsensitive = parent.getField().getChildren().get(1).getChildren();
Set<String> fieldNamesCaseInsensitive = getFieldNames(fieldsCaseInsensitive);
assertEquals(7, fieldNamesCaseInsensitive.size());
assertTrue(fieldNamesCaseInsensitive.contains("int_field"));
assertTrue(fieldNamesCaseInsensitive.contains("float_field"));
assertTrue(fieldNamesCaseInsensitive.contains("struct_field"));
assertTrue(fieldNamesCaseInsensitive.contains("struct_field::char_field"));
assertTrue(fieldNamesCaseSensitive.contains("list_field"));
assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$"));
assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::bit_field"));
}
}
@Test
public void timeStampSecWriter() throws Exception {
// test values
final long expectedSecs = 981173106L;
final LocalDateTime expectedSecDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 0);
try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) {
// write
ComplexWriter writer = new ComplexWriterImpl("root", parent);
StructWriter rootWriter = writer.rootAsStruct();
{
TimeStampSecWriter timeStampSecWriter = rootWriter.timeStampSec("sec");
timeStampSecWriter.setPosition(0);
timeStampSecWriter.writeTimeStampSec(expectedSecs);
}
{
TimeStampSecTZWriter timeStampSecTZWriter = rootWriter.timeStampSecTZ("secTZ", "UTC");
timeStampSecTZWriter.setPosition(1);
timeStampSecTZWriter.writeTimeStampSecTZ(expectedSecs);
}
// schema
List<Field> children = parent.getField().getChildren().get(0).getChildren();
checkTimestampField(children.get(0), "sec");
checkTimestampTZField(children.get(1), "secTZ", "UTC");
// read
StructReader rootReader = new SingleStructReaderImpl(parent).reader("root");
{
FieldReader secReader = rootReader.reader("sec");
secReader.setPosition(0);
LocalDateTime secDateTime = secReader.readLocalDateTime();
assertEquals(expectedSecDateTime, secDateTime);
long secLong = secReader.readLong();
assertEquals(expectedSecs, secLong);
}
{
FieldReader secTZReader = rootReader.reader("secTZ");
secTZReader.setPosition(1);
long secTZLong = secTZReader.readLong();
assertEquals(expectedSecs, secTZLong);
}
}
}
@Test
public void timeStampMilliWriters() throws Exception {
// test values
final long expectedMillis = 981173106123L;
final LocalDateTime expectedMilliDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123 * 1_000_000);