presto_unnest: Unnest array columns in Presto tables

View source: R/presto_unnest.R

presto_unnestR Documentation

Unnest array columns in Presto tables

Description

[Experimental]

Expands array columns into rows using Presto's ⁠CROSS JOIN UNNEST⁠ syntax. This is similar to tidyr::unnest() but works with Presto database tables.

Usage

presto_unnest(
  data,
  cols,
  ...,
  values_to = NULL,
  with_ordinality = FALSE,
  ordinality_to = NULL
)

## S3 method for class 'tbl_presto'
presto_unnest(
  data,
  cols,
  ...,
  values_to = NULL,
  with_ordinality = FALSE,
  ordinality_to = NULL
)

Arguments

data

A tbl_presto object

cols

Column(s) to unnest. Currently only supports a single column.

...

Additional arguments (currently unused)

values_to

Name of column to store unnested values. If NULL, uses the original column name with "_elem" appended (e.g., arr becomes arr_elem).

with_ordinality

If TRUE, includes an ordinality column with the position of each element in the array.

ordinality_to

Name of ordinality column when with_ordinality = TRUE. Must be provided if with_ordinality = TRUE.

Value

A tbl_presto object with the array column unnested into rows.

Examples

## Not run: 
# Connect to Presto
con <- DBI::dbConnect(RPresto::Presto(), ...)

# Create a table with an array column
DBI::dbExecute(con, "CREATE TABLE test (id BIGINT, arr ARRAY(BIGINT))")
DBI::dbExecute(con, "INSERT INTO test VALUES (1, ARRAY[10, 20, 30])")

# Unnest the array column
tbl(con, "test") %>%
  presto_unnest(arr) %>%
  collect()
# Without values_to, the unnested column is named "arr_elem"

# Or specify a custom name
tbl(con, "test") %>%
  presto_unnest(arr, values_to = "elem") %>%
  collect()

## End(Not run)

RPresto documentation built on Dec. 2, 2025, 9:07 a.m.