Skip to content

Commit b32070a

Browse files
Round tip schema test for enums
1 parent 7fdcb96 commit b32070a

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

adapter/avro/src/test/java/org/apache/arrow/adapter/avro/RoundTripSchemaTest.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,50 @@
2121
import java.util.Arrays;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import org.apache.arrow.memory.BufferAllocator;
25+
import org.apache.arrow.memory.RootAllocator;
26+
import org.apache.arrow.vector.VarCharVector;
27+
import org.apache.arrow.vector.dictionary.Dictionary;
28+
import org.apache.arrow.vector.dictionary.DictionaryProvider;
2429
import org.apache.arrow.vector.types.DateUnit;
2530
import org.apache.arrow.vector.types.FloatingPointPrecision;
2631
import org.apache.arrow.vector.types.TimeUnit;
2732
import org.apache.arrow.vector.types.pojo.ArrowType;
33+
import org.apache.arrow.vector.types.pojo.DictionaryEncoding;
2834
import org.apache.arrow.vector.types.pojo.Field;
2935
import org.apache.arrow.vector.types.pojo.FieldType;
3036
import org.apache.avro.Schema;
37+
import org.junit.jupiter.api.Assertions;
3138
import org.junit.jupiter.api.Test;
3239

3340
public class RoundTripSchemaTest {
3441

3542
private void doRoundTripTest(List<Field> fields) {
43+
doRoundTripTest(fields, null);
44+
}
3645

37-
AvroToArrowConfig config = new AvroToArrowConfig(null, 1, null, Collections.emptySet(), false);
46+
private void doRoundTripTest(List<Field> fields, DictionaryProvider dictionaries) {
3847

39-
Schema avroSchema = ArrowToAvroUtils.createAvroSchema(fields, "TestRecord");
48+
DictionaryProvider.MapDictionaryProvider decodeDictionaries =
49+
new DictionaryProvider.MapDictionaryProvider();
50+
AvroToArrowConfig decodeConfig =
51+
new AvroToArrowConfig(null, 1, decodeDictionaries, Collections.emptySet(), false);
52+
53+
Schema avroSchema = ArrowToAvroUtils.createAvroSchema(fields, "TestRecord", null, dictionaries);
4054
org.apache.arrow.vector.types.pojo.Schema arrowSchema =
41-
AvroToArrowUtils.createArrowSchema(avroSchema, config);
55+
AvroToArrowUtils.createArrowSchema(avroSchema, decodeConfig);
4256

4357
// Compare string representations - equality not defined for logical types
4458
assertEquals(fields, arrowSchema.getFields());
59+
60+
for (int i = 0; i < fields.size(); i++) {
61+
Field field = fields.get(i);
62+
Field rtField = arrowSchema.getFields().get(i);
63+
if (field.getDictionary() != null) {
64+
// Dictionary content is not decoded until the data is consumed
65+
Assertions.assertNotNull(rtField.getDictionary());
66+
}
67+
}
4568
}
4669

4770
// Schema round trip for primitive types, nullable and non-nullable
@@ -440,4 +463,38 @@ public void testRoundTripStructType() {
440463

441464
doRoundTripTest(fields);
442465
}
466+
467+
@Test
468+
public void testRoundTripEnumType() {
469+
470+
BufferAllocator allocator = new RootAllocator();
471+
472+
FieldType dictionaryField = new FieldType(false, new ArrowType.Utf8(), null);
473+
VarCharVector dictionaryVector =
474+
new VarCharVector(new Field("dictionary", dictionaryField, null), allocator);
475+
476+
dictionaryVector.allocateNew(3);
477+
dictionaryVector.set(0, "apple".getBytes());
478+
dictionaryVector.set(1, "banana".getBytes());
479+
dictionaryVector.set(2, "cherry".getBytes());
480+
dictionaryVector.setValueCount(3);
481+
482+
// For simplicity, ensure the index type matches what will be decoded during Avro enum decoding
483+
Dictionary dictionary =
484+
new Dictionary(
485+
dictionaryVector, new DictionaryEncoding(0L, false, new ArrowType.Int(8, true)));
486+
DictionaryProvider dictionaries = new DictionaryProvider.MapDictionaryProvider(dictionary);
487+
488+
List<Field> fields =
489+
Arrays.asList(
490+
new Field(
491+
"enumField",
492+
new FieldType(
493+
true,
494+
new ArrowType.Int(8, true),
495+
new DictionaryEncoding(0L, false, new ArrowType.Int(8, true))),
496+
null));
497+
498+
doRoundTripTest(fields, dictionaries);
499+
}
443500
}

0 commit comments

Comments
 (0)