uqa_sql/binding/stored_columns/
statements.rs1use crate::ast::{MergeStmt, MergeWhen, RuleEvent};
10
11use super::{
12 action_returning_scope, ColumnBindingContext, ColumnBindingMode, ColumnScope, RelationIdentity,
13 RuleColumnDependency, SQLError, Statement, StoredColumnBinder, StoredColumnBindingContext,
14};
15
16pub fn rewrite_stored_statement_column(
17 catalog: StoredColumnBindingContext<'_>,
18 statement: &mut Statement,
19 relation: &RelationIdentity,
20 from: &str,
21 to: &str,
22) -> Result<bool, SQLError> {
23 let mode = ColumnBindingMode::Rename { relation, from, to };
24 let mut binder = StoredColumnBinder::new(catalog, mode);
25 binder.bind_statement(statement, &[], &ColumnBindingContext::default())?;
26 let shape_changed = binder.alias_shape_changed();
27 Ok(binder.finish().contains(&RuleColumnDependency {
28 relation: relation.clone(),
29 column: from.to_string(),
30 }) || shape_changed)
31}
32
33impl StoredColumnBinder<'_> {
34 pub(super) fn bind_merge(
35 &mut self,
36 merge: &mut MergeStmt,
37 outer: &[ColumnScope],
38 context: &ColumnBindingContext,
39 ) -> Result<(), SQLError> {
40 let context = self.bind_ctes(&mut merge.with, outer, context)?;
41 let target = self.table_scope(
42 &merge.target,
43 &merge.target_qualifier,
44 merge.target_alias.as_deref(),
45 &[],
46 None,
47 &ColumnBindingContext::default(),
48 )?;
49 let (local, scopes) =
50 self.bind_dml_source(Some(&mut merge.source), &target, outer, &context)?;
51 let dropped_targets =
52 crate::routines::merge_columns::dropped_stored_merge_targets(self.catalog.merge, merge);
53 self.bind_expr(&mut merge.join_condition, &scopes, &context)?;
54 for action in &mut merge.when_clauses {
55 let condition = match action {
56 MergeWhen::UpdateMatched {
57 condition,
58 assignments,
59 }
60 | MergeWhen::UpdateNotMatchedBySource {
61 condition,
62 assignments,
63 } => {
64 for (column, expression) in assignments {
65 if self.mode.is_rename() && !dropped_targets.contains(&column.column) {
66 self.bind_target_name(&mut column.column, &target);
67 }
68 for expression in column.expressions_mut() {
69 self.bind_expr(expression, &scopes, &context)?;
70 }
71 self.bind_expr(expression, &scopes, &context)?;
72 }
73 condition
74 }
75 MergeWhen::InsertNotMatched {
76 condition,
77 columns,
78 values,
79 } => {
80 if columns.is_empty() {
81 *columns = target
82 .output
83 .iter()
84 .take(values.len())
85 .map(|column| column.current_name.clone().into())
86 .collect();
87 }
88 for column in columns {
89 if self.mode.is_rename() && !dropped_targets.contains(&column.column) {
90 self.bind_target_name(&mut column.column, &target);
91 }
92 for expression in column.expressions_mut() {
93 self.bind_expr(expression, &scopes, &context)?;
94 }
95 }
96 for expression in values {
97 self.bind_expr(expression, &scopes, &context)?;
98 }
99 condition
100 }
101 MergeWhen::DeleteMatched { condition }
102 | MergeWhen::DeleteNotMatchedBySource { condition }
103 | MergeWhen::NothingMatched { condition }
104 | MergeWhen::NothingNotMatched { condition }
105 | MergeWhen::NothingNotMatchedBySource { condition } => condition,
106 };
107 if let Some(condition) = condition {
108 self.bind_expr(condition, &scopes, &context)?;
109 }
110 }
111 if let ColumnBindingMode::Rename { relation, from, to } = self.mode {
112 if merge.target == relation.qualified_name() && !dropped_targets.contains(from) {
113 if let Some(binding) = merge.target_column_bindings.remove(from) {
114 merge.target_column_bindings.insert(to.to_string(), binding);
115 }
116 }
117 }
118 let returning =
119 action_returning_scope(&local, &target, RuleEvent::Update, &merge.returning_aliases);
120 let mut scopes = vec![returning.clone()];
121 scopes.extend_from_slice(outer);
122 self.bind_projections(&mut merge.returning, Some(&returning), &scopes, &context)
123 }
124}