tidyverse / dbplyr

Database (DBI) backend for dplyr
https://dbplyr.tidyverse.org
Other
478 stars 174 forks source link

Joins with more then 3 lazy_frames #1127

Closed joshseiv closed 1 year ago

joshseiv commented 1 year ago

I am having an issue with joins with 2.3 for some reason the query is not being built correctly, in the example below you will see the third lazy_frame (lf3) continues to be referenced in the select for e and f.

lf1 <- lazy_frame(a = 1, b = 2, .name = "lf1")
lf2 <- lazy_frame(b = 2, c = 3, .name = "lf2")
lf3 <- lazy_frame(c = 3, d = 4, .name = "lf3")
lf4 <- lazy_frame(d = 4, e = 5, .name = "lf4")
lf5 <- lazy_frame(e = 5, f = 6, .name = "lf5")
lf6 <- lazy_frame(f = 6, g = 7, .name = "lf6")

lf1 |> 
  left_join(lf2, by = "b") |> 
  left_join(lf3, by = "c") |> 
  left_join(lf4, by = "d") |> 
  left_join(lf5, by = "e") |> 
  left_join(lf6, by = "f") 
<SQL>
SELECT
  `lf1`.*,
  `lf2`.`c` AS `c`,
  `lf3`.`d` AS `d`,
  `lf3`.`e` AS `e`,
  `lf3`.`f` AS `f`,
  `g`
FROM `lf1`
LEFT JOIN `lf2`
  ON (`lf1`.`b` = `lf2`.`b`)
LEFT JOIN `lf3`
  ON (`lf2`.`c` = `lf3`.`c`)
LEFT JOIN `lf4`
  ON (`lf3`.`d` = `lf4`.`d`)
LEFT JOIN `lf5`
  ON (`lf3`.`e` = `lf5`.`e`)
LEFT JOIN `lf6`
  ON (`lf3`.`f` = `lf6`.`f`)
mgirlich commented 1 year ago

Thanks for filing the issue. This is already fixed in the dev version. You can install it via devtools::install_github("tidyverse/dbplyr"). We plan to release the fixes to CRAN soon.