Skip to content

Commit 391ab64

Browse files
committed
ARROW-309: Types.getMinorTypeForArrowType() does not work for Union type
Author: Julien Le Dem <julien@dremio.com> Closes #151 from julienledem/fix_union and squashes the following commits: 01bea42 [Julien Le Dem] fix union
1 parent 30f6083 commit 391ab64

File tree

1 file changed

+107
-38
lines changed
  • java/vector/src/main/java/org/apache/arrow/vector/types

1 file changed

+107
-38
lines changed

java/vector/src/main/java/org/apache/arrow/vector/types/Types.java

Lines changed: 107 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
*/
1818
package org.apache.arrow.vector.types;
1919

20-
import java.util.HashMap;
21-
import java.util.Map;
22-
2320
import org.apache.arrow.flatbuf.IntervalUnit;
2421
import org.apache.arrow.flatbuf.Precision;
25-
import org.apache.arrow.flatbuf.Type;
2622
import org.apache.arrow.flatbuf.UnionMode;
2723
import org.apache.arrow.memory.BufferAllocator;
2824
import org.apache.arrow.vector.FieldVector;
@@ -74,9 +70,11 @@
7470
import org.apache.arrow.vector.complex.impl.VarCharWriterImpl;
7571
import org.apache.arrow.vector.complex.writer.FieldWriter;
7672
import org.apache.arrow.vector.types.pojo.ArrowType;
73+
import org.apache.arrow.vector.types.pojo.ArrowType.ArrowTypeVisitor;
7774
import org.apache.arrow.vector.types.pojo.ArrowType.Binary;
7875
import org.apache.arrow.vector.types.pojo.ArrowType.Bool;
7976
import org.apache.arrow.vector.types.pojo.ArrowType.Date;
77+
import org.apache.arrow.vector.types.pojo.ArrowType.Decimal;
8078
import org.apache.arrow.vector.types.pojo.ArrowType.FloatingPoint;
8179
import org.apache.arrow.vector.types.pojo.ArrowType.Int;
8280
import org.apache.arrow.vector.types.pojo.ArrowType.Interval;
@@ -92,26 +90,25 @@
9290

