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

Support ORDER BY in subqueries for MSSQL #275

Closed
krlmlr opened this issue Apr 9, 2019 · 0 comments · Fixed by #277
Closed

Support ORDER BY in subqueries for MSSQL #275

krlmlr opened this issue Apr 9, 2019 · 0 comments · Fixed by #277

Comments

@krlmlr
Copy link
Member

krlmlr commented Apr 9, 2019

MSSQL is sensitive to subqueries that have an ORDER BY clause -- they are not allowed. A suggested workaround is to use TOP 100 PERCENT, but I believe we can also solve this by more accurate optimization.

This is an actual problem if window functions are involved, in this case it's not easy to move the arrange() as noted in #94 (comment).

library(tidyverse)
library(dbplyr)

df <- lazy_frame(x = 1:4, g = rep(c(1, 2), each = 2), con = simulate_mssql())

df %>%
  group_by(g) %>%
  arrange(x) %>%
  mutate(r = cumsum(x))
#> <SQL>
#> SELECT `x`, `g`, SUM(`x`) OVER (PARTITION BY `g` ORDER BY `x` ROWS UNBOUNDED PRECEDING) AS `r`
#> FROM (SELECT *
#> FROM `df`
#> ORDER BY `x`) `dbplyr_001`

Created on 2019-04-09 by the reprex package (v0.2.1.9000)

@krlmlr krlmlr changed the title Postpone op_arrange() Move ORDER BY clause to outermost SELECT Apr 9, 2019
@krlmlr krlmlr changed the title Move ORDER BY clause to outermost SELECT Support ORDER BY in subqueries for MSSQL Apr 9, 2019
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 a pull request may close this issue.

1 participant