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

Use LHS.* after left_join() #959

Merged
merged 7 commits into from
Aug 8, 2022
Merged

Use LHS.* after left_join() #959

merged 7 commits into from
Aug 8, 2022

Conversation

mgirlich
Copy link
Collaborator

@mgirlich mgirlich commented Aug 4, 2022

Closes #898. There should be no issues with Oracle this time as this uses a table alias.

library(dbplyr)

lf1 <- lazy_frame(!!!setNames(as.list(1:26), letters))
lf2 <- lazy_frame(a = 1, a1 = 1, a2 = 1)

lf1 |> 
  left_join(lf2)
#> Joining, by = "a"
#> <SQL>
#> SELECT `LHS`.*, `a1`, `a2`
#> FROM `df` AS `LHS`
#> LEFT JOIN `df` AS `RHS`
#>   ON (`LHS`.`a` = `RHS`.`a`)

# and a small bonus thanks to inlining `select()` into joins
lf1 |> 
  left_join(lf2) %>% 
  relocate(a1)
#> Joining, by = "a"
#> <SQL>
#> SELECT `a1`, `LHS`.*, `a2`
#> FROM `df` AS `LHS`
#> LEFT JOIN `df` AS `RHS`
#>   ON (`LHS`.`a` = `RHS`.`a`)

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

@mgirlich mgirlich requested a review from hadley August 4, 2022 07:32
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.

Looks great!

R/query-join.R Show resolved Hide resolved
NEWS.md Outdated
@@ -1,5 +1,8 @@
# dbplyr (development version)

* When possible, dbplyr now uses `SELECT *` after a left join instead of
Copy link
Member

Choose a reason for hiding this comment

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

Or inner join?

Could we apply the same logic to right joins too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not that simple to extend this to all type of joins but now they are all supported (note that for full_join() one can never use *).

@@ -4,7 +4,7 @@
lazy_frame(x = 1, y = 1) %>% tidyr::expand(x, y)
Output
<SQL>
SELECT `x`, `y`
SELECT `LHS`.*, `RHS`.*
Copy link
Member

Choose a reason for hiding this comment

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

If it's easy, maybe we should only use * if there is more than one variable selected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this looks a bit better now.

Copy link
Member

Choose a reason for hiding this comment

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

Nice!

@mgirlich mgirlich merged commit 57e4cd8 into main Aug 8, 2022
@mgirlich mgirlich deleted the join-use-star branch August 8, 2022 06:57
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.

* in joins
2 participants