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

Translate evaluated expression #674

Merged
merged 13 commits into from
Dec 14, 2021
Merged

Conversation

mgirlich
Copy link
Collaborator

@mgirlich mgirlich commented Jul 6, 2021

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.

R/translate-sql-helpers.R Outdated Show resolved Hide resolved
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) {
Copy link
Member

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 -?

Copy link
Collaborator Author

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)

Copy link
Collaborator Author

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?

Copy link
Member

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?

@mgirlich
Copy link
Collaborator Author

I think this slightly changed approach should handle binary and unary minus.

tests/testthat/test-translate-sql-helpers.R Outdated Show resolved Hide resolved
tests/testthat/test-translate-sql-helpers.R Outdated Show resolved Hide resolved
mgirlich and others added 3 commits December 14, 2021 08:21
@mgirlich mgirlich merged commit 63f59ac into main Dec 14, 2021
@mgirlich mgirlich deleted the translate-evaluated-expression branch December 14, 2021 07:59
@hadley
Copy link
Member

hadley commented Dec 14, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

R expression to SQL expression translation bug
2 participants