Skip to content

Commit 23785a0

Browse files
committed
feat: optimizations arguments [skip ci]
1 parent a042672 commit 23785a0

File tree

2 files changed

+110
-36
lines changed

2 files changed

+110
-36
lines changed

R/lazyframe-frame.R

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -446,59 +446,54 @@ lazyframe__profile <- function(
446446
#' lazy_query <- lazy_frame$sort("Species")$filter(pl$col("Species") != "setosa")
447447
#'
448448
#' # This is the query that was written by the user, without any optimizations
449-
#' # (use cat() for better printing)
450-
#' lazy_query$explain(optimized = FALSE) |> cat()
449+
#' # (use writeLines() for better printing)
450+
#' lazy_query$explain(optimized = FALSE) |> writeLines()
451451
#'
452452
#' # This is the query after `polars` optimizes it: instead of sorting first and
453453
#' # then filtering, it is faster to filter first and then sort the rest.
454-
#' lazy_query$explain() |> cat()
454+
#' lazy_query$explain() |> writeLines()
455455
#'
456456
#' # Also possible to see this as tree format
457-
#' lazy_query$explain(format = "tree") |> cat()
457+
#' lazy_query$explain(format = "tree") |> writeLines()
458458
lazyframe__explain <- function(
459459
...,
460460
format = c("plain", "tree"),
461+
engine = c("auto", "in-memory", "streaming"),
461462
optimized = TRUE,
462-
type_coercion = TRUE,
463-
`_type_check` = TRUE,
464-
predicate_pushdown = TRUE,
465-
projection_pushdown = TRUE,
466-
simplify_expression = TRUE,
467-
slice_pushdown = TRUE,
468-
comm_subplan_elim = TRUE,
469-
comm_subexpr_elim = TRUE,
470-
cluster_with_columns = TRUE,
471-
collapse_joins = deprecated(),
472-
`_check_order` = TRUE
463+
optimizations = QueryOptFlags(),
464+
type_coercion = deprecated(),
465+
predicate_pushdown = deprecated(),
466+
projection_pushdown = deprecated(),
467+
simplify_expression = deprecated(),
468+
slice_pushdown = deprecated(),
469+
comm_subplan_elim = deprecated(),
470+
comm_subexpr_elim = deprecated(),
471+
cluster_with_columns = deprecated(),
472+
collapse_joins = deprecated()
473473
) {
474474
wrap({
475475
check_dots_empty0(...)
476476

477477
format <- arg_match0(format, c("plain", "tree"))
478+
engine <- arg_match0(engine, c("auto", "in-memory", "streaming"))
479+
check_is_S7(optimizations, QueryOptFlags)
478480

479-
if (is_present(collapse_joins)) {
480-
deprecate_warn(
481-
c(
482-
`!` = sprintf("%s is deprecated.", format_arg("collapse_joins")),
483-
`i` = sprintf("Use %s instead.", format_arg("predicate_pushdown"))
484-
)
485-
)
486-
}
481+
optimizations <- forward_old_opt_flags(
482+
optimizations,
483+
type_coercion = type_coercion,
484+
predicate_pushdown = predicate_pushdown,
485+
projection_pushdown = projection_pushdown,
486+
simplify_expression = simplify_expression,
487+
slice_pushdown = slice_pushdown,
488+
comm_subplan_elim = comm_subplan_elim,
489+
comm_subexpr_elim = comm_subexpr_elim,
490+
cluster_with_columns = cluster_with_columns,
491+
collapse_joins = collapse_joins
492+
)
487493

488494
if (isTRUE(optimized)) {
489-
ldf <- self$`_ldf`$optimization_toggle(
490-
type_coercion = type_coercion,
491-
`_type_check` = `_type_check`,
492-
predicate_pushdown = predicate_pushdown,
493-
projection_pushdown = projection_pushdown,
494-
simplify_expression = simplify_expression,
495-
slice_pushdown = slice_pushdown,
496-
comm_subplan_elim = comm_subplan_elim,
497-
comm_subexpr_elim = comm_subexpr_elim,
498-
cluster_with_columns = cluster_with_columns,
499-
`_check_order` = `_check_order`,
500-
`_eager` = FALSE
501-
)
495+
prop(optimizations, "streaming", check = FALSE) <- engine == "streaming"
496+
ldf <- self$`_ldf` # TODO: with_optimizations
502497

503498
if (format == "tree") {
504499
ldf$describe_optimized_plan_tree()

R/lazyframe-utils.R

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,82 @@ parse_percentiles <- function(percentiles, inject_median = FALSE) {
158158

159159
c(sub_50_percentiles, at_or_above_50_percentiles)
160160
}
161+
162+
forward_old_opt_flags <- function(
163+
optimizations,
164+
type_coercion = deprecated(),
165+
predicate_pushdown = deprecated(),
166+
projection_pushdown = deprecated(),
167+
simplify_expression = deprecated(),
168+
slice_pushdown = deprecated(),
169+
comm_subplan_elim = deprecated(),
170+
comm_subexpr_elim = deprecated(),
171+
cluster_with_columns = deprecated(),
172+
collapse_joins = deprecated()
173+
) {
174+
call <- caller_env(2L)
175+
warn_func <- function(arg_name) {
176+
deprecate_warn(
177+
c(
178+
`!` = sprintf("%s is deprecated.", format_arg(arg_name)),
179+
`i` = sprintf("Use %s instead.", format_arg("optimizations"))
180+
),
181+
always = TRUE,
182+
user_env = call
183+
)
184+
}
185+
186+
need_validation <- FALSE
187+
188+
if (is_present(type_coercion)) {
189+
warn_func("type_coercion")
190+
prop(optimizations, "type_coercion", check = FALSE) <- type_coercion
191+
need_validation <- TRUE
192+
}
193+
if (is_present(predicate_pushdown)) {
194+
warn_func("predicate_pushdown")
195+
prop(optimizations, "predicate_pushdown", check = FALSE) <- predicate_pushdown
196+
need_validation <- TRUE
197+
}
198+
if (is_present(projection_pushdown)) {
199+
warn_func("projection_pushdown")
200+
prop(optimizations, "projection_pushdown", check = FALSE) <- projection_pushdown
201+
need_validation <- TRUE
202+
}
203+
if (is_present(simplify_expression)) {
204+
warn_func("simplify_expression")
205+
prop(optimizations, "simplify_expression", check = FALSE) <- simplify_expression
206+
need_validation <- TRUE
207+
}
208+
if (is_present(slice_pushdown)) {
209+
warn_func("slice_pushdown")
210+
prop(optimizations, "slice_pushdown", check = FALSE) <- slice_pushdown
211+
need_validation <- TRUE
212+
}
213+
if (is_present(comm_subplan_elim)) {
214+
warn_func("comm_subplan_elim")
215+
prop(optimizations, "comm_subplan_elim", check = FALSE) <- comm_subplan_elim
216+
need_validation <- TRUE
217+
}
218+
if (is_present(comm_subexpr_elim)) {
219+
warn_func("comm_subexpr_elim")
220+
prop(optimizations, "comm_subexpr_elim", check = FALSE) <- comm_subexpr_elim
221+
need_validation <- TRUE
222+
}
223+
if (is_present(cluster_with_columns)) {
224+
warn_func("cluster_with_columns")
225+
prop(optimizations, "cluster_with_columns", check = FALSE) <- cluster_with_columns
226+
need_validation <- TRUE
227+
}
228+
if (is_present(collapse_joins)) {
229+
warn_func("collapse_joins")
230+
prop(optimizations, "predicate_pushdown", check = FALSE) <- collapse_joins
231+
need_validation <- TRUE
232+
}
233+
234+
if (need_validation) {
235+
validate(optimizations)
236+
}
237+
238+
optimizations
239+
}

0 commit comments

Comments
 (0)