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