-
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
dplyr is translating FALSE to "0" in SQL. #2614
Comments
See #2052 for discussion, and https://www.postgresql.org/docs/9.1/static/datatype-boolean.html, which suggests that 0 and 1 are ok. |
Documentation seems literally correct: library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
y <- c(1, 2, 3, NA)
pg <- src_postgres()
df <-
copy_to(pg, tibble(y),
name = "adhaksdhjkalsda",
overwrite = TRUE) %>%
mutate(y_big = coalesce(y > 2, FALSE))
df %>% show_query()
#> <SQL>
#> SELECT "y", COALESCE("y" > 2.0, 0) AS "y_big"
#> FROM "adhaksdhjkalsda"
df <-
copy_to(pg, tibble(y),
name = "adhaksdhjkalsda",
overwrite = TRUE) %>%
mutate(y_big = coalesce(y > 2, sql("'0'")))
df %>% show_query()
#> <SQL>
#> SELECT "y", COALESCE("y" > 2.0, '0') AS "y_big"
#> FROM "adhaksdhjkalsda"
df
#> Source: lazy query [?? x 2]
#> Database: postgres 9.6.2 [[email protected]:5432/crsp]
#>
#> y y_big
#> <dbl> <lgl>
#> 1 1 FALSE
#> 2 2 FALSE
#> 3 3 TRUE
#> 4 NA FALSE |
Literals |
Hmmm, and there's currently no double dispatch on vector type and database backend. But generally dplyr translation assumes SQL-92, so maybe it's ok to just translate to |
Seems to work OK with SQLite (if it broke there, then clearly not worth trying): library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
y <- c(1, 2, 3, NA)
df <-
memdb_frame(y) %>%
mutate(y_big = coalesce(y > 2, sql("'0'")))
df
#> Source: lazy query [?? x 2]
#> Database: sqlite 3.11.1 [:memory:]
#>
#> y y_big
#> <dbl> <int>
#> 1 1 0
#> 2 2 0
#> 3 3 1
#> 4 NA 0 |
Unfortunately MySQL yields:
|
I can't find any single translation that works across these three backends |
In the long run, hopefully DBI can provide a new generic: r-dbi/DBI#172. In the short-run, need another method like |
Thanks
It seems |
Argh, it's worse than that since there seems to be a major logic failing and |
Rather than continuing to painstaking thread the current connection through every function call, it'll probably be easier to generalise |
Translating
FALSE
to0
works in SQLite, which has few real types. But it breaks existing PostgreSQL code.The text was updated successfully, but these errors were encountered: