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

translate CROSS JOIN UNNEST #267

Open
copernican opened this issue Apr 24, 2024 · 1 comment
Open

translate CROSS JOIN UNNEST #267

copernican opened this issue Apr 24, 2024 · 1 comment
Assignees

Comments

@copernican
Copy link
Contributor

Is there any interest in adding a dplyr-like function that implements CROSS JOIN UNNEST? I use the below frequently and am curious if it could be useful in RPresto.

cross_join_unnest <- function(x, ..., values_to = "value") {
  conn <- x$src$con
  qry <- dbplyr::build_sql(
    "SELECT a.*, ",
    dbplyr::ident(values_to),
    " FROM (",
    dbplyr::sql_render(x),
    ") a ",
    "\nCROSS JOIN UNNEST (",
    dbplyr::ident(rlang::as_name(...)),
    ") AS t(",
    dbplyr::ident(values_to),
    ")",
    con = conn
  )

  dplyr::tbl(conn, dbplyr::sql(qry))
}

This allows a workflow like

conn <- dbConnect(RPresto::Presto(), ...)
my_tbl <- dplyr::tbl(conn, "my_tbl")

my_tbl |>
  cross_join_unnest("my_array_col") |>
  select(-my_array_col) |>
  collect()

If yes, I'd be happy to create a PR.

@jarodmeng
Copy link
Contributor

Interesting idea! Similar to this question raised on RPostgres. Are you planning to whip a PR on this? If yes, can you include a few different test cases (e.g. cross_join_unnest directly on a remote table vs. on a sql, etc)? I will also put the function's lifecycle as experimental. Thanks!

@jarodmeng jarodmeng self-assigned this Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants