From e3aa43f28941ff18dc6f87c73a9d06885a72cdf7 Mon Sep 17 00:00:00 2001 From: "Jarod G.R. Meng" Date: Fri, 13 Dec 2024 10:57:35 +0700 Subject: [PATCH 1/4] Make sure temp table created in dbWriteTable is in the same schema as the destination table --- R/dbWriteTable.R | 5 +++++ tests/testthat/test-copy_to.src_presto.R | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/R/dbWriteTable.R b/R/dbWriteTable.R index 7481238..a40b4e9 100644 --- a/R/dbWriteTable.R +++ b/R/dbWriteTable.R @@ -94,6 +94,11 @@ NULL rn <- paste0( "temp_", paste(sample(letters, 10, replace = TRUE), collapse = "") ) + if (inherits(name, "dbplyr_schema")) { + name_sql <- DBI::dbQuoteIdentifier(conn, name) + name_with_schema <- DBI::dbUnquoteIdentifier(conn, name_sql)[[1]]@name + rn <- dbplyr::in_schema(name_with_schema[1], rn) + } if (found && overwrite) { # Without implementation of TRANSACTION, we play it safe by renaming # the to-be-overwritten table rather than deleting it right away diff --git a/tests/testthat/test-copy_to.src_presto.R b/tests/testthat/test-copy_to.src_presto.R index e1f08c8..a69dce3 100644 --- a/tests/testthat/test-copy_to.src_presto.R +++ b/tests/testthat/test-copy_to.src_presto.R @@ -47,6 +47,20 @@ source("utilities.R") ) expect_true(dbExistsTable(con, test_table_name)) expect_equal_data_frame(collect(tbl), test_df) + # a different schema works + new_schema_name <- paste0(con@schema, "2") + DBI::dbExecute(con, paste0("CREATE SCHEMA IF NOT EXISTS ", new_schema_name)) + test_table_name_4 <- dbplyr::in_schema(new_schema_name, test_table_name) + tbl <- copy_to(dest = src, df = test_df, name = test_table_name_4) + expect_true(dbExistsTable(con, test_table_name_4)) + expect_equal_data_frame(collect(tbl), test_df) + # overwriting a different schema works + tbl <- copy_to( + dest = src, df = test_df, name = test_table_name_4, overwrite = TRUE + ) + expect_true(dbExistsTable(con, test_table_name_4)) + expect_equal_data_frame(collect(tbl), test_df) + expect_true(dbRemoveTable(con, test_table_name_4)) } test_that("dplyr::copy_to works for src_presto", { From 60b0e7a3c4098eb681713c805fd1037272ecc2ac Mon Sep 17 00:00:00 2001 From: "Jarod G.R. Meng" Date: Fri, 13 Dec 2024 22:02:09 +0700 Subject: [PATCH 2/4] Add news for dbWriteTable(overwrite = TRUE) --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 1b36f1f..c6e5431 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,8 @@ * Fixed a bug whereby nested CTEs result in nested WITH. (#261) * Fixed `paste()` and `paste0()` translation (#266) * RPresto is now compatible with dbplyr 2.5.0 (#272, #274, #277) +* `dbWriteTable(overwrite = TRUE)` makes sure that the temp table is created + in the same schema as the destination table (#279) # RPresto 1.4.6 From 801a6dbf35ec8a68af2a3a8e5616ec23251d69a4 Mon Sep 17 00:00:00 2001 From: "Jarod G.R. Meng" Date: Thu, 19 Dec 2024 10:27:26 +0700 Subject: [PATCH 3/4] Requires user to use DBI >= 1.2.0 when using dbplyr >= 2.5.0 to be compatible --- R/cte.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/cte.R b/R/cte.R index 8de58d9..c369ed9 100644 --- a/R/cte.R +++ b/R/cte.R @@ -18,6 +18,12 @@ get_tables_from_sql.lazy_base_remote_query <- function(query) { if (inherits(query$x, "dbplyr_table_path")) { # dbplyr >= 2.5.0 sim_conn <- dbplyr::simulate_dbi("PrestoConnection") table_name <- dbplyr::table_path_name(query$x, sim_conn) + if (utils::packageVersion("DBI") < "1.2.0") { + stop( + "DBI version needs to be >= 1.2.0 to be used with dbplyr >= 2.5.0", + call. = FALSE + ) + } DBI::dbUnquoteIdentifier(sim_conn, table_name)[[1]]@name } else if (inherits(query$x, "dbplyr_table_ident")) { # dbplyr >= 2.4.0 vctrs::field(query$x, "table") From d201651f95d626957dacb6afa2b5d08653cf8d3c Mon Sep 17 00:00:00 2001 From: "Jarod G.R. Meng" Date: Thu, 19 Dec 2024 10:27:45 +0700 Subject: [PATCH 4/4] Add ProjectId to RPresto.Rproj --- RPresto.Rproj | 1 + 1 file changed, 1 insertion(+) diff --git a/RPresto.Rproj b/RPresto.Rproj index cba1b6b..52f8ca0 100644 --- a/RPresto.Rproj +++ b/RPresto.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: eb727814-0836-4309-a3c1-778215feb3ca RestoreWorkspace: No SaveWorkspace: No