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

ARROW-5583: [Java] When the isSet of a NullableValueHolder is 0, the buffer field should not be used #4543

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ public void set(int index, NullableVarBinaryHolder holder) {
assert index >= 0;
fillHoles(index);
BitVectorHelper.setValidityBit(validityBuffer, index, holder.isSet);
final int dataLength = holder.end - holder.start;
final int dataLength = holder.isSet == 0 ? 0 : holder.end - holder.start;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the arrow spec, when a variable width slot is null the length should be zero. What symptom were you seeing that this is trying to fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see this is on the write path.

I think it is clearer if you handle this the other way:

if (holder.isSet) {
/// set the bytes
}
lastSet = index;

Copy link
Contributor Author

@liyafan82 liyafan82 Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion.
I have revised the code. Please see if it looks better.

final int startOffset = getStartOffset(index);
offsetBuffer.setInt((index + 1) * OFFSET_WIDTH, startOffset + dataLength);
valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
if (holder.isSet != 0) {
valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
}
lastSet = index;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ public void set(int index, NullableVarCharHolder holder) {
assert index >= 0;
fillHoles(index);
BitVectorHelper.setValidityBit(validityBuffer, index, holder.isSet);
final int dataLength = holder.end - holder.start;
final int dataLength = holder.isSet == 0 ? 0 : holder.end - holder.start;
final int startOffset = getStartOffset(index);
offsetBuffer.setInt((index + 1) * OFFSET_WIDTH, startOffset + dataLength);
valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
if (holder.isSet != 0) {
valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
}
lastSet = index;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.apache.arrow.memory.BaseAllocator;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.holders.NullableVarBinaryHolder;
import org.apache.arrow.vector.holders.NullableVarCharHolder;
import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
import org.apache.arrow.vector.types.Types.MinorType;
import org.apache.arrow.vector.types.pojo.ArrowType;
Expand Down Expand Up @@ -2051,4 +2053,64 @@ public void testDefaultAllocNewAll() {

}
}

@Test
public void testSetNullableVarCharHolder() {
try (VarCharVector vector = new VarCharVector("", allocator)) {
vector.allocateNew(100, 10);

NullableVarCharHolder nullHolder = new NullableVarCharHolder();
nullHolder.isSet = 0;

NullableVarCharHolder stringHolder = new NullableVarCharHolder();
stringHolder.isSet = 1;

String str = "hello";
ArrowBuf buf = allocator.buffer(16);
buf.setBytes(0, str.getBytes());

stringHolder.start = 0;
stringHolder.end = str.length();
stringHolder.buffer = buf;

vector.set(0, nullHolder);
vector.set(1, stringHolder);

// verify results
assertTrue(vector.isNull(0));
assertEquals(str, new String(vector.get(1)));

buf.close();
}
}

@Test
public void testSetNullableVarBinaryHolder() {
try (VarBinaryVector vector = new VarBinaryVector("", allocator)) {
vector.allocateNew(100, 10);

NullableVarBinaryHolder nullHolder = new NullableVarBinaryHolder();
nullHolder.isSet = 0;

NullableVarBinaryHolder binHolder = new NullableVarBinaryHolder();
binHolder.isSet = 1;

String str = "hello";
ArrowBuf buf = allocator.buffer(16);
buf.setBytes(0, str.getBytes());

binHolder.start = 0;
binHolder.end = str.length();
binHolder.buffer = buf;

vector.set(0, nullHolder);
vector.set(1, binHolder);

// verify results
assertTrue(vector.isNull(0));
assertEquals(str, new String(vector.get(1)));

buf.close();
}
}
}