Given 2 remote tables (simulated with tbl_lazy
for this example)
library("dplyr")
library("dbplyr")
t1 <- tbl_lazy(df = iris, src = dbplyr::simulate_mysql())
t2 <- tbl_lazy(df = mtcars, src = dbplyr::simulate_mysql())
How can I perform an actual* cross join between t1 and t2 using R and dbplyr?
* i.e. using CROSS JOIN
in the translated SQL query
Note that I know how to perform all the other types of joins, this is precisely about CROSS joins.
I am aware of the following trick:
joined <- t1 %>%
mutate(tmp = 1) %>%
full_join(mutate(t2, tmp = 1), by = "tmp") %>%
select(-tmp)
However
- This is ugly (even if it could be hidden in a function)
- I would like to take advantage of the highly optimised join capabilities of the DB, so I'd like to pass a real SQL
CROSS JOIN
. Usingshow_query(joined)
shows that the generated SQL query usesLEFT JOIN
.
Sadly, there is no cross_join
operator in dplyr
and sql_join(t1, t2, type = "cross")
does not work either (not implemented for tbl
s, works only on DB connections).
How can I generate an SQL CROSS JOIN
with dbplyr?