1use super::{
10 concat_lock_origins, Arc, ResultRow, RowLockOrigin, RowSchema, SmallVec, Value,
11 INLINE_ROW_FRAGMENTS, NULL_SLOT, NULL_VALUE,
12};
13
14#[derive(Debug, Clone, PartialEq)]
15pub(super) struct RowFragment {
16 pub(super) values: Arc<Vec<Value>>,
17 pub(super) projection: Option<Arc<[usize]>>,
21}
22
23impl RowFragment {
24 fn contiguous(values: Arc<Vec<Value>>) -> Self {
25 Self {
26 values,
27 projection: None,
28 }
29 }
30
31 fn projected(values: Arc<Vec<Value>>, projection: Arc<[usize]>) -> Self {
32 debug_assert!(projection
33 .iter()
34 .all(|slot| *slot == NULL_SLOT || *slot < values.len()));
35 let identity = projection.len() == values.len()
36 && projection
37 .iter()
38 .enumerate()
39 .all(|(index, slot)| index == *slot);
40 if identity {
41 Self::contiguous(values)
42 } else {
43 Self {
44 values,
45 projection: Some(projection),
46 }
47 }
48 }
49
50 pub(super) fn len(&self) -> usize {
51 self.projection
52 .as_ref()
53 .map_or(self.values.len(), |projection| projection.len())
54 }
55
56 pub(super) fn get(&self, slot: usize) -> Option<&Value> {
57 match self.projection.as_ref() {
58 Some(projection) => match projection.get(slot).copied()? {
59 NULL_SLOT => Some(&NULL_VALUE),
60 stored => self.values.get(stored),
61 },
62 None => self.values.get(slot),
63 }
64 }
65
66 fn stored_slot(&self, slot: usize) -> Option<usize> {
67 match self.projection.as_ref() {
68 Some(projection) => projection.get(slot).copied(),
69 None => (slot < self.values.len()).then_some(slot),
70 }
71 }
72
73 fn into_prefix(mut self, width: usize) -> Self {
74 debug_assert!(width <= self.len());
75 if width == self.len() {
76 return self;
77 }
78 if let Some(projection) = self.projection.as_ref() {
79 self.projection = Some(Arc::from(&projection[..width]));
80 return self;
81 }
82 if let Some(values) = Arc::get_mut(&mut self.values) {
83 values.truncate(width);
84 } else {
85 self.projection = Some((0..width).collect::<Arc<[usize]>>());
86 }
87 self
88 }
89}
90
91pub(super) type RowFragments = SmallVec<[RowFragment; INLINE_ROW_FRAGMENTS]>;
92
93#[derive(Debug, Clone, Default, PartialEq)]
96pub struct PhysicalRow {
97 pub(super) fragments: RowFragments,
98 pub(super) lock_origins: Option<Arc<Vec<RowLockOrigin>>>,
99}
100
101#[derive(Debug, Clone, PartialEq)]
103pub enum RowProjectionValue {
104 InputSlot(usize),
106 Owned(Value),
108}
109
110impl PhysicalRow {
111 pub fn from_values(values: Vec<Value>) -> Self {
112 let mut fragments = RowFragments::new();
113 if !values.is_empty() {
114 fragments.push(RowFragment::contiguous(Arc::new(values)));
115 }
116 Self {
117 fragments,
118 lock_origins: None,
119 }
120 }
121
122 pub fn from_shared_values(values: Arc<Vec<Value>>, projection: Arc<[usize]>) -> Self {
126 let mut fragments = RowFragments::new();
127 if !projection.is_empty() {
128 fragments.push(RowFragment::projected(values, projection));
129 }
130 Self {
131 fragments,
132 lock_origins: None,
133 }
134 }
135
136 pub fn from_result_row(schema: &RowSchema, mut row: ResultRow) -> Self {
137 let values = schema
138 .columns()
139 .iter()
140 .map(|column| row.remove(column).unwrap_or(Value::Null))
141 .collect();
142 Self::from_values(values)
143 }
144
145 pub fn nulls(width: usize) -> Self {
146 Self::from_values(vec![Value::Null; width])
147 }
148
149 pub fn append_values(mut self, values: Vec<Value>) -> Self {
150 if !values.is_empty() {
151 self.fragments
152 .push(RowFragment::contiguous(Arc::new(values)));
153 }
154 self
155 }
156
157 pub fn concat(left: &Self, right: &Self) -> Self {
158 let mut fragments =
159 RowFragments::with_capacity(left.fragments.len() + right.fragments.len());
160 fragments.extend(left.fragments.iter().cloned());
161 fragments.extend(right.fragments.iter().cloned());
162 let lock_origins =
163 concat_lock_origins(left.lock_origins.as_ref(), right.lock_origins.as_ref());
164 Self {
165 fragments,
166 lock_origins,
167 }
168 }
169
170 pub fn concat_left_owned(mut left: Self, right: &Self) -> Self {
171 left.fragments.extend(right.fragments.iter().cloned());
172 left.lock_origins =
173 concat_lock_origins(left.lock_origins.as_ref(), right.lock_origins.as_ref());
174 left
175 }
176
177 pub fn concat_right_owned(left: &Self, mut right: Self) -> Self {
178 let mut fragments =
179 RowFragments::with_capacity(left.fragments.len() + right.fragments.len());
180 fragments.extend(left.fragments.iter().cloned());
181 fragments.append(&mut right.fragments);
182 let lock_origins =
183 concat_lock_origins(left.lock_origins.as_ref(), right.lock_origins.as_ref());
184 Self {
185 fragments,
186 lock_origins,
187 }
188 }
189
190 pub(crate) fn value(&self, mut slot: usize) -> Option<&Value> {
191 for fragment in &self.fragments {
192 if slot < fragment.len() {
193 return fragment.get(slot);
194 }
195 slot -= fragment.len();
196 }
197 None
198 }
199
200 pub(crate) fn values(&self) -> impl Iterator<Item = Option<&Value>> {
202 self.fragments
203 .iter()
204 .flat_map(|fragment| (0..fragment.len()).map(move |slot| fragment.get(slot)))
205 }
206
207 pub(crate) fn project_slots(&self, slots: &[usize]) -> Self {
212 let mut output = RowFragments::new();
213 let null_values = Arc::new(Vec::new());
214 let null_source = self.fragments.len();
215 let mut current_source = None;
216 let mut current_values: Option<Arc<Vec<Value>>> = None;
217 let mut current_projection = Vec::new();
218
219 let flush = |output: &mut RowFragments,
220 values: &mut Option<Arc<Vec<Value>>>,
221 projection: &mut Vec<usize>| {
222 if let Some(values) = values.take() {
223 output.push(RowFragment::projected(
224 values,
225 Arc::from(std::mem::take(projection)),
226 ));
227 }
228 };
229
230 for requested in slots {
231 let mut remaining = *requested;
232 let resolved = if remaining == NULL_SLOT {
233 None
234 } else {
235 let mut found = None;
236 for (fragment_index, fragment) in self.fragments.iter().enumerate() {
237 if remaining < fragment.len() {
238 found = fragment
239 .stored_slot(remaining)
240 .filter(|slot| *slot != NULL_SLOT)
241 .map(|stored| (fragment_index, Arc::clone(&fragment.values), stored));
242 break;
243 }
244 remaining -= fragment.len();
245 }
246 found
247 };
248 let (source, values, stored) =
249 resolved.unwrap_or_else(|| (null_source, Arc::clone(&null_values), NULL_SLOT));
250 if current_source != Some(source) {
251 flush(&mut output, &mut current_values, &mut current_projection);
252 current_source = Some(source);
253 current_values = Some(values);
254 }
255 current_projection.push(stored);
256 }
257 flush(&mut output, &mut current_values, &mut current_projection);
258 Self {
259 fragments: output,
260 lock_origins: self.lock_origins.clone(),
261 }
262 }
263
264 pub fn project_with_values(
266 &self,
267 values: impl IntoIterator<Item = RowProjectionValue>,
268 ) -> Self {
269 fn flush_slots(source: &PhysicalRow, output: &mut RowFragments, slots: &mut Vec<usize>) {
270 if slots.is_empty() {
271 return;
272 }
273 let mut projected = source.project_slots(slots);
274 output.append(&mut projected.fragments);
275 slots.clear();
276 }
277
278 fn flush_owned(output: &mut RowFragments, owned: &mut Vec<Value>) {
279 if owned.is_empty() {
280 return;
281 }
282 output.push(RowFragment::contiguous(Arc::new(std::mem::take(owned))));
283 }
284
285 let mut fragments = RowFragments::new();
286 let mut slots = Vec::new();
287 let mut owned = Vec::new();
288 for value in values {
289 match value {
290 RowProjectionValue::InputSlot(slot) => {
291 flush_owned(&mut fragments, &mut owned);
292 slots.push(slot);
293 }
294 RowProjectionValue::Owned(value) => {
295 flush_slots(self, &mut fragments, &mut slots);
296 owned.push(value);
297 }
298 }
299 }
300 flush_slots(self, &mut fragments, &mut slots);
301 flush_owned(&mut fragments, &mut owned);
302 Self {
303 fragments,
304 lock_origins: self.lock_origins.clone(),
305 }
306 }
307
308 pub fn fragment_count(&self) -> usize {
309 self.fragments.len()
310 }
311
312 pub(crate) fn into_prefix(self, width: usize) -> Self {
313 let mut remaining = width;
314 let mut fragments = RowFragments::new();
315 for fragment in self.fragments {
316 if remaining == 0 {
317 break;
318 }
319 let fragment_width = fragment.len();
320 if fragment_width <= remaining {
321 fragments.push(fragment);
322 remaining -= fragment_width;
323 } else {
324 fragments.push(fragment.into_prefix(remaining));
325 remaining = 0;
326 }
327 }
328 debug_assert_eq!(remaining, 0, "physical row prefix exceeds row width");
329 Self {
330 fragments,
331 lock_origins: self.lock_origins,
332 }
333 }
334}