-
Notifications
You must be signed in to change notification settings - Fork 2k
/
GenericArrowVectorAccessorFactory.java
870 lines (744 loc) · 31.4 KB
/
GenericArrowVectorAccessorFactory.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
/*
* 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.iceberg.arrow.vectorized;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.BitVector;
import org.apache.arrow.vector.DateDayVector;
import org.apache.arrow.vector.DecimalVector;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.FixedSizeBinaryVector;
import org.apache.arrow.vector.Float4Vector;
import org.apache.arrow.vector.Float8Vector;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.TimeMicroVector;
import org.apache.arrow.vector.TimeStampMicroTZVector;
import org.apache.arrow.vector.TimeStampMicroVector;
import org.apache.arrow.vector.ValueVector;
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.StructVector;
import org.apache.arrow.vector.util.DecimalUtility;
import org.apache.iceberg.parquet.ParquetUtil;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.parquet.column.ColumnDescriptor;
import org.apache.parquet.column.Dictionary;
import org.apache.parquet.schema.OriginalType;
import org.apache.parquet.schema.PrimitiveType;
/**
* This class is creates typed {@link ArrowVectorAccessor} from {@link VectorHolder}. It provides a
* generic implementation for following Arrow types:
*
* <ul>
* <li>Decimal type can be deserialized to a type that supports decimal, e.g. BigDecimal or
* Spark's Decimal.
* <li>UTF8 String type can deserialized to a Java String or Spark's UTF8String.
* <li>List type: the child elements of a list can be deserialized to Spark's ColumnarArray or
* similar type.
* <li>Struct type: the child elements of a struct can be deserialized to a Spark's
* ArrowColumnVector or similar type.
* </ul>
*
* @param <DecimalT> A concrete type that can represent a decimal.
* @param <Utf8StringT> A concrete type that can represent a UTF8 string.
* @param <ArrayT> A concrete type that can represent an array value in a list vector, e.g. Spark's
* ColumnarArray.
* @param <ChildVectorT> A concrete type that can represent a child vector in a struct, e.g. Spark's
* ArrowColumnVector.
*/
public class GenericArrowVectorAccessorFactory<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable> {
private final Supplier<DecimalFactory<DecimalT>> decimalFactorySupplier;
private final Supplier<StringFactory<Utf8StringT>> stringFactorySupplier;
private final Supplier<StructChildFactory<ChildVectorT>> structChildFactorySupplier;
private final Supplier<ArrayFactory<ChildVectorT, ArrayT>> arrayFactorySupplier;
/**
* The constructor is parameterized using the decimal, string, struct and array factories. If a
* specific type is not supported, the factory supplier can raise an {@link
* UnsupportedOperationException}.
*/
protected GenericArrowVectorAccessorFactory(
Supplier<DecimalFactory<DecimalT>> decimalFactorySupplier,
Supplier<StringFactory<Utf8StringT>> stringFactorySupplier,
Supplier<StructChildFactory<ChildVectorT>> structChildFactorySupplier,
Supplier<ArrayFactory<ChildVectorT, ArrayT>> arrayFactorySupplier) {
this.decimalFactorySupplier = decimalFactorySupplier;
this.stringFactorySupplier = stringFactorySupplier;
this.structChildFactorySupplier = structChildFactorySupplier;
this.arrayFactorySupplier = arrayFactorySupplier;
}
public ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> getVectorAccessor(
VectorHolder holder) {
Dictionary dictionary = holder.dictionary();
boolean isVectorDictEncoded = holder.isDictionaryEncoded();
FieldVector vector = holder.vector();
ColumnDescriptor desc = holder.descriptor();
// desc could be null when the holder is ConstantVectorHolder/PositionVectorHolder
PrimitiveType primitive = desc == null ? null : desc.getPrimitiveType();
if (isVectorDictEncoded) {
return getDictionaryVectorAccessor(dictionary, desc, vector, primitive);
} else {
return getPlainVectorAccessor(vector, primitive);
}
}
private ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT>
getDictionaryVectorAccessor(
Dictionary dictionary,
ColumnDescriptor desc,
FieldVector vector,
PrimitiveType primitive) {
Preconditions.checkState(
vector instanceof IntVector, "Dictionary ids should be stored in IntVectors only");
if (primitive.getOriginalType() != null) {
switch (desc.getPrimitiveType().getOriginalType()) {
case ENUM:
case JSON:
case UTF8:
case BSON:
return new DictionaryStringAccessor<>(
(IntVector) vector, dictionary, stringFactorySupplier.get());
case INT_64:
case TIME_MICROS:
case TIMESTAMP_MILLIS:
case TIMESTAMP_MICROS:
return new DictionaryLongAccessor<>((IntVector) vector, dictionary);
case DECIMAL:
switch (primitive.getPrimitiveTypeName()) {
case BINARY:
case FIXED_LEN_BYTE_ARRAY:
return new DictionaryDecimalBinaryAccessor<>(
(IntVector) vector, dictionary, decimalFactorySupplier.get());
case INT64:
return new DictionaryDecimalLongAccessor<>(
(IntVector) vector, dictionary, decimalFactorySupplier.get());
case INT32:
return new DictionaryDecimalIntAccessor<>(
(IntVector) vector, dictionary, decimalFactorySupplier.get());
default:
throw new UnsupportedOperationException(
"Unsupported base type for decimal: " + primitive.getPrimitiveTypeName());
}
default:
throw new UnsupportedOperationException(
"Unsupported logical type: " + primitive.getOriginalType());
}
} else {
switch (primitive.getPrimitiveTypeName()) {
case FIXED_LEN_BYTE_ARRAY:
case BINARY:
return new DictionaryBinaryAccessor<>((IntVector) vector, dictionary);
case FLOAT:
return new DictionaryFloatAccessor<>((IntVector) vector, dictionary);
case INT64:
return new DictionaryLongAccessor<>((IntVector) vector, dictionary);
case INT96:
// Impala & Spark used to write timestamps as INT96 by default. For backwards
// compatibility we try to read INT96 as timestamps. But INT96 is not recommended
// and deprecated (see https://issues.apache.org/jira/browse/PARQUET-323)
return new DictionaryTimestampInt96Accessor<>((IntVector) vector, dictionary);
case DOUBLE:
return new DictionaryDoubleAccessor<>((IntVector) vector, dictionary);
default:
throw new UnsupportedOperationException("Unsupported type: " + primitive);
}
}
}
@SuppressWarnings("checkstyle:CyclomaticComplexity")
private ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> getPlainVectorAccessor(
FieldVector vector, PrimitiveType primitive) {
if (vector instanceof BitVector) {
return new BooleanAccessor<>((BitVector) vector);
} else if (vector instanceof IntVector) {
if (isDecimal(primitive)) {
return new IntBackedDecimalAccessor<>((IntVector) vector, decimalFactorySupplier.get());
}
return new IntAccessor<>((IntVector) vector);
} else if (vector instanceof BigIntVector) {
if (isDecimal(primitive)) {
return new LongBackedDecimalAccessor<>((BigIntVector) vector, decimalFactorySupplier.get());
}
return new LongAccessor<>((BigIntVector) vector);
} else if (vector instanceof Float4Vector) {
return new FloatAccessor<>((Float4Vector) vector);
} else if (vector instanceof Float8Vector) {
return new DoubleAccessor<>((Float8Vector) vector);
} else if (vector instanceof DecimalVector) {
return new DecimalAccessor<>((DecimalVector) vector, decimalFactorySupplier.get());
} else if (vector instanceof VarCharVector) {
return new StringAccessor<>((VarCharVector) vector, stringFactorySupplier.get());
} else if (vector instanceof VarBinaryVector) {
return new BinaryAccessor<>((VarBinaryVector) vector);
} else if (vector instanceof DateDayVector) {
return new DateAccessor<>((DateDayVector) vector);
} else if (vector instanceof TimeStampMicroTZVector) {
return new TimestampMicroTzAccessor<>((TimeStampMicroTZVector) vector);
} else if (vector instanceof TimeStampMicroVector) {
return new TimestampMicroAccessor<>((TimeStampMicroVector) vector);
} else if (vector instanceof ListVector) {
ListVector listVector = (ListVector) vector;
return new ArrayAccessor<>(listVector, arrayFactorySupplier.get());
} else if (vector instanceof StructVector) {
StructVector structVector = (StructVector) vector;
return new StructAccessor<>(structVector, structChildFactorySupplier.get());
} else if (vector instanceof TimeMicroVector) {
return new TimeMicroAccessor<>((TimeMicroVector) vector);
} else if (vector instanceof FixedSizeBinaryVector) {
if (isDecimal(primitive)) {
return new FixedSizeBinaryBackedDecimalAccessor<>(
(FixedSizeBinaryVector) vector, decimalFactorySupplier.get());
}
return new FixedSizeBinaryAccessor<>(
(FixedSizeBinaryVector) vector, stringFactorySupplier.get());
}
throw new UnsupportedOperationException("Unsupported vector: " + vector.getClass());
}
private static boolean isDecimal(PrimitiveType primitive) {
return primitive != null && OriginalType.DECIMAL.equals(primitive.getOriginalType());
}
private static class BooleanAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final BitVector vector;
BooleanAccessor(BitVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final boolean getBoolean(int rowId) {
return vector.get(rowId) == 1;
}
}
private static class IntAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector vector;
IntAccessor(IntVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final int getInt(int rowId) {
return vector.get(rowId);
}
@Override
public final long getLong(int rowId) {
return getInt(rowId);
}
}
private static class LongAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final BigIntVector vector;
LongAccessor(BigIntVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final long getLong(int rowId) {
return vector.get(rowId);
}
}
private static class DictionaryLongAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector offsetVector;
private final Dictionary dictionary;
DictionaryLongAccessor(IntVector vector, Dictionary dictionary) {
super(vector);
this.offsetVector = vector;
this.dictionary = dictionary;
}
@Override
public final long getLong(int rowId) {
return dictionary.decodeToLong(offsetVector.get(rowId));
}
}
private static class FloatAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final Float4Vector vector;
FloatAccessor(Float4Vector vector) {
super(vector);
this.vector = vector;
}
@Override
public final float getFloat(int rowId) {
return vector.get(rowId);
}
@Override
public final double getDouble(int rowId) {
return getFloat(rowId);
}
}
private static class DictionaryFloatAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector offsetVector;
private final Dictionary dictionary;
DictionaryFloatAccessor(IntVector vector, Dictionary dictionary) {
super(vector);
this.offsetVector = vector;
this.dictionary = dictionary;
}
@Override
public final float getFloat(int rowId) {
return dictionary.decodeToFloat(offsetVector.get(rowId));
}
@Override
public final double getDouble(int rowId) {
return getFloat(rowId);
}
}
private static class DoubleAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final Float8Vector vector;
DoubleAccessor(Float8Vector vector) {
super(vector);
this.vector = vector;
}
@Override
public final double getDouble(int rowId) {
return vector.get(rowId);
}
}
private static class DictionaryDoubleAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector offsetVector;
private final Dictionary dictionary;
DictionaryDoubleAccessor(IntVector vector, Dictionary dictionary) {
super(vector);
this.offsetVector = vector;
this.dictionary = dictionary;
}
@Override
public final double getDouble(int rowId) {
return dictionary.decodeToDouble(offsetVector.get(rowId));
}
}
private static class StringAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final VarCharVector vector;
private final StringFactory<Utf8StringT> stringFactory;
StringAccessor(VarCharVector vector, StringFactory<Utf8StringT> stringFactory) {
super(vector);
this.vector = vector;
this.stringFactory = stringFactory;
}
@Override
public final Utf8StringT getUTF8String(int rowId) {
return stringFactory.ofRow(vector, rowId);
}
}
private static class DictionaryStringAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final Dictionary dictionary;
private final StringFactory<Utf8StringT> stringFactory;
private final IntVector offsetVector;
private final Utf8StringT[] cache;
DictionaryStringAccessor(
IntVector vector, Dictionary dictionary, StringFactory<Utf8StringT> stringFactory) {
super(vector);
this.offsetVector = vector;
this.dictionary = dictionary;
this.stringFactory = stringFactory;
this.cache = genericArray(stringFactory.getGenericClass(), dictionary.getMaxId() + 1);
}
@Override
public final Utf8StringT getUTF8String(int rowId) {
int offset = offsetVector.get(rowId);
if (cache[offset] == null) {
cache[offset] =
stringFactory.ofByteBuffer(dictionary.decodeToBinary(offset).toByteBuffer());
}
return cache[offset];
}
}
private static class BinaryAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final VarBinaryVector vector;
BinaryAccessor(VarBinaryVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final byte[] getBinary(int rowId) {
return vector.get(rowId);
}
}
private static class DictionaryBinaryAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector offsetVector;
private final Dictionary dictionary;
DictionaryBinaryAccessor(IntVector vector, Dictionary dictionary) {
super(vector);
this.offsetVector = vector;
this.dictionary = dictionary;
}
@Override
public final byte[] getBinary(int rowId) {
return dictionary.decodeToBinary(offsetVector.get(rowId)).getBytes();
}
}
private static class DictionaryTimestampInt96Accessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector offsetVector;
private final Dictionary dictionary;
DictionaryTimestampInt96Accessor(IntVector vector, Dictionary dictionary) {
super(vector);
this.offsetVector = vector;
this.dictionary = dictionary;
}
@Override
public final long getLong(int rowId) {
ByteBuffer byteBuffer =
dictionary
.decodeToBinary(offsetVector.get(rowId))
.toByteBuffer()
.order(ByteOrder.LITTLE_ENDIAN);
return ParquetUtil.extractTimestampInt96(byteBuffer);
}
}
private static class DateAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final DateDayVector vector;
DateAccessor(DateDayVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final int getInt(int rowId) {
return vector.get(rowId);
}
}
private static class TimestampMicroTzAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final TimeStampMicroTZVector vector;
TimestampMicroTzAccessor(TimeStampMicroTZVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final long getLong(int rowId) {
return vector.get(rowId);
}
}
private static class TimestampMicroAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final TimeStampMicroVector vector;
TimestampMicroAccessor(TimeStampMicroVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final long getLong(int rowId) {
return vector.get(rowId);
}
}
private static class TimeMicroAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final TimeMicroVector vector;
TimeMicroAccessor(TimeMicroVector vector) {
super(vector);
this.vector = vector;
}
@Override
public final long getLong(int rowId) {
return vector.get(rowId);
}
}
private static class FixedSizeBinaryAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final FixedSizeBinaryVector vector;
private final StringFactory<Utf8StringT> stringFactory;
FixedSizeBinaryAccessor(FixedSizeBinaryVector vector) {
super(vector);
this.vector = vector;
this.stringFactory = null;
}
FixedSizeBinaryAccessor(
FixedSizeBinaryVector vector, StringFactory<Utf8StringT> stringFactory) {
super(vector);
this.vector = vector;
this.stringFactory = stringFactory;
}
@Override
public byte[] getBinary(int rowId) {
return vector.get(rowId);
}
@Override
public Utf8StringT getUTF8String(int rowId) {
return null == stringFactory
? super.getUTF8String(rowId)
: stringFactory.ofRow(vector, rowId);
}
}
private static class ArrayAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final ListVector vector;
private final ChildVectorT arrayData;
private final ArrayFactory<ChildVectorT, ArrayT> arrayFactory;
ArrayAccessor(ListVector vector, ArrayFactory<ChildVectorT, ArrayT> arrayFactory) {
super(vector);
this.vector = vector;
this.arrayFactory = arrayFactory;
this.arrayData = arrayFactory.ofChild(vector.getDataVector());
}
@Override
public final ArrayT getArray(int rowId) {
return arrayFactory.ofRow(vector, arrayData, rowId);
}
}
private static class StructAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
StructAccessor(StructVector structVector, StructChildFactory<ChildVectorT> structChildFactory) {
super(
structVector,
IntStream.range(0, structVector.size())
.mapToObj(structVector::getVectorById)
.map(structChildFactory::of)
.toArray(genericArray(structChildFactory.getGenericClass())));
}
}
private static class DecimalAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final DecimalVector vector;
private final DecimalFactory<DecimalT> decimalFactory;
DecimalAccessor(DecimalVector vector, DecimalFactory<DecimalT> decimalFactory) {
super(vector);
this.vector = vector;
this.decimalFactory = decimalFactory;
}
@Override
public final DecimalT getDecimal(int rowId, int precision, int scale) {
return decimalFactory.ofBigDecimal(
DecimalUtility.getBigDecimalFromArrowBuf(
vector.getDataBuffer(), rowId, scale, DecimalVector.TYPE_WIDTH),
precision,
scale);
}
}
private static class IntBackedDecimalAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final IntVector vector;
private final DecimalFactory<DecimalT> decimalFactory;
IntBackedDecimalAccessor(IntVector vector, DecimalFactory<DecimalT> decimalFactory) {
super(vector);
this.vector = vector;
this.decimalFactory = decimalFactory;
}
@Override
public final DecimalT getDecimal(int rowId, int precision, int scale) {
return decimalFactory.ofLong(vector.get(rowId), precision, scale);
}
}
private static class LongBackedDecimalAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final BigIntVector vector;
private final DecimalFactory<DecimalT> decimalFactory;
LongBackedDecimalAccessor(BigIntVector vector, DecimalFactory<DecimalT> decimalFactory) {
super(vector);
this.vector = vector;
this.decimalFactory = decimalFactory;
}
@Override
public final DecimalT getDecimal(int rowId, int precision, int scale) {
return decimalFactory.ofLong(vector.get(rowId), precision, scale);
}
}
private static class FixedSizeBinaryBackedDecimalAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final FixedSizeBinaryVector vector;
private final DecimalFactory<DecimalT> decimalFactory;
FixedSizeBinaryBackedDecimalAccessor(
FixedSizeBinaryVector vector, DecimalFactory<DecimalT> decimalFactory) {
super(vector);
this.vector = vector;
this.decimalFactory = decimalFactory;
}
@Override
public final DecimalT getDecimal(int rowId, int precision, int scale) {
byte[] bytes = vector.get(rowId);
BigInteger bigInteger = new BigInteger(bytes);
BigDecimal javaDecimal = new BigDecimal(bigInteger, scale);
return decimalFactory.ofBigDecimal(javaDecimal, precision, scale);
}
}
@SuppressWarnings("checkstyle:VisibilityModifier")
private abstract static class DictionaryDecimalAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends ArrowVectorAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
private final DecimalT[] cache;
private final IntVector offsetVector;
protected final DecimalFactory<DecimalT> decimalFactory;
protected final Dictionary parquetDictionary;
private DictionaryDecimalAccessor(
IntVector vector, Dictionary dictionary, DecimalFactory<DecimalT> decimalFactory) {
super(vector);
this.offsetVector = vector;
this.parquetDictionary = dictionary;
this.decimalFactory = decimalFactory;
this.cache = genericArray(decimalFactory.getGenericClass(), dictionary.getMaxId() + 1);
}
@Override
public final DecimalT getDecimal(int rowId, int precision, int scale) {
int offset = offsetVector.get(rowId);
if (cache[offset] == null) {
cache[offset] = decode(offset, precision, scale);
}
return cache[offset];
}
protected abstract DecimalT decode(int dictId, int precision, int scale);
}
private static class DictionaryDecimalBinaryAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends DictionaryDecimalAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
DictionaryDecimalBinaryAccessor(
IntVector vector, Dictionary dictionary, DecimalFactory<DecimalT> decimalFactory) {
super(vector, dictionary, decimalFactory);
}
@Override
protected DecimalT decode(int dictId, int precision, int scale) {
ByteBuffer byteBuffer = parquetDictionary.decodeToBinary(dictId).toByteBuffer();
BigDecimal value =
DecimalUtility.getBigDecimalFromByteBuffer(byteBuffer, scale, byteBuffer.remaining());
return decimalFactory.ofBigDecimal(value, precision, scale);
}
}
private static class DictionaryDecimalLongAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends DictionaryDecimalAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
DictionaryDecimalLongAccessor(
IntVector vector, Dictionary dictionary, DecimalFactory<DecimalT> decimalFactory) {
super(vector, dictionary, decimalFactory);
}
@Override
protected DecimalT decode(int dictId, int precision, int scale) {
return decimalFactory.ofLong(parquetDictionary.decodeToLong(dictId), precision, scale);
}
}
private static class DictionaryDecimalIntAccessor<
DecimalT, Utf8StringT, ArrayT, ChildVectorT extends AutoCloseable>
extends DictionaryDecimalAccessor<DecimalT, Utf8StringT, ArrayT, ChildVectorT> {
DictionaryDecimalIntAccessor(
IntVector vector, Dictionary dictionary, DecimalFactory<DecimalT> decimalFactory) {
super(vector, dictionary, decimalFactory);
}
@Override
protected DecimalT decode(int dictId, int precision, int scale) {
return decimalFactory.ofLong(parquetDictionary.decodeToInt(dictId), precision, scale);
}
}
/**
* Create a decimal value of type {@code DecimalT} from arrow vector value.
*
* @param <DecimalT> A concrete type that can represent a decimal, e.g, Spark's Decimal.
*/
protected interface DecimalFactory<DecimalT> {
/** Class of concrete decimal type. */
Class<DecimalT> getGenericClass();
/** Create a decimal from the given long value, precision and scale. */
DecimalT ofLong(long value, int precision, int scale);
/** Create a decimal from the given {@link BigDecimal} value, precision and scale. */
DecimalT ofBigDecimal(BigDecimal value, int precision, int scale);
}
/**
* Create a UTF8 String value of type {@code Utf8StringT} from arrow vector value.
*
* @param <Utf8StringT> A concrete type that can represent a UTF8 string.
*/
protected interface StringFactory<Utf8StringT> {
/** Class of concrete UTF8 String type. */
Class<Utf8StringT> getGenericClass();
/** Create a UTF8 String from the row value in the arrow vector. */
Utf8StringT ofRow(VarCharVector vector, int rowId);
/** Create a UTF8 String from the row value in the FixedSizeBinaryVector vector. */
default Utf8StringT ofRow(FixedSizeBinaryVector vector, int rowId) {
throw new UnsupportedOperationException(
String.format(
"Creating %s from a FixedSizeBinaryVector is not supported",
getGenericClass().getSimpleName()));
}
/** Create a UTF8 String from the byte array. */
Utf8StringT ofBytes(byte[] bytes);
/** Create a UTF8 String from the byte buffer. */
Utf8StringT ofByteBuffer(ByteBuffer byteBuffer);
}
/**
* Create an array value of type {@code ArrayT} from arrow vector value.
*
* @param <ArrayT> A concrete type that can represent an array value in a list vector, e.g.
* Spark's ColumnarArray.
* @param <ChildVectorT> A concrete type that can represent a child vector in a struct, e.g.
* Spark's ArrowColumnVector.
*/
protected interface ArrayFactory<ChildVectorT, ArrayT> {
/** Create a child vector of type {@code ChildVectorT} from the arrow child vector. */
ChildVectorT ofChild(ValueVector childVector);
/** Create an Arrow of type {@code ArrayT} from the row value in the arrow child vector. */
ArrayT ofRow(ValueVector vector, ChildVectorT childData, int rowId);
}
/**
* Create a struct child vector of type {@code ChildVectorT} from arrow vector value.
*
* @param <ChildVectorT> A concrete type that can represent a child vector in a struct, e.g.
* Spark's ArrowColumnVector.
*/
protected interface StructChildFactory<ChildVectorT> {
/** Class of concrete child vector type. */
Class<ChildVectorT> getGenericClass();
/**
* Create the child vector of type such as Spark's ArrowColumnVector from the arrow child
* vector.
*/
ChildVectorT of(ValueVector childVector);
}
private static <T> IntFunction<T[]> genericArray(Class<T> genericClass) {
return length -> genericArray(genericClass, length);
}
@SuppressWarnings("unchecked")
private static <T> T[] genericArray(Class<T> genericClass, int length) {
return (T[]) Array.newInstance(genericClass, length);
}
}