1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//
//! PL/pgSQL datum and `INTO` assignment semantics.
use super::{coerce_routine_value, Interpreter, IntoTarget, PLpgSQLDatum, SQLError, Value};
impl Interpreter<'_> {
pub(super) fn datum_name(&self, idx: usize) -> Result<String, SQLError> {
let datum = self.datums.get(idx).ok_or_else(|| {
SQLError::Internal(format!("PL/pgSQL references missing datum {idx}"))
})?;
datum.name().map(ToString::to_string).ok_or_else(|| {
SQLError::Internal(format!(
"PL/pgSQL datum {idx} does not have a bindable name"
))
})
}
/// Store into a datum applying CONSTANT / type / NOT NULL rules.
pub(super) fn assign_datum(&mut self, idx: usize, value: Value) -> Result<(), SQLError> {
match &self.datums[idx] {
PLpgSQLDatum::Var(var) => {
if var.constant {
return Err(SQLError::Routine {
sqlstate: "22005".into(),
message: format!("variable \"{}\" is declared CONSTANT", var.name),
});
}
let value = coerce_routine_value(self.engine, &value, &var.type_name)?;
if var.not_null && matches!(value, Value::Null) {
return Err(SQLError::Routine {
sqlstate: "22004".into(),
message: format!(
"null value cannot be assigned to variable \"{}\" declared NOT NULL",
var.name
),
});
}
self.values[idx] = value;
Ok(())
}
PLpgSQLDatum::Rec { .. } => match value {
Value::Record(_) | Value::Null => {
self.values[idx] = value;
Ok(())
}
Value::Row(values) => {
self.values[idx] = Value::Record(
values
.into_iter()
.enumerate()
.map(|(index, value)| (format!("f{}", index + 1), value))
.collect(),
);
Ok(())
}
_ => Err(SQLError::Routine {
sqlstate: "42804".into(),
message: "cannot assign non-composite value to a record variable".into(),
}),
},
PLpgSQLDatum::RecField { field, parent } => {
let parent_name = self.datum_name(*parent)?;
match &mut self.values[*parent] {
Value::Record(fields) => {
let field_value = fields
.iter_mut()
.find(|(name, _)| name == field)
.map(|(_, value)| value)
.ok_or_else(|| SQLError::Routine {
sqlstate: "42703".into(),
message: format!(
"record \"{parent_name}\" has no field \"{field}\""
),
})?;
*field_value = value;
Ok(())
}
_ => Err(SQLError::Routine {
sqlstate: "55000".into(),
message: format!("record \"{parent_name}\" is not assigned yet"),
}),
}
}
PLpgSQLDatum::Row { .. } => Err(SQLError::Internal(
"direct assignment to a row datum".into(),
)),
}
}
/// Assign a query result row (or NULLs) to an INTO target.
pub(super) fn assign_into(
&mut self,
target: &IntoTarget,
columns: &[String],
values: Option<&[Value]>,
) -> Result<(), SQLError> {
match target {
IntoTarget::Rec(dno) => {
let value = match values {
Some(values) => Value::Record(
columns
.iter()
.enumerate()
.map(|(index, column)| {
(
column.clone(),
values.get(index).cloned().unwrap_or(Value::Null),
)
})
.collect(),
),
None => Value::Null,
};
self.values[*dno] = value;
Ok(())
}
IntoTarget::Row(fields) => {
for (idx, field) in fields.iter().enumerate() {
let value = values
.and_then(|values| values.get(idx))
.cloned()
.unwrap_or(Value::Null);
self.assign_datum(field.varno, value)?;
}
Ok(())
}
}
}
}