-
Notifications
You must be signed in to change notification settings - Fork 174
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
Translate evaluated expression #674
Conversation
R/translate-sql-helpers.R
Outdated
build_sql(x, sql(f), y) | ||
} | ||
} | ||
} | ||
|
||
escape_infix_expr <- function(x) { | ||
infix_calls <- c("+", "-", "*", "/", "%%", "^") | ||
if (quo_is_call(x, n = 2) && call_name(x) %in% infix_calls) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think quo_is_call(x, name, n = 2)
will work?
And I'm not sure n = 2
is correct? Doesn't the same problem arise with unary +
and -
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, when do we need parantheses for unary +
and -
? I came up with something like
translate_sql(-!!expr(-x))
#> <SQL> --`x`
Do you see other cases?
Thanks to your question I just noticed that unary +
isn't supported at all currently:
library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
# works
translate_sql(-2)
#> <SQL> -2.0
# error
translate_sql(+2)
#> Error in eval_bare(x, .env): argument "y" is missing, with no default
Created on 2021-12-09 by the reprex package (v2.0.1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how/whether we want to handle translate_sql(-!!expr(-x))
. First the inner part, -x
, translated to SQL -`x`
. Then the outer minus is translated. Here one would have to analyse the SQL to decide whether parantheses are needed.
Probably it should be sufficient to check if the SQL starts with "-". What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of this case:
dbplyr::translate_sql(-!!rlang::expr(1+2))
#> <SQL> -1.0 + 2.0
Created on 2021-12-09 by the reprex package (v2.0.1)
OTOH is this very common? Should we just suggest that if you're going to interpolate in expressions involving infix operators, you should wrap the whole thing in parentheses to avoid operator precedence issues?
I think this slightly changed approach should handle binary and unary minus. |
Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
I forgot to say this earlier, but can you please do squash-merges? I think that gives a cleaner history, and in most cases we don't need to preserve the history within in a PR. |
Fixes #634.
I think the problem of partially evaluated expressions should only be an issue for infix expressions. I wasn't sure about adding operators like
|
,&&
, or==
to the list but it might make sense as well.