Skip to content

Commit

Permalink
ARROW-7405: [Java] ListVector isEmpty API is incorrect
Browse files Browse the repository at this point in the history
Related to [ARROW-7405](https://issues.apache.org/jira/browse/ARROW-7405).

Currently isEmpty API is always return false in BaseRepeatedValueVector, and its subclass ListVector did not overwrite this method.
This will lead to incorrect result, for example, a ListVector with data [1,2], null, [], [5,6] should get [false, false, true, false] with this API, but now it would return [false, false, false, false].

This change implements `isEmpty(int index)` for a `ListVector` that will return `true` if the index is an empty list or a null value, and `false` otherwise.

Closes #6044 from tianchen92/ARROW-7405 and squashes the following commits:

797f9b7 <tianchen> fix style and comments
bbe35db <tianchen> treat null as empty
41e0ed5 <tianchen> resolve some comments
12810e4 <tianchen> ARROW-7405:  ListVector isEmpty API is incorrect

Authored-by: tianchen <niki.lj@alibaba-inc.com>
Signed-off-by: Bryan Cutler <cutlerb@gmail.com>
  • Loading branch information
tianchen92 authored and kszucs committed Feb 7, 2020
1 parent 1c8ff91 commit 7e81725
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -719,14 +719,30 @@ public Object getObject(int index) {
/**
* Check if element at given index is null.
*
* @param index position of element
* @param index position of element
* @return true if element at given index is null, false otherwise
*/
@Override
public boolean isNull(int index) {
return (isSet(index) == 0);
}

/**
* Check if element at given index is empty list.
* @param index position of element
* @return true if element at given index is empty list or NULL, false otherwise
*/
@Override
public boolean isEmpty(int index) {
if (isNull(index)) {
return true;
} else {
final int start = offsetBuffer.getInt(index * OFFSET_WIDTH);
final int end = offsetBuffer.getInt((index + 1) * OFFSET_WIDTH);
return start == end;
}
}

/**
* Same as {@link #isNull(int)}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,28 @@ public void testGetBufferSizeFor() {
}
}

@Test
public void testIsEmpty() {
try (final ListVector vector = ListVector.empty("list", allocator)) {
UnionListWriter writer = vector.getWriter();
writer.allocate();

// set values [1,2], null, [], [5,6]
writeIntValues(writer, new int[] {1, 2});
writer.setPosition(2);
writeIntValues(writer, new int[] {});
writeIntValues(writer, new int[] {5, 6});
writer.setValueCount(4);

assertFalse(vector.isEmpty(0));
assertTrue(vector.isNull(1));
assertTrue(vector.isEmpty(1));
assertFalse(vector.isNull(2));
assertTrue(vector.isEmpty(2));
assertFalse(vector.isEmpty(3));
}
}

private void writeIntValues(UnionListWriter writer, int[] values) {
writer.startList();
for (int v: values) {
Expand Down

0 comments on commit 7e81725

Please sign in to comment.