-
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
MSSQL Incorrect Translation of Boolean and row_number while Filtering Zero Rows #1233
Comments
I don't know much about MS SQL translation. From what I see in the dbplyr code it is a bit complicated handling logical values. |
re The query below succeeds against my test mssql db with nycflights data: SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS "x"
FROM "flights" This only happens when no argument is passed to library(dplyr)
con <- DBI::dbConnect(...)
# ***** FAILS *****
tbl(con, "flights") |>
transmute(year, month, x = row_number()) |>
show_query()
#> <SQL>
#> SELECT "year", "month", ROW_NUMBER() OVER () AS "x"
#> FROM "flights"
# ***** SUCCEEDS *****
tbl(con, "flights") |>
arrange(year) |>
transmute(year, month, x = row_number()) |>
show_query()
#> <SQL>
#> SELECT "year", "month", ROW_NUMBER() OVER (ORDER BY "year") AS "x"
#> FROM "flights"
#> ORDER BY "year"
# ***** SUCCEEDS *****
tbl(con, "flights") |>
dbplyr::window_order(year) |>
transmute(year, month, x = row_number()) |>
show_query()
#> <SQL>
#> SELECT "year", "month", ROW_NUMBER() OVER (ORDER BY "year") AS "x"
#> FROM "flights"
# ***** SUCCEEDS *****
tbl(con, "flights") |>
transmute(year, month, x = row_number(year)) |>
show_query()
#> <SQL>
#> SELECT
#> "year",
#> "month",
#> CASE
#> WHEN (NOT(("year" IS NULL))) THEN ROW_NUMBER() OVER (PARTITION BY (CASE WHEN (("year" IS NULL)) THEN 1 ELSE 0 END) ORDER BY "year")
#> END AS "x"
#> FROM "flights" Created on 2023-04-28 with reprex v2.0.2 I've got a possible fix on my fork (6a9ad63) that patches on 6a9ad63 : library(dplyr)
con <- DBI::dbConnect(...)
tbl(con, "flights") |> filter(row_number() == -1)
#> # Source: SQL [0 x 19]
#> # Database: Microsoft SQL Server 15.00.2000[dbo@ac43d9cc0a62/test_dbplyr]
#> # … with 19 variables: year <int>, month <int>, day <int>, dep_time <int>,
#> # sched_dep_time <int>, dep_delay <dbl>, arr_time <int>,
#> # sched_arr_time <int>, arr_delay <dbl>, carrier <chr>, flight <int>,
#> # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#> # hour <dbl>, minute <dbl>, time_hour <dttm>
tbl(con, "flights") |>
transmute(year, month, x = row_number()) |>
show_query()
#> <SQL>
#> SELECT "year", "month", ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS "x"
#> FROM "flights" Created on 2023-04-28 with reprex v2.0.2 |
Closed by #1331. |
I'm trying to filter all rows from a table to handle certain edge cases where I want to pass a table forward for use in joins but just have it be empty if necessary. Typically, a
filter(FALSE)
is all that I need to do, but when connected to an MS SQL Server viaodbc
and thefreetds
driver, there's some translation hiccups that prevent the two most common patterns for this from happening.My guess is that,
TRUE
andFALSE
are being incorrectly translated to numerics instead of booleans? And therow_number()
translation could probably rely on some sort of sensible default windowing schemeReprex:
Created on 2023-03-30 with reprex v2.0.2
Specifically, the two errors for the failing filter queries in my environment are:
and
The text was updated successfully, but these errors were encountered: