uqa_execution/mutation/rules/
returning.rs1use crate::mutation::output::MutationOutput;
9use crate::{
10 mutation::returning::{
11 build_returning_value_row, dml_returning_result, DmlReturningShape,
12 ReturningExecutionContext, ReturningRows, ReturningValueProjectionRow,
13 },
14 OwnedPhysicalRow, PhysicalRow, RowSchema,
15};
16use uqa_core::Value;
17use uqa_sql::SQLError;
18
19mod consumer;
20pub(super) use consumer::RuleReturningConsumer;
21mod projection;
22pub use projection::{RuleOutputProjection, RuleReturningProjection};
23
24pub enum RuleReturningResult {
25 Images(RuleReturningImages),
26 Projected {
27 rows: ReturningRows,
28 affected_rows: u64,
29 },
30}
31
32impl RuleReturningResult {
33 pub fn empty() -> Self {
34 Self::Images(RuleReturningImages::empty())
35 }
36
37 pub fn project<S: Clone + 'static>(
38 self,
39 context: ReturningExecutionContext<'_, S>,
40 shape: DmlReturningShape<'_, S>,
41 ) -> Result<MutationOutput, SQLError> {
42 match self {
43 Self::Images(images) => images.project(&context, shape),
44 Self::Projected {
45 rows,
46 affected_rows,
47 } => dml_returning_result(context, shape, rows, affected_rows),
48 }
49 }
50}
51
52pub struct RuleReturningImages {
53 images: ReturningRows,
54 width: usize,
55 context_schema: Option<RowSchema>,
56 affected_rows: u64,
57}
58
59impl RuleReturningImages {
60 pub fn empty() -> Self {
61 Self {
62 images: ReturningRows::default(),
63 width: 0,
64 context_schema: None,
65 affected_rows: 0,
66 }
67 }
68
69 pub fn project<S: Clone + 'static>(
70 self,
71 context: &ReturningExecutionContext<'_, S>,
72 shape: DmlReturningShape<'_, S>,
73 ) -> Result<MutationOutput, SQLError> {
74 let mut rows = ReturningRows::new(context.relational.runtime)?;
75 for image in self.images.into_rows()? {
76 let image = image?;
77 let values = image.row.into_physical_values();
78 let source = source_context(&values, self.width * 3, self.context_schema.as_ref())?;
79 rows.push_to(
80 build_returning_value_row(
81 *context,
82 ReturningValueProjectionRow {
83 table: shape.table,
84 target_qualifier: shape.target_qualifier,
85 current: &values[..self.width],
86 old: Some(&values[self.width..self.width * 2]),
87 new: Some(&values[self.width * 2..self.width * 3]),
88 aliases: shape.aliases,
89 context: source.as_ref(),
90 },
91 shape.returning,
92 shape.params,
93 shape.ctes,
94 )?,
95 context.destination,
96 )?;
97 }
98 dml_returning_result(*context, shape, rows, self.affected_rows)
99 }
100}
101
102fn coerce_image(
103 assignment: &dyn uqa_sql::assignment::AssignmentContext,
104 columns: &[uqa_sql::ast::ColumnDef],
105 context_schema: Option<&RowSchema>,
106 schema: &RowSchema,
107 input: &OwnedPhysicalRow,
108) -> Result<OwnedPhysicalRow, SQLError> {
109 let width = columns.len();
110 let image_width = width.saturating_mul(3);
111 let expected_width = image_width + context_schema.map_or(0, RowSchema::physical_width);
112 let row = input.view();
113 let mut values = Vec::with_capacity(expected_width);
114 for offset in [0, width, width * 2] {
115 for (position, column) in columns.iter().enumerate() {
116 let value = row.value_at(offset + position).cloned().ok_or_else(|| {
117 SQLError::Internal("rewrite-rule RETURNING provider lost a positional value".into())
118 })?;
119 values.push(uqa_sql::assignment::conversion::coerce_assignment_value(
120 assignment,
121 value,
122 &column.ty,
123 input.schema.column_type(offset + position),
124 )?);
125 }
126 }
127 for position in image_width..expected_width {
128 values.push(row.value_at(position).cloned().ok_or_else(|| {
129 SQLError::Internal("rewrite-rule RETURNING provider lost its source context".into())
130 })?);
131 }
132 Ok(OwnedPhysicalRow::new(
133 schema.clone(),
134 PhysicalRow::from_values(values),
135 ))
136}
137
138fn source_context(
139 values: &[Value],
140 image_width: usize,
141 schema: Option<&RowSchema>,
142) -> Result<Option<OwnedPhysicalRow>, SQLError> {
143 schema
144 .map(|schema| {
145 let context = values
146 .get(image_width..)
147 .filter(|values| values.len() == schema.physical_width())
148 .ok_or_else(|| {
149 SQLError::Internal(
150 "rewrite-rule RETURNING provider lost its source context".into(),
151 )
152 })?;
153 Ok(OwnedPhysicalRow::new(
154 schema.clone(),
155 PhysicalRow::from_values(context.to_vec()),
156 ))
157 })
158 .transpose()
159}
160
161#[cfg(test)]
162mod tests;