Skip to content

Commit

Permalink
fix(java/driver/jdbc): check for existence when getting table schema (#…
Browse files Browse the repository at this point in the history
…567)

Fixes #565.
  • Loading branch information
lidavidm authored Mar 31, 2023
1 parent 2b8e429 commit 639ca71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.arrow.adbc.core.AdbcConnection;
import org.apache.arrow.adbc.core.AdbcException;
import org.apache.arrow.adbc.core.AdbcStatement;
import org.apache.arrow.adbc.core.AdbcStatusCode;
import org.apache.arrow.adbc.core.BulkIngestMode;
import org.apache.arrow.adbc.core.IsolationLevel;
import org.apache.arrow.adbc.core.StandardSchemas;
Expand Down Expand Up @@ -121,6 +122,21 @@ public ArrowReader getObjects(
@Override
public Schema getTableSchema(String catalog, String dbSchema, String tableName)
throws AdbcException {
// Check for existence
try (final ResultSet rs =
connection.getMetaData().getTables(catalog, dbSchema, tableName, /*tableTypes*/ null)) {
if (!rs.next()) {
throw new AdbcException(
JdbcDriverUtil.prefixExceptionMessage("Table not found: " + tableName), /*cause*/
null,
AdbcStatusCode.NOT_FOUND, /*sqlState*/
null, /*vendorCode*/
0);
}
} catch (SQLException e) {
throw JdbcDriverUtil.fromSqlException(e);
}

// Reconstruct the schema from the metadata
// XXX: this may be inconsistent with reading the table
final List<Field> fields = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.arrow.adbc.driver.testsuite;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -28,8 +29,10 @@
import java.util.stream.IntStream;
import org.apache.arrow.adbc.core.AdbcConnection;
import org.apache.arrow.adbc.core.AdbcDatabase;
import org.apache.arrow.adbc.core.AdbcException;
import org.apache.arrow.adbc.core.AdbcInfoCode;
import org.apache.arrow.adbc.core.AdbcStatement;
import org.apache.arrow.adbc.core.AdbcStatusCode;
import org.apache.arrow.adbc.core.BulkIngestMode;
import org.apache.arrow.adbc.core.StandardSchemas;
import org.apache.arrow.memory.BufferAllocator;
Expand Down Expand Up @@ -306,6 +309,15 @@ public void getTableSchema() throws Exception {
.isEqualTo(schema);
}

@Test
public void getTableSchemaDoesNotExist() throws Exception {
final AdbcException thrown =
assertThrows(
AdbcException.class,
() -> connection.getTableSchema(/*catalog*/ null, /*dbSchema*/ null, "DOESNOTEXIST"));
assertThat(thrown.getStatus()).isEqualTo(AdbcStatusCode.NOT_FOUND);
}

@Test
public void getTableTypes() throws Exception {
try (final ArrowReader reader = connection.getTableTypes()) {
Expand Down

0 comments on commit 639ca71

Please sign in to comment.