Create a string representation of the query plan
Description
The query plan is read from bottom to top. When optimized =
FALSE
, the query as it was written by the user is shown. This is
not what Polars runs. Instead, it applies optimizations that are
displayed by default by
$explain()
. One classic example
is the predicate pushdown, which applies the filter as early as possible
(i.e. at the bottom of the plan).
Usage
<LazyFrame>$explain(
...,
format = c("plain", "tree"),
optimized = TRUE,
type_coercion = TRUE,
`_type_check` = TRUE,
predicate_pushdown = TRUE,
projection_pushdown = TRUE,
simplify_expression = TRUE,
slice_pushdown = TRUE,
comm_subplan_elim = TRUE,
comm_subexpr_elim = TRUE,
cluster_with_columns = TRUE,
collapse_joins = TRUE,
streaming = FALSE,
`_check_order` = TRUE
)
Arguments
Value
A character value containing the query plan.
Examples
library("polars")
lazy_frame <- as_polars_lf(iris)
# Prepare your query
lazy_query <- lazy_frame$sort("Species")$filter(pl$col("Species") != "setosa")
# This is the query that was written by the user, without any optimizations
# (use cat() for better printing)
lazy_query$explain(optimized = FALSE) |> cat()
#> FILTER [(col("Species")) != ("setosa")]
#> FROM
#> SORT BY [col("Species")]
#> DF ["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", ...]; PROJECT */5 COLUMNS
# This is the query after `polars` optimizes it: instead of sorting first and
# then filtering, it is faster to filter first and then sort the rest.
lazy_query$explain() |> cat()
#> SORT BY [col("Species")]
#> FILTER [(col("Species")) != ("setosa")]
#> FROM
#> DF ["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", ...]; PROJECT */5 COLUMNS
#> 0 1 2
#> ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#> │
#> │ ╭─────────╮
#> 0 │ │ SORT BY │
#> │ ╰────┬┬───╯
#> │ ││
#> │ │╰───────────────────────────╮
#> │ │ │
#> │ ╭───────┴────────╮ │
#> │ │ expression: │ ╭───┴────╮
#> 1 │ │ col("Species") │ │ FILTER │
#> │ ╰────────────────╯ ╰───┬┬───╯
#> │ ││
#> │ │╰───────────────────────────────────────────────────────╮
#> │ │ │
#> │ ╭────────────────┴─────────────────╮ ╭───────────────────────────────────┴────────────────────────────────────╮
#> │ │ predicate: │ │ FROM: │
#> 2 │ │ [(col("Species")) != ("setosa")] │ │ DF ["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", ...] │
#> │ ╰──────────────────────────────────╯ │ PROJECT */5 COLUMNS │
#> │ ╰────────────────────────────────────────────────────────────────────────╯