From f7a65bc7d162eff9f2f3dbef3c0118750ed0a5ad Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 8 Nov 2023 07:53:25 -0600 Subject: [PATCH] Extract table name from id string --- R/db-sql.R | 4 ++-- R/table-name.R | 17 +++++++++++++++++ R/verb-joins.R | 4 ++-- tests/testthat/_snaps/backend-.md | 4 ++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/R/db-sql.R b/R/db-sql.R index 75bb64708..b6751044a 100644 --- a/R/db-sql.R +++ b/R/db-sql.R @@ -174,8 +174,8 @@ sql_table_index.DBIConnection <- function(con, ..., call = caller_env()) { table <- as_table_name(table, con, error_call = call) - hash <- substr(hash(table), 1, 6) - name <- name %||% paste0(c("dbplyr", hash, columns), collapse = "_") + table <- db_table_name_extract(con, table) + name <- name %||% paste0(c(table, columns), collapse = "_") glue_sql2( con, "CREATE ", if (unique) "UNIQUE ", "INDEX {.name name}", diff --git a/R/table-name.R b/R/table-name.R index a8bae7db4..434974ca1 100644 --- a/R/table-name.R +++ b/R/table-name.R @@ -89,6 +89,23 @@ is_table_name <- function(x) { inherits(x, "dbplyr_table_name") } +# TODO: make this generic +db_parse_table_name <- function(con, x) { + quote_char <- substr(as_table_name("", con = con), 1, 1) + scan( + text = x, + what = character(), + quote = quote_char, + quiet = TRUE, + na.strings = character(), + sep = "." + ) +} +db_table_name_extract <- function(con, x) { + out <- db_parse_table_name(con, x) + out[[length(out)]] +} + #' @export escape.dbplyr_table_name <- function(x, parens = FALSE, collapse = ", ", con = NULL) { alias <- names2(x) # assume alias is already escaped diff --git a/R/verb-joins.R b/R/verb-joins.R index 20ef52aa0..73d55ddb2 100644 --- a/R/verb-joins.R +++ b/R/verb-joins.R @@ -821,8 +821,8 @@ join_two_table_alias <- function(names, from) { tables_have_same_name <- from[1] == "name" && from[2] == "name" && identical(names[1], names[2]) if (tables_have_same_name) { out <- c( - paste0(names[1], "_LHS"), - paste0(names[2], "_RHS") + paste0(db_table_name_extract(con, names[1]), "_LHS"), + paste0(db_table_name_extract(con, names[2]), "_RHS") ) return(out) } diff --git a/tests/testthat/_snaps/backend-.md b/tests/testthat/_snaps/backend-.md index 35b7fcca1..c9bb64654 100644 --- a/tests/testthat/_snaps/backend-.md +++ b/tests/testthat/_snaps/backend-.md @@ -89,14 +89,14 @@ Code sql_table_index(con, in_schema("schema", "tbl"), c("a", "b")) Output - CREATE INDEX `dbplyr_119f50_a_b` ON `schema`.`tbl` (`a`, `b`) + CREATE INDEX `tbl_a_b` ON `tbl` (`a`, `b`) --- Code sql_table_index(con, in_schema("schema", "tbl"), "c", unique = TRUE) Output - CREATE UNIQUE INDEX `dbplyr_119f50_c` ON `schema`.`tbl` (`c`) + CREATE UNIQUE INDEX `tbl_c` ON `tbl` (`c`) ---