Skip to main content

uqa_sql/semantics/
source_shape.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Source qualifiers, outer-join nullability, and subquery dependencies.
8
9use super::from_qualifier_set;
10use crate::plan::SourcePlan;
11use crate::ScalarExpr;
12use std::collections::BTreeSet;
13
14/// Qualifiers whose rows can be synthesized as NULLs by an outer join cannot
15/// receive an arbitrary WHERE predicate before that join. A predicate such as
16/// `right.id IS NULL` accepts the synthesized row; pushing it into the right
17/// scan first can remove a real match, manufacture a NULL-extended row, and
18/// turn a non-result into a result. Keep predicates on these qualifiers above
19/// the outer join unless a separate rewrite has first reduced it to an inner
20/// join.
21pub fn outer_join_nullable_qualifiers(from: &SourcePlan) -> BTreeSet<String> {
22    let SourcePlan::Join {
23        left,
24        right,
25        kind,
26        alias,
27        ..
28    } = from
29    else {
30        return BTreeSet::new();
31    };
32    let left_nullable = outer_join_nullable_qualifiers(left);
33    let right_nullable = outer_join_nullable_qualifiers(right);
34    if let Some(alias) = alias {
35        let nullable = matches!(
36            kind,
37            crate::ast::JoinKind::Left | crate::ast::JoinKind::Right | crate::ast::JoinKind::Full
38        ) || !left_nullable.is_empty()
39            || !right_nullable.is_empty();
40        return if nullable {
41            BTreeSet::from([alias.clone()])
42        } else {
43            BTreeSet::new()
44        };
45    }
46    let mut nullable = left_nullable;
47    nullable.extend(right_nullable);
48    match kind {
49        crate::ast::JoinKind::Left => nullable.extend(from_qualifier_set(right)),
50        crate::ast::JoinKind::Right => nullable.extend(from_qualifier_set(left)),
51        crate::ast::JoinKind::Full => {
52            nullable.extend(from_qualifier_set(left));
53            nullable.extend(from_qualifier_set(right));
54        }
55        crate::ast::JoinKind::Inner | crate::ast::JoinKind::Cross => {}
56    }
57    nullable
58}
59
60pub fn collect_from_qualifiers(from: &SourcePlan, out: &mut Vec<String>) {
61    match from {
62        crate::plan::SourcePlan::RetainedRows(_) => {}
63        SourcePlan::Join {
64            left, right, alias, ..
65        } => {
66            if let Some(alias) = alias {
67                out.push(alias.clone());
68            } else {
69                collect_from_qualifiers(left, out);
70                collect_from_qualifiers(right, out);
71            }
72        }
73        SourcePlan::Table { .. }
74        | SourcePlan::Values { .. }
75        | SourcePlan::Function { .. }
76        | SourcePlan::FunctionGroup { .. }
77        | SourcePlan::Subquery { .. } => {
78            if let Some(qualifier) = from.visible_qualifier() {
79                out.push(qualifier.to_string());
80            }
81        }
82    }
83}
84
85pub fn collect_subquery_ids(expression: &ScalarExpr, output: &mut BTreeSet<usize>) {
86    expression.visit(&mut |node| match node {
87        ScalarExpr::ScalarSubquery(id)
88        | ScalarExpr::Exists { subquery: id, .. }
89        | ScalarExpr::InSubquery { subquery: id, .. } => {
90            output.insert(*id);
91        }
92        _ => {}
93    });
94}
95
96pub fn collect_frame_bound_subquery_ids(
97    bound: &crate::ScalarFrameBound,
98    output: &mut BTreeSet<usize>,
99) {
100    match bound {
101        crate::ScalarFrameBound::Preceding(expression)
102        | crate::ScalarFrameBound::Following(expression) => {
103            collect_subquery_ids(expression, output);
104        }
105        crate::ScalarFrameBound::UnboundedPreceding
106        | crate::ScalarFrameBound::UnboundedFollowing
107        | crate::ScalarFrameBound::CurrentRow => {}
108    }
109}