Learn R Programming

RPresto (version 1.4.8)

presto_unnest: 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 tbl_presto presto_unnest( data, cols, ..., values_to = NULL, with_ordinality = FALSE, ordinality_to = NULL )

Value

A tbl_presto object with the array column unnested into rows.

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.

Examples

Run this code
if (FALSE) {
# 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()
}

Run the code above in your browser using DataLab