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

Inline select() after *_join() #876

Merged
merged 12 commits into from
Aug 4, 2022
Merged

Inline select() after *_join() #876

merged 12 commits into from
Aug 4, 2022

Conversation

mgirlich
Copy link
Collaborator

@mgirlich mgirlich commented May 18, 2022

Closes #866.

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

lf1 <- lazy_frame(x = 1, a = 1, .name = "lf1")
lf2 <- lazy_frame(x = 1, b = 2, .name = "lf2")

left/right/full/inner_join()

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

before

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

after

SELECT `b`, `LHS`.`x` AS `x`
FROM `lf1` AS `LHS`
LEFT JOIN `lf2` AS `RHS`
  ON (`LHS`.`x` = `RHS`.`x`)

semi/anti_join()

semi_join(lf1, lf2, by = "x") %>%
  select(x)

Before

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

After

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

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

For semi/anti_join() this only works with projections (i.e. select() and rename()). In the long run one could merge lazy_semi_join_query() and lazy_select_query() as semi_join() and anti_join() are simply translated to WHERE (NOT) EXISTS. But I'd keep them separate for now and explore that at a different time.

@mgirlich mgirlich added this to the 2.3.0 milestone May 18, 2022
@mgirlich mgirlich mentioned this pull request May 27, 2022
Conflicts:
	R/lazy-join-query.R
	R/verb-joins.R
	R/verb-select.R
	tests/testthat/_snaps/verb-select.md
	tests/testthat/test-verb-select.R
@mgirlich mgirlich requested a review from hadley August 2, 2022 08:12
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.

LGTM!

I tried a couple of more exotic join edge cases and the results look great 😄

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

lf1 <- lazy_frame(x = 1, y = 1, .name = "lf1")
lf2 <- lazy_frame(x = 1, y = 2, .name = "lf2")

lf1 |> 
  left_join(lf2, by = "x") |> 
  select(x, y = y.y)
#> <SQL>
#> SELECT `LHS`.`x` AS `x`, `RHS`.`y` AS `y`
#> FROM `lf1` AS `LHS`
#> LEFT JOIN `lf2` AS `RHS`
#>   ON (`LHS`.`x` = `RHS`.`x`)

lf1 |> 
  left_join(lf2 |> rename(z = y), by = "x") |> 
  select(z)
#> <SQL>
#> SELECT `RHS`.`y` AS `z`
#> FROM `lf1` AS `LHS`
#> LEFT JOIN `lf2` AS `RHS`
#>   ON (`LHS`.`x` = `RHS`.`x`)

Created on 2022-08-03 by the reprex package (v2.0.1)

R/verb-select.R Outdated Show resolved Hide resolved
@mgirlich mgirlich merged commit 14305df into main Aug 4, 2022
@mgirlich mgirlich deleted the join-select branch August 4, 2022 04:36
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.

Combine *_join() + select() in one query
2 participants