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

Nicer conditional SQL #738

Merged
merged 8 commits into from
Dec 23, 2021
Merged

Nicer conditional SQL #738

merged 8 commits into from
Dec 23, 2021

Conversation

mgirlich
Copy link
Collaborator

This slightly improves the SQL translations of is.na(), if(), ifelse(), if_else() and case_when()

Before

library(dbplyr, warn.conflicts = FALSE)
translate_sql(ifelse(is.na(x), 0L, 1L))
#> <SQL> CASE WHEN (((`x`) IS NULL)) THEN (0) WHEN NOT(((`x`) IS NULL)) THEN (1) END

translate_sql(case_when(is.na(x) ~ 0L, TRUE ~ 1L))
#> <SQL> CASE
#> WHEN (((`x`) IS NULL)) THEN (0)
#> ELSE (1)
#> END

After

library(dbplyr, warn.conflicts = FALSE)
translate_sql(ifelse(is.na(x), 0L, 1L))
#> <SQL> CASE WHEN ((`x` IS NULL)) THEN 0 WHEN NOT ((`x` IS NULL)) THEN 1 END

translate_sql(case_when(is.na(x) ~ 0L, TRUE ~ 1L))
#> <SQL> CASE WHEN ((`x` IS NULL)) THEN 0 ELSE 1 END

@mgirlich mgirlich mentioned this pull request Dec 20, 2021
R/backend-.R Outdated Show resolved Hide resolved
R/translate-sql-conditional.R Outdated Show resolved Hide resolved
R/translate-sql-conditional.R Outdated Show resolved Hide resolved
tests/testthat/test-translate-sql.R Outdated Show resolved Hide resolved
@mgirlich
Copy link
Collaborator Author

I had to change from enexpr() to enquo() due to an issue with the pipe: translate_sql(x %>% ifelse(1, 2)) didn't work but it does now.

@mgirlich mgirlich merged commit 912bb23 into main Dec 23, 2021
@mgirlich mgirlich deleted the nicer-case-when-sql branch January 17, 2022 10:26
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.

2 participants