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

Shorter SQL for verb() + distinct() #880

Merged
merged 8 commits into from
Aug 1, 2022
Merged

Shorter SQL for verb() + distinct() #880

merged 8 commits into from
Aug 1, 2022

Conversation

mgirlich
Copy link
Collaborator

Closes #879.

library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)

lf <- lazy_frame(x = 1, y = 1)

Renamed variable

lf %>%
    select(a = x, y) %>%
    distinct(a)

Before

SELECT DISTINCT `a`
FROM (
  SELECT `x` AS `a`, `y`
  FROM `df`
) `q01`

After

SELECT DISTINCT `x` AS `a`
FROM `df`

Unnecessary mutated variable

lf %>%
    mutate(z = 1) %>%
    group_by(x) %>%
    distinct(y)

Before

SELECT DISTINCT `x`, `y`
FROM (
  SELECT `x`, `y`, 1.0 AS `z`
  FROM `df`
) `q01`

After

SELECT DISTINCT *
FROM `df`

@mgirlich mgirlich added this to the 2.3.0 milestone May 19, 2022
@mgirlich mgirlich requested a review from hadley July 31, 2022 10:33
Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

(With the caveat that I'm terrible at coming up with adversarial edge cases that will break the generated SQL)

@mgirlich mgirlich merged commit 718ed68 into main Aug 1, 2022
@mgirlich mgirlich deleted the optimise-distinct branch August 1, 2022 15:02
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 this pull request may close these issues.

Shorter SQL for select() + distinct()
2 participants