Skip to content

Commit

Permalink
fix(c/driver): return NOT_FOUND for GetTableSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
lidavidm committed Sep 1, 2023
1 parent 932b721 commit 1f26859
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
7 changes: 6 additions & 1 deletion c/driver/postgresql/connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,12 @@ AdbcStatusCode PostgresConnection::GetTableSchema(const char* catalog,
StringBuilderReset(&query);

RAISE_ADBC(result_helper.Prepare());
RAISE_ADBC(result_helper.Execute());
auto result = result_helper.Execute();
if (result != ADBC_STATUS_OK) {
if (std::string(error->sqlstate, 5) == "42P01") {
return ADBC_STATUS_NOT_FOUND;
}
}

auto uschema = nanoarrow::UniqueSchema();
ArrowSchemaInit(uschema.get());
Expand Down
1 change: 1 addition & 0 deletions c/driver/postgresql/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ int TupleReader::GetNext(struct ArrowArray* out) {
struct ArrowArray tmp;
NANOARROW_RETURN_NOT_OK(BuildOutput(&tmp, &error));

PQclear(result_);
// Check the server-side response
result_ = PQgetResult(conn_);
const ExecStatusType pq_status = PQresultStatus(result_);
Expand Down
5 changes: 3 additions & 2 deletions c/driver/sqlite/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ AdbcStatusCode SqliteConnectionGetTableSchema(struct AdbcConnection* connection,
return ADBC_STATUS_INTERNAL;
}

// TODO(apache/arrow-adbc#1025): escape
if (StringBuilderAppend(&query, "%s%s", "SELECT * FROM ", table_name) != 0) {
StringBuilderReset(&query);
SetError(error, "[SQLite] Call to StringBuilderAppend failed");
Expand All @@ -888,8 +889,8 @@ AdbcStatusCode SqliteConnectionGetTableSchema(struct AdbcConnection* connection,
sqlite3_prepare_v2(conn->conn, query.buffer, query.size, &stmt, /*pzTail=*/NULL);
StringBuilderReset(&query);
if (rc != SQLITE_OK) {
SetError(error, "[SQLite] Failed to prepare query: %s", sqlite3_errmsg(conn->conn));
return ADBC_STATUS_INTERNAL;
SetError(error, "[SQLite] GetTableSchema: %s", sqlite3_errmsg(conn->conn));
return ADBC_STATUS_NOT_FOUND;
}

struct ArrowArrayStream stream = {0};
Expand Down
1 change: 1 addition & 0 deletions c/integration/duckdb/duckdb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class DuckDbConnectionTest : public ::testing::Test,

void TestAutocommitDefault() { GTEST_SKIP(); }
void TestMetadataGetTableSchema() { GTEST_SKIP(); }
void TestMetadataGetTableSchemaNotFound() { GTEST_SKIP(); }
void TestMetadataGetTableTypes() { GTEST_SKIP(); }

protected:
Expand Down
13 changes: 13 additions & 0 deletions c/validation/adbc_validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ void ConnectionTest::TestMetadataGetTableSchema() {
{"strings", NANOARROW_TYPE_STRING, NULLABLE}}));
}

void ConnectionTest::TestMetadataGetTableSchemaNotFound() {
ASSERT_THAT(AdbcConnectionNew(&connection, &error), IsOkStatus(&error));
ASSERT_THAT(AdbcConnectionInit(&connection, &database, &error), IsOkStatus(&error));
ASSERT_THAT(quirks()->DropTable(&connection, "thistabledoesnotexist", &error),
IsOkStatus(&error));

Handle<ArrowSchema> schema;
ASSERT_THAT(AdbcConnectionGetTableSchema(&connection, /*catalog=*/nullptr,
/*db_schema=*/nullptr, "thistabledoesnotexist",
&schema.value, &error),
IsStatus(ADBC_STATUS_NOT_FOUND, &error));
}

void ConnectionTest::TestMetadataGetTableTypes() {
ASSERT_THAT(AdbcConnectionNew(&connection, &error), IsOkStatus(&error));
ASSERT_THAT(AdbcConnectionInit(&connection, &database, &error), IsOkStatus(&error));
Expand Down
4 changes: 4 additions & 0 deletions c/validation/adbc_validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class ConnectionTest {

void TestMetadataGetInfo();
void TestMetadataGetTableSchema();
void TestMetadataGetTableSchemaNotFound();
void TestMetadataGetTableTypes();

void TestMetadataGetObjectsCatalogs();
Expand Down Expand Up @@ -219,6 +220,9 @@ class ConnectionTest {
TEST_F(FIXTURE, MetadataCurrentDbSchema) { TestMetadataCurrentDbSchema(); } \
TEST_F(FIXTURE, MetadataGetInfo) { TestMetadataGetInfo(); } \
TEST_F(FIXTURE, MetadataGetTableSchema) { TestMetadataGetTableSchema(); } \
TEST_F(FIXTURE, MetadataGetTableSchemaNotFound) { \
TestMetadataGetTableSchemaNotFound(); \
} \
TEST_F(FIXTURE, MetadataGetTableTypes) { TestMetadataGetTableTypes(); } \
TEST_F(FIXTURE, MetadataGetObjectsCatalogs) { TestMetadataGetObjectsCatalogs(); } \
TEST_F(FIXTURE, MetadataGetObjectsDbSchemas) { TestMetadataGetObjectsDbSchemas(); } \
Expand Down
4 changes: 4 additions & 0 deletions go/adbc/driver/snowflake/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func errToAdbcErr(code adbc.Status, err error) error {
var sqlstate [5]byte
copy(sqlstate[:], []byte(sferr.SQLState))

if sferr.SQLState == "42S02" {
code = adbc.StatusNotFound
}

return adbc.Error{
Code: code,
Msg: sferr.Error(),
Expand Down

0 comments on commit 1f26859

Please sign in to comment.