Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutate() %>% compute() fails. Error in .transformer(): ! value must be a string or scalar SQL, not the number. #1420

Closed
erydit opened this issue Dec 13, 2023 · 5 comments · Fixed by #1427
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL

Comments

@erydit
Copy link

erydit commented Dec 13, 2023

Hello.
An error started to occur in ‘dbplyr’ version 2.4.0.

I am trying to collect and make some calculations in SQLite database:

DB_CON <- dbConnect(drv = RSQLite::SQLite(), dbname = "BandsTS.db")
BANDS_tbl <- tbl(DB_CON, "bands_ts_tbl") %>%
  mutate( ARVI2 = -0.18 + 1.17 * ((B08 - B04) / (B08 + B04)) ) %>%
compute()
That code (and also show_query() instead of compute() ) throws an error: ```r Error in `.transformer()`: ! `value` must be a string or scalar SQL, not the number -0.18. Run `rlang::last_trace()` to see where the error occurred. > rlang::last_trace() Error in `.transformer()`: ! `value` must be a string or scalar SQL, not the number -0.18. --- Backtrace: ▆ 1. ├─dplyr::show_query(BANDS_tbl) 2. ├─dbplyr:::show_query.tbl_lazy(BANDS_tbl) 3. │ └─dbplyr::remote_query(x, cte = cte, sql_options = sql_options) 4. │ └─dbplyr::db_sql_render(remote_con(x), x, cte = cte, sql_options = sql_options) 5. │ ├─dbplyr::db_sql_render(con, sql, sql_options = sql_options) 6. │ └─dbplyr:::db_sql_render.DBIConnection(con, sql, sql_options = sql_options) 7. │ ├─dbplyr::sql_render(sql, con = con, ..., sql_options = sql_options) 8. │ └─dbplyr:::sql_render.tbl_lazy(sql, con = con, ..., sql_options = sql_options) 9. │ ├─dbplyr::sql_render(...) 10. │ └─dbplyr:::sql_render.lazy_query(...) 11. │ ├─dbplyr::sql_build(query, con = con, sql_options = sql_options) 12. │ └─dbplyr:::sql_build.lazy_select_query(query, con = con, sql_options = sql_options) 13. │ ├─dbplyr::sql_build(op$x, con, sql_options = sql_options) 14. │ └─dbplyr:::sql_build.lazy_select_query(op$x, con, sql_options = sql_options) 15. │ └─dbplyr:::get_select_sql(...) 16. │ └─dbplyr:::translate_select_sql(con, select) 17. │ └─dbplyr::translate_sql_(...) 18. │ └─base::lapply(...) 19. │ └─dbplyr (local) FUN(X[[i]], ...) 20. │ ├─dbplyr::escape(eval_tidy(x, mask), con = con) 21. │ └─rlang::eval_tidy(x, mask) 22. ├─-0.18 + 1.17 * ((B08 - B04)/(B08 + B04)) 23. │ └─dbplyr:::escape_infix_expr(enexpr(x), x, escape_unary_minus = TRUE) 24. │ └─dbplyr:::glue_sql2(sql_current_con(), "({x})") 25. │ ├─dbplyr::sql(...) 26. │ │ └─dbplyr:::c_character(...) 27. │ └─glue::glue(...) 28. │ └─glue::glue_data(...) 29. └─glue (local) ``("x") 30. ├─.transformer(expr, env) %||% .null 31. └─dbplyr (local) .transformer(text = expr, envir = env) ```
@hadley

This comment was marked as outdated.

@hadley hadley added the reprex needs a minimal reproducible example label Dec 19, 2023
@erydit

This comment was marked as outdated.

@hadley
Copy link
Member

hadley commented Dec 19, 2023

Actual reprex:

library(tidyverse)
library(DBI)

db <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(conn = db, name = "tbl", value = data.frame(x = 1:10))

db_tbl <- tbl(db, "tbl")

db_tbl %>% mutate(z = -1 + x)
#> Error in `.transformer()`:
#> ! `value` must be a string or scalar SQL, not the number -1.

Created on 2023-12-19 with reprex v2.0.2.9000

Looks like problem is happening in escape_infix_expr().

@hadley hadley added bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL and removed reprex needs a minimal reproducible example labels Dec 19, 2023
@hadley
Copy link
Member

hadley commented Dec 22, 2023

More minimal reprex:

library(dbplyr)
library(dplyr, warn.conflicts = FALSE)

lf <- lazy_frame(x = 1)
lf %>% mutate(z = -1 + x)

@hadley
Copy link
Member

hadley commented Dec 22, 2023

Even more minimal reprex:

library(dbplyr)
test_translate_sql(-1 + x, con = simulate_dbi())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants