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, ..., n = Inf, 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, ..., n = Inf, cte = TRUE)

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

# S3 method for class 'tbl_polarssql_connection'
collect(x, ..., n = Inf, 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) |>
  mutate(cyl = sql("CAST (cyl AS Int64)"))

as_polars_lf(t)
#> <polars_lazy_frame>

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

compute(t, n = Inf, engine = "streaming")
#> shape: (11, 11)
#> ┌──────┬─────┬───────┬───────┬───┬─────┬─────┬──────┬──────┐
#> │ mpg  ┆ cyl ┆ disp  ┆ hp    ┆ … ┆ vs  ┆ am  ┆ gear ┆ carb │
#> │ ---  ┆ --- ┆ ---   ┆ ---   ┆   ┆ --- ┆ --- ┆ ---  ┆ ---  │
#> │ f64  ┆ i64 ┆ f64   ┆ f64   ┆   ┆ f64 ┆ f64 ┆ f64  ┆ f64  │
#> ╞══════╪═════╪═══════╪═══════╪═══╪═════╪═════╪══════╪══════╡
#> │ 22.8 ┆ 4   ┆ 108.0 ┆ 93.0  ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> │ 24.4 ┆ 4   ┆ 146.7 ┆ 62.0  ┆ … ┆ 1.0 ┆ 0.0 ┆ 4.0  ┆ 2.0  │
#> │ 22.8 ┆ 4   ┆ 140.8 ┆ 95.0  ┆ … ┆ 1.0 ┆ 0.0 ┆ 4.0  ┆ 2.0  │
#> │ 32.4 ┆ 4   ┆ 78.7  ┆ 66.0  ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> │ 30.4 ┆ 4   ┆ 75.7  ┆ 52.0  ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 2.0  │
#> │ …    ┆ …   ┆ …     ┆ …     ┆ … ┆ …   ┆ …   ┆ …    ┆ …    │
#> │ 21.5 ┆ 4   ┆ 120.1 ┆ 97.0  ┆ … ┆ 1.0 ┆ 0.0 ┆ 3.0  ┆ 1.0  │
#> │ 27.3 ┆ 4   ┆ 79.0  ┆ 66.0  ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> │ 26.0 ┆ 4   ┆ 120.3 ┆ 91.0  ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 2.0  │
#> │ 30.4 ┆ 4   ┆ 95.1  ┆ 113.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 5.0  ┆ 2.0  │
#> │ 21.4 ┆ 4   ┆ 121.0 ┆ 109.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 2.0  │
#> └──────┴─────┴───────┴───────┴───┴─────┴─────┴──────┴──────┘

collect(t, n = 5, int64 = "character")
#> # A tibble: 5 × 11
#>     mpg cyl    disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  22.8 4     108      93  3.85  2.32  18.6     1     1     4     1
#> 2  24.4 4     147.     62  3.69  3.19  20       1     0     4     2
#> 3  22.8 4     141.     95  3.92  3.15  22.9     1     0     4     2
#> 4  32.4 4      78.7    66  4.08  2.2   19.5     1     1     4     1
#> 5  30.4 4      75.7    52  4.93  1.62  18.5     1     1     4     2

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