Skip to contents

This function is available for LazyFrames only.

By default, explain() shows the query plan that is optimized and then run by Polars. Setting optimized = FALSE shows the query plan as-is, without any optimization done, but this is not the query performed. Note that the plans are read from bottom to top.

Usage

# S3 method for class 'RPolarsLazyFrame'
explain(x, optimized = TRUE, ...)

Arguments

x

A Polars LazyFrame.

optimized

Logical. If TRUE (default), show the query optimized by Polars. Otherwise, show the initial query.

...

Ignored.

Examples

query <- mtcars |>
  as_polars_lf() |>
  arrange(drat) |>
  filter(cyl == 3) |>
  select(mpg)

# unoptimized query plan:
no_opt <- explain(query, optimized = FALSE)
no_opt
#> [1] " SELECT [col(\"mpg\")] FROM\n  FILTER [(col(\"cyl\")) == (3.0)] FROM\n    SORT BY [col(\"drat\")]\n      DF [\"mpg\", \"cyl\", \"disp\", \"hp\"]; PROJECT */11 COLUMNS; SELECTION: None"

# better printing with cat():
cat(no_opt)
#>  SELECT [col("mpg")] FROM
#>   FILTER [(col("cyl")) == (3.0)] FROM
#>     SORT BY [col("drat")]
#>       DF ["mpg", "cyl", "disp", "hp"]; PROJECT */11 COLUMNS; SELECTION: None

# optimized query run by polars
cat(explain(query))
#> simple π 1/3 ["mpg"]
#>   SORT BY [col("drat")]
#>     DF ["mpg", "cyl", "disp", "hp"]; PROJECT 3/11 COLUMNS; SELECTION: [(col("cyl")) == (3.0)]