Skip to content

Commit d71431d

Browse files
committed
Misc minor optimizations to query optimizer performance
1 parent 4a7330f commit d71431d

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use arrow::compute::can_cast_types;
2121
use datafusion_expr::binary::BinaryTypeCoercer;
2222
use itertools::{Itertools as _, izip};
23-
use std::sync::Arc;
23+
use std::sync::{Arc, LazyLock};
2424

2525
use crate::analyzer::AnalyzerRule;
2626
use crate::utils::NamePreserver;
@@ -91,11 +91,11 @@ impl AnalyzerRule for TypeCoercion {
9191
}
9292

9393
fn analyze(&self, plan: LogicalPlan, config: &ConfigOptions) -> Result<LogicalPlan> {
94-
let empty_schema = DFSchema::empty();
94+
static EMPTY_SCHEMA: LazyLock<DFSchema> = LazyLock::new(DFSchema::empty);
9595

9696
// recurse
9797
let transformed_plan = plan
98-
.transform_up_with_subqueries(|plan| analyze_internal(&empty_schema, plan))?
98+
.transform_up_with_subqueries(|plan| analyze_internal(&EMPTY_SCHEMA, plan))?
9999
.data;
100100

101101
// finish

datafusion/optimizer/src/common_subexpr_eliminate.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,7 @@ impl CommonSubexprEliminate {
325325
.map(|expr| Some(name_preserver.save(expr)))
326326
.collect::<Vec<_>>()
327327
} else {
328-
new_aggr_expr
329-
.clone()
330-
.into_iter()
331-
.map(|_| None)
332-
.collect::<Vec<_>>()
328+
(0..new_aggr_expr.len()).map(|_| None).collect()
333329
};
334330

335331
let mut agg_exprs = common_exprs

datafusion/optimizer/src/optimize_unions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ impl OptimizerRule for OptimizeUnions {
6464
let inputs = inputs
6565
.into_iter()
6666
.flat_map(extract_plans_from_union)
67-
.map(|plan| coerce_plan_expr_for_schema(plan, &schema))
67+
.map(|plan| Ok(Arc::new(coerce_plan_expr_for_schema(plan, &schema)?)))
6868
.collect::<Result<Vec<_>>>()?;
6969

7070
Ok(Transformed::yes(LogicalPlan::Union(Union {
71-
inputs: inputs.into_iter().map(Arc::new).collect_vec(),
71+
inputs,
7272
schema,
7373
})))
7474
}

datafusion/optimizer/src/push_down_filter.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,8 @@ fn can_evaluate_as_join_condition(predicate: &Expr) -> Result<bool> {
320320
/// * do nothing.
321321
fn extract_or_clauses_for_join<'a>(
322322
filters: &'a [Expr],
323-
schema: &'a DFSchema,
323+
schema_cols: &'a HashSet<Column>,
324324
) -> impl Iterator<Item = Expr> + 'a {
325-
let schema_columns = schema_columns(schema);
326-
327325
// new formed OR clauses and their column references
328326
filters.iter().filter_map(move |expr| {
329327
if let Expr::BinaryExpr(BinaryExpr {
@@ -332,8 +330,8 @@ fn extract_or_clauses_for_join<'a>(
332330
right,
333331
}) = expr
334332
{
335-
let left_expr = extract_or_clause(left.as_ref(), &schema_columns);
336-
let right_expr = extract_or_clause(right.as_ref(), &schema_columns);
333+
let left_expr = extract_or_clause(left.as_ref(), &schema_cols);
334+
let right_expr = extract_or_clause(right.as_ref(), &schema_cols);
337335

338336
// If nothing can be extracted from any sub clauses, do nothing for this OR clause.
339337
if let (Some(left_expr), Some(right_expr)) = (left_expr, right_expr) {
@@ -421,6 +419,10 @@ fn push_down_all_join(
421419
// 3) should be kept as filter conditions
422420
let left_schema = join.left.schema();
423421
let right_schema = join.right.schema();
422+
423+
let left_schema_columns = schema_columns(left_schema.as_ref());
424+
let right_schema_columns = schema_columns(right_schema.as_ref());
425+
424426
let mut left_push = vec![];
425427
let mut right_push = vec![];
426428
let mut keep_predicates = vec![];
@@ -467,26 +469,38 @@ fn push_down_all_join(
467469
// Extract from OR clause, generate new predicates for both side of join if possible.
468470
// We only track the unpushable predicates above.
469471
if left_preserved {
470-
left_push.extend(extract_or_clauses_for_join(&keep_predicates, left_schema));
471-
left_push.extend(extract_or_clauses_for_join(&join_conditions, left_schema));
472+
left_push.extend(extract_or_clauses_for_join(
473+
&keep_predicates,
474+
&left_schema_columns,
475+
));
476+
left_push.extend(extract_or_clauses_for_join(
477+
&join_conditions,
478+
&left_schema_columns,
479+
));
472480
}
473481
if right_preserved {
474-
right_push.extend(extract_or_clauses_for_join(&keep_predicates, right_schema));
475-
right_push.extend(extract_or_clauses_for_join(&join_conditions, right_schema));
482+
right_push.extend(extract_or_clauses_for_join(
483+
&keep_predicates,
484+
&right_schema_columns,
485+
));
486+
right_push.extend(extract_or_clauses_for_join(
487+
&join_conditions,
488+
&right_schema_columns,
489+
));
476490
}
477491

478492
// For predicates from join filter, we should check with if a join side is preserved
479493
// in term of join filtering.
480494
if on_left_preserved {
481495
left_push.extend(extract_or_clauses_for_join(
482496
&on_filter_join_conditions,
483-
left_schema,
497+
&left_schema_columns,
484498
));
485499
}
486500
if on_right_preserved {
487501
right_push.extend(extract_or_clauses_for_join(
488502
&on_filter_join_conditions,
489-
right_schema,
503+
&right_schema_columns,
490504
));
491505
}
492506

datafusion/optimizer/src/push_down_limit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ impl OptimizerRule for PushDownLimit {
4747
true
4848
}
4949

50+
#[expect(clippy::only_used_in_recursion)]
5051
fn rewrite(
5152
&self,
5253
plan: LogicalPlan,
5354
config: &dyn OptimizerConfig,
5455
) -> Result<Transformed<LogicalPlan>> {
55-
let _ = config.options();
5656
let LogicalPlan::Limit(mut limit) = plan else {
5757
return Ok(Transformed::no(plan));
5858
};

0 commit comments

Comments
 (0)