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

sql() does not evaluate in the correct environment #1039

Closed
mgirlich opened this issue Nov 3, 2022 · 0 comments · Fixed by #1164
Closed

sql() does not evaluate in the correct environment #1039

mgirlich opened this issue Nov 3, 2022 · 0 comments · Fixed by #1164
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL

Comments

@mgirlich
Copy link
Collaborator

mgirlich commented Nov 3, 2022

From #1036

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

memdb_frame(x = 1, y = 2) %>% 
  summarise(across(x, ~ sum(.) / sum(sql(gsub("x","y",cur_column()))))) %>%
  show_query()
#> <SQL>
#> SELECT SUM(`x`) / SUM(gsub('x', 'y', 'x')) AS `x`
#> FROM `dbplyr_001`

Created on 2022-11-03 with reprex v2.0.2

It seems this can be solved by adapting sql_data_mask():

special_calls2$sql <- function(...) {
  dots <- exprs(...)
  env <- env_parent(get_env(expr), 2)
  dots <- purrr::map(dots, eval_tidy, env = env)
  exec(sql, !!!dots)
}
@hadley hadley added bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL labels Dec 5, 2022
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