datafusion-optimizer 56.0.0

DataFusion Query Optimizer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! [`EliminateFilter`] removes filters that accept all or no input rows.

use datafusion_common::tree_node::Transformed;
use datafusion_common::{Result, ScalarValue};
use datafusion_expr::expr::InList;
use datafusion_expr::{EmptyRelation, Expr, Filter, LogicalPlan, Operator};
use std::sync::Arc;

use crate::optimizer::ApplyOrder;
use crate::{OptimizerConfig, OptimizerRule};

/// Optimization rule that eliminates filters whose predicates always accept or
/// reject their input rows.
///
/// This saves time in planning and executing the query.
/// Note that this rule should be applied after simplify expressions optimizer rule.
#[derive(Default, Debug)]
pub struct EliminateFilter;

impl EliminateFilter {
    #[expect(missing_docs)]
    pub fn new() -> Self {
        Self {}
    }
}

impl OptimizerRule for EliminateFilter {
    fn name(&self) -> &str {
        "eliminate_filter"
    }

    fn apply_order(&self) -> Option<ApplyOrder> {
        Some(ApplyOrder::TopDown)
    }

    fn rewrite(
        &self,
        plan: LogicalPlan,
        _config: &dyn OptimizerConfig,
    ) -> Result<Transformed<LogicalPlan>> {
        let LogicalPlan::Filter(Filter {
            predicate, input, ..
        }) = plan
        else {
            return Ok(Transformed::no(plan));
        };

        let simplified = simplify_filter_predicate(predicate);
        match simplified.predicate {
            FilterPredicate::AcceptsAll => {
                Ok(Transformed::yes(Arc::unwrap_or_clone(input)))
            }
            FilterPredicate::RejectsAll(_) => Ok(Transformed::yes(
                LogicalPlan::EmptyRelation(EmptyRelation {
                    produce_one_row: false,
                    schema: Arc::clone(input.schema()),
                }),
            )),
            FilterPredicate::Expression(predicate) => Ok(Transformed::new_transformed(
                LogicalPlan::Filter(Filter::new(predicate, input)),
                simplified.transformed,
            )),
        }
    }
}

/// The outcome of a predicate when only rows for which it evaluates to `TRUE`
/// are retained.
enum FilterPredicate {
    AcceptsAll,
    RejectsAll(Expr),
    Expression(Expr),
}

struct SimplifiedFilterPredicate {
    predicate: FilterPredicate,
    transformed: bool,
}

/// Simplifies positive `AND` / `OR` trees according to filter semantics.
///
/// A `NULL` predicate rejects a row just like `FALSE`. This equivalence is safe
/// throughout an `AND` / `OR` tree because both operators are monotonic with
/// respect to whether their result can be `TRUE`. It is not safe through `NOT`
/// or arbitrary expressions, so those are deliberately treated as leaves.
///
/// The same equivalence lets `IN` lists drop their `NULL` members, and makes
/// `NOT IN` lists that contain one reject every row. See
/// [`simplify_filter_in_list`].
fn simplify_filter_predicate(expr: Expr) -> SimplifiedFilterPredicate {
    match expr {
        Expr::Literal(ScalarValue::Boolean(Some(true)), _) => {
            simplified(FilterPredicate::AcceptsAll, false)
        }
        expr @ Expr::Literal(ScalarValue::Boolean(Some(false) | None), _) => {
            simplified(FilterPredicate::RejectsAll(expr), false)
        }
        Expr::BinaryExpr(binary) if binary.op == Operator::And => {
            simplify_filter_and(*binary.left, *binary.right)
        }
        Expr::BinaryExpr(binary) if binary.op == Operator::Or => {
            simplify_filter_or(*binary.left, *binary.right)
        }
        Expr::InList(in_list) => simplify_filter_in_list(in_list),
        expr => simplified(FilterPredicate::Expression(expr), false),
    }
}

fn is_null_literal(expr: &Expr) -> bool {
    matches!(expr, Expr::Literal(scalar, _) if scalar.is_null())
}

