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

row_number(desc(x)) produces invalid SQL #1118

Closed
donour opened this issue Jan 31, 2023 · 4 comments · Fixed by #1122
Closed

row_number(desc(x)) produces invalid SQL #1118

donour opened this issue Jan 31, 2023 · 4 comments · Fixed by #1122
Milestone

Comments

@donour
Copy link

donour commented Jan 31, 2023


The following example produces a non-compliant SQL query on dbplyr 2.3.0, but works correctly with 2.2.

#######################################################################
# this is site specific code needed to reproduce the error on a live DB
con <- dbi_connect()
######################################################################

tbl(con, sql("test_table")) %>%
     select(OBJ_TYPE, CREATED_AT) %>%
           group_by(OBJ_TYPE) %>%
                 filter(1L == row_number(desc(CREATED_AT))) %>%
                       show_query()
                       
 ...
 
 <SQL>
SELECT "OBJ_TYPE", "CREATED_AT"
FROM (
  SELECT
    "OBJ_TYPE",
    "CREATED_AT",
    CASE
WHEN (NOT(("CREATED_AT" DESC IS NULL))) THEN ROW_NUMBER() OVER (PARTITION BY "OBJ_TYPE", (CASE WHEN (("CREATED_AT" DESC IS NULL)) THEN 1 ELSE 0 END) ORDER BY "CREATED_AT" DESC)
END AS "q02"
  FROM (test_table) "q01"
) "q02"
WHERE (1 = "q02")
> 
                      
@donour donour changed the title Groupby+filter produces invalid SQL on tidyverse 2.3.0 Groupby+filter produces invalid SQL on dbplyr 2.3.0 Jan 31, 2023
@hadley
Copy link
Member

hadley commented Feb 1, 2023

Moving to the dbplyr repo.

@hadley hadley transferred this issue from tidyverse/dplyr Feb 1, 2023
@donour
Copy link
Author

donour commented Feb 1, 2023

thanks @hadley !

@mgirlich mgirlich added this to the 2.3.1 milestone Feb 2, 2023
@mgirlich mgirlich changed the title Groupby+filter produces invalid SQL on dbplyr 2.3.0 row_number(desc(x)) produces invalid SQL Feb 3, 2023
@mgirlich
Copy link
Collaborator

mgirlich commented Feb 3, 2023

Thanks for creating the issue! Should be fixed in the dev version 😄

@donour
Copy link
Author

donour commented Feb 3, 2023

woot!

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.

3 participants