Skip to contents

Compute results of a dbplyr query

Usage

# S3 method for class 'tbl_polarssql_connection'
as_polars_lf(x, ..., cte = TRUE)

# S3 method for class 'tbl_polarssql_connection'
as_polars_df(x, ..., cte = TRUE)

# S3 method for class 'tbl_polarssql_connection'
compute(x, ..., n = Inf, cte = TRUE)

# S3 method for class 'tbl_polarssql_connection'
as.data.frame(x, ..., cte = TRUE)

Arguments

x

A tbl_polarssql_connection object.

...
cte

[Experimental] Use common table expressions in the generated SQL?

n

Number of rows to fetch. Defaults to Inf, meaning all rows.

Examples

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

t <- tbl_polarssql(mtcars) |>
  filter(cyl == 4)

as_polars_lf(t)
#> Note: method with signature ‘polarssql_connection#character’ chosen for function ‘dbQuoteIdentifier’,
#>  target signature ‘polarssql_connection#SQL’.
#>  "DBIConnection#SQL" would also be valid
#> polars LazyFrame
#>  $describe_optimized_plan() : Show the optimized query plan.
#> 
#> Naive plan:
#>  SELECT [col("mpg"), col("cyl"), col("disp"), col("hp"), col("drat"), col("wt"), col("qsec"), col("vs"), col("am"), col("gear"), col("carb")] FROM
#>   FILTER [(col("cyl")) == (4.0)] FROM
#>     DF ["mpg", "cyl", "disp", "hp"]; PROJECT */11 COLUMNS; SELECTION: None

as_polars_df(t, n_rows = 1)
#> shape: (1, 11)
#> ┌──────┬─────┬───────┬──────┬───┬─────┬─────┬──────┬──────┐
#> │ mpg  ┆ cyl ┆ disp  ┆ hp   ┆ … ┆ vs  ┆ am  ┆ gear ┆ carb │
#> │ ---  ┆ --- ┆ ---   ┆ ---  ┆   ┆ --- ┆ --- ┆ ---  ┆ ---  │
#> │ f64  ┆ f64 ┆ f64   ┆ f64  ┆   ┆ f64 ┆ f64 ┆ f64  ┆ f64  │
#> ╞══════╪═════╪═══════╪══════╪═══╪═════╪═════╪══════╪══════╡
#> │ 22.8 ┆ 4.0 ┆ 108.0 ┆ 93.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> └──────┴─────┴───────┴──────┴───┴─────┴─────┴──────┴──────┘

compute(t, n = 1) # Equivalent to `as_polars_df(t, n_rows = 1)`
#> shape: (1, 11)
#> ┌──────┬─────┬───────┬──────┬───┬─────┬─────┬──────┬──────┐
#> │ mpg  ┆ cyl ┆ disp  ┆ hp   ┆ … ┆ vs  ┆ am  ┆ gear ┆ carb │
#> │ ---  ┆ --- ┆ ---   ┆ ---  ┆   ┆ --- ┆ --- ┆ ---  ┆ ---  │
#> │ f64  ┆ f64 ┆ f64   ┆ f64  ┆   ┆ f64 ┆ f64 ┆ f64  ┆ f64  │
#> ╞══════╪═════╪═══════╪══════╪═══╪═════╪═════╪══════╪══════╡
#> │ 22.8 ┆ 4.0 ┆ 108.0 ┆ 93.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> └──────┴─────┴───────┴──────┴───┴─────┴─────┴──────┴──────┘

as.data.frame(t, n_rows = 1)
#>    mpg cyl disp hp drat   wt  qsec vs am gear carb
#> 1 22.8   4  108 93 3.85 2.32 18.61  1  1    4    1

# Clean up
DBI::dbDisconnect(polarssql_default_connection())