You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by GitBox <gi...@apache.org> on 2022/08/15 19:17:36 UTC

[GitHub] [arrow-adbc] lidavidm commented on a diff in pull request #65: [C] Basic libpq-based driver

lidavidm commented on code in PR #65:
URL: https://github.com/apache/arrow-adbc/pull/65#discussion_r946050839


##########
c/drivers/postgres/statement.cc:
##########
@@ -0,0 +1,283 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "statement.h"
+
+#include <netinet/in.h>
+#include <cerrno>
+#include <cstring>
+#include <iostream>
+#include <memory>
+#include <vector>
+
+#include <adbc.h>
+#include <libpq-fe.h>
+#include <nanoarrow.h>
+
+#include "connection.h"
+#include "util.h"
+
+namespace adbcpq {
+
+namespace {
+/// \brief An ArrowArrayStream that reads tuples from a PGresult.
+class TupleReader {
+ public:
+  explicit TupleReader(PGresult* result) : result_(result) {}
+
+  int GetSchema(struct ArrowSchema* out) {
+    std::memset(out, 0, sizeof(*out));
+    const int num_fields = PQnfields(result_);
+    NA_RETURN_NOT_OK(ArrowSchemaInit(out, NANOARROW_TYPE_STRUCT));
+    NA_RETURN_NOT_OK(ArrowSchemaAllocateChildren(out, num_fields));
+    for (int i = 0; i < num_fields; i++) {
+      ArrowType field_type = NANOARROW_TYPE_NA;
+      const Oid pg_type = PQftype(result_, i);
+      switch (pg_type) {
+        // TODO: at startup, query pg_type to build up this mapping instead of hardcoding
+        // it
+        case 16:  // BOOLOID
+          field_type = NANOARROW_TYPE_BOOL;
+          break;
+        case 20:  // INT8OID
+          field_type = NANOARROW_TYPE_INT64;
+          break;
+        case 21:  // INT2OID
+          field_type = NANOARROW_TYPE_INT16;
+          break;
+        case 23:  // INT4OID
+          field_type = NANOARROW_TYPE_INT32;
+          break;
+        default:
+          last_error_ =
+              StringBuilder("[libpq] Column #", i + 1, " (\"", PQfname(result_, i),
+                            "\") has unknown type code ", pg_type);
+          return ENOTSUP;
+      }
+      NA_RETURN_NOT_OK(ArrowSchemaInit(out->children[i], field_type));
+      NA_RETURN_NOT_OK(ArrowSchemaSetName(out->children[i], PQfname(result_, i)));
+    }
+
+    NA_RETURN_NOT_OK(ArrowSchemaDeepCopy(out, &schema_));
+    return 0;
+  }
+
+  int GetNext(struct ArrowArray* out) {
+    if (!result_) {
+      out->release = nullptr;
+      return 0;
+    }
+
+    const int num_rows = PQntuples(result_);
+
+    NA_RETURN_NOT_OK(ArrowArrayInit(out, NANOARROW_TYPE_STRUCT));
+    NA_RETURN_NOT_OK(ArrowArrayAllocateChildren(out, schema_.n_children));
+
+    std::vector<struct ArrowSchemaView> fields(schema_.n_children);
+
+    for (int col = 0; col < schema_.n_children; col++) {
+      NA_RETURN_NOT_OK(ArrowSchemaViewInit(&fields[col], schema_.children[col], nullptr));
+      NA_RETURN_NOT_OK(ArrowArrayInit(out->children[col], fields[col].data_type));
+      NA_RETURN_NOT_OK(
+          ArrowBitmapReserve(ArrowArrayValidityBitmap(out->children[col]), num_rows));
+      switch (fields[col].data_type) {
+        case NANOARROW_TYPE_INT32:
+          NA_RETURN_NOT_OK(ArrowBufferReserve(ArrowArrayBuffer(out->children[col], 1),
+                                              num_rows * sizeof(int32_t)));
+          break;
+        default:
+          last_error_ = StringBuilder("[libpq] Column #", col + 1, " (\"",
+                                      schema_.children[col]->name,
+                                      "\") has unsupported type ", fields[col].data_type);
+          return ENOTSUP;
+      }
+    }
+
+    for (int row = 0; row < num_rows; row++) {
+      for (int col = 0; col < schema_.n_children; col++) {
+        struct ArrowBitmap* bitmap = ArrowArrayValidityBitmap(out->children[col]);
+        NA_RETURN_NOT_OK(ArrowBitmapAppend(bitmap, !PQgetisnull(result_, row, col), 1));
+
+        switch (fields[col].data_type) {
+          case NANOARROW_TYPE_INT32: {
+            struct ArrowBuffer* buffer = ArrowArrayBuffer(out->children[col], 1);
+            // TODO: assert PQgetlength is 4
+            NA_RETURN_NOT_OK(ArrowBufferAppendInt32(
+                buffer, ntohl(*(reinterpret_cast<int*>(PQgetvalue(result_, row, col))))));
+            break;
+          }
+          default:
+            last_error_ = StringBuilder(
+                "[libpq] Column #", col + 1, " (\"", schema_.children[col]->name,
+                "\") has unsupported type ", fields[col].data_type);
+            return ENOTSUP;
+        }
+      }
+    }
+
+    for (int col = 0; col < schema_.n_children; col++) {
+      NA_RETURN_NOT_OK(ArrowArrayFinishBuilding(out->children[col], 0));
+      out->children[col]->length = num_rows;
+    }
+    NA_RETURN_NOT_OK(ArrowArrayFinishBuilding(out, 0));
+    out->length = num_rows;
+
+    Release();
+    return 0;
+  }
+
+  const char* last_error() const { return last_error_.c_str(); }
+
+  void Release() {
+    if (result_) {
+      PQclear(result_);
+      result_ = nullptr;
+    }
+    if (schema_.release) {
+      schema_.release(&schema_);
+      schema_.release = nullptr;

Review Comment:
   Fixed - thanks for catching that!



-- 
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: issues-unsubscribe@arrow.apache.org

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