Skip to content

Commit

Permalink
ARROW-8086: [Java] Support writing decimal from big endian byte array…
Browse files Browse the repository at this point in the history
… in UnionListWriter

Closes #6593 from projjal/writedecimal and squashes the following commits:

f27f514 <Projjal Chanda> write decimal from bytearray for unionlistwriter

Authored-by: Projjal Chanda <iam@pchanda.com>
Signed-off-by: Praveen <praveen@dremio.com>
  • Loading branch information
projjal authored and praveenbingo committed Mar 13, 2020
1 parent a75e645 commit f67b210
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
Expand Up @@ -197,6 +197,14 @@ public void writeDecimal(BigDecimal value) {
writer.setPosition(writer.idx() + 1);
}

public void writeBigEndianBytesToDecimal(byte[] value, ArrowType arrowType) {
if (writer.idx() >= (idx() + 1) * listSize) {
throw new IllegalStateException(String.format("values at index %s is greater than listSize %s", idx(), listSize));
}
writer.writeBigEndianBytesToDecimal(value, arrowType);
writer.setPosition(writer.idx() + 1);
}

<#list vv.types as type>
<#list type.minor as minor>
<#assign name = minor.class?cap_first />
Expand Down
5 changes: 5 additions & 0 deletions java/vector/src/main/codegen/templates/UnionListWriter.java
Expand Up @@ -193,6 +193,11 @@ public void writeDecimal(BigDecimal value) {
writer.setPosition(writer.idx()+1);
}

public void writeBigEndianBytesToDecimal(byte[] value, ArrowType arrowType){
writer.writeBigEndianBytesToDecimal(value, arrowType);
writer.setPosition(writer.idx() + 1);
}

<#list vv.types as type>
<#list type.minor as minor>
<#assign name = minor.class?cap_first />
Expand Down
6 changes: 6 additions & 0 deletions java/vector/src/main/codegen/templates/UnionWriter.java
Expand Up @@ -179,6 +179,12 @@ public void write(${name}Holder holder) {
getDecimalWriter(arrowType).setPosition(idx());
getDecimalWriter(arrowType).writeDecimal(value);
}

public void writeBigEndianBytesToDecimal(byte[] value, ArrowType arrowType) {
data.setType(idx(), MinorType.DECIMAL);
getDecimalWriter(arrowType).setPosition(idx());
getDecimalWriter(arrowType).writeBigEndianBytesToDecimal(value, arrowType);
}
</#if>
</#if>
</#list>
Expand Down
Expand Up @@ -208,13 +208,14 @@ public void testCopyListVector() {

@Test
public void testCopyFixedSizedListOfDecimalsVector() {
try (FixedSizeListVector from = FixedSizeListVector.empty("v", 3, allocator);
FixedSizeListVector to = FixedSizeListVector.empty("v", 3, allocator)) {
try (FixedSizeListVector from = FixedSizeListVector.empty("v", 4, allocator);
FixedSizeListVector to = FixedSizeListVector.empty("v", 4, allocator)) {
from.addOrGetVector(FieldType.nullable(new ArrowType.Decimal(3, 0)));
to.addOrGetVector(FieldType.nullable(new ArrowType.Decimal(3, 0)));

DecimalHolder holder = new DecimalHolder();
holder.buffer = allocator.buffer(DecimalUtility.DECIMAL_BYTE_LENGTH);
ArrowType arrowType = new ArrowType.Decimal(3, 0);

// populate from vector
UnionFixedSizeListWriter writer = from.getWriter();
Expand All @@ -229,7 +230,10 @@ public void testCopyFixedSizedListOfDecimalsVector() {
writer.decimal().write(holder);

DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(i * 3), holder.buffer, 0);
writer.decimal().writeDecimal(0, holder.buffer, new ArrowType.Decimal(3, 0));
writer.decimal().writeDecimal(0, holder.buffer, arrowType);

writer.decimal().writeBigEndianBytesToDecimal(BigDecimal.valueOf(i * 4).unscaledValue().toByteArray(),
arrowType);

writer.endList();
}
Expand Down Expand Up @@ -265,6 +269,8 @@ public void testCopyUnionListWithDecimal() {

listWriter.decimal().writeDecimal(BigDecimal.valueOf(i * 2));
listWriter.integer().writeInt(i);
listWriter.decimal().writeBigEndianBytesToDecimal(BigDecimal.valueOf(i * 3).unscaledValue().toByteArray(),
new ArrowType.Decimal(3, 0));

listWriter.endList();
}
Expand Down Expand Up @@ -304,6 +310,8 @@ public void testCopyStructVector() {
innerStructWriter.start();
innerStructWriter.integer("innerint").writeInt(i * 3);
innerStructWriter.decimal("innerdec", 0, 38).writeDecimal(BigDecimal.valueOf(i * 4));
innerStructWriter.decimal("innerdec", 0, 38).writeBigEndianBytesToDecimal(BigDecimal.valueOf(i * 4)
.unscaledValue().toByteArray(), new ArrowType.Decimal(3, 0));
innerStructWriter.end();
structWriter.end();
}
Expand Down
Expand Up @@ -273,17 +273,24 @@ public void listDecimalType() {
UnionListWriter listWriter = new UnionListWriter(listVector);
DecimalHolder holder = new DecimalHolder();
holder.buffer = allocator.buffer(DecimalUtility.DECIMAL_BYTE_LENGTH);
ArrowType arrowType = new ArrowType.Decimal(10, 0);
for (int i = 0; i < COUNT; i++) {
listWriter.startList();
for (int j = 0; j < i % 7; j++) {
if (j % 2 == 0) {
if (j % 4 == 0) {
listWriter.writeDecimal(new BigDecimal(j));
} else {
} else if (j % 4 == 1) {
DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0);
holder.start = 0;
holder.scale = 0;
holder.precision = 10;
listWriter.write(holder);
} else if (j % 4 == 2) {
DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0);
listWriter.writeDecimal(0, holder.buffer, arrowType);
} else {
byte[] value = BigDecimal.valueOf(j).unscaledValue().toByteArray();
listWriter.writeBigEndianBytesToDecimal(value, arrowType);
}
}
listWriter.endList();
Expand Down

0 comments on commit f67b210

Please sign in to comment.