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

Implement DISTINCT ON for dbplyr backend #384

Open
cy-james-lee opened this issue Sep 18, 2024 · 0 comments
Open

Implement DISTINCT ON for dbplyr backend #384

cy-james-lee opened this issue Sep 18, 2024 · 0 comments
Labels
help wanted ❤️ we'd love your help!

Comments

@cy-james-lee
Copy link

Currently, if distinct is used with .keep_all = TRUE, ROW_NUMBER is used. Which is normal dbplyr behavior for generics. But in my daily uses, I found DISTINCT ON is much faster than filtering a window function of a subquery.

library(duckdb)
#> Loading required package: DBI
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
conn1 <- DBI::dbConnect(duckdb())

duckdb_register(conn1, "iris", iris)
tbl_iris <- tbl(conn1, "iris")

# Current behavior
tbl_iris |> 
  distinct(
    Petal.Width, 
    Species,
    .keep_all = TRUE
  ) |> 
  show_query()
#> <SQL>
#> SELECT "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", Species
#> FROM (
#>   SELECT
#>     iris.*,
#>     ROW_NUMBER() OVER (PARTITION BY "Petal.Width", Species ORDER BY "Sepal.Length") AS col01
#>   FROM iris
#> ) q01
#> WHERE (col01 = 1)

dbDisconnect(conn1)
rm(conn1, tbl_iris)

Created on 2024-09-18 with reprex v2.1.1

@krlmlr krlmlr added the help wanted ❤️ we'd love your help! label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted ❤️ we'd love your help!
Projects
None yet
Development

No branches or pull requests

2 participants