-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Case_when translation for ORACLE does not work #3521
Comments
As a workaround, you might try |
I tried something to add the correct translation to oracle CASE WHEN. See case-when-oracle branch in my fork. Fisrt, add in a sql_translate_env.Oracle <- function(con) {
sql_variant(
sql_translator(.parent = base_odbc_scalar,
# Data type conversions are mostly based on this article
# https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements001.htm
as.character = sql_cast("VARCHAR(255)"),
as.numeric = sql_cast("NUMBER"),
as.double = sql_cast("NUMBER"),
case_when = function(...) sql_case_when_oracle(...)
),
base_odbc_agg,
base_odbc_win
)
} and a sql_case_when_oracle <- function(...) {
# TODO: switch to dplyr::case_when_prepare when available
formulas <- dots_list(...)
n <- length(formulas)
if (n == 0) {
abort("No cases provided")
}
query <- vector("list", n)
value <- vector("list", n)
for (i in seq_len(n)) {
f <- formulas[[i]]
env <- environment(f)
query[[i]] <- escape(eval_bare(f[[2]], env), con = sql_current_con())
value[[i]] <- escape(eval_bare(f[[3]], env), con = sql_current_con())
}
clauses <- purrr::map2_chr(query, value, ~ paste0("WHEN (", .x, ") THEN (", .y, ")"))
# if a formula like TRUE ~ "other" is at the end of a sequence, use ELSE statement
if (query[[n]] == "TRUE") {
clauses[[n]] <- paste0("ELSE (", value[[n]], ")")
}
sql(paste0(
"CASE\n",
paste0(clauses, collapse = "\n"),
"\nEND"
))
} there is surely other way to do that and I am willing to help with a PR if you put a the correct path, one that you could accept. Moreover, a @edgararuiz have you some advices for me on this ? Thanks. |
Hi @cderv , thank you for researching this. Let me take a closer look and follow up with you. Thank you for your willingness to send over a PR. |
I think the right way to fix this is to modify |
OK I'll work on something. |
This issue was moved by krlmlr to tidyverse/dbplyr/issues/112. |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
I work with an oracle connection so difficult to make a reprex sorry. I just will use the code as-is as I think it is enough to see the issue in SQL translation.
When using dplyr on an oracle connection, I had this error
The error message is
A call to
show_query
instead ofcollect
gives the following SQLThis as been mentionned in #2928 but not fixed it seems.
I think the problem is with
WHEN (TRUE)
that is not valid with ORACLE. It should be aELSE
What do you think ?
If you need another simpler example, please tell me I will work on one.
The text was updated successfully, but these errors were encountered: