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

[Java] ArrowIPCTableWriter writes en empty batch in the case of an empty table. #11883

Merged
merged 1 commit into from
Oct 11, 2022
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
10 changes: 9 additions & 1 deletion java/src/main/native/src/TableJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,15 @@ class native_arrow_ipc_writer_handle final {
writer = *tmp_writer;
initialized = true;
}
writer->WriteTable(*arrow_tab, max_chunk);
if (arrow_tab->num_rows() == 0) {
// Arrow C++ IPC writer will not write an empty batch in the case of an
// empty table, so need to write an empty batch explicitly.
// For more please see https://issues.apache.org/jira/browse/ARROW-17912.
auto empty_batch = arrow::RecordBatch::MakeEmpty(arrow_tab->schema());
writer->WriteRecordBatch(*(*empty_batch));
} else {
writer->WriteTable(*arrow_tab, max_chunk);
}
}

void close() {
Expand Down
29 changes: 29 additions & 0 deletions java/src/test/java/ai/rapids/cudf/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7937,6 +7937,35 @@ void testArrowIPCWriteToBufferChunked() {
}
}

@Test
void testArrowIPCWriteEmptyToBufferChunked() {
try (Table emptyTable = new Table.TestBuilder().timestampDayColumn().build();
MyBufferConsumer consumer = new MyBufferConsumer()) {
ArrowIPCWriterOptions options = ArrowIPCWriterOptions.builder()
.withColumnNames("day")
.build();
try (TableWriter writer = Table.writeArrowIPCChunked(options, consumer)) {
writer.write(emptyTable);
}
try (StreamedTableReader reader = Table.readArrowIPCChunked(new MyBufferProvider(consumer))) {
boolean done = false;
int count = 0;
while (!done) {
try (Table t = reader.getNextIfAvailable()) {
if (t == null) {
done = true;
} else {
Comment on lines +7951 to +7957
Copy link
Contributor

@ttnghia ttnghia Oct 8, 2022

Choose a reason for hiding this comment

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

Nit: We can do this:

Suggested change
boolean done = false;
int count = 0;
while (!done) {
try (Table t = reader.getNextIfAvailable()) {
if (t == null) {
done = true;
} else {
int count = 0;
while (true) {
try (Table t = reader.getNextIfAvailable()) {
if (t == null) {
break;
} else {

assertTablesAreEqual(emptyTable, t);
count++;
}
}
}
// Expect one empty batch for the empty table.
assertEquals(1, count);
}
}
}

@Test
void testORCWriteToBufferChunked() {
String[] selectedColumns = WriteUtils.getAllColumns(false);
Expand Down