Skip to main content

uqa_sql/plan/mutable/commands/
view_columns.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! View column translation replaces only the selected names; indirection and values keep their owners.
8
9use super::{item, CommandPlan, MutableCommandPlan, RewriteMemory, SQLError};
10use crate::plan::{copy::Copying, MergeWhenPlan};
11use uqa_core::memory::{BudgetedVec, Produced, ProductionControl};
12
13impl MutableCommandPlan {
14    pub fn map_insert_column(
15        mut self,
16        index: usize,
17        control: &ProductionControl<'_>,
18        mut map: impl FnMut(&str) -> Result<Produced<String>, SQLError>,
19    ) -> Result<Self, SQLError> {
20        self.assert_owner(control);
21        control.check()?;
22        let CommandPlan::Insert(plan) = &mut self.command else {
23            return Err(SQLError::Internal(
24                "INSERT column translation requires INSERT".into(),
25            ));
26        };
27        let memory = self
28            .memory
29            .as_mut()
30            .map(|memory| item(body(memory), "columns", index));
31        super::assignments::rename_target(&mut plan.columns[index], memory, control, &mut map)?;
32        Ok(self)
33    }
34
35    pub fn map_merge_insert_column(
36        mut self,
37        clause: usize,
38        index: usize,
39        control: &ProductionControl<'_>,
40        mut map: impl FnMut(&str) -> Result<Produced<String>, SQLError>,
41    ) -> Result<Self, SQLError> {
42        self.assert_owner(control);
43        control.check()?;
44        let CommandPlan::Merge(plan) = &mut self.command else {
45            return Err(SQLError::Internal(
46                "MERGE column translation requires MERGE".into(),
47            ));
48        };
49        let MergeWhenPlan::InsertNotMatched { columns, .. } = &mut plan.when_clauses[clause] else {
50            return Err(SQLError::Internal(
51                "MERGE column translation requires an INSERT action".into(),
52            ));
53        };
54        let memory = self
55            .memory
56            .as_mut()
57            .map(|memory| item(item(body(memory), "when_clauses", clause), "columns", index));
58        super::assignments::rename_target(&mut columns[index], memory, control, &mut map)?;
59        Ok(self)
60    }
61
62    pub fn set_implicit_merge_columns<'a>(
63        mut self,
64        clause: usize,
65        names: impl ExactSizeIterator<Item = &'a str>,
66        control: &ProductionControl<'_>,
67    ) -> Result<Self, SQLError> {
68        self.assert_owner(control);
69        control.check()?;
70        let CommandPlan::Merge(plan) = &mut self.command else {
71            return Err(SQLError::Internal(
72                "implicit MERGE columns require MERGE".into(),
73            ));
74        };
75        let MergeWhenPlan::InsertNotMatched { columns, .. } = &mut plan.when_clauses[clause] else {
76            return Err(SQLError::Internal(
77                "implicit MERGE columns require an INSERT action".into(),
78            ));
79        };
80        if !columns.is_empty() {
81            return Ok(self);
82        }
83        let (output, memory) = if let Some(mut copying) = Copying::for_rewrite(*control) {
84            let columns = copying.implicit_insert_columns(names)?;
85            (columns, Some(copying.into_rewrite_memory()))
86        } else {
87            (
88                names
89                    .map(|name| crate::ast::AssignmentTarget::from(name.to_owned()))
90                    .collect(),
91                None,
92            )
93        };
94        *columns = output;
95        if let Some(owner) = &mut self.memory {
96            *item(body(owner), "when_clauses", clause).field_mut("columns") = memory;
97        }
98        Ok(self)
99    }
100
101    pub fn map_conflict_columns(
102        mut self,
103        control: &ProductionControl<'_>,
104        mut map: impl FnMut(&str) -> Result<Produced<String>, SQLError>,
105    ) -> Result<Self, SQLError> {
106        self.assert_owner(control);
107        control.check()?;
108        let CommandPlan::Insert(plan) = &mut self.command else {
109            return Err(SQLError::Internal("conflict columns require INSERT".into()));
110        };
111        let Some(conflict) = &mut plan.on_conflict else {
112            return Ok(self);
113        };
114        for (index, name) in conflict.conflict_columns.iter_mut().enumerate() {
115            let (output, memory) = map(name)?.into_parts();
116            let (output, memory) = control.finish(output, memory)?.into_parts();
117            *name = output;
118            if let Some(owner) = &mut self.memory {
119                *body(owner)
120                    .field_mut("on_conflict")
121                    .as_mut()
122                    .expect("conflict")
123                    .field_mut("conflict_columns")
124                    .as_mut()
125                    .expect("conflict columns")
126                    .item_mut(index) = memory.map(|direct| RewriteMemory::Fields {
127                    fields: BudgetedVec::new(direct.budget()),
128                    direct,
129                });
130            }
131        }
132        Ok(self)
133    }
134}
135
136fn body(memory: &mut RewriteMemory) -> &mut RewriteMemory {
137    memory.field_mut("value0").as_mut().expect("mutation body")
138}