Skip to content

Commit

Permalink
ARROW-7405: [Java] ListVector isEmpty API is incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
tianchen92 committed Feb 5, 2020
1 parent 25fd97b commit 12810e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -719,14 +719,29 @@ 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, false otherwise
*/
public boolean isEmpty(int index) {
if (isSet(index) == 0) {
return false;
} 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,33 @@ 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);

assertTrue(vector.isNull(1));
assertFalse(vector.isEmpty(1));

assertFalse(vector.isNull(2));
assertTrue(vector.isEmpty(2));

assertFalse(vector.isEmpty(0));
assertFalse(vector.isEmpty(3));

}
}


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

0 comments on commit 12810e4

Please sign in to comment.