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


##########
r/adbcdrivermanager/R/adbc.R:
##########
@@ -0,0 +1,440 @@
+# 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.
+
+#' Databases
+#'
+#' @param driver An [adbc_driver()].
+#' @param database An [adbc_database][adbc_database_init].

Review Comment:
   Hmm, where is this parameter? Am I missing something about R? (Ditto for 
`...` and `options` below, which seem redundant, though I suppose they get 
consolidated?)



##########
r/adbcdrivermanager/configure:
##########
@@ -0,0 +1,53 @@
+# 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.
+
+# If we are building from within the ADBC repo, we copy the files we
+# need to ensure commit-level consistency
+if [ -f "../../adbc.h" ]; then
+  echo "Copying files from local ADBC"
+  rm -f src/adbc.h src/adbc_driver_manager.h src/adbc_driver_manager.cc
+  cp ../../adbc.h \
+    ../../c/driver_manager/adbc_driver_manager.h \
+    ../../c/driver_manager/adbc_driver_manager.cc \
+    src
+fi
+
+if [ -f "src/adbc.h" ] && [ -f "src/adbc_driver_manager.h" ] && [ -f 
"src/adbc_driver_manager.cc" ]; then
+  echo "Found vendored ADBC"
+  exit 0
+fi
+
+# We have a situation where the package has been built via R CMD build
+# but there is no vendored adbc.h or driver manager. This occurs with
+# remotes::install_github() with the default arguments. In this case, pull
+# the latest files from GitHub. To ensure commit-level consistency,
+# use remotes::install_github(build = FALSE)
+curl -L https://github.com/apache/arrow-adbc/raw/main/adbc.h \
+  --output src/adbc.h --silent
+curl -L 
https://github.com/apache/arrow-adbc/raw/main/c/driver_manager/adbc_driver_manager.h
 \
+  --output src/adbc_driver_manager.h --silent
+curl -L 
https://github.com/apache/arrow-adbc/raw/main/c/driver_manager/adbc_driver_manager.cc
 \
+  --output src/adbc_driver_manager.cc --silent
+
+if [ -f "src/adbc.h" ] && [ -f "src/adbc_driver_manager.h" ] && [ -f 
"src/adbc_driver_manager.cc" ]; then
+  echo "Fetched ADBC from https://github.com/apache/arrow-adbc";
+  exit 0
+fi
+
+echo "Vendored src/adbc.h and/or src/adbc_driver_manager.cc are missing"
+echo "This source package was probably built incorrectly and it's probably not 
your fault"

Review Comment:
   Heh.
   
   The automatic fetching from GitHub above looks somewhat sketchy, but I 
accept that it's necessary/convenient.



