uqa_sql/assignment/routines/
production.rs1use super::{RoutineValueContext, RoutineValueDescription, RoutineValueKind};
10use crate::{ColumnType, SQLError};
11use uqa_core::{
12 memory::{Produced, ProductionControl, ProductionVec},
13 RecordValue, Value,
14};
15
16pub fn coerce_routine_value_from_with_control(
17 context: &dyn RoutineValueContext,
18 value: Produced<Value>,
19 type_name: &str,
20 source: Option<&ColumnType>,
21 control: &ProductionControl<'_>,
22) -> Result<Produced<Value>, SQLError> {
23 match RoutineValueDescription::resolve_with_control(context, type_name, control)? {
24 Some(description) => description.coerce_with_control(context, value, source, control),
25 None => crate::expr::cast_value_from_with_control(&value, type_name, None, control),
26 }
27}
28
29impl RoutineValueDescription {
30 pub fn resolve_with_control(
31 context: &dyn RoutineValueContext,
32 name: &str,
33 control: &ProductionControl<'_>,
34 ) -> Result<Option<Produced<Self>>, SQLError> {
35 let canonical =
36 crate::type_resolution::canonical_routine_type_name_with_control(name, control)?;
37 let kind = match canonical.as_str() {
38 "record" => RoutineValueKind::Record,
39 "trigger" => RoutineValueKind::Trigger,
40 "anyarray" => RoutineValueKind::AnyArray,
41 "refcursor" => RoutineValueKind::RefCursor,
42 "void" => RoutineValueKind::Void,
43 _ => {
44 let Some(ty) = context.catalog_column_type_with_control(name, control)? else {
45 return Ok(None);
46 };
47 let (ty, memory) = ty.into_parts();
48 return Ok(Some(
49 control.finish(Self(RoutineValueKind::Catalog(ty)), memory)?,
50 ));
51 }
52 };
53 Ok(Some(
54 control.finish(Self(kind), control.empty_reservation())?,
55 ))
56 }
57
58 pub fn coerce_with_control(
59 &self,
60 context: &dyn RoutineValueContext,
61 value: Produced<Value>,
62 source: Option<&ColumnType>,
63 control: &ProductionControl<'_>,
64 ) -> Result<Produced<Value>, SQLError> {
65 control.check()?;
66 let (value, memory) = value.into_parts();
67 let value = control.finish(value, memory)?;
68 match &self.0 {
69 RoutineValueKind::Record => match &*value {
70 Value::Record(_) | Value::Null => Ok(value),
71 Value::Datum(datum)
72 if crate::expr::composites::datum_is_composite_with_control(
73 datum, context, control,
74 )? =>
75 {
76 Ok(value)
77 }
78 Value::Row(row) if row.field_types().is_some() => Ok(value),
79 Value::Row(row) => record(row.values(), control),
80 _ => Err(type_error("cannot cast non-composite value to type record")),
81 },
82 RoutineValueKind::Trigger => match &*value {
83 Value::Record(_) | Value::Row(_) | Value::Null => Ok(value),
84 Value::Datum(datum)
85 if crate::expr::composites::datum_is_composite_with_control(
86 datum, context, control,
87 )? =>
88 {
89 Ok(value)
90 }
91 _ => Err(type_error("trigger function must return a row or NULL")),
92 },
93 RoutineValueKind::AnyArray => match &*value {
94 Value::Array(_) | Value::LegacyVector(_) | Value::List(_) | Value::Null => {
95 Ok(value)
96 }
97 _ => Err(type_error("cannot cast non-array value to type anyarray")),
98 },
99 RoutineValueKind::RefCursor => match &*value {
100 Value::Str(_) | Value::Null => Ok(value),
101 _ => Err(type_error("cannot cast value to type refcursor")),
102 },
103 RoutineValueKind::Void if matches!(*value, Value::Null) => Ok(value),
104 RoutineValueKind::Void => Err(type_error("cannot cast non-null value to type void")),
105 RoutineValueKind::Catalog(target) => {
106 crate::assignment::conversion::coerce_assignment_value_with_control(
107 context, value, target, source, control,
108 )
109 }
110 }
111 }
112}
113
114fn type_error(message: &str) -> SQLError {
115 SQLError::Routine {
116 sqlstate: "42804".into(),
117 message: message.into(),
118 }
119}
120
121fn record(values: &[Value], control: &ProductionControl<'_>) -> Result<Produced<Value>, SQLError> {
122 let mut fields = ProductionVec::new(*control);
123 fields.reserve(values.len())?;
124 for (index, value) in values.iter().enumerate() {
125 let name = control.format(format_args!("f{}", index + 1))?;
126 let value = control.copy_value(value)?;
127 let (name, name_memory) = name.into_parts();
128 let (value, value_memory) = value.into_parts();
129 fields.push_produced(
130 control.finish((name, value), control.combine(name_memory, value_memory))?,
131 )?;
132 }
133 let (record, memory) = RecordValue::with_control(fields.finish()?, None, control)?.into_parts();
134 Ok(control.finish(Value::Record(record), memory)?)
135}
136
137#[cfg(test)]
138mod tests;