9391
public class Types {
9492

95-
public static final Field NULL_FIELD = new Field("", true, Null.INSTANCE, null);
96-
public static final Field TINYINT_FIELD = new Field("", true, new Int(8, true), null);
97-
public static final Field SMALLINT_FIELD = new Field("", true, new Int(16, true), null);
98-
public static final Field INT_FIELD = new Field("", true, new Int(32, true), null);
99-
public static final Field BIGINT_FIELD = new Field("", true, new Int(64, true), null);
100-
public static final Field UINT1_FIELD = new Field("", true, new Int(8, false), null);
101-
public static final Field UINT2_FIELD = new Field("", true, new Int(16, false), null);
102-
public static final Field UINT4_FIELD = new Field("", true, new Int(32, false), null);
103-
public static final Field UINT8_FIELD = new Field("", true, new Int(64, false), null);
104-
public static final Field DATE_FIELD = new Field("", true, Date.INSTANCE, null);
105-
public static final Field TIME_FIELD = new Field("", true, Time.INSTANCE, null);
106-
public static final Field TIMESTAMP_FIELD = new Field("", true, new Timestamp(""), null);
107-
public static final Field INTERVALDAY_FIELD = new Field("", true, new Interval(IntervalUnit.DAY_TIME), null);
108-
public static final Field INTERVALYEAR_FIELD = new Field("", true, new Interval(IntervalUnit.YEAR_MONTH), null);
109-
public static final Field FLOAT4_FIELD = new Field("", true, new FloatingPoint(Precision.SINGLE), null);
110-
public static final Field FLOAT8_FIELD = new Field("", true, new FloatingPoint(Precision.DOUBLE), null);
111-
public static final Field LIST_FIELD = new Field("", true, List.INSTANCE, null);
112-
public static final Field VARCHAR_FIELD = new Field("", true, Utf8.INSTANCE, null);
113-
public static final Field VARBINARY_FIELD = new Field("", true, Binary.INSTANCE, null);
114-
public static final Field BIT_FIELD = new Field("", true, Bool.INSTANCE, null);
93+
private static final Field NULL_FIELD = new Field("", true, Null.INSTANCE, null);
94+
private static final Field TINYINT_FIELD = new Field("", true, new Int(8, true), null);
95+
private static final Field SMALLINT_FIELD = new Field("", true, new Int(16, true), null);
96+
private static final Field INT_FIELD = new Field("", true, new Int(32, true), null);
97+
private static final Field BIGINT_FIELD = new Field("", true, new Int(64, true), null);
98+
private static final Field UINT1_FIELD = new Field("", true, new Int(8, false), null);
99+
private static final Field UINT2_FIELD = new Field("", true, new Int(16, false), null);
100+
private static final Field UINT4_FIELD = new Field("", true, new Int(32, false), null);
101+
private static final Field UINT8_FIELD = new Field("", true, new Int(64, false), null);
102+
private static final Field DATE_FIELD = new Field("", true, Date.INSTANCE, null);
103+
private static final Field TIME_FIELD = new Field("", true, Time.INSTANCE, null);
104+
private static final Field TIMESTAMP_FIELD = new Field("", true, new Timestamp(""), null);
105+
private static final Field INTERVALDAY_FIELD = new Field("", true, new Interval(IntervalUnit.DAY_TIME), null);
106+
private static final Field INTERVALYEAR_FIELD = new Field("", true, new Interval(IntervalUnit.YEAR_MONTH), null);
107+
private static final Field FLOAT4_FIELD = new Field("", true, new FloatingPoint(Precision.SINGLE), null);
108+
private static final Field FLOAT8_FIELD = new Field("", true, new FloatingPoint(Precision.DOUBLE), null);
109+
private static final Field VARCHAR_FIELD = new Field("", true, Utf8.INSTANCE, null);
110+
private static final Field VARBINARY_FIELD = new Field("", true, Binary.INSTANCE, null);
111+
private static final Field BIT_FIELD = new Field("", true, Bool.INSTANCE, null);
115112

116113

117114
public enum MinorType {
@@ -427,7 +424,7 @@ public FieldWriter getNewFieldWriter(ValueVector vector) {
427424
UINT4(new Int(32, false)) {
428425
@Override
429426
public Field getField() {
430-
return UINT8_FIELD;
427+
return UINT4_FIELD;
431428
}
432429

433430
@Override
@@ -506,22 +503,94 @@ public ArrowType getType() {
506503
public abstract FieldWriter getNewFieldWriter(ValueVector vector);
507504
}
508505

509-
private static final Map<ArrowType,MinorType> ARROW_TYPE_MINOR_TYPE_MAP;
510-
511506
public static MinorType getMinorTypeForArrowType(ArrowType arrowType) {
512-
if (arrowType.getTypeType() == Type.Decimal) {
513-
return MinorType.DECIMAL;
514-
}
515-
return ARROW_TYPE_MINOR_TYPE_MAP.get(arrowType);
516-
}
507+
return arrowType.accept(new ArrowTypeVisitor<MinorType>() {
508+
@Override public MinorType visit(Null type) {
509+
return MinorType.NULL;
510+
}
517511

518-
static {
519-
ARROW_TYPE_MINOR_TYPE_MAP = new HashMap<>();
520-
for (MinorType minorType : MinorType.values()) {
521-
if (minorType != MinorType.DECIMAL) {
522-
ARROW_TYPE_MINOR_TYPE_MAP.put(minorType.getType(), minorType);
512+
@Override public MinorType visit(Struct_ type) {
513+
return MinorType.MAP;
523514
}
524-
}
515+
516+
@Override public MinorType visit(List type) {
517+
return MinorType.LIST;
518+
}
519+
520+
@Override public MinorType visit(Union type) {
521+
return MinorType.UNION;
522+
}
523+
524+
@Override
525+
public MinorType visit(Int type) {
526+
switch (type.getBitWidth()) {
527+
case 8:
528+
return type.getIsSigned() ? MinorType.TINYINT : MinorType.UINT1;
529+
case 16:
530+
return type.getIsSigned() ? MinorType.SMALLINT : MinorType.UINT2;
531+
case 32:
532+
return type.getIsSigned() ? MinorType.INT : MinorType.UINT4;
533+
case 64:
534+
return type.getIsSigned() ? MinorType.BIGINT : MinorType.UINT8;
535+
default:
536+
throw new IllegalArgumentException("only 8, 16, 32, 64 supported: " + type);
537+
}
538+
}
539+
540+
@Override
541+
public MinorType visit(FloatingPoint type) {
542+
switch (type.getPrecision()) {
543+
case Precision.HALF:
544+
throw new UnsupportedOperationException("NYI: " + type);
545+
case Precision.SINGLE:
546+
return MinorType.FLOAT4;
547+
case Precision.DOUBLE:
548+
return MinorType.FLOAT8;
549+
default:
550+
throw new IllegalArgumentException("unknown precision: " + type);
551+
}
552+
}
553+
554+
@Override public MinorType visit(Utf8 type) {
555+
return MinorType.VARCHAR;
556+
}
557+
558+
@Override public MinorType visit(Binary type) {
559+
return MinorType.VARBINARY;
560+
}
561+
562+
@Override public MinorType visit(Bool type) {
563+
return MinorType.BIT;
564+
}
565+
566+
@Override public MinorType visit(Decimal type) {
567+
return MinorType.DECIMAL;
568+
}
569+
570+
@Override public MinorType visit(Date type) {
571+
return MinorType.DATE;
572+
}
573+
574+
@Override public MinorType visit(Time type) {
575+
return MinorType.TIME;
576+
}
577+
578+
@Override public MinorType visit(Timestamp type) {
579+
return MinorType.TIMESTAMP;
580+
}
581+
582+
@Override
583+
public MinorType visit(Interval type) {
584+
switch (type.getUnit()) {
585+
case IntervalUnit.DAY_TIME:
586+
return MinorType.INTERVALDAY;
587+
case IntervalUnit.YEAR_MONTH:
588+
return MinorType.INTERVALYEAR;
589+
default:
590+
throw new IllegalArgumentException("unknown unit: " + type);
591+
}
592+
}
593+
});
525594
}
526595

527596
}

0 commit comments

Comments
 (0)