From 5c187765b671cbafac2262d29942bafd511a8a4d Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 20 Feb 2024 17:14:39 -0600 Subject: [PATCH] Give useful error if probably missing I() Fixes #1451 --- R/tbl-sql.R | 22 +++++++++++++++++++--- tests/testthat/_snaps/tbl-sql.md | 14 ++++++++++++++ tests/testthat/test-tbl-sql.R | 7 +++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/R/tbl-sql.R b/R/tbl-sql.R index fd93ccc9e..6aa61dc9c 100644 --- a/R/tbl-sql.R +++ b/R/tbl-sql.R @@ -17,13 +17,29 @@ tbl_sql <- function(subclass, src, from, ..., vars = NULL, check_from = TRUE) { # Can't use check_dots_used(), #1429 check_character(vars, allow_null = TRUE) - from <- as_table_source(from, con = src$con) - vars <- vars %||% dbplyr_query_fields(src$con, from) + is_suspicious <- is_bare_string(from) && grepl(".", from, fixed = TRUE) + source <- as_table_source(from, con = src$con) + + withCallingHandlers( + vars <- vars %||% dbplyr_query_fields(src$con, source), + error = function(err) { + if (!is_suspicious) return() + + cli::cli_abort( + c( + "Failed to find table {source}.", + i = "Did you mean {.code from = I({.str {from}})}?" + ), + parent = err + ) + } + ) + dplyr::make_tbl( c(subclass, "sql", "lazy"), src = src, - lazy_query = lazy_query_remote(from, vars) + lazy_query = lazy_query_remote(source, vars) ) } diff --git a/tests/testthat/_snaps/tbl-sql.md b/tests/testthat/_snaps/tbl-sql.md index 7b9b38a82..c3b92694a 100644 --- a/tests/testthat/_snaps/tbl-sql.md +++ b/tests/testthat/_snaps/tbl-sql.md @@ -11,3 +11,17 @@ 2 2 2 3 3 1 +# useful error if missing I() + + Code + tbl(src_memdb(), "foo.bar") + Condition + Error in `tbl_sql()`: + ! Failed to find table `foo.bar`. + i Did you mean `from = I("foo.bar")`? + Caused by error in `db_query_fields.DBIConnection()`: + ! Can't query fields. + i Using SQL: SELECT * FROM `foo.bar` AS `q05` WHERE (0 = 1) + Caused by error: + ! no such table: foo.bar + diff --git a/tests/testthat/test-tbl-sql.R b/tests/testthat/test-tbl-sql.R index 5177faaff..069a825eb 100644 --- a/tests/testthat/test-tbl-sql.R +++ b/tests/testthat/test-tbl-sql.R @@ -54,6 +54,13 @@ test_that("can distinguish 'schema.table' from 'schema'.'table'", { expect_equal(as.character(tbl_vars(df)), c("a", "b", "c")) }) +test_that("useful error if missing I()", { + expect_snapshot( + tbl(src_memdb(), "foo.bar"), + error = TRUE + ) +}) + # n_groups ---------------------------------------------------------------- test_that("check basic group size implementation", {