/// Simplifies an `IN` list under filter semantics by dropping `NULL` members.
///
/// `x NOT IN (a, NULL)` is `FALSE` when `x` matches `a` and `NULL` otherwise, so
/// it is never `TRUE` and rejects every row.
///
/// This is only sound under filter semantics; the same rewrite in a general
/// expression context would change `SELECT 3 IN (1, NULL)` from `NULL` to `FALSE`.
fn simplify_filter_in_list(in_list: InList) -> SimplifiedFilterPredicate {
    let InList {
        expr,
        mut list,
        negated,
    } = in_list;

    // `retain` drops the `NULL`s in place. Most lists have none, and that case
    // then costs one pass and no allocation.
    let len_before = list.len();
    list.retain(|e| !is_null_literal(e));

    if list.len() == len_before {
        // No `NULL` members, so there is nothing to simplify.
        return simplified(
            FilterPredicate::Expression(Expr::InList(InList::new(expr, list, negated))),
            false,
        );
    }

    if negated || list.is_empty() {
        // `NOT IN` with a `NULL` member, and `IN` over nothing but `NULL`s, are
        // both `NULL` for every row that they do not reject outright.
        return simplified(
            FilterPredicate::RejectsAll(Expr::Literal(ScalarValue::Boolean(None), None)),
            true,
        );
    }

    simplified(
        FilterPredicate::Expression(Expr::InList(InList::new(expr, list, negated))),
        true,
    )
}

fn simplify_filter_and(left: Expr, right: Expr) -> SimplifiedFilterPredicate {
    let left = simplify_filter_predicate(left);
    let right = simplify_filter_predicate(right);
    let children_transformed = left.transformed || right.transformed;

    match (left.predicate, right.predicate) {
        (FilterPredicate::RejectsAll(left), FilterPredicate::RejectsAll(_)) => {
            simplified(FilterPredicate::RejectsAll(left), true)
        }
        (FilterPredicate::RejectsAll(rejects), FilterPredicate::AcceptsAll)
        | (FilterPredicate::AcceptsAll, FilterPredicate::RejectsAll(rejects)) => {
            simplified(FilterPredicate::RejectsAll(rejects), true)
        }
        (FilterPredicate::RejectsAll(rejects), FilterPredicate::Expression(expr)) => {
            if is_safe_to_discard(&expr) {
                simplified(FilterPredicate::RejectsAll(rejects), true)
            } else {
                simplified(
                    FilterPredicate::Expression(rejects.and(expr)),
                    children_transformed,
                )
            }
        }
        (FilterPredicate::Expression(expr), FilterPredicate::RejectsAll(rejects)) => {
            if is_safe_to_discard(&expr) {
                simplified(FilterPredicate::RejectsAll(rejects), true)
            } else {
                simplified(
                    FilterPredicate::Expression(expr.and(rejects)),
                    children_transformed,
                )
            }
        }
        (FilterPredicate::AcceptsAll, predicate)
        | (predicate, FilterPredicate::AcceptsAll) => simplified(predicate, true),
        (FilterPredicate::Expression(left), FilterPredicate::Expression(right)) => {
            simplified(
                FilterPredicate::Expression(left.and(right)),
                children_transformed,
            )
        }
    }
}

fn simplify_filter_or(left: Expr, right: Expr) -> SimplifiedFilterPredicate {
    let left = simplify_filter_predicate(left);
    let right = simplify_filter_predicate(right);
    let children_transformed = left.transformed || right.transformed;

    match (left.predicate, right.predicate) {
        (FilterPredicate::AcceptsAll, _) | (_, FilterPredicate::AcceptsAll) => {
            simplified(FilterPredicate::AcceptsAll, true)
        }
        (FilterPredicate::RejectsAll(_), predicate)
        | (predicate, FilterPredicate::RejectsAll(_)) => simplified(predicate, true),
        (FilterPredicate::Expression(left), FilterPredicate::Expression(right)) => {
            simplified(
                FilterPredicate::Expression(left.or(right)),
                children_transformed,
            )
        }
    }
}

