You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "WillAyd (via GitHub)" <gi...@apache.org> on 2023/04/07 19:34:34 UTC

[GitHub] [arrow-adbc] WillAyd commented on a diff in pull request #577: feat(c/driver/postgres): Implement GetTableSchema

WillAyd commented on code in PR #577:
URL: https://github.com/apache/arrow-adbc/pull/577#discussion_r1160917606


##########
c/driver/postgresql/connection.cc:
##########
@@ -47,7 +48,97 @@ AdbcStatusCode PostgresConnection::GetTableSchema(const char* catalog,
                                                   const char* table_name,
                                                   struct ArrowSchema* schema,
                                                   struct AdbcError* error) {
-  return ADBC_STATUS_NOT_IMPLEMENTED;
+  // TODO: sqlite uses a StringBuilder class that seems roughly equivalent
+  // to a similar tool in arrow/util/string_builder.h but wasn't clear on
+  // structure and how to use, so relying on simplistic appends for now
+  AdbcStatusCode final_status;
+
+  std::string query =
+      "SELECT attname, atttypid "
+      "FROM pg_catalog.pg_class AS cls "
+      "INNER JOIN pg_catalog.pg_attribute AS attr ON cls.oid = attr.attrelid "
+      "INNER JOIN pg_catalog.pg_type AS typ ON attr.atttypid = typ.oid "
+      "WHERE attr.attnum >= 0 AND cls.oid = '";
+  if (db_schema != nullptr) {
+    query.append(db_schema);
+    query.append(".");
+  }
+  query.append(table_name);
+  query.append("'::regclass::oid");
+
+  // char* stmt = PQescapeLiteral(conn_, query.c_str(), query.length());
+  // if (stmt == nullptr) {
+  //   SetError(error, "Failed to get table schema: ", PQerrorMessage(conn_));
+  //  return ADBC_STATUS_INVALID_ARGUMENT;
+  // }
+
+  pg_result* result = PQexec(conn_, query.c_str());
+  // PQfreemem(stmt);
+
+  ExecStatusType pq_status = PQresultStatus(result);
+  if (pq_status == PGRES_TUPLES_OK) {
+    int num_rows = PQntuples(result);
+    ArrowSchemaInit(schema);
+    CHECK_NA_ADBC(ArrowSchemaSetTypeStruct(schema, num_rows), error);
+
+    // TODO: much of this code is copied from statement.cc InferSchema

Review Comment:
   I _think_ this should be moved out of statement.cc and probably put into type.cc



##########
c/driver/postgresql/connection.cc:
##########
@@ -47,7 +48,97 @@ AdbcStatusCode PostgresConnection::GetTableSchema(const char* catalog,
                                                   const char* table_name,
                                                   struct ArrowSchema* schema,
                                                   struct AdbcError* error) {
-  return ADBC_STATUS_NOT_IMPLEMENTED;
+  // TODO: sqlite uses a StringBuilder class that seems roughly equivalent

Review Comment:
   To clarify this comment a bit further, knowing that the sqlite connector was just of put together as a POC I wasn't sure if we wanted to pull the StringBuilder out of that and make it shared across all drivers or if I should provide a separate implementation for postgres (seems like that might have been started?)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org