-
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
MSSQL connection. Errors in dplyr select() after arrange #3062
Comments
Hi @pssguy , it looks like this is a nuance of the queries MSSQL accepts. In order for it to work, the query from
Should actually translate to this:
We will need to figure a way to optimize this query. |
@edgararuiz Thanks. Not just optimizing, of course. Currently an error is thrown |
Right, optimize was probably the wrong word choice, I meant we'll need to figure out a way to merge the two SQL query layers to prevent the error from happening. |
@edgararuiz you may want to look at RSQLServer's |
Thanks @imanuelcostigan ! I'll take a look |
@edgararuiz do you want to work on a PR for this issue? |
Yes, I'll be happy to |
Minimal reprex: library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
mf <- copy_to(con, data.frame(x = 1:5, y = 5:1), name = "test")
mf %>%
arrange(x) %>%
select(y) %>%
show_query()
#> <SQL>
#> SELECT `y`
#> FROM (SELECT *
#> FROM `test`
#> ORDER BY `x`)
DBI::dbGetQuery(con, "SELECT y FROM test ORDER BY x")
#> y
#> 1 5
#> 2 4
#> 3 3
#> 4 2
#> 5 1 Ideally this would only generate one query because conceptually the select happens after the arrange. I think that implies we can fix this issue by reordering present <- c(
where = length(x$where) > 0,
group_by = length(x$group_by) > 0,
having = length(x$having) > 0,
select = !identical(x$select, sql("*")),
distinct = x$distinct,
order_by = length(x$order_by) > 0,
limit = !is.null(x$limit)
) Currently And indeed if we move SELECT `y`
FROM `test`
ORDER BY `x` It remains to consider if this is actually correct - i.e. are there situations when this change would yield invalid SQL |
Ah I think the problem with performing this optimisation is this query: memdb_frame(x = 1:2) %>%
arrange(x) %>%
mutate(x = -x) This should return
which yields But this is a memdb_frame(x = 1:2, y = 3:2) %>%
arrange(x) %>%
select(x = y) %>%
show_query()
#> SELECT `y` AS `x`
#> FROM `bmvfznmfws`
#> ORDER BY `x` |
But maybe we can just do the optimisation when the |
I tried that and couldn't get it to work 😢 |
This issue was moved by hadley to tidyverse/dbplyr/issues/94. |
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 am attempting to use an MSSQL connection and hitting this issue
I first replicate the example from the dbplyr intro
This works fine
Now with an MSSQL connection
So it appears as though error has a conflict re use of TOP
Reversing the select and arrange commands
This works in a simple example but I will sometimes need to arrange data prior to other processes in a pipe
When I look at the problem code it does not exactly replicate error i.e no mention of Top 1000
Trying several alternatives in SQL
something that looks like error code
replacing top 1000 in sub-query produces the desired output
Not sure if this is an error or just something that has not yet been addressed for MSSQL.
p.s. Why no issues option under dbplyr?
The text was updated successfully, but these errors were encountered: