uqa-planner 0.3.8

Cost model, cardinality, DPccp join enumeration, optimizer rewrites
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Qualifier and CTE output-filter pushdown and specialization.

use std::collections::{BTreeMap, BTreeSet};
use uqa_sql::semantics::volatility::{
    expr_contains_volatile_function, query_contains_volatile_function,
};
use uqa_sql::semantics::{
    expr_contains_function, expr_contains_subquery, expr_has_unqualified_column, expr_qualifiers,
    flatten_and_filter_parts, from_qualifier_set, projection_columns, qualify_unqualified_columns,
    TABLE_OID_COLUMN, XMIN_COLUMN,
};
use uqa_sql::{
    plan::{
        source_projection::QualifierFilters, ComputePlan, ProjectionPlan, QueryBlockPlan,
        QueryPlan, RelationalPlan, SourcePlan, UnifiedPlan,
    },
    SQLError, ScalarExpr,
};
pub mod context;
use context::{FilterPushdownContext, FilterPushdownScope};
mod source_columns;

use source_columns::{source_column_owners, ColumnOwners};

pub fn qualifier_filters_for_stmt(
    context: FilterPushdownContext<'_>,
    stmt: &QueryBlockPlan,
    from: &SourcePlan,
    scope: FilterPushdownScope<'_>,
) -> Result<Option<QualifierFilters>, SQLError> {
    let Some(filter) = stmt.r#where.as_ref() else {
        return Ok(None);
    };
    // Pushdown is decided per conjunct, not for the WHERE clause as a whole.
    // `qualifier_filter_for_part` already refuses any part containing a
    // subquery, and `final_filter_after_qualifier_pushdown` recomputes the
    // residual with that same rule, so the two stay consistent.
    //
    // Bailing out here whenever the clause mentioned a subquery anywhere also
    // abandoned conjuncts that push safely. A retrieval predicate then stayed
    // in the residual scalar filter, where registered retrieval functions
    // cannot be evaluated at all, so `text_match(...) AND EXISTS (...)` failed
    // with "scalar evaluation of `text_match` is not supported".
    let from_quals = from_qualifier_set(from);
    if from_quals.is_empty() {
        return Ok(None);
    }
    let single_qualifier = (from_quals.len() == 1)
        .then(|| from_quals.iter().next().cloned())
        .flatten();
    let column_owners = source_column_owners(scope, from)?;
    let nullable_qualifiers = outer_join_nullable_qualifiers(from);
    let mut filters = QualifierFilters::new();
    for part in flatten_and_filter_parts(filter) {
        if let Some((qualifier, filter)) = qualifier_filter_for_part(
            context,
            part,
            &from_quals,
            single_qualifier.as_deref(),
            &column_owners,
            &stmt.subqueries,
        ) {
            if !nullable_qualifiers.contains(&qualifier) {
                filters.entry(qualifier).or_default().push(filter);
            }
        } else if qualifier_filter_elision_safe(from) {
            for (qualifier, filter) in derived_disjunctive_qualifier_filters(
                context,
                part,
                &from_quals,
                single_qualifier.as_deref(),
                &column_owners,
                &stmt.subqueries,
            ) {
                if !nullable_qualifiers.contains(&qualifier) {
                    filters.entry(qualifier).or_default().push(filter);
                }
            }
        }
    }
    Ok((!filters.is_empty()).then_some(filters))
}

pub use uqa_sql::semantics::outer_join_nullable_qualifiers;

/// Project a multi-relation disjunction onto each relation as a necessary
/// predicate. For `(A1 AND B1) OR (A2 AND B2)`, `A1 OR A2` is safe to apply to
/// A before the join, and likewise for B. The original disjunction remains as
/// a residual filter, so this never turns the necessary predicate into a
/// sufficient one.
fn derived_disjunctive_qualifier_filters(
    context: FilterPushdownContext<'_>,
    part: &ScalarExpr,
    from_quals: &BTreeSet<String>,
    single_qualifier: Option<&str>,
    column_owners: &ColumnOwners,
    subqueries: &[QueryPlan],
) -> Vec<(String, ScalarExpr)> {
    let ScalarExpr::Or(disjuncts) = part else {
        return Vec::new();
    };
    if disjuncts.len() < 2
        || expr_contains_subquery(part)
        || expr_contains_volatile_function(context.volatility, part)
    {
        return Vec::new();
    }

    let mut derived = Vec::new();
    for qualifier in from_quals {
        let mut projected_disjuncts = Vec::with_capacity(disjuncts.len());
        let mut complete = true;
        for disjunct in disjuncts {
            let local = flatten_and_filter_parts(disjunct)
                .into_iter()
                .filter_map(|conjunct| {
                    let (owner, predicate) = qualifier_filter_for_part(
                        context,
                        conjunct,
                        from_quals,
                        single_qualifier,
                        column_owners,
                        subqueries,
                    )?;
                    (owner == *qualifier).then_some(predicate)
                })
                .collect();
            let Some(projected) = combine_filter_parts(local) else {
                complete = false;
                break;
            };
            projected_disjuncts.push(projected);
        }
        if complete {
            let predicate = match projected_disjuncts.len() {
                0 => continue,
                1 => projected_disjuncts.pop().expect("one projected disjunct"),
                _ => ScalarExpr::Or(projected_disjuncts),
            };
            derived.push((qualifier.clone(), predicate));
        }
    }
    derived
}

fn qualifier_filter_for_part(
    context: FilterPushdownContext<'_>,
    part: &ScalarExpr,
    from_quals: &BTreeSet<String>,
    single_qualifier: Option<&str>,
    column_owners: &ColumnOwners,
    subqueries: &[QueryPlan],
) -> Option<(String, ScalarExpr)> {
    if expression_contains_internal_column(part) {
        return None;
    }
    let contains_subquery = expr_contains_subquery(part);
    let unsafe_subquery = contains_subquery
        && (!subqueries_are_uncorrelated_and_stable(context, part, subqueries)
            || outer_expression_contains_volatile_function(context, part));
    let unsafe_scalar = !contains_subquery
        && expr_contains_volatile_function(context.volatility, part)
        && !crate::optimizer::contains_retrieval(part);
    if unsafe_subquery || unsafe_scalar {
        return None;
    }
    let qualifiers = expr_qualifiers(part);
    let has_unqualified = expr_has_unqualified_column(part);
    if qualifiers.len() == 1 && (!has_unqualified || from_quals.len() == 1) {
        let qualifier = qualifiers.iter().next()?;
        if from_quals.contains(qualifier) {
            return Some((qualifier.clone(), part.clone()));
        }
    }
    if qualifiers.is_empty() && (has_unqualified || crate::optimizer::contains_retrieval(part)) {
        if let Some(qualifier) = unique_unqualified_column_owner(part, column_owners) {
            if from_quals.contains(qualifier) {
                return Some((
                    qualifier.to_string(),
                    qualify_unqualified_columns(part, qualifier),
                ));
            }
        }
        if let Some(qualifier) = single_qualifier {
            return Some((
                qualifier.to_string(),
                qualify_unqualified_columns(part, qualifier),
            ));
        }
    }
    None
}

fn expression_contains_internal_column(expression: &ScalarExpr) -> bool {
    let mut found = false;
    expression.visit(&mut |node| {
        found |= matches!(node, ScalarExpr::InternalColumn(_));
    });
    found
}

pub fn final_filter_after_qualifier_pushdown(
    context: FilterPushdownContext<'_>,
    stmt: &QueryBlockPlan,
    from: &SourcePlan,
    filters: Option<&QualifierFilters>,
    scope: FilterPushdownScope<'_>,
) -> Result<Option<ScalarExpr>, SQLError> {
    let Some(filter) = stmt.r#where.as_ref() else {
        return Ok(None);
    };
    if !qualifier_filter_elision_safe(from) {
        return Ok(Some(filter.clone()));
    }
    let from_quals = from_qualifier_set(from);
    let single_qualifier = (from_quals.len() == 1)
        .then(|| from_quals.iter().next().cloned())
        .flatten();
    let column_owners = source_column_owners(scope, from)?;
    let mut guaranteed = Vec::new();
    collect_guaranteed_join_filters(from, &mut guaranteed);
    let residual: Vec<ScalarExpr> = flatten_and_filter_parts(filter)
        .into_iter()
        .filter(|part| {
            let pushed = filters.is_some()
                && qualifier_filter_for_part(
                    context,
                    part,
                    &from_quals,
                    single_qualifier.as_deref(),
                    &column_owners,
                    &stmt.subqueries,
                )
                .is_some();
            let guaranteed_by_join = !expr_contains_volatile_function(context.volatility, part)
                && guaranteed.contains(part);
            !pushed && !guaranteed_by_join
        })
        .cloned()
        .collect();
    Ok(combine_filter_parts(residual))
}

pub fn qualifier_filter_elision_safe(from: &SourcePlan) -> bool {
    match from {
        SourcePlan::Join {
            left,
            right,
            kind,
            alias,
            ..
        } => {
            alias.is_none()
                && matches!(
                    kind,
                    uqa_sql::ast::JoinKind::Inner | uqa_sql::ast::JoinKind::Cross
                )
                && qualifier_filter_elision_safe(left)
                && qualifier_filter_elision_safe(right)
        }
        SourcePlan::Table { .. }
        | SourcePlan::Values { .. }
        | SourcePlan::Function { .. }
        | SourcePlan::FunctionGroup { .. }
        | SourcePlan::Subquery { .. } => true,
    }
}

pub fn combine_filter_parts(mut parts: Vec<ScalarExpr>) -> Option<ScalarExpr> {
    match parts.len() {
        0 => None,
        1 => parts.pop(),
        _ => Some(ScalarExpr::And(parts)),
    }
}

/// Find predicates on a directly referenced CTE output. The predicate remains
/// on the consumer and is duplicated into the CTE only when that CTE has one
/// reference in this query block. This makes the rewrite semantics-preserving
/// for shared CTE materializations.
pub fn cte_output_filters(
    context: FilterPushdownContext<'_>,
    plan: &QueryPlan,
    scope: FilterPushdownScope<'_>,
) -> Result<BTreeMap<String, (String, ScalarExpr)>, SQLError> {
    let RelationalPlan::QueryBlock(block) = &plan.root else {
        return Ok(BTreeMap::new());
    };
    let (Some(from), Some(filter)) = (block.from.as_ref(), block.r#where.as_ref()) else {
        return Ok(BTreeMap::new());
    };
    if expr_contains_subquery(filter) || expr_contains_volatile_function(context.volatility, filter)
    {
        return Ok(BTreeMap::new());
    }

    let cte_names: BTreeSet<&str> = plan.ctes.iter().map(|cte| cte.name.as_str()).collect();
    let mut references: BTreeMap<String, Vec<String>> = BTreeMap::new();
    collect_cte_source_references(from, &cte_names, &mut references);
    let qualifier_to_cte: BTreeMap<String, String> = references
        .into_iter()
        .filter_map(|(cte, qualifiers)| {
            (qualifiers.len() == 1).then(|| (qualifiers[0].clone(), cte))
        })
        .collect();
    if qualifier_to_cte.is_empty() {
        return Ok(BTreeMap::new());
    }

    let from_qualifiers = from_qualifier_set(from);
    let single_qualifier = (from_qualifiers.len() == 1)
        .then(|| from_qualifiers.iter().next().cloned())
        .flatten();
    let column_owners = source_column_owners(scope, from)?;
    let mut grouped: BTreeMap<String, (String, Vec<ScalarExpr>)> = BTreeMap::new();
    for part in flatten_and_filter_parts(filter) {
        let Some((qualifier, predicate)) = qualifier_filter_for_part(
            context,
            part,
            &from_qualifiers,
            single_qualifier.as_deref(),
            &column_owners,
            &block.subqueries,
        ) else {
            continue;
        };
        let Some(cte_name) = qualifier_to_cte.get(&qualifier) else {
            continue;
        };
        let entry = grouped
            .entry(cte_name.clone())
            .or_insert_with(|| (qualifier, Vec::new()));
        entry.1.push(predicate);
    }

    Ok(grouped
        .into_iter()
        .filter_map(|(name, (qualifier, predicates))| {
            combine_filter_parts(predicates).map(|predicate| (name, (qualifier, predicate)))
        })
        .collect())
}

mod subqueries;
pub use subqueries::collect_subquery_ids;
use subqueries::{
    outer_expression_contains_volatile_function, subqueries_are_uncorrelated_and_stable,
    unique_unqualified_column_owner,
};
fn collect_pushdown_outer_columns(expression: &ScalarExpr, output: &mut BTreeSet<String>) -> bool {
    match expression {
        ScalarExpr::Column(column) => {
            output.insert(column.clone());
            true
        }
        ScalarExpr::QualifiedColumn { column, .. } => {
            output.insert(column.clone());
            true
        }
        ScalarExpr::Literal(_)
        | ScalarExpr::TypedLiteral { .. }
        | ScalarExpr::Param(_)
        | ScalarExpr::ScalarSubquery(_)
        | ScalarExpr::Exists { .. } => true,
        ScalarExpr::InSubquery { expr, .. } => collect_pushdown_outer_columns(expr, output),
        ScalarExpr::Array(items)
        | ScalarExpr::Row(items)
        | ScalarExpr::And(items)
        | ScalarExpr::Or(items) => items
            .iter()
            .all(|item| collect_pushdown_outer_columns(item, output)),
        ScalarExpr::Func {
            args,
            order_by,
            filter,
            ..
        } => {
            args.iter()
                .all(|argument| collect_pushdown_outer_columns(argument, output))
                && order_by
                    .iter()
                    .all(|order| collect_pushdown_outer_columns(&order.expr, output))
                && filter
                    .as_deref()
                    .is_none_or(|filter| collect_pushdown_outer_columns(filter, output))
        }
        ScalarExpr::Binary { lhs, rhs, .. } => {
            collect_pushdown_outer_columns(lhs, output)
                && collect_pushdown_outer_columns(rhs, output)
        }
        ScalarExpr::Not(inner)
        | ScalarExpr::UnaryMinus(inner)
        | ScalarExpr::IsNull { expr: inner, .. }
        | ScalarExpr::Cast { expr: inner, .. } => collect_pushdown_outer_columns(inner, output),
        ScalarExpr::Between { expr, low, high } => {
            collect_pushdown_outer_columns(expr, output)
                && collect_pushdown_outer_columns(low, output)
                && collect_pushdown_outer_columns(high, output)
        }
        ScalarExpr::InList { expr, list, .. } => {
            collect_pushdown_outer_columns(expr, output)
                && list
                    .iter()
                    .all(|item| collect_pushdown_outer_columns(item, output))
        }
        ScalarExpr::Case {
            base,
            when,
            else_branch,
        } => {
            base.as_deref()
                .is_none_or(|base| collect_pushdown_outer_columns(base, output))
                && when.iter().all(|(condition, result)| {
                    collect_pushdown_outer_columns(condition, output)
                        && collect_pushdown_outer_columns(result, output)
                })
                && else_branch
                    .as_deref()
                    .is_none_or(|branch| collect_pushdown_outer_columns(branch, output))
        }
        ScalarExpr::Default
        | ScalarExpr::Star
        | ScalarExpr::Position(_)
        | ScalarExpr::InternalColumn(_)
        | ScalarExpr::QualifiedStar(_)
        | ScalarExpr::WindowCall { .. } => false,
    }
}

fn collect_guaranteed_join_filters<'a>(source: &'a SourcePlan, filters: &mut Vec<&'a ScalarExpr>) {
    if let SourcePlan::Join {
        left,
        right,
        kind: uqa_sql::ast::JoinKind::Inner | uqa_sql::ast::JoinKind::Cross,
        on,
        ..
    } = source
    {
        collect_guaranteed_join_filters(left, filters);
        collect_guaranteed_join_filters(right, filters);
        if let Some(on) = on {
            filters.extend(flatten_and_filter_parts(on));
        }
    }
}

pub fn collect_cte_source_references(
    source: &SourcePlan,
    cte_names: &BTreeSet<&str>,
    references: &mut BTreeMap<String, Vec<String>>,
) {
    match source {
        SourcePlan::Table {
            name,
            qualifier,
            alias,
            ..
        } => {
            if let Some(name) = uqa_sql::semantics::cte_reference_name(name)
                .filter(|name| cte_names.contains(name.as_str()))
            {
                references
                    .entry(name)
                    .or_default()
                    .push(alias.clone().unwrap_or_else(|| qualifier.clone()));
            }
        }
        SourcePlan::Join { left, right, .. } => {
            collect_cte_source_references(left, cte_names, references);
            collect_cte_source_references(right, cte_names, references);
        }
        SourcePlan::Values { .. }
        | SourcePlan::Function { .. }
        | SourcePlan::FunctionGroup { .. }
        | SourcePlan::Subquery { .. } => {}
    }
}

/// Specialize a physical query plan with a predicate on its output columns.
/// The caller keeps the original predicate as a residual check; this function
/// only returns a plan when pushing the predicate below the output boundary is
/// provably safe.
pub fn push_output_filter_into_query_plan(
    context: FilterPushdownContext<'_>,
    plan: &QueryPlan,
    qualifier: &str,
    filter: &ScalarExpr,
    output_columns_override: Option<&[String]>,
) -> Result<Option<QueryPlan>, SQLError> {
    if expr_contains_subquery(filter)
        || expr_contains_volatile_function(context.volatility, filter)
        || query_contains_volatile_function(context.volatility, plan)?
    {
        return Ok(None);
    }
    let Some(specialized) =
        specialize_query_output_filter(context, plan, qualifier, filter, output_columns_override)
    else {
        return Ok(None);
    };
    match (context.optimizer)(UnifiedPlan::Query(Box::new(specialized)))? {
        UnifiedPlan::Query(plan) => Ok(Some(*plan)),
        UnifiedPlan::Command(_) => Err(SQLError::Internal(
            "query optimizer changed a query into a command plan".into(),
        )),
    }
}

pub fn specialize_query_output_filter(
    context: FilterPushdownContext<'_>,
    plan: &QueryPlan,
    qualifier: &str,
    filter: &ScalarExpr,
    output_columns_override: Option<&[String]>,
) -> Option<QueryPlan> {
    let mut specialized = plan.clone();
    specialize_relational_output_filter(
        context,
        &mut specialized.root,
        qualifier,
        filter,
        output_columns_override,
    )?;
    Some(specialized)
}

pub fn specialize_relational_output_filter(
    context: FilterPushdownContext<'_>,
    root: &mut RelationalPlan,
    qualifier: &str,
    filter: &ScalarExpr,
    output_columns_override: Option<&[String]>,
) -> Option<()> {
    match root {
        RelationalPlan::QueryBlock(block) => specialize_query_block_output_filter(
            context,
            block,
            qualifier,
            filter,
            output_columns_override,
        ),
        RelationalPlan::SetOp {
            left,
            right,
            limit,
            offset,
            ..
        } => {
            if limit.is_some() || offset.is_some() {
                return None;
            }
            let output_columns = match output_columns_override {
                Some(columns) => columns.to_vec(),
                None => query_plan_output_columns(left)?,
            };
            let specialized_left = specialize_query_output_filter(
                context,
                left,
                qualifier,
                filter,
                Some(&output_columns),
            )?;
            let specialized_right = specialize_query_output_filter(
                context,
                right,
                qualifier,
                filter,
                Some(&output_columns),
            )?;
            **left = specialized_left;
            **right = specialized_right;
            Some(())
        }
        RelationalPlan::Values { .. } => None,
    }
}

pub use uqa_sql::semantics::query_plan_output_columns;

pub fn specialize_query_block_output_filter(
    context: FilterPushdownContext<'_>,
    block: &mut QueryBlockPlan,
    qualifier: &str,
    filter: &ScalarExpr,
    output_columns_override: Option<&[String]>,
) -> Option<()> {
    if block.limit.is_some()
        || block.offset.is_some()
        || matches!(block.compute, ComputePlan::Window)
        || !block.distinct_on.is_empty()
        || !block.grouping_sets.is_empty()
    {
        return None;
    }

    let output_columns = output_columns_override.map_or_else(
        || projection_columns(&block.projections),
        <[String]>::to_vec,
    );
    if output_columns.len() != block.projections.len() {
        return None;
    }
    let mut used = BTreeSet::new();
    let rewritten = rewrite_output_filter(
        filter,
        qualifier,
        &output_columns,
        &block.projections,
        &mut used,
    )?;
    if used.is_empty() {
        return None;
    }

    for index in &used {
        let expression = &block.projections[*index].expr;
        if matches!(expression, ScalarExpr::Star)
            || expression.contains_window()
            || expr_contains_subquery(expression)
            || expr_contains_volatile_function(context.volatility, expression)
        {
            return None;
        }
        if matches!(block.compute, ComputePlan::Aggregate)
            && !block.group_by.iter().any(|group| group == expression)
        {
            return None;
        }
    }
    if block.distinct
        && block
            .projections
            .iter()
            .enumerate()
            .any(|(index, projection)| {
                !used.contains(&index) && expr_contains_function(&projection.expr)
            })
    {
        return None;
    }

    block.r#where = match block.r#where.take() {
        Some(existing) => Some(ScalarExpr::And(vec![existing, rewritten])),
        None => Some(rewritten),
    };
    Some(())
}

