1use super::{
10 ColumnIdentity, ColumnType, HashMap, PhysicalLayout, ProjectedSlot, RowSchema,
11 SchemaBuildMetadata, SchemaLayoutError, SchemaLayoutResult, SchemaMap, ScoreSource, NULL_SLOT,
12};
13
14impl RowSchema {
15 pub fn capture_output(input: &Self, relation: crate::ast::InternalRelationId) -> Self {
17 let mut has_null = false;
18 let attributes = (0..input.len())
19 .map(|position| {
20 (
21 relation.column(position),
22 input.column_type(position).cloned(),
23 input.physical_slot(position).map_or_else(
24 || {
25 has_null = true;
26 ProjectedSlot::Computed {
27 position: 0,
28 collation: input.column_collation(position),
29 }
30 },
31 |physical| ProjectedSlot::BoundInput {
32 physical: Some(physical),
33 collation: input.column_collation(position),
34 },
35 ),
36 )
37 })
38 .collect();
39 Self::project_with_sources(input, Vec::new(), attributes, usize::from(has_null), false)
40 }
41
42 pub fn without_internal_attributes(
46 input: &Self,
47 columns: &[crate::ast::InternalColumnRef],
48 ) -> Self {
49 let mut internal = input.index.executor_attributes.clone();
50 let mut internal_types = input.index.cold.executor_attribute_types.clone();
51 for column in columns {
52 internal.remove(column);
53 internal_types.remove(column);
54 }
55 let score_sources = input
56 .index
57 .cold
58 .score_sources
59 .iter()
60 .filter(|source| internal.contains_key(&source.column))
61 .cloned()
62 .collect();
63 let row_presence = input
64 .index
65 .cold
66 .row_presence
67 .retaining(|marker| internal.contains_key(&marker));
68 Self::from_typed_parts_with_aliases_and_exact_precedence(
69 input.columns().to_vec(),
70 input.identities().to_vec(),
71 input.column_types().to_vec(),
72 input.index.slots.to_vec(),
73 input.physical_width(),
74 SchemaBuildMetadata {
75 collations: input.index.cold.collations.clone(),
76 record_fields: input.index.cold.record_fields.clone(),
77 aliases: input.index.aliases.clone(),
78 alias_types: input.index.cold.aliases.clone(),
79 internal,
80 internal_types,
81 score_sources,
82 wildcard_hidden: input.index.cold.wildcard_hidden.clone(),
83 binding_only: input.index.cold.binding_only.clone(),
84 row_types: input.index.cold.row_types.clone(),
85 row_presence,
86 open_qualifiers: input.index.cold.open_qualifiers.clone(),
87 ..SchemaBuildMetadata::default()
88 },
89 )
90 }
91
92 pub fn select(input: &Self, columns: &[(String, String)]) -> Self {
95 let mut collations = input.index.cold.collations.clone();
96 collations.clear_columns();
97 for (position, (_, source)) in columns.iter().enumerate() {
98 let oid = input
99 .position(source)
100 .and_then(|position| input.inherited_column_collation(position));
101 collations.column(position, oid, input.exact_type(source));
102 }
103 let output_names = columns
104 .iter()
105 .map(|(output, _)| output.clone())
106 .collect::<Vec<_>>();
107 let slots = columns
108 .iter()
109 .map(|(_, source)| input.exact_slot(source).unwrap_or(NULL_SLOT))
110 .collect();
111 let types = columns
112 .iter()
113 .map(|(_, source)| input.exact_type(source).cloned())
114 .collect();
115 let identities = output_names
116 .iter()
117 .cloned()
118 .map(ColumnIdentity::unqualified)
119 .collect();
120 Self::from_typed_parts_with_aliases_and_exact_precedence(
121 output_names,
122 identities,
123 types,
124 slots,
125 input.physical_width(),
126 SchemaBuildMetadata {
127 collations,
128 record_fields: input.index.cold.record_fields.clone(),
129 aliases: SchemaMap::default(),
130 alias_types: SchemaMap::default(),
131 internal: input.index.executor_attributes.clone(),
132 internal_types: input.index.cold.executor_attribute_types.clone(),
133 score_sources: input.index.cold.score_sources.clone(),
134 binding_only: SchemaMap::default(),
135 ..SchemaBuildMetadata::default()
136 },
137 )
138 }
139
140 pub fn project_with_sources(
142 input: &Self,
143 projected: Vec<(String, Option<ColumnType>, ProjectedSlot)>,
144 projected_internal: Vec<(
145 crate::ast::InternalColumnRef,
146 Option<ColumnType>,
147 ProjectedSlot,
148 )>,
149 computed_count: usize,
150 pass_through: bool,
151 ) -> Self {
152 let mut collations = input.index.cold.collations.clone();
153 let resolve_slot = |source: ProjectedSlot| match source {
154 ProjectedSlot::Input(slot) | ProjectedSlot::BoundInput { physical: slot, .. } => {
155 slot.unwrap_or(NULL_SLOT)
156 }
157 ProjectedSlot::Computed { position, .. } => input.physical_width() + position,
158 };
159 let source_collation = |source: ProjectedSlot| match source {
160 ProjectedSlot::Input(Some(slot)) => input.inherited_physical_collation(slot),
161 ProjectedSlot::BoundInput { collation, .. }
162 | ProjectedSlot::Computed { collation, .. } => collation,
163 ProjectedSlot::Input(None) => None,
164 };
165 let physical_width = input.physical_width() + computed_count;
166 let mut internal = input.index.executor_attributes.clone();
167 let mut internal_types = input.index.cold.executor_attribute_types.clone();
168 for (column, ty, source) in projected_internal {
169 collations.internal(column, source_collation(source), ty.as_ref());
170 internal.insert(column, resolve_slot(source));
171 internal_types.insert(column, ty);
172 }
173
174 if pass_through {
175 let mut columns = input.columns().to_vec();
176 let mut identities = input.identities().to_vec();
177 let mut types = input.column_types().to_vec();
178 let mut slots = input.index.slots.to_vec();
179 let mut wildcard_hidden = input.index.cold.wildcard_hidden.clone();
180 for (name, ty, source) in projected {
181 let slot = resolve_slot(source);
182 if let Some(position) = columns.iter().position(|column| column == &name) {
183 slots[position] = slot;
184 identities[position] = ColumnIdentity::unqualified(name);
185 collations.column(position, source_collation(source), ty.as_ref());
186 types[position] = ty;
187 wildcard_hidden.remove(&position);
188 } else {
189 identities.push(ColumnIdentity::unqualified(name.clone()));
190 columns.push(name);
191 collations.column(types.len(), source_collation(source), ty.as_ref());
192 types.push(ty);
193 slots.push(slot);
194 }
195 }
196 return Self::from_typed_parts_with_aliases_and_exact_precedence(
197 columns,
198 identities,
199 types,
200 slots,
201 physical_width,
202 SchemaBuildMetadata {
203 collations,
204 record_fields: input.index.cold.record_fields.clone(),
205 aliases: input.index.aliases.clone(),
206 alias_types: input.index.cold.aliases.clone(),
207 internal,
208 internal_types,
209 score_sources: input.index.cold.score_sources.clone(),
210 wildcard_hidden: wildcard_hidden.into_iter().collect(),
211 binding_only: input.index.cold.binding_only.clone(),
212 row_types: input.index.cold.row_types.clone(),
213 row_presence: input.index.cold.row_presence.clone(),
214 open_qualifiers: input.index.cold.open_qualifiers.clone(),
215 ..SchemaBuildMetadata::default()
216 },
217 );
218 }
219
220 collations.clear_columns();
221 let mut columns = Vec::with_capacity(projected.len());
222 let mut identities = Vec::with_capacity(projected.len());
223 let mut types = Vec::with_capacity(projected.len());
224 let mut slots = Vec::with_capacity(projected.len());
225 for (name, ty, source) in projected {
226 slots.push(resolve_slot(source));
227 identities.push(ColumnIdentity::unqualified(name.clone()));
228 columns.push(name);
229 collations.column(types.len(), source_collation(source), ty.as_ref());
230 types.push(ty);
231 }
232 Self::from_typed_parts_with_aliases_and_exact_precedence(
233 columns,
234 identities,
235 types,
236 slots,
237 physical_width,
238 SchemaBuildMetadata {
239 collations,
240 record_fields: input.index.cold.record_fields.clone(),
241 aliases: SchemaMap::default(),
242 alias_types: SchemaMap::default(),
243 internal,
244 internal_types,
245 score_sources: input.index.cold.score_sources.clone(),
246 binding_only: SchemaMap::default(),
247 ..SchemaBuildMetadata::default()
248 },
249 )
250 }
251
252 pub fn canonical_projection(&self) -> (Self, Vec<usize>) {
257 fn remap_slot(
258 slot: usize,
259 source_slots: &mut Vec<usize>,
260 positions: &mut HashMap<usize, usize>,
261 ) -> usize {
262 if slot == NULL_SLOT {
263 return NULL_SLOT;
264 }
265 if let Some(position) = positions.get(&slot) {
266 return *position;
267 }
268 let position = source_slots.len();
269 source_slots.push(slot);
270 positions.insert(slot, position);
271 position
272 }
273
274 let mut source_slots = Vec::new();
275 let mut positions = HashMap::new();
276 let slots = self
277 .index
278 .slots
279 .iter()
280 .map(|slot| remap_slot(*slot, &mut source_slots, &mut positions))
281 .collect();
282 let mut source_aliases = self.index.aliases.iter().collect::<Vec<_>>();
283 source_aliases.sort_unstable_by_key(|(left, _)| *left);
284 let aliases = source_aliases
285 .into_iter()
286 .map(|(name, slot)| {
287 (
288 name.clone(),
289 remap_slot(*slot, &mut source_slots, &mut positions),
290 )
291 })
292 .collect();
293 let mut source_internal = self.index.executor_attributes.iter().collect::<Vec<_>>();
294 source_internal.sort_unstable_by_key(|(column, _)| **column);
295 let internal = source_internal
296 .into_iter()
297 .map(|(column, slot)| {
298 (
299 *column,
300 remap_slot(*slot, &mut source_slots, &mut positions),
301 )
302 })
303 .collect();
304 (
305 Self::from_typed_parts_with_aliases_and_exact_precedence(
306 self.columns().to_vec(),
307 self.identities().to_vec(),
308 self.column_types().to_vec(),
309 slots,
310 source_slots.len(),
311 SchemaBuildMetadata {
312 collations: self.index.cold.collations.clone(),
313 record_fields: self
314 .index
315 .cold
316 .record_fields
317 .iter()
318 .filter_map(|(slot, fields)| {
319 positions.get(slot).map(|mapped| (*mapped, fields.clone()))
320 })
321 .collect(),
322 aliases,
323 alias_types: self.index.cold.aliases.clone(),
324 internal,
325 internal_types: self.index.cold.executor_attribute_types.clone(),
326 score_sources: self.index.cold.score_sources.clone(),
327 wildcard_hidden: self.index.cold.wildcard_hidden.clone(),
328 binding_only: self.index.cold.binding_only.clone(),
329 row_types: self.index.cold.row_types.clone(),
330 row_presence: self.index.cold.row_presence.clone(),
331 open_qualifiers: self.index.cold.open_qualifiers.clone(),
332 ..SchemaBuildMetadata::default()
333 },
334 ),
335 source_slots,
336 )
337 }
338
339 #[expect(
345 clippy::too_many_lines,
346 reason = "projection keeps schema and physical column positions aligned"
347 )]
348 pub fn from_physical_layout(layout: PhysicalLayout) -> SchemaLayoutResult<Self> {
349 let PhysicalLayout {
350 columns,
351 identities,
352 types,
353 slots,
354 physical_width,
355 aliases,
356 internal,
357 score_sources,
358 wildcard_hidden,
359 } = layout;
360 if columns.len() != slots.len() {
361 return Err(SchemaLayoutError(format!(
362 "physical schema has {} columns but {} logical slots",
363 columns.len(),
364 slots.len()
365 )));
366 }
367 if columns.len() != types.len() {
368 return Err(SchemaLayoutError(format!(
369 "physical schema has {} columns but {} logical types",
370 columns.len(),
371 types.len()
372 )));
373 }
374 if columns.len() != identities.len() {
375 return Err(SchemaLayoutError(format!(
376 "physical schema has {} columns but {} logical identities",
377 columns.len(),
378 identities.len()
379 )));
380 }
381 if wildcard_hidden
382 .iter()
383 .any(|position| *position >= columns.len())
384 {
385 return Err(SchemaLayoutError(
386 "physical schema wildcard-hidden position is outside logical width".into(),
387 ));
388 }
389 let slots = slots
390 .into_iter()
391 .map(|slot| match slot {
392 Some(slot) if slot < physical_width => Ok(slot),
393 Some(slot) => Err(SchemaLayoutError(format!(
394 "physical schema logical slot {slot} is outside width {physical_width}"
395 ))),
396 None => Ok(NULL_SLOT),
397 })
398 .collect::<SchemaLayoutResult<Vec<_>>>()?;
399 let mut lookup_aliases =
400 SchemaMap::with_capacity_and_hasher(aliases.len(), ahash::RandomState::default());
401 let mut alias_types =
402 SchemaMap::with_capacity_and_hasher(aliases.len(), ahash::RandomState::default());
403 for (identity, slot, ty) in aliases {
404 let slot = match slot {
405 Some(slot) if slot < physical_width => slot,
406 Some(slot) => {
407 return Err(SchemaLayoutError(format!(
408 "physical schema alias `{identity:?}` slot {slot} is outside width {physical_width}"
409 )))
410 }
411 None => NULL_SLOT,
412 };
413 if lookup_aliases.insert(identity.clone(), slot).is_some() {
414 return Err(SchemaLayoutError(format!(
415 "physical schema contains duplicate alias `{identity:?}`"
416 )));
417 }
418 alias_types.insert(identity, ty);
419 }
420 let mut internal_slots =
421 SchemaMap::with_capacity_and_hasher(internal.len(), ahash::RandomState::default());
422 let mut internal_types =
423 SchemaMap::with_capacity_and_hasher(internal.len(), ahash::RandomState::default());
424 for (column, slot, ty) in internal {
425 let slot = match slot {
426 Some(slot) if slot < physical_width => slot,
427 Some(slot) => {
428 return Err(SchemaLayoutError(format!(
429 "physical schema internal attribute `{column:?}` slot {slot} is outside width {physical_width}"
430 )))
431 }
432 None => NULL_SLOT,
433 };
434 if internal_slots.insert(column, slot).is_some() {
435 return Err(SchemaLayoutError(format!(
436 "physical schema contains duplicate internal attribute `{column:?}`"
437 )));
438 }
439 internal_types.insert(column, ty);
440 }
441 let score_sources = score_sources
442 .into_iter()
443 .map(|(qualifier, column)| {
444 if !internal_slots.contains_key(&column) {
445 return Err(SchemaLayoutError(format!(
446 "physical schema score source references missing internal attribute `{column:?}`"
447 )));
448 }
449 Ok(ScoreSource {
450 qualifier: qualifier.map(Box::<str>::from),
451 column,
452 })
453 })
454 .collect::<SchemaLayoutResult<Vec<_>>>()?;
455 Ok(Self::from_typed_parts_with_aliases_and_exact_precedence(
456 columns,
457 identities,
458 types,
459 slots,
460 physical_width,
461 SchemaBuildMetadata {
462 aliases: lookup_aliases,
463 alias_types,
464 internal: internal_slots,
465 internal_types,
466 score_sources,
467 wildcard_hidden: wildcard_hidden.into_iter().collect(),
468 binding_only: SchemaMap::default(),
469 ..SchemaBuildMetadata::default()
470 },
471 ))
472 }
473
474 pub fn lookup_aliases(&self) -> Vec<(&ColumnIdentity, Option<usize>)> {
475 let mut aliases = self
476 .index
477 .aliases
478 .iter()
479 .map(|(identity, slot)| (identity, (*slot != NULL_SLOT).then_some(*slot)))
480 .collect::<Vec<_>>();
481 aliases.sort_unstable_by_key(|(identity, _)| *identity);
482 aliases
483 }
484
485 pub fn lookup_aliases_with_types(
486 &self,
487 ) -> Vec<(&ColumnIdentity, Option<usize>, Option<&ColumnType>)> {
488 self.lookup_aliases()
489 .into_iter()
490 .map(|(identity, slot)| {
491 (
492 identity,
493 slot,
494 self.index
495 .cold
496 .aliases
497 .get(identity)
498 .and_then(Option::as_ref),
499 )
500 })
501 .collect()
502 }
503
504 pub fn internal_columns_with_types(
505 &self,
506 ) -> Vec<(
507 crate::ast::InternalColumnRef,
508 Option<usize>,
509 Option<&ColumnType>,
510 )> {
511 let mut columns = self
512 .index
513 .executor_attributes
514 .iter()
515 .map(|(column, slot)| {
516 (
517 *column,
518 (*slot != NULL_SLOT).then_some(*slot),
519 self.index
520 .cold
521 .executor_attribute_types
522 .get(column)
523 .and_then(Option::as_ref),
524 )
525 })
526 .collect::<Vec<_>>();
527 columns.sort_unstable_by_key(|(column, _, _)| *column);
528 columns
529 }
530
531 pub fn score_sources(
532 &self,
533 ) -> impl Iterator<Item = (Option<&str>, crate::ast::InternalColumnRef)> {
534 self.index
535 .cold
536 .score_sources
537 .iter()
538 .map(|source| (source.qualifier.as_deref(), source.column))
539 }
540
541 pub fn wildcard_hidden_positions(&self) -> impl Iterator<Item = usize> + '_ {
542 self.index.cold.wildcard_hidden.iter().copied()
543 }
544
545 pub fn remap_positions(
547 input: &Self,
548 columns: &[(String, usize)],
549 aliases: &[(ColumnIdentity, usize)],
550 ) -> Self {
551 let columns = columns
552 .iter()
553 .map(|(name, logical)| (name.clone(), *logical, input.column_type(*logical).cloned()))
554 .collect::<Vec<_>>();
555 Self::remap_typed_positions(input, &columns, aliases)
556 }
557
558 pub fn remap_typed_positions(
560 input: &Self,
561 columns: &[(String, usize, Option<ColumnType>)],
562 aliases: &[(ColumnIdentity, usize)],
563 ) -> Self {
564 let mut collations = input.index.cold.collations.clone();
565 collations.clear_columns();
566 for (position, (_, source, ty)) in columns.iter().enumerate() {
567 collations.column(
568 position,
569 input.inherited_column_collation(*source),
570 ty.as_ref(),
571 );
572 }
573 let output_names = columns
574 .iter()
575 .map(|(output, _, _)| output.clone())
576 .collect::<Vec<_>>();
577 let slots = columns
578 .iter()
579 .map(|(_, logical, _)| input.slot(*logical).unwrap_or(NULL_SLOT))
580 .collect();
581 let types = columns.iter().map(|(_, _, ty)| ty.clone()).collect();
582 let identities = output_names
583 .iter()
584 .cloned()
585 .map(ColumnIdentity::unqualified)
586 .collect();
587 let wildcard_hidden = columns
588 .iter()
589 .enumerate()
590 .filter_map(|(output, (_, logical, _))| {
591 input
592 .index
593 .cold
594 .wildcard_hidden
595 .contains(logical)
596 .then_some(output)
597 })
598 .collect();
599 let mut lookup_aliases = input.index.aliases.clone();
600 let mut alias_types = input.index.cold.aliases.clone();
601 for (identity, logical) in aliases {
602 collations.alias(
603 identity,
604 input.inherited_column_collation(*logical),
605 input.column_type(*logical),
606 );
607 lookup_aliases.insert(identity.clone(), input.slot(*logical).unwrap_or(NULL_SLOT));
608 alias_types.insert(identity.clone(), input.column_type(*logical).cloned());
609 }
610 Self::from_typed_parts_with_aliases_and_exact_precedence(
611 output_names,
612 identities,
613 types,
614 slots,
615 input.physical_width(),
616 SchemaBuildMetadata {
617 collations,
618 record_fields: input.index.cold.record_fields.clone(),
619 aliases: lookup_aliases,
620 alias_types,
621 internal: input.index.executor_attributes.clone(),
622 internal_types: input.index.cold.executor_attribute_types.clone(),
623 score_sources: input.index.cold.score_sources.clone(),
624 wildcard_hidden,
625 binding_only: input.index.cold.binding_only.clone(),
626 row_types: input.index.cold.row_types.clone(),
627 row_presence: input.index.cold.row_presence.clone(),
628 open_qualifiers: input.index.cold.open_qualifiers.clone(),
629 ..SchemaBuildMetadata::default()
630 },
631 )
632 }
633}
634
635#[cfg(test)]
636mod capture_tests {
637 use super::*;
638 use crate::ast::InternalRelationId;
639 use std::sync::Arc;
640
641 #[test]
642 fn output_capture_keeps_positional_metadata_without_sql_aliases() {
643 let fields: crate::schema::RecordFields = Arc::from([Some(ColumnType::SmallInteger)]);
644 let input = RowSchema::with_qualified_types(
645 "source",
646 vec!["unused".into(), "text".into(), "record".into()],
647 vec![
648 Some(ColumnType::Integer),
649 Some(ColumnType::Text),
650 Some(ColumnType::Record),
651 ],
652 )
653 .with_column_collations([None, Some(950), None])
654 .with_record_fields([(2, Arc::clone(&fields))]);
655 let selected =
656 RowSchema::remap_positions(&input, &[("same".into(), 2), ("same".into(), 1)], &[]);
657 let relation = InternalRelationId::allocate();
658 let captured = RowSchema::capture_output(&selected, relation);
659 assert!(captured.is_empty());
660 assert_eq!(captured.physical_width(), 3);
661 assert_eq!(captured.internal_slot(relation.column(0)), Some(2));
662 assert_eq!(captured.internal_slot(relation.column(1)), Some(1));
663 assert_eq!(
664 captured.internal_type(relation.column(0)),
665 Some(&ColumnType::Record)
666 );
667 assert_eq!(captured.internal_collation(relation.column(1)), Some(950));
668 assert!(Arc::ptr_eq(
669 captured.physical_record_fields(2).unwrap(),
670 &fields
671 ));
672 assert_eq!(captured.qualified_slot("source", "record"), None);
673 assert_eq!(captured.position("same"), None);
674 let other = InternalRelationId::allocate();
675 let second = RowSchema::capture_output(&selected, other);
676 let joined = RowSchema::join(&captured, &second, []);
677 assert_eq!(joined.internal_slot(relation.column(0)), Some(2));
678 assert_eq!(joined.internal_slot(other.column(0)), Some(5));
679 }
680
681 #[test]
682 fn output_capture_assigns_a_slot_to_implicit_null_values() {
683 let input = RowSchema::new(vec!["present".into()]);
684 let selected = RowSchema::select(&input, &[("missing".into(), "absent".into())]);
685 let relation = InternalRelationId::allocate();
686 let captured = RowSchema::capture_output(&selected, relation);
687 assert_eq!(captured.internal_slot(relation.column(0)), Some(1));
688 assert_eq!(captured.physical_width(), 2);
689 assert!(captured.is_empty());
690 }
691}