Skip to content
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

DX-85876: Failure in UnionReader.read after DecimalVector promotion to UnionVector #61

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion java/vector/src/main/codegen/templates/UnionVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.arrow.util.Preconditions;
import org.apache.arrow.vector.BaseValueVector;
import org.apache.arrow.vector.BitVectorHelper;
import org.apache.arrow.vector.DecimalVector;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.ValueVector;
import org.apache.arrow.vector.complex.AbstractStructVector;
Expand Down Expand Up @@ -293,7 +294,10 @@ public StructVector getStruct() {
<#if minor.class?starts_with("Decimal")>
public ${name}Vector get${name}Vector() {
if (${uncappedName}Vector == null) {
throw new IllegalArgumentException("No ${uncappedName} present. Provide ArrowType argument to create a new vector");
${uncappedName}Vector = internalStruct.getChild(fieldName(MinorType.${name?upper_case}), ${name}Vector.class);
if (${uncappedName}Vector == null) {
throw new IllegalArgumentException("No ${uncappedName} present. Provide ArrowType argument to create a new vector");
}
}
return ${uncappedName}Vector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.math.BigDecimal;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.DecimalVector;
import org.apache.arrow.vector.DirtyRootAllocator;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.NonNullableStructVector;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.complex.writer.BaseWriter.StructWriter;
import org.apache.arrow.vector.holders.NullableDecimalHolder;
import org.apache.arrow.vector.holders.NullableIntHolder;
import org.apache.arrow.vector.holders.UnionHolder;
import org.apache.arrow.vector.types.Types;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.ArrowType.ArrowTypeID;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.util.DecimalUtility;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -164,4 +172,41 @@ public void testNoPromoteToUnionWithNull() throws Exception {
lv.close();
}
}

@Test
public void testPromoteToUnionFromDecimal() throws Exception {
try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator);
final DecimalVector v = container.addOrGet("dec",
FieldType.nullable(new ArrowType.Decimal(38, 1, 128)), DecimalVector.class);
final PromotableWriter writer = new PromotableWriter(v, container)) {

container.allocateNew();
container.setValueCount(1);

writer.setPosition(0);
writer.writeDecimal(new BigDecimal("0.1"));
writer.setPosition(1);
writer.writeInt(1);

container.setValueCount(3);

UnionVector unionVector = (UnionVector) container.getChild("dec");
UnionHolder holder = new UnionHolder();

unionVector.get(0, holder);
NullableDecimalHolder decimalHolder = new NullableDecimalHolder();
holder.reader.read(decimalHolder);

assertEquals(1, decimalHolder.isSet);
assertEquals(new BigDecimal("0.1"),
DecimalUtility.getBigDecimalFromArrowBuf(decimalHolder.buffer, 0, decimalHolder.scale, 128));

unionVector.get(1, holder);
NullableIntHolder intHolder = new NullableIntHolder();
holder.reader.read(intHolder);

assertEquals(1, intHolder.isSet);
assertEquals(1, intHolder.value);
}
}
}
Loading