/// Returns true when skipping evaluation of `expr` cannot hide a runtime error
/// or an observable side effect.
///
/// Logical expressions do not currently expose general infallibility metadata,
/// so this is deliberately a small allowlist. In particular, arithmetic,
/// casts, and function calls are retained even when another branch proves that
/// a filter cannot accept a row.
fn is_safe_to_discard(expr: &Expr) -> bool {
    match expr {
        Expr::Column(_) | Expr::Literal(_, _) => true,
        Expr::BinaryExpr(binary)
            if matches!(
                binary.op,
                Operator::Eq
                    | Operator::NotEq
                    | Operator::Lt
                    | Operator::LtEq
                    | Operator::Gt
                    | Operator::GtEq
                    | Operator::And
                    | Operator::Or
                    | Operator::IsDistinctFrom
                    | Operator::IsNotDistinctFrom
            ) =>
        {
            is_safe_to_discard(&binary.left) && is_safe_to_discard(&binary.right)
        }
        Expr::Not(expr)
        | Expr::IsNotNull(expr)
        | Expr::IsNull(expr)
        | Expr::IsTrue(expr)
        | Expr::IsFalse(expr)
        | Expr::IsUnknown(expr)
        | Expr::IsNotTrue(expr)
        | Expr::IsNotFalse(expr)
        | Expr::IsNotUnknown(expr) => is_safe_to_discard(expr),
        _ => false,
    }
}

fn simplified(
    predicate: FilterPredicate,
    transformed: bool,
) -> SimplifiedFilterPredicate {
    SimplifiedFilterPredicate {
        predicate,
        transformed,
    }
}

#[cfg(test)]
mod tests {
    use std::ops::Not;
    use std::sync::Arc;

    use crate::OptimizerContext;
    use crate::assert_optimized_plan_eq_snapshot;
    use datafusion_common::{Result, ScalarValue};
    use datafusion_expr::{Expr, col, lit, logical_plan::builder::LogicalPlanBuilder};

    use crate::eliminate_filter::{
        EliminateFilter, FilterPredicate, simplify_filter_predicate,
    };
    use crate::test::*;
    use datafusion_expr::test::function_stub::sum;

    macro_rules! assert_optimized_plan_equal {
        (
            $plan:expr,
            @ $expected:literal $(,)?
        ) => {{
            let optimizer_ctx = OptimizerContext::new().with_max_passes(1);
            let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> = vec![Arc::new(EliminateFilter::new())];
            assert_optimized_plan_eq_snapshot!(
                optimizer_ctx,
                rules,
                $plan,
                @ $expected,
            )
        }};
    }

    #[test]
    fn filter_false() -> Result<()> {
        let filter_expr = lit(false);

        let table_scan = test_table_scan().unwrap();
        let plan = LogicalPlanBuilder::from(table_scan)
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .filter(filter_expr)?
            .build()?;

        // No aggregate / scan / limit
        assert_optimized_plan_equal!(plan, @"EmptyRelation: rows=0")
    }

    #[test]
    fn filter_null() -> Result<()> {
        let filter_expr = Expr::Literal(ScalarValue::Boolean(None), None);

        let table_scan = test_table_scan().unwrap();
        let plan = LogicalPlanBuilder::from(table_scan)
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .filter(filter_expr)?
            .build()?;

        // No aggregate / scan / limit
        assert_optimized_plan_equal!(plan, @"EmptyRelation: rows=0")
    }

    #[test]
    fn filter_and_null() -> Result<()> {
        let null = Expr::Literal(ScalarValue::Boolean(None), None);

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(col("b").and(null.clone()))?
            .build()?;
        assert_optimized_plan_equal!(plan, @"EmptyRelation: rows=0")?;

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(null.and(col("b")))?
            .build()?;
        assert_optimized_plan_equal!(plan, @"EmptyRelation: rows=0")?;

        Ok(())
    }

    #[test]
    fn filter_and_null_preserves_fallible_expression() -> Result<()> {
        let null = Expr::Literal(ScalarValue::Boolean(None), None);
        let fallible = (col("a") / lit(0u32)).gt(lit(0u32));

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(fallible.clone().and(null.clone()))?
            .build()?;
        assert_optimized_plan_equal!(
            plan,
            @r"
            Filter: test.a / UInt32(0) > UInt32(0) AND Boolean(NULL)
              TableScan: test
            "
        )?;

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(null.and(fallible))?
            .build()?;
        assert_optimized_plan_equal!(
            plan,
            @r"
            Filter: Boolean(NULL) AND test.a / UInt32(0) > UInt32(0)
              TableScan: test
            "
        )?;

        Ok(())
    }

