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

Combine *_join() + select() in one query #866

Closed
mgirlich opened this issue May 10, 2022 · 0 comments · Fixed by #876
Closed

Combine *_join() + select() in one query #866

mgirlich opened this issue May 10, 2022 · 0 comments · Fixed by #876
Milestone

Comments

@mgirlich
Copy link
Collaborator

A *_join() followed by select() currently produces an unnecessary subquery. This can easily be combined in a single query.

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

lf <- lazy_frame(x = 1, a = 1)
lf2 <- lazy_frame(x = 1, b = 2)

left_join(lf, lf2, by = "x") %>% 
  select(x, b)

Currently

SELECT `x`, `b`
FROM (
  SELECT `LHS`.`x` AS `x`, `a`, `b`
  FROM `df` AS `LHS`
  LEFT JOIN `df` AS `RHS`
    ON (`LHS`.`x` = `RHS`.`x`)
) `q01`

Should be

SELECT `LHS`.`x` AS `x`, `b`
FROM `df` AS `LHS`
LEFT JOIN `df` AS `RHS`
ON (`LHS`.`x` = `RHS`.`x`)
semi_join(lf, lf2, by = "x") %>% 
  select(x)

Currently

SELECT `x`
FROM (
  SELECT * FROM `df` AS `LHS`
  WHERE EXISTS (
    SELECT 1 FROM `df` AS `RHS`
    WHERE (`LHS`.`x` = `RHS`.`x`)
  )
) `q01`

Should be

SELECT x FROM `df` AS `LHS`
WHERE EXISTS (
  SELECT 1 FROM `df` AS `RHS`
  WHERE (`LHS`.`x` = `RHS`.`x`)
)

Created on 2022-05-10 by the reprex package (v2.0.1)

@mgirlich mgirlich added this to the 2.3.0 milestone May 10, 2022
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