You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ra...@apache.org on 2019/06/18 08:26:11 UTC
[arrow] branch master updated: ARROW-4923: [Java] Add methods to
set long value at given index in DecimalVector
This is an automated email from the ASF dual-hosted git repository.
ravindra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 0631357 ARROW-4923: [Java] Add methods to set long value at given index in DecimalVector
0631357 is described below
commit 06313579d0c5a9dc6da7939b9f011fd90de50a82
Author: Prudhvi Porandla <pr...@icloud.com>
AuthorDate: Tue Jun 18 13:55:40 2019 +0530
ARROW-4923: [Java] Add methods to set long value at given index in DecimalVector
Author: Prudhvi Porandla <pr...@icloud.com>
Closes #4591 from pprudhvi/arrow-4923 and squashes the following commits:
51e0610fe <Prudhvi Porandla> set value count
36a81b0e5 <Prudhvi Porandla> correct unittest
a7be82d4c <Prudhvi Porandla> checkstyle change
10ac3c25f <Prudhvi Porandla> refactor: variable rename, use for-loop to set and assert in TestDecimalVector.java
62b47c020 <Prudhvi Porandla> import order
8a586e822 <Prudhvi Porandla> minor refactor
f34f77009 <Prudhvi Porandla> use primitive instead of Long
885123eaf <Prudhvi Porandla> setter method for long in DecimalVector
---
.../org/apache/arrow/vector/DecimalVector.java | 24 ++++++++++++++++++++++
.../apache/arrow/vector/util/DecimalUtility.java | 11 ++++++++++
.../org/apache/arrow/vector/TestDecimalVector.java | 21 +++++++++++++++++++
3 files changed, 56 insertions(+)
diff --git a/java/vector/src/main/java/org/apache/arrow/vector/DecimalVector.java b/java/vector/src/main/java/org/apache/arrow/vector/DecimalVector.java
index 33b3460..9664bee 100644
--- a/java/vector/src/main/java/org/apache/arrow/vector/DecimalVector.java
+++ b/java/vector/src/main/java/org/apache/arrow/vector/DecimalVector.java
@@ -335,6 +335,17 @@ public class DecimalVector extends BaseFixedWidthVector {
}
/**
+ * Set the element at the given index to the given value.
+ *
+ * @param index position of element
+ * @param value long value.
+ */
+ public void set(int index, long value) {
+ BitVectorHelper.setValidityBitToOne(validityBuffer, index);
+ DecimalUtility.writeLongToArrowBuf(value, valueBuffer, index);
+ }
+
+ /**
* Set the element at the given index to the value set in data holder.
* If the value in holder is not indicated as set, element in the
* at the given index will be null.
@@ -415,6 +426,19 @@ public class DecimalVector extends BaseFixedWidthVector {
}
/**
+ * Same as {@link #set(int, long)} except that it handles the
+ * case when index is greater than or equal to existing
+ * value capacity {@link #getValueCapacity()}.
+ *
+ * @param index position of element
+ * @param value long value.
+ */
+ public void setSafe(int index, long value) {
+ handleSafe(index);
+ set(index, value);
+ }
+
+ /**
* Same as {@link #set(int, NullableDecimalHolder)} except that it handles the
* case when index is greater than or equal to existing
* value capacity {@link #getValueCapacity()}.
diff --git a/java/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java b/java/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
index 0f00e72..d091152 100644
--- a/java/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
+++ b/java/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
@@ -22,6 +22,7 @@ import java.math.BigInteger;
import java.nio.ByteBuffer;
import io.netty.buffer.ArrowBuf;
+import io.netty.util.internal.PlatformDependent;
/**
* Utility methods for configurable precision Decimal values (e.g. {@link BigDecimal}).
@@ -104,6 +105,16 @@ public class DecimalUtility {
}
/**
+ * Write the given long to the ArrowBuf at the given value index.
+ */
+ public static void writeLongToArrowBuf(long value, ArrowBuf bytebuf, int index) {
+ final long addressOfValue = bytebuf.memoryAddress() + index * DECIMAL_BYTE_LENGTH;
+ PlatformDependent.putLong(addressOfValue, value);
+ final long padValue = Long.signum(value) == -1 ? -1L : 0L;
+ PlatformDependent.putLong(addressOfValue + Long.BYTES, padValue);
+ }
+
+ /**
* Write the given byte array to the ArrowBuf at the given value index. Will throw an
* UnsupportedOperationException if the decimal size is greater than the Decimal vector byte
* width.
diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java
index e900135..321a120 100644
--- a/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java
+++ b/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java
@@ -160,6 +160,27 @@ public class TestDecimalVector {
}
@Test
+ public void testLongReadWrite() {
+ try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal",
+ new ArrowType.Decimal(38, 0), allocator)) {
+ decimalVector.allocateNew();
+
+ long[] longValues = {0L, -2L, Long.MAX_VALUE, Long.MIN_VALUE, 187L};
+
+ for (int i = 0; i < longValues.length; ++i) {
+ decimalVector.set(i, longValues[i]);
+ }
+
+ decimalVector.setValueCount(longValues.length);
+
+ for (int i = 0; i < longValues.length; ++i) {
+ assertEquals(new BigDecimal(longValues[i]), decimalVector.getObject(i));
+ }
+ }
+ }
+
+
+ @Test
public void testBigDecimalReadWrite() {
try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal",
new ArrowType.Decimal(38, 9), allocator);) {