Skip to contents

Use simulate_polarssql() with dbplyr::tbl_lazy() or dbplyr::lazy_frame() to see simulated SQL without converting to live access. tbl_polarssql() is similar to dbplyr::tbl_memdb(), but the backend is Polars instead of SQLite. It uses polarssql_default_connection() as the DBI connection.

Usage

simulate_polarssql()

tbl_polarssql(df, name = deparse(substitute(df)), ..., overwrite = FALSE)

Arguments

df

Data frame to copy

name

Name of table in database: defaults to a random name that's unlikely to conflict with an existing table.

...

Ignored.

overwrite

If TRUE, overwrite the existing table which has the same name. If not TRUE (default), skip writing a table if it already exists.

Examples

library(dplyr, warn.conflicts = FALSE)

# Test connection shows the SQL query.
dbplyr::tbl_lazy(mtcars, simulate_polarssql(), name = "mtcars") |>
  filter(cyl == 4) |>
  arrange(desc(mpg)) |>
  select(contains("c")) |>
  head(n = 3)
#> <SQL>
#> SELECT `cyl`, `qsec`, `carb`
#> FROM `mtcars`
#> WHERE (`cyl` = 4.0)
#> ORDER BY `mpg` DESC
#> LIMIT 3

# Actual polarssql connection shows the Polars naive plan (LazyFrame).
tbl_polarssql(mtcars) |>
  filter(cyl == 4) |>
  arrange(desc(mpg)) |>
  select(contains("c")) |>
  head(n = 3)
#> # Source:   SQL [?? x 3]
#> # Database: polarssql_connection
#>     cyl  qsec  carb
#>   <dbl> <dbl> <dbl>
#> 1     4  19.9     1
#> 2     4  19.5     1
#> 3     4  18.5     2

# Unlike other dbplyr backends, `compute` has a special behavior.
# It returns a polars DataFrame.
tbl_polarssql(mtcars) |>
  filter(cyl == 4) |>
  arrange(desc(mpg)) |>
  select(contains("c")) |>
  head(n = 3) |>
  compute()
#> shape: (3, 3)
#> ┌─────┬───────┬──────┐
#> │ cyl ┆ qsec  ┆ carb │
#> │ --- ┆ ---   ┆ ---  │
#> │ f64 ┆ f64   ┆ f64  │
#> ╞═════╪═══════╪══════╡
#> │ 4.0 ┆ 19.9  ┆ 1.0  │
#> │ 4.0 ┆ 19.47 ┆ 1.0  │
#> │ 4.0 ┆ 18.52 ┆ 2.0  │
#> └─────┴───────┴──────┘