1use super::{CompositeAttribute, CompositeTypeDescriptor};
10use uqa_core::{
11 memory::{Produced, ProductionControl, ProductionVec},
12 ValueRetentionError,
13};
14
15impl CompositeTypeDescriptor {
16 pub fn expand_record_with_control(
18 &self,
19 value: &uqa_core::Value,
20 control: &ProductionControl<'_>,
21 ) -> Result<Produced<uqa_core::Value>, crate::SQLError> {
22 use crate::catalog::type_metadata::{
23 pg_type_align, pg_type_len, pg_type_modifier, pg_type_oid,
24 };
25 use uqa_core::{RecordDatumField, Value};
26 let Value::Datum(datum) = value else {
27 return Err(crate::SQLError::Internal(
28 "expanded record has no physical tuple".into(),
29 ));
30 };
31 if datum.record_fields().is_some() {
32 return Ok(control.copy_value(value)?);
33 }
34 let mut removed = ProductionVec::new(*control);
35 removed.extend_from_slice(&self.dropped)?;
36 removed
37 .as_mut_slice()
38 .sort_unstable_by_key(|attribute| attribute.number);
39 let mut dropped = removed.iter().peekable();
40 let mut live = self.attributes.iter().peekable();
41 let mut fields = ProductionVec::new(*control);
42 for number in 1..=self.physical_width() {
43 control.check()?;
44 let field = if let Some(attribute) =
45 live.peek().filter(|attribute| attribute.number == number)
46 {
47 let (name, memory) = control.copy_text(&attribute.name)?.into_parts();
48 let field = control.finish(
49 RecordDatumField {
50 name,
51 type_oid: pg_type_oid(&attribute.ty) as u32,
52 type_modifier: pg_type_modifier(&attribute.ty) as i32,
53 collation_oid: attribute.collation_oid,
54 length: pg_type_len(&attribute.ty),
55 alignment: pg_type_align(&attribute.ty).as_bytes()[0],
56 dropped: false,
57 },
58 memory,
59 )?;
60 live.next();
61 field
62 } else if let Some(attribute) = dropped
63 .peek()
64 .filter(|attribute| attribute.number == number)
65 {
66 let field = control.finish(
67 RecordDatumField {
68 name: String::new(),
69 type_oid: 0,
70 type_modifier: -1,
71 collation_oid: None,
72 length: attribute.length,
73 alignment: attribute.alignment,
74 dropped: true,
75 },
76 control.empty_reservation(),
77 )?;
78 dropped.next();
79 field
80 } else {
81 return Err(crate::SQLError::Internal(
82 "expanded record descriptor is missing a physical attribute".into(),
83 ));
84 };
85 fields.push_produced(field)?;
86 }
87 let (datum, memory) = datum
88 .with_record_fields_with_control(fields.finish()?, control)?
89 .into_parts();
90 Ok(control.finish(Value::Datum(datum), memory)?)
91 }
92
93 pub fn expanded_with_control(
96 datum: &uqa_core::DatumValue,
97 catalog: Option<&dyn crate::expr::SQLValueCatalog>,
98 control: &ProductionControl<'_>,
99 ) -> Result<Option<Produced<Self>>, crate::SQLError> {
100 let Some(fields) = datum.record_fields() else {
101 return Ok(None);
102 };
103 let mut attributes = ProductionVec::new(*control);
104 let mut dropped = ProductionVec::new(*control);
105 for (index, field) in fields.iter().enumerate() {
106 control.check()?;
107 let number = i16::try_from(index + 1)
108 .map_err(|_| crate::SQLError::Internal("invalid expanded record width".into()))?;
109 if field.dropped {
110 dropped.push_copy(super::DroppedCompositeAttribute {
111 number,
112 length: field.length,
113 alignment: field.alignment,
114 })?;
115 } else {
116 let (ty, type_memory) = crate::expr::value_catalog::field_type_with_control(
117 field.type_oid,
118 field.type_modifier,
119 catalog,
120 control,
121 )?
122 .into_parts();
123 let (name, name_memory) = control.copy_text(&field.name)?.into_parts();
124 attributes.push_produced(control.finish(
125 CompositeAttribute {
126 name,
127 ty,
128 number,
129 collation_oid: field.collation_oid,
130 },
131 control.combine(name_memory, type_memory),
132 )?)?;
133 }
134 }
135 let (attributes, memory) = attributes.finish()?.into_parts();
136 let (dropped, dropped_memory) = dropped.finish()?.into_parts();
137 Ok(Some(control.finish(
138 Self {
139 type_oid: datum.type_oid(),
140 relation_oid: 0,
141 attributes,
142 dropped,
143 },
144 control.combine(memory, dropped_memory),
145 )?))
146 }
147
148 pub fn clone_with_control(
149 &self,
150 control: &ProductionControl<'_>,
151 ) -> Result<Produced<Self>, ValueRetentionError> {
152 let mut attributes = ProductionVec::new(*control);
153 attributes.reserve(self.attributes.len())?;
154 for attribute in &self.attributes {
155 let (name, name_memory) = control.copy_text(&attribute.name)?.into_parts();
156 let (ty, type_memory) = attribute.ty.clone_with_control(control)?.into_parts();
157 attributes.push_produced(control.finish(
158 CompositeAttribute {
159 name,
160 ty,
161 number: attribute.number,
162 collation_oid: attribute.collation_oid,
163 },
164 control.combine(name_memory, type_memory),
165 )?)?;
166 }
167 let mut dropped = ProductionVec::new(*control);
168 dropped.extend_from_slice(&self.dropped)?;
169 let (attributes, memory) = attributes.finish()?.into_parts();
170 let (dropped, dropped_memory) = dropped.finish()?.into_parts();
171 control.finish(
172 Self {
173 attributes,
174 dropped,
175 type_oid: self.type_oid,
176 relation_oid: self.relation_oid,
177 },
178 control.combine(memory, dropped_memory),
179 )
180 }
181}