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 } => {
81 if columns.is_empty() {
82 *columns = target
83 .output
84 .iter()
85 .take(values.len())
86 .map(|column| column.current_name.clone().into())
87 .collect();
88 }
89 for column in columns {
90 if self.mode.is_rename() && !dropped_targets.contains(&column.column) {
91 self.bind_target_name(&mut column.column, &target);
92 }
93 for expression in column.expressions_mut() {
94 self.bind_expr(expression, &scopes, &context)?;
95 }
96 }
97 for expression in values {
98 self.bind_expr(expression, &scopes, &context)?;
99 }
100 condition
101 }
102 MergeWhen::DeleteMatched { condition }
103 | MergeWhen::DeleteNotMatchedBySource { condition }
104 | MergeWhen::NothingMatched { condition }
105 | MergeWhen::NothingNotMatched { condition }
106 | MergeWhen::NothingNotMatchedBySource { condition } => condition,
107 };
108 if let Some(condition) = condition {
109 self.bind_expr(condition, &scopes, &context)?;
110 }
111 }
112 if let ColumnBindingMode::Rename { relation, from, to } = self.mode {
113 if merge.target == relation.qualified_name() && !dropped_targets.contains(from) {
114 if let Some(binding) = merge.target_column_bindings.remove(from) {
115 merge.target_column_bindings.insert(to.to_string(), binding);
116 }
117 }
118 }
119 let returning =
120 action_returning_scope(&local, &target, RuleEvent::Update, &merge.returning_aliases);
121 let mut scopes = vec![returning.clone()];
122 scopes.extend_from_slice(outer);
123 self.bind_projections(&mut merge.returning, Some(&returning), &scopes, &context)
124 }
125}