uqa_sql/semantics/
source_shape.rs1use super::from_qualifier_set;
10use crate::plan::SourcePlan;
11use crate::ScalarExpr;
12use std::collections::BTreeSet;
13
14pub 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}