From 04dac69936d6fa9605ac39b4d587d779b4c74eb5 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Fri, 9 Oct 2026 14:49:19 +0100 Subject: [PATCH 1/2] Preserve Decimal256 type metadata in promoted union writers --- .../codegen/templates/PromotableWriter.java | 1 + .../impl/TestDecimal256WriterPromotion.java | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java diff --git a/vector/src/main/codegen/templates/PromotableWriter.java b/vector/src/main/codegen/templates/PromotableWriter.java index 11d34f72c9..405309cc1c 100644 --- a/vector/src/main/codegen/templates/PromotableWriter.java +++ b/vector/src/main/codegen/templates/PromotableWriter.java @@ -316,6 +316,7 @@ public void setPosition(int index) { protected boolean requiresArrowType(MinorType type) { return type == MinorType.DECIMAL + || type == MinorType.DECIMAL256 || type == MinorType.MAP || type == MinorType.DURATION || type == MinorType.FIXEDSIZEBINARY diff --git a/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java b/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java new file mode 100644 index 0000000000..76af703bf4 --- /dev/null +++ b/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java @@ -0,0 +1,48 @@ +/* + * 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.arrow.vector.complex.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigDecimal; +import java.util.Arrays; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.vector.complex.ListVector; +import org.junit.jupiter.api.Test; + +public class TestDecimal256WriterPromotion { + @Test + public void testDecimal256InPromotedUnion() { + BigDecimal narrow = new BigDecimal("123.45"); + BigDecimal wide = new BigDecimal("123456789012345678901234567890123456789.01"); + try (BufferAllocator allocator = new RootAllocator(); + ListVector vector = ListVector.empty("decimals", allocator)) { + UnionListWriter writer = vector.getWriter(); + writer.startList(); + writer.decimal().writeDecimal(narrow); + writer.decimal256().writeDecimal256(wide); + writer.decimal().writeDecimal(narrow.negate()); + writer.decimal256().writeDecimal256(wide.negate()); + writer.endList(); + vector.setValueCount(1); + assertEquals( + Arrays.asList(narrow, wide, narrow.negate(), wide.negate()), vector.getObject(0)); + vector.validateFull(); + } + } +} From 1c85ca231abadf28c8027bd6aacaca42be94b72b Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Tue, 24 Nov 2026 10:27:12 +0000 Subject: [PATCH 2/2] Cover Decimal256 union and struct promotion in TestPromotableWriter --- .../impl/TestDecimal256WriterPromotion.java | 48 ---------------- .../complex/impl/TestPromotableWriter.java | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 48 deletions(-) delete mode 100644 vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java diff --git a/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java b/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java deleted file mode 100644 index 76af703bf4..0000000000 --- a/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestDecimal256WriterPromotion.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.arrow.vector.complex.impl; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.math.BigDecimal; -import java.util.Arrays; -import org.apache.arrow.memory.BufferAllocator; -import org.apache.arrow.memory.RootAllocator; -import org.apache.arrow.vector.complex.ListVector; -import org.junit.jupiter.api.Test; - -public class TestDecimal256WriterPromotion { - @Test - public void testDecimal256InPromotedUnion() { - BigDecimal narrow = new BigDecimal("123.45"); - BigDecimal wide = new BigDecimal("123456789012345678901234567890123456789.01"); - try (BufferAllocator allocator = new RootAllocator(); - ListVector vector = ListVector.empty("decimals", allocator)) { - UnionListWriter writer = vector.getWriter(); - writer.startList(); - writer.decimal().writeDecimal(narrow); - writer.decimal256().writeDecimal256(wide); - writer.decimal().writeDecimal(narrow.negate()); - writer.decimal256().writeDecimal256(wide.negate()); - writer.endList(); - vector.setValueCount(1); - assertEquals( - Arrays.asList(narrow, wide, narrow.negate(), wide.negate()), vector.getObject(0)); - vector.validateFull(); - } - } -} diff --git a/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java b/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java index 5b6d65d6ba..2066a23084 100644 --- a/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java +++ b/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java @@ -25,6 +25,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Objects; import java.util.UUID; import org.apache.arrow.memory.ArrowBuf; @@ -759,6 +760,60 @@ public void testPromoteLargeVarBinaryHelpersDirect() throws Exception { } } + @Test + public void testDecimal256InPromotedUnion() { + BigDecimal narrow = new BigDecimal("123.45"); + BigDecimal wide = new BigDecimal("123456789012345678901234567890123456789.01"); + try (ListVector vector = ListVector.empty("decimals", allocator)) { + UnionListWriter writer = vector.getWriter(); + writer.startList(); + writer.decimal().writeDecimal(narrow); + writer.decimal256().writeDecimal256(wide); + writer.decimal().writeDecimal(narrow.negate()); + writer.decimal256().writeDecimal256(wide.negate()); + writer.endList(); + vector.setValueCount(1); + assertEquals( + Arrays.asList(narrow, wide, narrow.negate(), wide.negate()), vector.getObject(0)); + vector.validateFull(); + } + } + + @Test + public void testAddDecimal256ToExistingUnion() { + BigDecimal decimal = new BigDecimal("-123.456"); + try (ListVector vector = ListVector.empty("values", allocator)) { + UnionListWriter writer = vector.getWriter(); + writer.startList(); + writer.integer().writeInt(7); + writer.bigInt().writeBigInt(8L); + writer.decimal256().writeDecimal256(decimal); + writer.endList(); + vector.setValueCount(1); + assertEquals(Arrays.asList(7, 8L, decimal), vector.getObject(0)); + } + } + + @Test + public void testPromoteStructFieldToDecimal256() throws Exception { + BigDecimal decimal = new BigDecimal("123.456"); + try (StructVector parent = StructVector.empty("parent", allocator); + PromotableWriter writer = new PromotableWriter(parent.addOrGetStruct("value"), parent)) { + parent.allocateNew(); + writer.setPosition(0); + writer.start(); + writer.integer("id").writeInt(7); + writer.end(); + writer.setPosition(1); + writer.writeDecimal256(decimal); + parent.setValueCount(2); + + UnionVector values = parent.getChild("value", UnionVector.class); + assertEquals(7, values.getStruct().getObject(0).get("id")); + assertEquals(decimal, values.getObject(1)); + } + } + @Test public void testPromoteToUnionFromDecimal() throws Exception { try (final NonNullableStructVector container =