#[expect(
    clippy::too_many_lines,
    reason = "preserves SELECT schema and row identity"
)]
pub fn rewrite_output_filter(
    expression: &ScalarExpr,
    qualifier: &str,
    output_columns: &[String],
    projections: &[ProjectionPlan],
    used: &mut BTreeSet<usize>,
) -> Option<ScalarExpr> {
    let map_column = |column: &str, used: &mut BTreeSet<usize>| {
        let index = output_columns
            .iter()
            .position(|candidate| candidate.eq_ignore_ascii_case(column))?;
        used.insert(index);
        Some(projections[index].expr.clone())
    };
    let recur = |expression: &ScalarExpr, used: &mut BTreeSet<usize>| {
        rewrite_output_filter(expression, qualifier, output_columns, projections, used)
    };

    Some(match expression {
        ScalarExpr::Column(column) => map_column(column, used)?,
        ScalarExpr::QualifiedColumn {
            qualifier: expression_qualifier,
            column,
            ..
        } if expression_qualifier.eq_ignore_ascii_case(qualifier) => map_column(column, used)?,
        ScalarExpr::Default
        | ScalarExpr::Position(_)
        | ScalarExpr::InternalColumn(_)
        | ScalarExpr::QualifiedColumn { .. }
        | ScalarExpr::Star
        | ScalarExpr::QualifiedStar(_)
        | ScalarExpr::WindowCall { .. }
        | ScalarExpr::ScalarSubquery(_)
        | ScalarExpr::Exists { .. }
        | ScalarExpr::InSubquery { .. } => return None,
        ScalarExpr::Literal(_) | ScalarExpr::TypedLiteral { .. } | ScalarExpr::Param(_) => {
            expression.clone()
        }
        ScalarExpr::Array(items) => ScalarExpr::Array(
            items
                .iter()
                .map(|item| recur(item, used))
                .collect::<Option<Vec<_>>>()?,
        ),
        ScalarExpr::Row(items) => ScalarExpr::Row(
            items
                .iter()
                .map(|item| recur(item, used))
                .collect::<Option<Vec<_>>>()?,
        ),
        ScalarExpr::Func {
            name,
            binding,
            args,
            distinct,
            order_by,
            filter,
        } => ScalarExpr::Func {
            name: name.clone(),
            binding: binding.clone(),
            args: args
                .iter()
                .map(|arg| recur(arg, used))
                .collect::<Option<Vec<_>>>()?,
            distinct: *distinct,
            order_by: order_by
                .iter()
                .map(|order| {
                    Some(uqa_sql::ScalarOrder {
                        expr: recur(&order.expr, used)?,
                        descending: order.descending,
                        nulls: order.nulls,
                    })
                })
                .collect::<Option<Vec<_>>>()?,
            filter: match filter.as_deref() {
                Some(filter) => Some(Box::new(recur(filter, used)?)),
                None => None,
            },
        },
        ScalarExpr::Binary { op, lhs, rhs } => ScalarExpr::Binary {
            op: *op,
            lhs: Box::new(recur(lhs, used)?),
            rhs: Box::new(recur(rhs, used)?),
        },
        ScalarExpr::Not(inner) => ScalarExpr::Not(Box::new(recur(inner, used)?)),
        ScalarExpr::UnaryMinus(inner) => ScalarExpr::UnaryMinus(Box::new(recur(inner, used)?)),
        ScalarExpr::And(items) => ScalarExpr::And(
            items
                .iter()
                .map(|item| recur(item, used))
                .collect::<Option<Vec<_>>>()?,
        ),
        ScalarExpr::Or(items) => ScalarExpr::Or(
            items
                .iter()
                .map(|item| recur(item, used))
                .collect::<Option<Vec<_>>>()?,
        ),
        ScalarExpr::IsNull { expr, negated } => ScalarExpr::IsNull {
            expr: Box::new(recur(expr, used)?),
            negated: *negated,
        },
        ScalarExpr::Between { expr, low, high } => ScalarExpr::Between {
            expr: Box::new(recur(expr, used)?),
            low: Box::new(recur(low, used)?),
            high: Box::new(recur(high, used)?),
        },
        ScalarExpr::InList {
            expr,
            list,
            negated,
        } => ScalarExpr::InList {
            expr: Box::new(recur(expr, used)?),
            list: list
                .iter()
                .map(|item| recur(item, used))
                .collect::<Option<Vec<_>>>()?,
            negated: *negated,
        },
        ScalarExpr::Case {
            base,
            when,
            else_branch,
        } => ScalarExpr::Case {
            base: match base.as_deref() {
                Some(base) => Some(Box::new(recur(base, used)?)),
                None => None,
            },
            when: when
                .iter()
                .map(|(condition, result)| Some((recur(condition, used)?, recur(result, used)?)))
                .collect::<Option<Vec<_>>>()?,
            else_branch: match else_branch.as_deref() {
                Some(branch) => Some(Box::new(recur(branch, used)?)),
                None => None,
            },
        },
        ScalarExpr::Cast { expr, ty } => ScalarExpr::Cast {
            expr: Box::new(recur(expr, used)?),
            ty: ty.clone(),
        },
    })
}

#[cfg(test)]
mod tests;