You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/07/22 15:35:49 UTC

[GitHub] [arrow] jonkeane commented on a change in pull request #10780: ARROW-12688: [R] Use DuckDB to query an Arrow Dataset

jonkeane commented on a change in pull request #10780:
URL: https://github.com/apache/arrow/pull/10780#discussion_r674925488



##########
File path: r/R/duckdb.R
##########
@@ -0,0 +1,91 @@
+# 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.
+
+arrow_duck_connection <- function() {
+  con <- getOption("arrow_duck_con")
+  if (is.null(con) || !DBI::dbIsValid(con)) {
+    con <- DBI::dbConnect(duckdb::duckdb())
+    # Use the same CPU count that the arrow library is set to
+    DBI::dbExecute(con, paste0("PRAGMA threads=", cpu_count()))
+    options(arrow_duck_con = con)
+  }
+  con
+}
+
+# TODO: note that this is copied from dbplyr
+unique_arrow_tablename <- function () {
+  i <- getOption("arrow_table_name", 0) + 1
+  options(arrow_table_name = i)
+  sprintf("arrow_%03i", i)
+}
+
+.alchemize_to_duckdb <- function(x, ...) {
+  rb_to_duckdb(x, groups = x$group, ...)
+}
+
+rb_to_duckdb <- function(x, con = arrow_duck_connection(), groups = NULL, auto_disconnect = TRUE) {
+  table_name <- unique_arrow_tablename()
+  duckdb::duckdb_register_arrow(con, table_name, x)
+
+  tbl <- tbl(con, table_name)
+  if (length(groups) > 0 && !is.null(groups)) {
+    tbl <- dplyr::group_by(tbl, !!rlang::sym(groups))
+  }
+
+  if (auto_disconnect) {
+    # this will add the correct connection disconnection when the tbl is gced.
+    # we should probably confirm that this use of src$disco is kosher.
+    tbl$src$disco <- duckdb_disconnector(con, table_name)

Review comment:
       This setups up a specific cleanup step for how we register the table. This [is very similar to what dbplyr does](https://github.com/tidyverse/dbplyr/blob/47e53ce30402d41ae4b38c803de12e63d64a9b6c/R/src_dbi.R#L113) but might be reaching into the internals a bit too much (though works very very well in a way that deleting the table in `collect()` doesn't). If we go this route, I'll open an issue with dbplyr to ask if this is ok / if this is something that they want a PR to expose more for packages like ours.




-- 
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