forked from apache/parquet-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariantConverters.java
More file actions
746 lines (633 loc) · 25.3 KB
/
Copy pathVariantConverters.java
File metadata and controls
746 lines (633 loc) · 25.3 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
/*
* 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.parquet.variant;
import static org.apache.parquet.schema.LogicalTypeAnnotation.TimeUnit.MICROS;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.BINARY;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.BOOLEAN;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.DOUBLE;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FLOAT;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT32;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT64;
import static org.apache.parquet.schema.Type.Repetition.REPEATED;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import org.apache.parquet.Preconditions;
import org.apache.parquet.column.Dictionary;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.io.api.Converter;
import org.apache.parquet.io.api.GroupConverter;
import org.apache.parquet.io.api.PrimitiveConverter;
import org.apache.parquet.schema.GroupType;
import org.apache.parquet.schema.LogicalTypeAnnotation;
import org.apache.parquet.schema.LogicalTypeAnnotation.ListLogicalTypeAnnotation;
import org.apache.parquet.schema.PrimitiveType;
import org.apache.parquet.schema.Type;
public class VariantConverters {
// do not allow instantiating this class
private VariantConverters() {}
public interface ParentConverter<T extends VariantBuilder> {
void build(Consumer<T> buildConsumer);
}
public static GroupConverter newVariantConverter(
GroupType variantGroup, Consumer<ByteBuffer> metadata, ParentConverter<VariantBuilder> parent) {
ValueConverter converter = newValueConverter(variantGroup, parent);
// if there is a metadata field, add a converter for it
if (variantGroup.containsField("metadata")) {
int metadataIndex = variantGroup.getFieldIndex("metadata");
Type metadataField = variantGroup.getType("metadata");
Preconditions.checkArgument(isBinary(metadataField), "Invalid metadata field: " + metadataField);
converter.setConverter(metadataIndex, new VariantMetadataConverter(metadata));
}
return converter;
}
static ValueConverter newValueConverter(GroupType valueGroup, ParentConverter<VariantBuilder> parent) {
if (valueGroup.containsField("typed_value")) {
Type typedValueField = valueGroup.getType("typed_value");
if (isShreddedObject(typedValueField)) {
return new ShreddedObjectConverter(valueGroup, parent);
}
}
return new ShreddedValueConverter(valueGroup, parent);
}
/**
* Converter for metadata
*/
static class VariantMetadataConverter extends BinaryConverter {
private final Consumer<ByteBuffer> handleMetadata;
public VariantMetadataConverter(Consumer<ByteBuffer> handleMetadata) {
this.handleMetadata = handleMetadata;
}
@Override
protected void handleBinary(Binary value) {
handleMetadata.accept(value.toByteBuffer());
}
}
/**
* Converter for a non-object value field; appends the encoded value to the builder
*/
static class VariantValueConverter extends BinaryConverter {
private final ParentConverter<VariantBuilder> parent;
public VariantValueConverter(ParentConverter<VariantBuilder> parent) {
this.parent = parent;
}
@Override
protected void handleBinary(Binary value) {
parent.build(builder -> builder.appendEncodedValue(value.toByteBuffer()));
}
}
static class ValueConverter extends GroupConverter {
private final Converter[] converters;
ValueConverter(GroupType group) {
this.converters = new Converter[group.getFieldCount()];
}
protected void setConverter(int index, Converter converter) {
this.converters[index] = converter;
}
@Override
public Converter getConverter(int fieldIndex) {
return converters[fieldIndex];
}
@Override
public void start() {}
@Override
public void end() {}
}
static class ShreddedValueConverter extends ValueConverter {
ShreddedValueConverter(GroupType group, ParentConverter<VariantBuilder> parent) {
super(group);
if (group.containsField("typed_value")) {
int typedValueIndex = group.getFieldIndex("typed_value");
Type valueField = group.getType(typedValueIndex);
if (valueField.isPrimitive()) {
setConverter(typedValueIndex, newScalarConverter(valueField.asPrimitiveType(), parent));
} else if (isShreddedArray(valueField)) {
setConverter(typedValueIndex, new ShreddedArrayConverter(valueField.asGroupType(), parent));
}
}
if (group.containsField("value")) {
int valueIndex = group.getFieldIndex("value");
Type typedField = group.getType(valueIndex);
Preconditions.checkArgument(
typedField.isPrimitive() && typedField.asPrimitiveType().getPrimitiveTypeName() == BINARY,
"Invalid variant value type: " + typedField);
setConverter(valueIndex, new VariantValueConverter(parent));
}
}
}
static class ShreddedObjectConverter extends ValueConverter {
private final ParentConverter<VariantBuilder> parent;
ShreddedObjectConverter(GroupType group, ParentConverter<VariantBuilder> parent) {
super(group);
this.parent = parent;
int typedValueIndex = group.getFieldIndex("typed_value");
Type typedField = group.getType(typedValueIndex);
Preconditions.checkArgument(
isShreddedObject(typedField), "Invalid typed_value for shredded object: " + typedField);
PartiallyShreddedFieldsConverter fieldsConverter =
new PartiallyShreddedFieldsConverter(typedField.asGroupType(), parent);
setConverter(typedValueIndex, fieldsConverter);
if (group.containsField("value")) {
int valueIndex = group.getFieldIndex("value");
Type valueField = group.getType(valueIndex);
Preconditions.checkArgument(isBinary(valueField), "Invalid variant value type: " + valueField);
setConverter(
valueIndex, new PartiallyShreddedValueConverter(parent, fieldsConverter.shreddedFieldNames()));
}
}
@Override
public void end() {
parent.build(VariantBuilder::endObjectIfExists);
}
}
/**
* Converter for a partially shredded object's value field; copies object fields or appends an encoded non-object.
*
* <p>This must be wrapped by {@link ShreddedObjectConverter} to finalize the object.
*/
static class PartiallyShreddedValueConverter extends BinaryConverter {
private final ParentConverter<VariantBuilder> parent;
private final Set<String> suppressedKeys;
public PartiallyShreddedValueConverter(ParentConverter<VariantBuilder> parent, Set<String> suppressedKeys) {
this.parent = parent;
this.suppressedKeys = suppressedKeys;
}
@Override
protected void handleBinary(Binary value) {
parent.build(builder -> {
ByteBuffer buffer = value.toByteBuffer();
if (VariantUtil.getType(buffer) == Variant.Type.OBJECT) {
builder.startOrContinuePartialObject(value.toByteBuffer(), suppressedKeys);
} else {
builder.appendEncodedValue(buffer);
}
});
}
}
/**
* Converter for a shredded object's shredded fields.
*
* <p>This must be wrapped by {@link ShreddedObjectConverter} to finalize the object.
*/
static class PartiallyShreddedFieldsConverter extends GroupConverter {
private final Converter[] converters;
private final ParentConverter<VariantBuilder> parent;
private final Set<String> shreddedFieldNames = new HashSet<>();
private VariantObjectBuilder objectBuilder = null;
PartiallyShreddedFieldsConverter(GroupType fieldsType, ParentConverter<VariantBuilder> parent) {
this.converters = new Converter[fieldsType.getFieldCount()];
this.parent = parent;
ParentConverter<VariantObjectBuilder> newParent = converter -> converter.accept(objectBuilder);
for (int index = 0; index < fieldsType.getFieldCount(); index += 1) {
Type field = fieldsType.getType(index);
Preconditions.checkArgument(!field.isPrimitive(), "Invalid field group: " + field);
String name = field.getName();
shreddedFieldNames.add(name);
converters[index] = new FieldValueConverter(name, field.asGroupType(), newParent);
}
}
private Set<String> shreddedFieldNames() {
return shreddedFieldNames;
}
@Override
public Converter getConverter(int fieldIndex) {
return converters[fieldIndex];
}
@Override
public void start() {
try {
// the builder may already have an object builder from the value field
parent.build(builder -> this.objectBuilder = builder.startOrContinueObject());
} catch (IllegalStateException e) {
throw new IllegalStateException("Invalid variant, conflicting value and typed_value", e);
}
}
@Override
public void end() {
this.objectBuilder = null;
}
private static class FieldValueConverter extends GroupConverter {
private final String fieldName;
private final GroupConverter converter;
private final ParentConverter<VariantObjectBuilder> parent;
private Long numFields = null;
FieldValueConverter(String fieldName, GroupType field, ParentConverter<VariantObjectBuilder> parent) {
this.fieldName = fieldName;
this.converter = newValueConverter(field.asGroupType(), consumer -> parent.build(consumer::accept));
this.parent = parent;
}
@Override
public Converter getConverter(int fieldIndex) {
return converter.getConverter(fieldIndex);
}
@Override
public void start() {
converter.start();
parent.build(builder -> {
this.numFields = builder.numValues;
builder.appendKey(fieldName);
});
}
@Override
public void end() {
parent.build(builder -> {
if (builder.numValues == numFields) {
builder.dropLastKey();
}
});
this.numFields = null;
converter.end();
}
}
}
/**
* Converter for a shredded array
*/
static class ShreddedArrayConverter extends GroupConverter {
private final ParentConverter<? extends VariantBuilder> parent;
private final ShreddedArrayRepeatedConverter repeatedConverter;
private VariantArrayBuilder arrayBuilder;
public ShreddedArrayConverter(GroupType list, ParentConverter<? extends VariantBuilder> parent) {
Preconditions.checkArgument(list.getFieldCount() == 1, "Invalid list type: " + list);
this.parent = parent;
Type repeated = list.getType(0);
Preconditions.checkArgument(
repeated.isRepetition(REPEATED)
&& !repeated.isPrimitive()
&& repeated.asGroupType().getFieldCount() == 1,
"Invalid repeated type in list: " + repeated);
this.repeatedConverter = new ShreddedArrayRepeatedConverter(
repeated.asGroupType(), consumer -> consumer.accept(arrayBuilder));
}
@Override
public Converter getConverter(int fieldIndex) {
if (fieldIndex > 1) {
throw new ArrayIndexOutOfBoundsException(fieldIndex);
}
return repeatedConverter;
}
@Override
public void start() {
try {
parent.build(builder -> this.arrayBuilder = builder.startArray());
} catch (IllegalStateException e) {
throw new IllegalStateException("Invalid variant, conflicting value and typed_value", e);
}
}
@Override
public void end() {
parent.build(VariantBuilder::endArray);
this.arrayBuilder = null;
}
}
/**
* Converter for the repeated field of a shredded array
*/
static class ShreddedArrayRepeatedConverter extends GroupConverter {
private final GroupConverter elementConverter;
private final ParentConverter<VariantArrayBuilder> parent;
private Long numValues = null;
public ShreddedArrayRepeatedConverter(GroupType repeated, ParentConverter<VariantArrayBuilder> parent) {
Type element = repeated.getType(0);
Preconditions.checkArgument(!element.isPrimitive(), "Invalid element type in variant array: " + element);
ParentConverter<VariantBuilder> newParent = consumer -> parent.build(consumer::accept);
this.elementConverter = newValueConverter(element.asGroupType(), newParent);
this.parent = parent;
}
@Override
public Converter getConverter(int fieldIndex) {
if (fieldIndex > 1) {
throw new ArrayIndexOutOfBoundsException(fieldIndex);
}
return elementConverter;
}
@Override
public void start() {
parent.build(builder -> this.numValues = builder.numValues());
}
@Override
public void end() {
parent.build(builder -> {
long valuesWritten = builder.numValues() - this.numValues;
if (valuesWritten > 1) {
throw new IllegalStateException("Invalid variant, conflicting value and typed_value");
} else if (valuesWritten == 0) {
builder.appendNull();
}
});
}
}
// Return an appropriate converter for the given Parquet type.
static Converter newScalarConverter(PrimitiveType primitive, ParentConverter<VariantBuilder> parent) {
ShreddedScalarConverter typedConverter = null;
LogicalTypeAnnotation annotation = primitive.getLogicalTypeAnnotation();
PrimitiveType.PrimitiveTypeName primitiveType = primitive.getPrimitiveTypeName();
if (primitiveType == BOOLEAN) {
typedConverter = new VariantBooleanConverter(parent);
} else if (annotation instanceof LogicalTypeAnnotation.IntLogicalTypeAnnotation) {
LogicalTypeAnnotation.IntLogicalTypeAnnotation intAnnotation =
(LogicalTypeAnnotation.IntLogicalTypeAnnotation) annotation;
if (!intAnnotation.isSigned()) {
throw new UnsupportedOperationException("Unsupported shredded value type: " + intAnnotation);
}
int width = intAnnotation.getBitWidth();
if (width == 8) {
typedConverter = new VariantByteConverter(parent);
} else if (width == 16) {
typedConverter = new VariantShortConverter(parent);
} else if (width == 32) {
typedConverter = new VariantIntConverter(parent);
} else if (width == 64) {
typedConverter = new VariantLongConverter(parent);
} else {
throw new UnsupportedOperationException("Unsupported shredded value type: " + intAnnotation);
}
} else if (annotation == null && primitiveType == INT32) {
typedConverter = new VariantIntConverter(parent);
} else if (annotation == null && primitiveType == INT64) {
typedConverter = new VariantLongConverter(parent);
} else if (primitiveType == FLOAT) {
typedConverter = new VariantFloatConverter(parent);
} else if (primitiveType == DOUBLE) {
typedConverter = new VariantDoubleConverter(parent);
} else if (annotation instanceof LogicalTypeAnnotation.DecimalLogicalTypeAnnotation) {
LogicalTypeAnnotation.DecimalLogicalTypeAnnotation decimalType =
(LogicalTypeAnnotation.DecimalLogicalTypeAnnotation) annotation;
typedConverter = new VariantDecimalConverter(parent, decimalType.getScale());
} else if (annotation instanceof LogicalTypeAnnotation.DateLogicalTypeAnnotation) {
typedConverter = new VariantDateConverter(parent);
} else if (annotation instanceof LogicalTypeAnnotation.TimestampLogicalTypeAnnotation) {
LogicalTypeAnnotation.TimestampLogicalTypeAnnotation timestampType =
(LogicalTypeAnnotation.TimestampLogicalTypeAnnotation) annotation;
if (timestampType.isAdjustedToUTC()) {
switch (timestampType.getUnit()) {
case MILLIS:
throw new UnsupportedOperationException("MILLIS not supported by Variant");
case MICROS:
typedConverter = new VariantTimestampConverter(parent);
break;
case NANOS:
typedConverter = new VariantTimestampNanosConverter(parent);
break;
}
} else {
switch (timestampType.getUnit()) {
case MILLIS:
throw new UnsupportedOperationException("MILLIS not supported by Variant");
case MICROS:
typedConverter = new VariantTimestampNtzConverter(parent);
break;
case NANOS:
typedConverter = new VariantTimestampNanosNtzConverter(parent);
break;
}
}
} else if (annotation instanceof LogicalTypeAnnotation.TimeLogicalTypeAnnotation) {
LogicalTypeAnnotation.TimeLogicalTypeAnnotation timeType =
(LogicalTypeAnnotation.TimeLogicalTypeAnnotation) annotation;
if (timeType.isAdjustedToUTC() || timeType.getUnit() != MICROS) {
throw new UnsupportedOperationException(timeType + " not supported by Variant");
} else {
typedConverter = new VariantTimeConverter(parent);
}
} else if (annotation instanceof LogicalTypeAnnotation.UUIDLogicalTypeAnnotation) {
typedConverter = new VariantUUIDConverter(parent);
} else if (annotation instanceof LogicalTypeAnnotation.StringLogicalTypeAnnotation) {
typedConverter = new VariantStringConverter(parent);
} else if (primitiveType == BINARY) {
typedConverter = new VariantBinaryConverter(parent);
} else {
throw new UnsupportedOperationException("Unsupported shredded value type: " + primitive);
}
return typedConverter;
}
// Base class for converting primitive typed_value fields.
static class ShreddedScalarConverter extends PrimitiveConverter {
protected ParentConverter<VariantBuilder> parent;
ShreddedScalarConverter(ParentConverter<VariantBuilder> parent) {
this.parent = parent;
}
}
static class VariantStringConverter extends ShreddedScalarConverter {
VariantStringConverter(ParentConverter<VariantBuilder> parent) {
super(parent);
}
@Override
public void addBinary(Binary value) {
parent.build(builder -> builder.appendAsString(value));
}
}
static class VariantBinaryConverter extends ShreddedScalarConverter {
VariantBinaryConverter(ParentConverter<VariantBuilder> parent) {
super(parent);
}
@Override
public void addBinary(Binary value) {
parent.build(builder -> builder.appendBinary(value.toByteBuffer()));
}
}
static class VariantDecimalConverter extends ShreddedScalarConverter {
private int scale;
VariantDecimalConverter(ParentConverter<VariantBuilder> parent, int scale) {
super(parent);
this.scale = scale;
}
@Override
public void addBinary(Binary value) {
parent.build(builder -> builder.appendDecimal(new BigDecimal(new BigInteger(value.getBytes()), scale)));
}
@Override
public void addLong(long value) {
BigDecimal decimal = BigDecimal.valueOf(value, scale);
parent.build(builder -> builder.appendDecimal(decimal));
}
@Override
public void addInt(int value) {
BigDecimal decimal = BigDecimal.valueOf(value, scale);
parent.build(builder -> builder.appendDecimal(decimal));
}
}
static class VariantUUIDConverter extends ShreddedScalarConverter {
VariantUUIDConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addBinary(Binary value) {
parent.build(builder -> builder.appendUUIDBytes(value.toByteBuffer()));
}
}
static class VariantBooleanConverter extends ShreddedScalarConverter {
VariantBooleanConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addBoolean(boolean value) {
parent.build(builder -> builder.appendBoolean(value));
}
}
static class VariantDoubleConverter extends ShreddedScalarConverter {
VariantDoubleConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addDouble(double value) {
parent.build(builder -> builder.appendDouble(value));
}
}
static class VariantFloatConverter extends ShreddedScalarConverter {
VariantFloatConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addFloat(float value) {
parent.build(builder -> builder.appendFloat(value));
}
}
static class VariantByteConverter extends ShreddedScalarConverter {
VariantByteConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addInt(int value) {
parent.build(builder -> builder.appendByte((byte) value));
}
}
static class VariantShortConverter extends ShreddedScalarConverter {
VariantShortConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addInt(int value) {
parent.build(builder -> builder.appendShort((short) value));
}
}
static class VariantIntConverter extends ShreddedScalarConverter {
VariantIntConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addInt(int value) {
parent.build(builder -> builder.appendInt(value));
}
}
static class VariantLongConverter extends ShreddedScalarConverter {
VariantLongConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addLong(long value) {
parent.build(builder -> builder.appendLong(value));
}
}
static class VariantDateConverter extends ShreddedScalarConverter {
VariantDateConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addInt(int value) {
parent.build(builder -> builder.appendDate(value));
}
}
static class VariantTimeConverter extends ShreddedScalarConverter {
VariantTimeConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addLong(long value) {
parent.build(builder -> builder.appendTime(value));
}
}
static class VariantTimestampConverter extends ShreddedScalarConverter {
VariantTimestampConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addLong(long value) {
parent.build(builder -> builder.appendTimestampTz(value));
}
}
static class VariantTimestampNtzConverter extends ShreddedScalarConverter {
VariantTimestampNtzConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addLong(long value) {
parent.build(builder -> builder.appendTimestampNtz(value));
}
}
static class VariantTimestampNanosConverter extends ShreddedScalarConverter {
VariantTimestampNanosConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addLong(long value) {
parent.build(builder -> builder.appendTimestampNanosTz(value));
}
}
static class VariantTimestampNanosNtzConverter extends ShreddedScalarConverter {
VariantTimestampNanosNtzConverter(ParentConverter<VariantBuilder> parentBuilder) {
super(parentBuilder);
}
@Override
public void addLong(long value) {
parent.build(builder -> builder.appendTimestampNanosNtz(value));
}
}
/**
* Base class for value and metadata converters, that both return a binary.
*/
private abstract static class BinaryConverter extends PrimitiveConverter {
Binary[] dict;
private BinaryConverter() {
dict = null;
}
protected abstract void handleBinary(Binary value);
@Override
public boolean hasDictionarySupport() {
return true;
}
@Override
public void setDictionary(Dictionary dictionary) {
dict = new Binary[dictionary.getMaxId() + 1];
for (int i = 0; i <= dictionary.getMaxId(); i++) {
dict[i] = dictionary.decodeToBinary(i);
}
}
@Override
public void addBinary(Binary value) {
handleBinary(value);
}
@Override
public void addValueFromDictionary(int dictionaryId) {
handleBinary(dict[dictionaryId]);
}
}
private static boolean isBinary(Type field) {
return field.isPrimitive() && field.asPrimitiveType().getPrimitiveTypeName() == BINARY;
}
private static boolean isShreddedArray(Type typedValueField) {
return typedValueField.getLogicalTypeAnnotation() instanceof ListLogicalTypeAnnotation;
}
private static boolean isShreddedObject(Type typedValueField) {
return !typedValueField.isPrimitive() && !isShreddedArray(typedValueField);
}
}