|
37 | 37 | import org.apache.arrow.vector.BitVector; |
38 | 38 | import org.apache.arrow.vector.DateDayVector; |
39 | 39 | import org.apache.arrow.vector.Decimal256Vector; |
| 40 | +import org.apache.arrow.vector.Decimal32Vector; |
| 41 | +import org.apache.arrow.vector.Decimal64Vector; |
40 | 42 | import org.apache.arrow.vector.DecimalVector; |
41 | 43 | import org.apache.arrow.vector.FieldVector; |
42 | 44 | import org.apache.arrow.vector.FixedSizeBinaryVector; |
@@ -621,6 +623,82 @@ public void testRoundTripNullableBinary() throws Exception { |
621 | 623 |
|
622 | 624 | // Data round trip for logical types, nullable and non-nullable |
623 | 625 |
|
| 626 | + @Test |
| 627 | + public void testRoundTripNarrowDecimals() throws Exception { |
| 628 | + |
| 629 | + // Field definitions |
| 630 | + FieldType decimal32Field = new FieldType(false, new ArrowType.Decimal(9, 3, 32), null); |
| 631 | + FieldType decimal64Field = new FieldType(false, new ArrowType.Decimal(18, 6, 64), null); |
| 632 | + |
| 633 | + // Create empty vectors |
| 634 | + BufferAllocator allocator = new RootAllocator(); |
| 635 | + Decimal32Vector decimal32Vector = |
| 636 | + new Decimal32Vector(new Field("decimal32", decimal32Field, null), allocator); |
| 637 | + Decimal64Vector decimal64Vector = |
| 638 | + new Decimal64Vector(new Field("decimal64", decimal64Field, null), allocator); |
| 639 | + |
| 640 | + // Set up VSR |
| 641 | + List<FieldVector> vectors = Arrays.asList(decimal32Vector, decimal64Vector); |
| 642 | + int rowCount = 3; |
| 643 | + |
| 644 | + try (VectorSchemaRoot root = new VectorSchemaRoot(vectors)) { |
| 645 | + |
| 646 | + root.setRowCount(rowCount); |
| 647 | + root.allocateNew(); |
| 648 | + |
| 649 | + // Set test data |
| 650 | + decimal32Vector.setSafe(0, new BigDecimal("123456.789")); |
| 651 | + decimal32Vector.setSafe(1, new BigDecimal("-999999.999")); |
| 652 | + decimal32Vector.setSafe(2, new BigDecimal("0.001")); |
| 653 | + |
| 654 | + decimal64Vector.setSafe(0, new BigDecimal("123456789012.345678")); |
| 655 | + decimal64Vector.setSafe(1, new BigDecimal("-999999999999.999999")); |
| 656 | + decimal64Vector.setSafe(2, new BigDecimal("0.000001")); |
| 657 | + |
| 658 | + File dataFile = new File(TMP, "testRoundTripNarrowDecimals.avro"); |
| 659 | + |
| 660 | + roundTripTest(root, allocator, dataFile, rowCount); |
| 661 | + } |
| 662 | + } |
| 663 | + |
| 664 | + @Test |
| 665 | + public void testRoundTripNullableNarrowDecimals() throws Exception { |
| 666 | + |
| 667 | + // Field definitions |
| 668 | + FieldType decimal32Field = new FieldType(true, new ArrowType.Decimal(9, 3, 32), null); |
| 669 | + FieldType decimal64Field = new FieldType(true, new ArrowType.Decimal(18, 6, 64), null); |
| 670 | + |
| 671 | + // Create empty vectors |
| 672 | + BufferAllocator allocator = new RootAllocator(); |
| 673 | + Decimal32Vector decimal32Vector = |
| 674 | + new Decimal32Vector(new Field("decimal32", decimal32Field, null), allocator); |
| 675 | + Decimal64Vector decimal64Vector = |
| 676 | + new Decimal64Vector(new Field("decimal64", decimal64Field, null), allocator); |
| 677 | + |
| 678 | + // Set up VSR |
| 679 | + List<FieldVector> vectors = Arrays.asList(decimal32Vector, decimal64Vector); |
| 680 | + int rowCount = 3; |
| 681 | + |
| 682 | + try (VectorSchemaRoot root = new VectorSchemaRoot(vectors)) { |
| 683 | + |
| 684 | + root.setRowCount(rowCount); |
| 685 | + root.allocateNew(); |
| 686 | + |
| 687 | + // Set test data |
| 688 | + decimal32Vector.setNull(0); |
| 689 | + decimal32Vector.setSafe(1, BigDecimal.ZERO.setScale(3, RoundingMode.UNNECESSARY)); |
| 690 | + decimal32Vector.setSafe(2, new BigDecimal("-1.500")); |
| 691 | + |
| 692 | + decimal64Vector.setNull(0); |
| 693 | + decimal64Vector.setSafe(1, BigDecimal.ZERO.setScale(6, RoundingMode.UNNECESSARY)); |
| 694 | + decimal64Vector.setSafe(2, new BigDecimal("-1.500000")); |
| 695 | + |
| 696 | + File dataFile = new File(TMP, "testRoundTripNullableNarrowDecimals.avro"); |
| 697 | + |
| 698 | + roundTripTest(root, allocator, dataFile, rowCount); |
| 699 | + } |
| 700 | + } |
| 701 | + |
624 | 702 | @Test |
625 | 703 | public void testRoundTripDecimals() throws Exception { |
626 | 704 |
|
|
0 commit comments