    #[test]
    fn filter_or_null() -> Result<()> {
        let null = Expr::Literal(ScalarValue::Boolean(None), None);

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(col("b").or(null.clone()))?
            .build()?;
        assert_optimized_plan_equal!(
            plan,
            @r"
            Filter: test.b
              TableScan: test
            "
        )?;

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(null.or(col("b")))?
            .build()?;
        assert_optimized_plan_equal!(
            plan,
            @r"
            Filter: test.b
              TableScan: test
            "
        )?;

        Ok(())
    }

    #[test]
    fn filter_nested_null_predicates() -> Result<()> {
        let null = Expr::Literal(ScalarValue::Boolean(None), None);

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(col("b").and(col("b").or(null.clone())))?
            .build()?;
        assert_optimized_plan_equal!(
            plan,
            @r"
            Filter: test.b AND test.b
              TableScan: test
            "
        )?;

        let plan = LogicalPlanBuilder::from(test_table_scan()?)
            .filter(col("b").or(col("c").and(null)))?
            .build()?;
        assert_optimized_plan_equal!(
            plan,
            @r"
            Filter: test.b
              TableScan: test
            "
        )?;

        Ok(())
    }

    #[test]
    fn filter_null_simplification_does_not_cross_not() {
        let null = Expr::Literal(ScalarValue::Boolean(None), None);
        let predicate = col("b").or(null).not();
        let result = simplify_filter_predicate(predicate.clone());

        assert!(!result.transformed);
        let FilterPredicate::Expression(actual) = result.predicate else {
            panic!("expected the predicate to remain an expression")
        };
        assert_eq!(actual, predicate);
    }

    #[test]
    fn filter_false_nested() -> Result<()> {
        let filter_expr = lit(false);

        let table_scan = test_table_scan()?;
        let plan1 = LogicalPlanBuilder::from(table_scan.clone())
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .build()?;
        let plan = LogicalPlanBuilder::from(table_scan)
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .filter(filter_expr)?
            .union(plan1)?
            .build()?;

        // Left side is removed
        assert_optimized_plan_equal!(plan, @r"
        Union
          EmptyRelation: rows=0
          Aggregate: groupBy=[[test.a]], aggr=[[sum(test.b)]]
            TableScan: test
        ")
    }

    #[test]
    fn filter_true() -> Result<()> {
        let filter_expr = lit(true);

        let table_scan = test_table_scan()?;
        let plan = LogicalPlanBuilder::from(table_scan)
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .filter(filter_expr)?
            .build()?;

        assert_optimized_plan_equal!(plan, @r"
        Aggregate: groupBy=[[test.a]], aggr=[[sum(test.b)]]
          TableScan: test
        ")
    }

    #[test]
    fn filter_true_nested() -> Result<()> {
        let filter_expr = lit(true);

        let table_scan = test_table_scan()?;
        let plan1 = LogicalPlanBuilder::from(table_scan.clone())
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .build()?;
        let plan = LogicalPlanBuilder::from(table_scan)
            .aggregate(vec![col("a")], vec![sum(col("b"))])?
            .filter(filter_expr)?
            .union(plan1)?
            .build()?;

        // Filter is removed
        assert_optimized_plan_equal!(plan, @r"
        Union
          Aggregate: groupBy=[[test.a]], aggr=[[sum(test.b)]]
            TableScan: test
          Aggregate: groupBy=[[test.a]], aggr=[[sum(test.b)]]
            TableScan: test
        ")
    }

    #[test]
    fn filter_from_subquery() -> Result<()> {
        // SELECT a FROM (SELECT a FROM test WHERE FALSE) WHERE TRUE

        let false_filter = lit(false);
        let table_scan = test_table_scan()?;
        let plan1 = LogicalPlanBuilder::from(table_scan)
            .project(vec![col("a")])?
            .filter(false_filter)?
            .build()?;

        let true_filter = lit(true);
        let plan = LogicalPlanBuilder::from(plan1)
            .project(vec![col("a")])?
            .filter(true_filter)?
            .build()?;

        // Filter is removed
        assert_optimized_plan_equal!(plan, @r"
        Projection: test.a
          EmptyRelation: rows=0
        ")
    }
}