Skip to content

Commit

Permalink
Fix JNI for TableWithMeta to use schema_info instead of column_names (#…
Browse files Browse the repository at this point in the history
…11566)

After #11364 `TableWithMeta` no longer returns the column names for a table since it is looking at the old `column_names` field instead of the newer `schema_info` field in the table metadata.  This updates the JNI to use the `schema_info` to get the column names and adds a test for this API which was missing before.

Authors:
  - Jason Lowe (https://github.com/jlowe)

Approvers:
  - Ryan Lee (https://github.com/rwlee)
  - Peixin (https://github.com/pxLi)

URL: #11566
  • Loading branch information
jlowe authored Aug 19, 2022
1 parent 7ad1a8b commit 5ffee5c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion java/src/main/java/ai/rapids/cudf/TableWithMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String[] getColumnNames() {
}

@Override
public void close() throws Exception {
public void close() {
if (handle != 0) {
close(handle);
handle = 0;
Expand Down
5 changes: 3 additions & 2 deletions java/src/main/native/src/TableJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,11 +1357,12 @@ JNIEXPORT jobjectArray JNICALL Java_ai_rapids_cudf_TableWithMeta_getColumnNames(
try {
cudf::jni::auto_set_device(env);
auto ptr = reinterpret_cast<cudf::io::table_with_metadata *>(handle);
auto length = ptr->metadata.column_names.size();
auto length = ptr->metadata.schema_info.size();
auto ret = static_cast<jobjectArray>(
env->NewObjectArray(length, env->FindClass("java/lang/String"), nullptr));
for (size_t i = 0; i < length; i++) {
env->SetObjectArrayElement(ret, i, env->NewStringUTF(ptr->metadata.column_names[i].c_str()));
env->SetObjectArrayElement(ret, i,
env->NewStringUTF(ptr->metadata.schema_info[i].name.c_str()));
}

return ret;
Expand Down
24 changes: 24 additions & 0 deletions java/src/test/java/ai/rapids/cudf/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ void testReadJSONBufferWithOffset() {
}
}

@Test
void testReadJSONTableWithMeta() {
JSONOptions opts = JSONOptions.builder()
.build();
byte[] data = ("{ \"A\": 1, \"B\": 2, \"C\": \"X\"}\n" +
"{ \"A\": 2, \"B\": 4, \"C\": \"Y\"}\n" +
"{ \"A\": 3, \"B\": 6, \"C\": \"Z\"}\n" +
"{ \"A\": 4, \"B\": 8, \"C\": \"W\"}\n").getBytes(StandardCharsets.UTF_8);
final int numBytes = data.length;
try (HostMemoryBuffer hostbuf = HostMemoryBuffer.allocate(numBytes)) {
hostbuf.setBytes(0, data, 0, numBytes);
try (Table expected = new Table.TestBuilder()
.column(1L, 2L, 3L, 4L)
.column(2L, 4L, 6L, 8L)
.column("X", "Y", "Z", "W")
.build();
TableWithMeta tablemeta = Table.readJSON(opts, hostbuf, 0, numBytes);
Table table = tablemeta.releaseTable()) {
assertArrayEquals(new String[] { "A", "B", "C" }, tablemeta.getColumnNames());
assertTablesAreEqual(expected, table);
}
}
}

@Test
void testReadCSVPrune() {
Schema schema = Schema.builder()
Expand Down

0 comments on commit 5ffee5c

Please sign in to comment.