This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git
The following commit(s) were added to refs/heads/main by this push:
new 1718ad7ae MINOR: Decode decimal holders with the correct byte order
and byte offset (#1518)
1718ad7ae is described below
commit 1718ad7aeef4209d3a620541672e1af7fc5cbb1e
Author: Robert Kruszewski <[email protected]>
AuthorDate: Mon Nov 23 17:08:01 2026 +0000
MINOR: Decode decimal holders with the correct byte order and byte offset
(#1518)
## What's Changed
Split out from #1228. Decode decimal values in decimal holder with
correct endianess - we need to flip from machine native endianess to big
endian
---
.../main/codegen/templates/HolderReaderImpl.java | 13 +---
.../apache/arrow/vector/util/DecimalUtility.java | 10 ++-
.../arrow/vector/TestDecimalHolderDecoding.java | 80 ++++++++++++++++++++++
3 files changed, 92 insertions(+), 11 deletions(-)
diff --git a/vector/src/main/codegen/templates/HolderReaderImpl.java
b/vector/src/main/codegen/templates/HolderReaderImpl.java
index cdbb65c4f..b67c1b26b 100644
--- a/vector/src/main/codegen/templates/HolderReaderImpl.java
+++ b/vector/src/main/codegen/templates/HolderReaderImpl.java
@@ -127,16 +127,9 @@ public class ${holderMode}${name}HolderReaderImpl extends
AbstractFieldReader {
return DurationVector.toDuration(holder.value, holder.unit);
<#elseif minor.class == "Bit" >
return Boolean.valueOf(holder.value != 0);
- <#elseif minor.class == "Decimal">
- byte[] bytes = new byte[${type.width}];
- holder.buffer.getBytes(holder.start, bytes, 0, ${type.width});
- ${friendlyType} value = new BigDecimal(new BigInteger(bytes),
holder.scale);
- return value;
- <#elseif minor.class == "Decimal256">
- byte[] bytes = new byte[${type.width}];
- holder.buffer.getBytes(holder.start, bytes, 0, ${type.width});
- ${friendlyType} value = new BigDecimal(new BigInteger(bytes),
holder.scale);
- return value;
+ <#elseif minor.class?starts_with("Decimal")>
+ return DecimalUtility.getBigDecimalFromArrowBufAtOffset(
+ holder.buffer, holder.start, holder.scale, ${type.width});
<#elseif minor.class == "FixedSizeBinary">
byte[] value = new byte [holder.byteWidth];
holder.buffer.getBytes(0, value, 0, holder.byteWidth);
diff --git
a/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
b/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
index a1a9cf7e1..46c6fb70c 100644
--- a/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
+++ b/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
@@ -45,9 +45,17 @@ public class DecimalUtility {
*/
public static BigDecimal getBigDecimalFromArrowBuf(
ArrowBuf bytebuf, int index, int scale, int byteWidth) {
+ return getBigDecimalFromArrowBufAtOffset(bytebuf, (long) index *
byteWidth, scale, byteWidth);
+ }
+
+ /**
+ * Read an ArrowType.Decimal at the given byte offset in the ArrowBuf and
convert to a BigDecimal
+ * with the given scale.
+ */
+ public static BigDecimal getBigDecimalFromArrowBufAtOffset(
+ ArrowBuf bytebuf, long startIndex, int scale, int byteWidth) {
byte[] value = new byte[byteWidth];
byte temp;
- final long startIndex = (long) index * byteWidth;
bytebuf.getBytes(startIndex, value, 0, byteWidth);
if (LITTLE_ENDIAN) {
diff --git
a/vector/src/test/java/org/apache/arrow/vector/TestDecimalHolderDecoding.java
b/vector/src/test/java/org/apache/arrow/vector/TestDecimalHolderDecoding.java
new file mode 100644
index 000000000..26259bd04
--- /dev/null
+++
b/vector/src/test/java/org/apache/arrow/vector/TestDecimalHolderDecoding.java
@@ -0,0 +1,80 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.math.BigDecimal;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.complex.impl.Decimal256HolderReaderImpl;
+import org.apache.arrow.vector.complex.impl.DecimalHolderReaderImpl;
+import org.apache.arrow.vector.complex.impl.NullableDecimal256HolderReaderImpl;
+import org.apache.arrow.vector.complex.impl.NullableDecimalHolderReaderImpl;
+import org.apache.arrow.vector.holders.Decimal256Holder;
+import org.apache.arrow.vector.holders.DecimalHolder;
+import org.apache.arrow.vector.holders.NullableDecimal256Holder;
+import org.apache.arrow.vector.holders.NullableDecimalHolder;
+import org.junit.jupiter.api.Test;
+
+public class TestDecimalHolderDecoding {
+ @Test
+ public void testDecimalHolderByteOrderAndOffset() {
+ try (BufferAllocator allocator = new RootAllocator();
+ DecimalVector vector = new DecimalVector("decimal", allocator, 38, 2))
{
+ vector.allocateNew(3);
+ vector.set(0, new BigDecimal("1.23"));
+ BigDecimal[] values = {new BigDecimal("1234567.89"), new
BigDecimal("-1234567.89")};
+ for (int i = 0; i < values.length; i++) {
+ vector.set(i + 1, values[i]);
+ NullableDecimalHolder nullableHolder = new NullableDecimalHolder();
+ vector.get(i + 1, nullableHolder);
+ assertEquals(
+ values[i], new
NullableDecimalHolderReaderImpl(nullableHolder).readBigDecimal());
+ DecimalHolder holder = new DecimalHolder();
+ holder.buffer = nullableHolder.buffer;
+ holder.start = nullableHolder.start;
+ holder.precision = nullableHolder.precision;
+ holder.scale = nullableHolder.scale;
+ assertEquals(values[i], new
DecimalHolderReaderImpl(holder).readBigDecimal());
+ }
+ }
+ }
+
+ @Test
+ public void testDecimal256HolderByteOrderAndOffset() {
+ try (BufferAllocator allocator = new RootAllocator();
+ Decimal256Vector vector = new Decimal256Vector("decimal", allocator,
76, 2)) {
+ vector.allocateNew(3);
+ vector.set(0, new BigDecimal("1.23"));
+ BigDecimal[] values = {new BigDecimal("1234567.89"), new
BigDecimal("-1234567.89")};
+ for (int i = 0; i < values.length; i++) {
+ vector.set(i + 1, values[i]);
+ NullableDecimal256Holder nullableHolder = new
NullableDecimal256Holder();
+ vector.get(i + 1, nullableHolder);
+ assertEquals(
+ values[i], new
NullableDecimal256HolderReaderImpl(nullableHolder).readBigDecimal());
+ Decimal256Holder holder = new Decimal256Holder();
+ holder.buffer = nullableHolder.buffer;
+ holder.start = nullableHolder.start;
+ holder.precision = nullableHolder.precision;
+ holder.scale = nullableHolder.scale;
+ assertEquals(values[i], new
Decimal256HolderReaderImpl(holder).readBigDecimal());
+ }
+ }
+ }
+}