Skip to main content

uqa_sql/binding/stored_columns/
statements.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Column dependencies of stored MERGE actions, including target assignments.
8
9use 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) {
66                            self.bind_target_name(column, &target);
67                        }
68                        self.bind_expr(expression, &scopes, &context)?;
69                    }
70                    condition
71                }
72                MergeWhen::InsertNotMatched {
73                    condition,
74                    columns,
75                    values,
76                } => {
77                    if columns.is_empty() {
78                        *columns = target
79                            .output
80                            .iter()
81                            .take(values.len())
82                            .map(|column| column.current_name.clone())
83                            .collect();
84                    }
85                    if self.mode.is_rename() {
86                        for column in columns {
87                            if !dropped_targets.contains(column) {
88                                self.bind_target_name(column, &target);
89                            }
90                        }
91                    }
92                    for expression in values {
93                        self.bind_expr(expression, &scopes, &context)?;
94                    }
95                    condition
96                }
97                MergeWhen::DeleteMatched { condition }
98                | MergeWhen::DeleteNotMatchedBySource { condition }
99                | MergeWhen::NothingMatched { condition }
100                | MergeWhen::NothingNotMatched { condition }
101                | MergeWhen::NothingNotMatchedBySource { condition } => condition,
102            };
103            if let Some(condition) = condition {
104                self.bind_expr(condition, &scopes, &context)?;
105            }
106        }
107        if let ColumnBindingMode::Rename { relation, from, to } = self.mode {
108            if merge.target == relation.qualified_name() && !dropped_targets.contains(from) {
109                if let Some(binding) = merge.target_column_bindings.remove(from) {
110                    merge.target_column_bindings.insert(to.to_string(), binding);
111                }
112            }
113        }
114        let returning =
115            action_returning_scope(&local, &target, RuleEvent::Update, &merge.returning_aliases);
116        let mut scopes = vec![returning.clone()];
117        scopes.extend_from_slice(outer);
118        self.bind_projections(&mut merge.returning, Some(&returning), &scopes, &context)
119    }
120}