##########
r/adbcdrivermanager/src/radbc.cc:
##########
@@ -0,0 +1,434 @@
+// 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.
+
+#define R_NO_REMAP
+#include <R.h>
+#include <Rinternals.h>
+
+#include <adbc.h>
+#include "adbc_driver_manager.h"
+
+#include "radbc.h"
+
+static AdbcError global_error_;
+
+static void adbc_global_error_init(void) { memset(&global_error_, 0, 
sizeof(AdbcError)); }
+
+static void adbc_global_error_reset(void) {
+  if (global_error_.release != nullptr) {
+    global_error_.release(&global_error_);
+  }
+
+  adbc_global_error_init();
+}
+
+static const char* adbc_global_error_message() {
+  if (global_error_.message == nullptr) {
+    return "";
+  } else {
+    return global_error_.message;
+  }
+}
+
+static void adbc_global_error_warn(int code, const char* context) {
+  if (code != ADBC_STATUS_OK) {
+    Rf_warning("<%s> %s", context, adbc_global_error_message());
+  }
+}
+
+static void adbc_global_error_stop(int code, const char* context) {
+  if (code != ADBC_STATUS_OK) {
+    Rf_error("<%s> %s", context, adbc_global_error_message());
+  }
+}
+
+static void finalize_database_xptr(SEXP database_xptr) {
+  auto database = 
reinterpret_cast<AdbcDatabase*>(R_ExternalPtrAddr(database_xptr));
+  if (database == nullptr) {
+    return;
+  }
+
+  if (database->private_data != nullptr) {
+    int status = AdbcDatabaseRelease(database, &global_error_);
+    adbc_global_error_warn(status, "finalize_database_xptr()");
+  }
+
+  adbc_xptr_default_finalize<AdbcDatabase>(database_xptr);
+}
+
+extern "C" SEXP RAdbcLoadDriver(SEXP driver_name_sexp, SEXP entrypoint_sexp) {
+  const char* driver_name = adbc_as_const_char(driver_name_sexp);
+  const char* entrypoint = adbc_as_const_char(entrypoint_sexp);
+
+  SEXP driver_xptr = PROTECT(adbc_allocate_xptr<AdbcDriver>());
+  auto driver = adbc_from_xptr<AdbcDriver>(driver_xptr);
+
+  adbc_global_error_reset();

Review Comment:
   I guess the single-threadedness of R is preventing us from trampling the 
global state here?



##########
r/adbcdrivermanager/R/driver_void.R:
##########
@@ -0,0 +1,89 @@
+# 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.
+
+#' Create ADBC drivers
+#'
+#' Creates the R object representation of an ADBC driver, which consists of a
+#' name and an initializer function with an optional subclass to control
+#' finer-grained behaviour at the R level.
+#'
+#' @param x,entrypoint An ADBC driver may be defined either as an init function
+#'   or as an identifier with an entrypoint name. A driver init func
+#'   must be an external pointer to a DL_FUNC with the type
+#'   `AdbcDriverInitFunc` specified in the adbc.h header.
+#' @param ... Further key/value parameters to store with the (R-level) driver
+#'   object.
+#' @param subclass An optional subclass for finer-grained control of
+#'   behaviour at the R level.
+#'
+#' @return An object of class 'adbc_driver'
+#' @export
+#'
+#' @examples
+#' adbc_driver_void()
+#'
+adbc_driver_void <- function() {
+  if (is.null(internal_driver_env$void)) {
+    internal_driver_env$void <- adbc_driver(
+      .Call(RAdbcVoidDriverInitFunc),
+      subclass = "adbc_driver_void"
+    )
+  }
+
+  internal_driver_env$void
+}
+
+#' @rdname adbc_driver_void
+#' @export
+adbc_driver <- function(x, entrypoint = NULL, ..., subclass = character()) {
+  if (inherits(x, "adbc_driver_init_func")) {
+    driver <- .Call(RAdbcLoadDriverFromInitFunc, x)
+    driver$driver_init_func <- x
+  } else {
+    driver <- .Call(RAdbcLoadDriver, x, entrypoint)
+    driver$name <- x
+    driver$entrypoint <- entrypoint
+  }
+
+  args <- list(...)
+  for (i in seq_along(args)) {
+    driver[[names(args)[i]]] <- args[[i]]
+  }
+
+  class(driver) <- c(subclass, class(driver))
+  driver
+}
+
+internal_driver_env <- new.env(parent = emptyenv())
+
+#' @export
+print.adbc_driver <- function(x, ...) {
+  str(x, ...)
+}
+
+#' @importFrom utils str
+#' @export
+str.adbc_driver <- function(object, ...) {

Review Comment:
   Hmm, connections have AdbcConnectionGetInfo to let you introspect them, but 
maybe that should've been on the database or driver level too. Then we could 
try to define this generically.



##########
r/adbcdrivermanager/src/radbc.cc:
##########
@@ -0,0 +1,434 @@
+// 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.
+
+#define R_NO_REMAP
+#include <R.h>
+#include <Rinternals.h>
+
+#include <adbc.h>
+#include "adbc_driver_manager.h"
+
+#include "radbc.h"
+
+static AdbcError global_error_;
+
+static void adbc_global_error_init(void) { memset(&global_error_, 0, 
sizeof(AdbcError)); }
+
+static void adbc_global_error_reset(void) {
+  if (global_error_.release != nullptr) {
+    global_error_.release(&global_error_);
+  }
+
+  adbc_global_error_init();
+}
+
+static const char* adbc_global_error_message() {
+  if (global_error_.message == nullptr) {
+    return "";
+  } else {
+    return global_error_.message;
+  }
+}
+
+static void adbc_global_error_warn(int code, const char* context) {
+  if (code != ADBC_STATUS_OK) {
+    Rf_warning("<%s> %s", context, adbc_global_error_message());
+  }
+}
+
+static void adbc_global_error_stop(int code, const char* context) {
+  if (code != ADBC_STATUS_OK) {
+    Rf_error("<%s> %s", context, adbc_global_error_message());
+  }
+}
+
+static void finalize_database_xptr(SEXP database_xptr) {
+  auto database = 
reinterpret_cast<AdbcDatabase*>(R_ExternalPtrAddr(database_xptr));
+  if (database == nullptr) {
+    return;
+  }
+
+  if (database->private_data != nullptr) {
+    int status = AdbcDatabaseRelease(database, &global_error_);
+    adbc_global_error_warn(status, "finalize_database_xptr()");
+  }
+
+  adbc_xptr_default_finalize<AdbcDatabase>(database_xptr);
+}
+
+extern "C" SEXP RAdbcLoadDriver(SEXP driver_name_sexp, SEXP entrypoint_sexp) {
+  const char* driver_name = adbc_as_const_char(driver_name_sexp);
+  const char* entrypoint = adbc_as_const_char(entrypoint_sexp);
+
+  SEXP driver_xptr = PROTECT(adbc_allocate_xptr<AdbcDriver>());
+  auto driver = adbc_from_xptr<AdbcDriver>(driver_xptr);
+
+  adbc_global_error_reset();

Review Comment:
   Relatedly, what prevents us from passing in an error here as with other 
functions? Or stack-allocating one and raising the error back up to R?



##########
r/adbcdrivermanager/R/driver_monkey.R:
##########
@@ -0,0 +1,61 @@
+# 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.
+
+#' Monkey see, monkey do!
+#'
+#' A driver whose query results are set in advance.
+#'
+#' @return An object of class 'adbc_driver_monkey'
+#' @export
+#'
+#' @examples
+#' db <- adbc_database_init(adbc_driver_monkey())
+#' con <- adbc_connection_init(db)
+#' stmt <- adbc_statement_init(con, mtcars)
+#' result <- adbc_statement_execute_query(stmt)
+#' as.data.frame(result$get_next())
+#'
+adbc_driver_monkey <- function() {
+  if (is.null(internal_driver_env$monkey)) {
+    internal_driver_env$monkey <- adbc_driver(
+      .Call(RAdbcMonkeyDriverInitFunc),
+      subclass = "adbc_driver_monkey"
+    )
+  }
+
+  internal_driver_env$monkey
+}
+
+#' @export
+adbc_database_init.adbc_driver_monkey <- function(driver, ...) {
+  adbc_database_init_default(driver, subclass = "adbc_database_monkey")
+}
+
+#' @export
+adbc_connection_init.adbc_database_monkey <- function(database, ...) {
+  adbc_connection_init_default(database, subclass = "adbc_connection_monkey")
+}
+
+#' @export
+adbc_statement_init.adbc_connection_monkey <- function(connection, stream, 
...) {

Review Comment:
   so we can override the R-level methods for more behavior, ok. 
   
   I suppose if you really wanted, you could use the bind methods to map this 
functionality in a generic way. But this is convenient for testing.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to