Skip to content

Commit a6c4f5d

Browse files
committed
feat(r): support target catalog/schema for ingestion
1 parent d380961 commit a6c4f5d

5 files changed

Lines changed: 64 additions & 2 deletions

File tree

r/adbcdrivermanager/R/helpers.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#' @param mode One of `"create"`, `"append"`, `"replace"`, `"create_append"` (error if the schema
3131
#' is not compatible or append otherwise), or `"default"` (use the `adbc.ingest.mode`
3232
#' argument of [adbc_statement_init()]). The default is `"default"`.
33+
#' @param catalog_name If not `NULL`, the catalog to create/locate the table in.
34+
#' **This API is EXPERIMENTAL.**
35+
#' @param db_schema_name If not `NULL`, the schema to create/locate the table in.
36+
#' **This API is EXPERIMENTAL.**
3337
#' @param query An SQL query
3438
#' @param bind A data.frame, nanoarrow_array, or nanoarrow_array_stream of
3539
#' bind parameters or NULL to skip the bind/prepare step.
@@ -69,7 +73,9 @@ execute_adbc <- function(db_or_con, query, ..., bind = NULL) {
6973
#' @export
7074
write_adbc <- function(tbl, db_or_con, target_table, ...,
7175
mode = c("default", "create", "append", "replace", "create_append"),
72-
temporary = FALSE) {
76+
temporary = FALSE,
77+
catalog_name = NULL,
78+
db_schema_name = NULL) {
7379
UseMethod("write_adbc", db_or_con)
7480
}
7581

@@ -113,7 +119,9 @@ execute_adbc.default <- function(db_or_con, query, ..., bind = NULL, stream = NU
113119
#' @export
114120
write_adbc.default <- function(tbl, db_or_con, target_table, ...,
115121
mode = c("default", "create", "append", "replace", "create_append"),
116-
temporary = FALSE) {
122+
temporary = FALSE,
123+
catalog_name = NULL,
124+
db_schema_name = NULL) {
117125
assert_adbc(db_or_con, c("adbc_database", "adbc_connection"))
118126
mode <- match.arg(mode)
119127

@@ -127,6 +135,8 @@ write_adbc.default <- function(tbl, db_or_con, target_table, ...,
127135
stmt <- adbc_statement_init(
128136
con,
129137
adbc.ingest.target_table = target_table,
138+
adbc.ingest.target_catalog = catalog_name,
139+
adbc.ingest.target_db_schema = db_schema_name,
130140
adbc.ingest.mode = if (!identical(mode, "default")) paste0("adbc.ingest.mode.", mode),
131141
adbc.ingest.temporary = if (temporary) "true"
132142
)

r/adbcpostgresql/R/adbcpostgresql-package.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ NULL
3434
#' @param adbc.connection.autocommit Use FALSE to disable the default
3535
#' autocommit behaviour.
3636
#' @param adbc.ingest.target_table The name of the target table for a bulk insert.
37+
#' @param adbc.ingest.target_db_schema The schema of the table for a bulk insert.
38+
#' **This API is EXPERIMENTAL.**
3739
#' @param adbc.ingest.mode Whether to create (the default) or append.
3840
#'
3941
#' @return An [adbcdrivermanager::adbc_driver()]
@@ -78,10 +80,12 @@ adbc_connection_init.adbcpostgresql_database <- function(database, ...,
7880
#' @export
7981
adbc_statement_init.adbcpostgresql_connection <- function(connection, ...,
8082
adbc.ingest.target_table = NULL,
83+
adbc.ingest.target_db_schema = NULL,
8184
adbc.ingest.mode = NULL) {
8285
options <- list(
8386
...,
8487
adbc.ingest.target_table = adbc.ingest.target_table,
88+
adbc.ingest.target_db_schema = adbc.ingest.target_db_schema,
8589
adbc.ingest.mode = adbc.ingest.mode
8690
)
8791

r/adbcpostgresql/tests/testthat/test-adbcpostgres-package.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,28 @@ test_that("default options can open a database and execute a query", {
8888

8989
adbcdrivermanager::adbc_statement_release(stmt)
9090
})
91+
92+
test_that("write_adbc() supports db_schema_name", {
93+
test_db_uri <- Sys.getenv("ADBC_POSTGRESQL_TEST_URI", "")
94+
skip_if(identical(test_db_uri, ""))
95+
96+
db <- adbc_database_init(adbcpostgresql(), uri = test_db_uri)
97+
con <- adbc_connection_init(db)
98+
on.exit({
99+
adbcdrivermanager::adbc_connection_release(con)
100+
adbcdrivermanager::adbc_database_release(db)
101+
})
102+
103+
adbcdrivermanager::execute_adbc(con, "CREATE SCHEMA IF NOT EXISTS testschema")
104+
adbcdrivermanager::execute_adbc(con, "DROP TABLE IF EXISTS testschema.df_schema")
105+
106+
df <- data.frame(x = as.double(1:3))
107+
expect_identical(
108+
adbcdrivermanager::write_adbc(df, con, "df_schema", db_schema_name = "testschema", mode = "create"),
109+
df
110+
)
111+
112+
stream <- adbcdrivermanager::read_adbc(con, "SELECT * FROM testschema.df_schema ORDER BY x")
113+
expect_identical(as.data.frame(stream), df)
114+
stream$release()
115+
})

r/adbcsqlite/R/adbcsqlite-package.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ NULL
3333
#' @param adbc.connection.autocommit Use FALSE to disable the default
3434
#' autocommit behaviour.
3535
#' @param adbc.ingest.target_table The name of the target table for a bulk insert.
36+
#' @param adbc.ingest.target_catalog The catalog of the table for a bulk insert.
37+
#' **This API is EXPERIMENTAL.**
3638
#' @param adbc.ingest.mode Whether to create (the default) or append.
3739
#' @param adbc.sqlite.query.batch_rows The number of rows per batch to return.
3840
#'
@@ -78,11 +80,13 @@ adbc_connection_init.adbcsqlite_database <- function(database, ...,
7880
#' @export
7981
adbc_statement_init.adbcsqlite_connection <- function(connection, ...,
8082
adbc.ingest.target_table = NULL,
83+
adbc.ingest.target_catalog = NULL,
8184
adbc.ingest.mode = NULL,
8285
adbc.sqlite.query.batch_rows = NULL) {
8386
options <- list(
8487
...,
8588
adbc.ingest.target_table = adbc.ingest.target_table,
89+
adbc.ingest.target_catalog = adbc.ingest.target_catalog,
8690
adbc.ingest.mode = adbc.ingest.mode,
8791
adbc.sqlite.query.batch_rows = adbc.sqlite.query.batch_rows
8892
)

r/adbcsqlite/tests/testthat/test-adbcsqlite-package.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ test_that("read/write/execute SQL work with sqlite connections", {
9999
adbcdrivermanager::adbc_database_release(db)
100100
})
101101

102+
test_that("write_adbc() supports catalog_name", {
103+
db <- adbc_database_init(adbcsqlite())
104+
con <- adbc_connection_init(db)
105+
on.exit({
106+
adbcdrivermanager::adbc_connection_release(con)
107+
adbcdrivermanager::adbc_database_release(db)
108+
})
109+
110+
df <- data.frame(x = as.double(1:3))
111+
expect_identical(
112+
adbcdrivermanager::write_adbc(df, con, "df_catalog", catalog_name = "main", mode = "replace"),
113+
df
114+
)
115+
116+
stream <- adbcdrivermanager::read_adbc(con, "SELECT * from main.df_catalog")
117+
expect_identical(as.data.frame(stream), df)
118+
stream$release()
119+
})
120+
102121
test_that("write_adbc() with temporary = TRUE works with sqlite databases", {
103122
skip_if_not(packageVersion("adbcdrivermanager") >= "0.6.0.9000")
104123

0 commit comments

Comments
 (0)