|
17 | 17 | */ |
18 | 18 | package org.apache.arrow.vector.types; |
19 | 19 |
|
20 | | -import java.util.HashMap; |
21 | | -import java.util.Map; |
22 | | - |
23 | 20 | import org.apache.arrow.flatbuf.IntervalUnit; |
24 | 21 | import org.apache.arrow.flatbuf.Precision; |
25 | | -import org.apache.arrow.flatbuf.Type; |
26 | 22 | import org.apache.arrow.flatbuf.UnionMode; |
27 | 23 | import org.apache.arrow.memory.BufferAllocator; |
28 | 24 | import org.apache.arrow.vector.FieldVector; |
|
74 | 70 | import org.apache.arrow.vector.complex.impl.VarCharWriterImpl; |
75 | 71 | import org.apache.arrow.vector.complex.writer.FieldWriter; |
76 | 72 | import org.apache.arrow.vector.types.pojo.ArrowType; |
| 73 | +import org.apache.arrow.vector.types.pojo.ArrowType.ArrowTypeVisitor; |
77 | 74 | import org.apache.arrow.vector.types.pojo.ArrowType.Binary; |
78 | 75 | import org.apache.arrow.vector.types.pojo.ArrowType.Bool; |
79 | 76 | import org.apache.arrow.vector.types.pojo.ArrowType.Date; |
| 77 | +import org.apache.arrow.vector.types.pojo.ArrowType.Decimal; |
80 | 78 | import org.apache.arrow.vector.types.pojo.ArrowType.FloatingPoint; |
81 | 79 | import org.apache.arrow.vector.types.pojo.ArrowType.Int; |
82 | 80 | import org.apache.arrow.vector.types.pojo.ArrowType.Interval; |
|
92 | 90 |
|
93 | 91 | public class Types { |
94 | 92 |
|
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); |
115 | 112 |
|
116 | 113 |
|
117 | 114 | public enum MinorType { |
@@ -427,7 +424,7 @@ public FieldWriter getNewFieldWriter(ValueVector vector) { |
427 | 424 | UINT4(new Int(32, false)) { |
428 | 425 | @Override |
429 | 426 | public Field getField() { |
430 | | - return UINT8_FIELD; |
| 427 | + return UINT4_FIELD; |
431 | 428 | } |
432 | 429 |
|
433 | 430 | @Override |
@@ -506,22 +503,94 @@ public ArrowType getType() { |
506 | 503 | public abstract FieldWriter getNewFieldWriter(ValueVector vector); |
507 | 504 | } |
508 | 505 |
|
509 | | - private static final Map<ArrowType,MinorType> ARROW_TYPE_MINOR_TYPE_MAP; |
510 | | - |
511 | 506 | 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 | + } |
517 | 511 |
|
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; |
523 | 514 | } |
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 | + }); |
525 | 594 | } |
526 | 595 |
|
527 | 596 | } |
0 commit comments