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 'polars_lazy_frame'
explain(x, optimized = TRUE, ...)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\")]\n FILTER [(col(\"cyl\")) == (3.0)]\n FROM\n SORT BY [nulls_last: [true]] [col(\"drat\")]\n DF [\"mpg\", \"cyl\", \"disp\", \"hp\", ...]; PROJECT */11 COLUMNS"
# better printing with cat():
cat(no_opt)
#> SELECT [col("mpg")]
#> FILTER [(col("cyl")) == (3.0)]
#> FROM
#> SORT BY [nulls_last: [true]] [col("drat")]
#> DF ["mpg", "cyl", "disp", "hp", ...]; PROJECT */11 COLUMNS
# optimized query run by polars
cat(explain(query))
#> simple π 1/1 ["mpg"]
#> SORT BY [nulls_last: [true]] [col("drat")]
#> FILTER [(col("cyl")) == (3.0)]
#> FROM
#> DF ["mpg", "cyl", "disp", "hp", ...]; PROJECT["mpg", "cyl", "drat"] 3/11 COLUMNS
