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

Joins and set operations queries not reused in CTEs #978

Closed
mgirlich opened this issue Aug 16, 2022 · 0 comments · Fixed by #979
Closed

Joins and set operations queries not reused in CTEs #978

mgirlich opened this issue Aug 16, 2022 · 0 comments · Fixed by #979

Comments

@mgirlich
Copy link
Collaborator

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

lf1 <- lazy_frame(x = 1, .name = "lf1")
lf <- lf1 %>% 
  filter(x == 1)

# the CTE for `lf` is reused
union_all(
  lf,
  lf
) %>% 
  show_query(cte = TRUE)
#> <SQL>
#> WITH `q01` AS (
#>   SELECT *
#>   FROM `lf1`
#>   WHERE (`x` = 1.0)
#> )
#> (
#>   SELECT *
#>   FROM `q01`
#> )
#> UNION ALL
#> (
#>   SELECT *
#>   FROM `q01`
#> )

# the CTE for `lf` is not reused
lf <- lf1 %>% 
  inner_join(lf1, by = "x")
union_all(
  lf,
  lf
) %>% 
  show_query(cte = TRUE)
#> <SQL>
#> WITH `q01` AS (
#>   SELECT `lf1_LHS`.`x` AS `x`
#>   FROM `lf1` AS `lf1_LHS`
#>   INNER JOIN `lf1` AS `lf1_RHS`
#>     ON (`lf1_LHS`.`x` = `lf1_RHS`.`x`)
#> ),
#> `q02` AS (
#>   SELECT `lf1_LHS`.`x` AS `x`
#>   FROM `lf1` AS `lf1_LHS`
#>   INNER JOIN `lf1` AS `lf1_RHS`
#>     ON (`lf1_LHS`.`x` = `lf1_RHS`.`x`)
#> )
#> (
#>   SELECT *
#>   FROM `q01`
#> )
#> UNION ALL
#> (
#>   SELECT *
#>   FROM `q02`
#> )

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

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