-
Notifications
You must be signed in to change notification settings - Fork 4.2k
ARROW-9861: [Java] Support big-endian in DecimalVector #8056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1db584c
support big-endian in DecimalVector
kiszk 0d696fa
address review comments
kiszk aa14a87
support multiple byteWidth in writeLongToArrowBuf()
kiszk d62d990
support BigEndian in Decimal256Vector
kiszk ff33c3c
make code simple
kiszk d9e9ce9
address review comments
kiszk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import static org.apache.arrow.vector.NullCheckingForGet.NULL_CHECKING_ENABLED; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.nio.ByteOrder; | ||
|
|
||
| import org.apache.arrow.memory.ArrowBuf; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
|
|
@@ -43,6 +44,7 @@ | |
| */ | ||
| public final class DecimalVector extends BaseFixedWidthVector { | ||
| public static final byte TYPE_WIDTH = 16; | ||
| private static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; | ||
| private final FieldReader reader; | ||
|
|
||
| private final int precision; | ||
|
|
@@ -197,7 +199,7 @@ public void set(int index, ArrowBuf buffer) { | |
|
|
||
| /** | ||
| * Set the decimal element at given index to the provided array of bytes. | ||
| * Decimal is now implemented as Little Endian. This API allows the user | ||
| * Decimal is now implemented as Native Endian. This API allows the user | ||
| * to pass a decimal value in the form of byte array in BE byte order. | ||
| * | ||
| * <p>Consumers of Arrow code can use this API instead of first swapping | ||
|
|
@@ -218,25 +220,38 @@ public void setBigEndian(int index, byte[] value) { | |
| valueBuffer.checkBytes((long) index * TYPE_WIDTH, (long) (index + 1) * TYPE_WIDTH); | ||
|
|
||
| long outAddress = valueBuffer.memoryAddress() + (long) index * TYPE_WIDTH; | ||
| // swap bytes to convert BE to LE | ||
| for (int byteIdx = 0; byteIdx < length; ++byteIdx) { | ||
| PlatformDependent.putByte(outAddress + byteIdx, value[length - 1 - byteIdx]); | ||
| } | ||
|
|
||
| if (length == TYPE_WIDTH) { | ||
| return; | ||
| } | ||
|
|
||
| if (length == 0) { | ||
| PlatformDependent.setMemory(outAddress, DecimalVector.TYPE_WIDTH, (byte) 0); | ||
| } else if (length < TYPE_WIDTH) { | ||
| // sign extend | ||
| final byte pad = (byte) (value[0] < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad); | ||
| return; | ||
| } | ||
| if (LITTLE_ENDIAN) { | ||
| // swap bytes to convert BE to LE | ||
| for (int byteIdx = 0; byteIdx < length; ++byteIdx) { | ||
| PlatformDependent.putByte(outAddress + byteIdx, value[length - 1 - byteIdx]); | ||
| } | ||
|
|
||
| if (length == TYPE_WIDTH) { | ||
| return; | ||
| } | ||
|
|
||
| if (length < TYPE_WIDTH) { | ||
| // sign extend | ||
| final byte pad = (byte) (value[0] < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad); | ||
| return; | ||
| } | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| "Invalid decimal value length. Valid length in [1 - 16], got " + length); | ||
| if (length <= TYPE_WIDTH) { | ||
| // copy data from value to outAddress | ||
| PlatformDependent.copyMemory(value, 0, outAddress + DecimalVector.TYPE_WIDTH - length, length); | ||
| // sign extend | ||
| final byte pad = (byte) (value[0] < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress, DecimalVector.TYPE_WIDTH - length, pad); | ||
| return; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException( | ||
| "Invalid decimal value length. Valid length in [1 - 16], got " + length); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be in a |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -255,7 +270,7 @@ public void set(int index, long start, ArrowBuf buffer) { | |
| * Sets the element at given index using the buffer whose size maybe <= 16 bytes. | ||
| * @param index index to write the decimal to | ||
| * @param start start of value in the buffer | ||
| * @param buffer contains the decimal in little endian bytes | ||
| * @param buffer contains the decimal in native endian bytes | ||
| * @param length length of the value in the buffer | ||
| */ | ||
| public void setSafe(int index, long start, ArrowBuf buffer, int length) { | ||
|
|
@@ -268,12 +283,22 @@ public void setSafe(int index, long start, ArrowBuf buffer, int length) { | |
|
|
||
| long inAddress = buffer.memoryAddress() + start; | ||
| long outAddress = valueBuffer.memoryAddress() + (long) index * TYPE_WIDTH; | ||
| PlatformDependent.copyMemory(inAddress, outAddress, length); | ||
| // sign extend | ||
| if (length < 16) { | ||
| byte msb = PlatformDependent.getByte(inAddress + length - 1); | ||
| final byte pad = (byte) (msb < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad); | ||
| if (LITTLE_ENDIAN) { | ||
| PlatformDependent.copyMemory(inAddress, outAddress, length); | ||
| // sign extend | ||
| if (length < TYPE_WIDTH) { | ||
| byte msb = PlatformDependent.getByte(inAddress + length - 1); | ||
| final byte pad = (byte) (msb < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad); | ||
| } | ||
| } else { | ||
| PlatformDependent.copyMemory(inAddress, outAddress + DecimalVector.TYPE_WIDTH - length, length); | ||
| // sign extend | ||
| if (length < TYPE_WIDTH) { | ||
| byte msb = PlatformDependent.getByte(inAddress); | ||
| final byte pad = (byte) (msb < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress, DecimalVector.TYPE_WIDTH - length, pad); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -296,16 +321,26 @@ public void setBigEndianSafe(int index, long start, ArrowBuf buffer, int length) | |
| // not using buffer.getByte() to avoid boundary checks for every byte. | ||
| long inAddress = buffer.memoryAddress() + start; | ||
| long outAddress = valueBuffer.memoryAddress() + (long) index * TYPE_WIDTH; | ||
| // swap bytes to convert BE to LE | ||
| for (int byteIdx = 0; byteIdx < length; ++byteIdx) { | ||
| byte val = PlatformDependent.getByte((inAddress + length - 1) - byteIdx); | ||
| PlatformDependent.putByte(outAddress + byteIdx, val); | ||
| } | ||
| // sign extend | ||
| if (length < 16) { | ||
| byte msb = PlatformDependent.getByte(inAddress); | ||
| final byte pad = (byte) (msb < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad); | ||
| if (LITTLE_ENDIAN) { | ||
| // swap bytes to convert BE to LE | ||
| for (int byteIdx = 0; byteIdx < length; ++byteIdx) { | ||
| byte val = PlatformDependent.getByte((inAddress + length - 1) - byteIdx); | ||
| PlatformDependent.putByte(outAddress + byteIdx, val); | ||
| } | ||
| // sign extend | ||
| if (length < TYPE_WIDTH) { | ||
| byte msb = PlatformDependent.getByte(inAddress); | ||
| final byte pad = (byte) (msb < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad); | ||
| } | ||
| } else { | ||
| PlatformDependent.copyMemory(inAddress, outAddress + DecimalVector.TYPE_WIDTH - length, length); | ||
| // sign extend | ||
| if (length < TYPE_WIDTH) { | ||
| byte msb = PlatformDependent.getByte(inAddress); | ||
| final byte pad = (byte) (msb < 0 ? 0xFF : 0x00); | ||
| PlatformDependent.setMemory(outAddress, DecimalVector.TYPE_WIDTH - length, pad); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -329,7 +364,7 @@ public void set(int index, BigDecimal value) { | |
| */ | ||
| public void set(int index, long value) { | ||
| BitVectorHelper.setBit(validityBuffer, index); | ||
| DecimalUtility.writeLongToArrowBuf(value, valueBuffer, index); | ||
| DecimalUtility.writeLongToArrowBuf(value, valueBuffer, index, TYPE_WIDTH); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
returniflength == TYPE_WIDTH?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add
if (length == TYPE_WIDTH) returnbefore line 244, no data is copied fromvaluetooutAddress. I will add a commentcopy data from value to outAddressafter line 244.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I see above it is already set during the swap. I guess there is no harm in calling
PlatformDependent.setMemory(outAddress, DecimalVector.TYPE_WIDTH - length, pad)iflength == TYPE_WIDTH