Skip to main content

uqa_execution/
column_selection.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Schema-only projection used after an expression-producing stage.
8
9use uqa_core::Value;
10
11use crate::{
12    Batch, ColumnIdentity, ExecResult, PhysicalOperator, PhysicalOrder, RowProjectionValue,
13    RowSchema,
14};
15
16fn remap_ordering(
17    ordering: &[PhysicalOrder],
18    input_positions: &[Option<usize>],
19) -> Vec<PhysicalOrder> {
20    ordering
21        .iter()
22        .map_while(|order| {
23            let position = input_positions
24                .iter()
25                .position(|input| *input == Some(order.position))?;
26            Some(PhysicalOrder {
27                position,
28                ..order.clone()
29            })
30        })
31        .collect()
32}
33
34/// Select already-computed columns without evaluating expressions again.
35///
36/// This is intentionally distinct from [`crate::Project`]: SQL `ORDER BY`
37/// may reference both source columns and SELECT aliases, so the expression
38/// projection first appends aliases, Sort consumes that augmented row, and
39/// `ColumnSelection` removes the non-output source columns afterwards. Volatile
40/// projection expressions are therefore evaluated exactly once.
41pub struct ColumnSelection<'a> {
42    child: Box<dyn PhysicalOperator + 'a>,
43    schema: RowSchema,
44    /// Physical ordering after the selected positions have been remapped.
45    /// Executor-only projection attributes are addressed structurally and
46    /// never enter the public SQL column namespace.
47    ordering: Vec<PhysicalOrder>,
48    rebind_lock_qualifier: Option<crate::RowLockName>,
49    discard_lock_origins: bool,
50    compact_slots: Option<Vec<Option<usize>>>,
51    _memory: Option<uqa_core::memory::MemoryReservation>,
52}
53
54impl<'a> ColumnSelection<'a> {
55    /// Hide a derived input's SQL namespace while sharing its physical values through positional structural references.
56    pub(crate) fn capturing_output(
57        child: Box<dyn PhysicalOperator + 'a>,
58        relation: uqa_sql::ast::InternalRelationId,
59    ) -> Self {
60        let schema = RowSchema::capture_output(child.row_schema(), relation);
61        let width = child.row_schema().physical_width();
62        let compact_slots =
63            (schema.physical_width() > width).then(|| (0..width).map(Some).chain([None]).collect());
64        Self {
65            child,
66            schema,
67            ordering: Vec::new(),
68            rebind_lock_qualifier: None,
69            discard_lock_origins: false,
70            _memory: None,
71            compact_slots,
72        }
73    }
74
75    /// Keep the visible layout unchanged while retiring consumed executor-only
76    /// attributes from the schema namespace.
77    pub fn dropping_internal_attributes(
78        child: Box<dyn PhysicalOperator + 'a>,
79        columns: &[uqa_sql::ast::InternalColumnRef],
80    ) -> Self {
81        let schema = RowSchema::without_internal_attributes(child.row_schema(), columns);
82        let ordering = child.output_ordering().to_vec();
83        Self {
84            child,
85            schema,
86            ordering,
87            rebind_lock_qualifier: None,
88            discard_lock_origins: false,
89            _memory: None,
90            compact_slots: None,
91        }
92    }
93
94    pub fn new(child: Box<dyn PhysicalOperator + 'a>, columns: Vec<String>) -> Self {
95        let columns = columns
96            .into_iter()
97            .map(|column| (column.clone(), column))
98            .collect();
99        Self::with_mapping(child, columns)
100    }
101
102    pub fn with_mapping(
103        child: Box<dyn PhysicalOperator + 'a>,
104        columns: Vec<(String, String)>,
105    ) -> Self {
106        let input_positions = columns
107            .iter()
108            .map(|(_, input)| child.row_schema().position(input))
109            .collect::<Vec<_>>();
110        let ordering = remap_ordering(child.output_ordering(), &input_positions);
111        let schema = RowSchema::select(child.row_schema(), &columns);
112        Self {
113            child,
114            schema,
115            ordering,
116            rebind_lock_qualifier: None,
117            discard_lock_origins: false,
118            _memory: None,
119            compact_slots: None,
120        }
121    }
122
123    /// Select and rename logical input positions without resolving them by
124    /// name. SQL result shaping uses this when repeated public labels must
125    /// remain distinct even though their qualified input names differ.
126    pub fn with_positions(
127        child: Box<dyn PhysicalOperator + 'a>,
128        columns: Vec<(String, usize)>,
129    ) -> Self {
130        let input_positions = columns
131            .iter()
132            .map(|(_, position)| Some(*position))
133            .collect::<Vec<_>>();
134        let ordering = remap_ordering(child.output_ordering(), &input_positions);
135        let schema = RowSchema::remap_positions(child.row_schema(), &columns, &[]);
136        Self {
137            child,
138            schema,
139            ordering,
140            rebind_lock_qualifier: None,
141            discard_lock_origins: false,
142            _memory: None,
143            compact_slots: None,
144        }
145    }
146
147    /// Select flattened physical slots, including executor-only internal
148    /// attributes that deliberately have no SQL-visible logical position.
149    pub fn with_physical_positions(
150        child: Box<dyn PhysicalOperator + 'a>,
151        columns: Vec<(String, usize)>,
152    ) -> Self {
153        let input_positions = columns
154            .iter()
155            .map(|(_, physical)| {
156                (0..child.row_schema().len())
157                    .find(|logical| child.row_schema().physical_slot(*logical) == Some(*physical))
158            })
159            .collect::<Vec<_>>();
160        let ordering = remap_ordering(child.output_ordering(), &input_positions);
161        let columns = columns
162            .into_iter()
163            .map(|(label, physical)| {
164                let ty = child.row_schema().physical_type(physical).cloned();
165                (
166                    label.clone(),
167                    ColumnIdentity::unqualified(label),
168                    physical,
169                    ty,
170                )
171            })
172            .collect::<Vec<_>>();
173        let schema = RowSchema::remap_typed_physical_identities(child.row_schema(), &columns, &[]);
174        Self {
175            child,
176            schema,
177            ordering,
178            rebind_lock_qualifier: None,
179            discard_lock_origins: false,
180            _memory: None,
181            compact_slots: None,
182        }
183    }
184
185    /// Select logical input positions and assign explicit structured SQL identities without encoding qualifiers into output labels.
186    pub fn with_identities(
187        child: Box<dyn PhysicalOperator + 'a>,
188        columns: Vec<(String, ColumnIdentity, usize)>,
189    ) -> Self {
190        let input_positions = columns
191            .iter()
192            .map(|(_, _, position)| Some(*position))
193            .collect::<Vec<_>>();
194        let ordering = remap_ordering(child.output_ordering(), &input_positions);
195        let columns = columns
196            .into_iter()
197            .map(|(label, identity, position)| {
198                let ty = child.row_schema().column_type(position).cloned();
199                (label, identity, position, ty)
200            })
201            .collect::<Vec<_>>();
202        let schema = RowSchema::remap_typed_identities(child.row_schema(), &columns, &[]);
203        Self {
204            child,
205            schema,
206            ordering,
207            rebind_lock_qualifier: None,
208            discard_lock_origins: false,
209            _memory: None,
210            compact_slots: None,
211        }
212    }
213
214    /// Hide recursive-CTE generated columns from `*` while retaining them as real qualified and unqualified lookup aliases. `PostgreSQL` adds SEARCH/CYCLE state after expanding the recursive term's wildcard, but still permits the recursive term to name the generated cycle columns explicitly.
215    pub fn hiding_trailing_columns(
216        child: Box<dyn PhysicalOperator + 'a>,
217        visible: usize,
218        qualifier: &str,
219    ) -> Self {
220        let visible = visible.min(child.row_schema().len());
221        let input_positions = (0..visible).map(Some).collect::<Vec<_>>();
222        let ordering = remap_ordering(child.output_ordering(), &input_positions);
223        let columns = (0..visible)
224            .map(|position| {
225                let label = child.row_schema().columns()[position].clone();
226                let identity = if qualifier.is_empty() {
227                    ColumnIdentity::unqualified(&label)
228                } else {
229                    ColumnIdentity::qualified(qualifier, &label)
230                };
231                let ty = child.row_schema().column_type(position).cloned();
232                (label, identity, position, ty)
233            })
234            .collect::<Vec<_>>();
235        let mut aliases = Vec::new();
236        for position in visible..child.row_schema().len() {
237            let name = child.row_schema().columns()[position].clone();
238            aliases.push((ColumnIdentity::unqualified(&name), position));
239            if !qualifier.is_empty() {
240                aliases.push((ColumnIdentity::qualified(qualifier, name), position));
241            }
242        }
243        let schema = RowSchema::remap_typed_identities(child.row_schema(), &columns, &aliases);
244        Self {
245            child,
246            schema,
247            ordering,
248            rebind_lock_qualifier: None,
249            discard_lock_origins: true,
250            _memory: None,
251            compact_slots: None,
252        }
253    }
254
255    /// Select positions under identities that replace, rather than extend, the child's SQL namespace.
256    pub fn with_fresh_identities(
257        child: Box<dyn PhysicalOperator + 'a>,
258        columns: Vec<(String, ColumnIdentity, usize)>,
259    ) -> Self {
260        let input_positions = columns
261            .iter()
262            .map(|(_, _, position)| Some(*position))
263            .collect::<Vec<_>>();
264        let ordering = remap_ordering(child.output_ordering(), &input_positions);
265        let columns = columns
266            .into_iter()
267            .map(|(label, identity, position)| {
268                let ty = child.row_schema().column_type(position).cloned();
269                (label, identity, position, ty)
270            })
271            .collect::<Vec<_>>();
272        let schema =
273            RowSchema::remap_typed_identities_without_input_aliases(child.row_schema(), &columns);
274        let (schema, slots) = production::relation_namespace(
275            schema,
276            &uqa_core::memory::ProductionControl::uncontrolled(),
277        )
278        .expect("ordinary relation namespace construction");
279        Self {
280            child,
281            schema,
282            ordering,
283            rebind_lock_qualifier: None,
284            discard_lock_origins: false,
285            _memory: None,
286            compact_slots: slots.map(|slots| slots.into_uncontrolled().expect("ordinary slots")),
287        }
288    }
289
290    /// Select logical input positions and compact them to one canonical positional layout. Use this only at an explicit state boundary, such as recursive working-table materialization, where independently planned inputs must share an identical physical schema.
291    pub fn compacting_with_positions(
292        child: Box<dyn PhysicalOperator + 'a>,
293        columns: Vec<(String, usize)>,
294    ) -> Self {
295        let input_positions = columns
296            .iter()
297            .map(|(_, position)| Some(*position))
298            .collect::<Vec<_>>();
299        let ordering = remap_ordering(child.output_ordering(), &input_positions);
300        let selected = RowSchema::remap_positions(child.row_schema(), &columns, &[]);
301        let (schema, source_slots) = selected.canonical_projection();
302        let identity_layout = child.row_schema().physical_width() == source_slots.len()
303            && source_slots
304                .iter()
305                .enumerate()
306                .all(|(position, slot)| *slot == position);
307        Self {
308            child,
309            schema,
310            ordering,
311            rebind_lock_qualifier: None,
312            discard_lock_origins: true,
313            _memory: None,
314            compact_slots: (!identity_layout).then(|| source_slots.into_iter().map(Some).collect()),
315        }
316    }
317
318    /// Map public outputs to their analyzed descriptor and retain structural score provenance. Analysis owns public collations and record descriptors; the producer owns shared value slots and opaque score identities.
319    pub(crate) fn with_bound_output(
320        child: Box<dyn PhysicalOperator + 'a>,
321        columns: &[(String, usize)],
322        mut schema: RowSchema,
323    ) -> ExecResult<Self> {
324        let (child, ()) = crate::physical::construction::OperatorBuild::run(child, |child| {
325            if !schema.is_identity_layout()
326                || schema.physical_width() != schema.len()
327                || columns.len() != schema.len()
328                || columns.iter().enumerate().any(|(output, (name, input))| {
329                    schema.columns()[output] != *name
330                        || *input >= child.row_schema().len()
331                        || schema.column_type(output) != child.row_schema().column_type(*input)
332                })
333            {
334                return Err(uqa_sql::SQLError::Internal(
335                    "query output does not match its analyzed columns and types".into(),
336                ));
337            }
338            Ok(())
339        })?;
340        let input_positions = columns
341            .iter()
342            .map(|(_, position)| Some(*position))
343            .collect::<Vec<_>>();
344        let ordering = remap_ordering(child.output_ordering(), &input_positions);
345        let mut slots = columns
346            .iter()
347            .map(|(_, position)| child.row_schema().physical_slot(*position))
348            .collect::<Vec<_>>();
349        if child.row_schema().score_sources().next().is_some() {
350            let score_columns = child
351                .row_schema()
352                .score_sources()
353                .map(|(_, column)| column)
354                .collect::<std::collections::BTreeSet<_>>();
355            let retired = child
356                .row_schema()
357                .internal_columns_with_types()
358                .into_iter()
359                .filter_map(|(column, _, _)| (!score_columns.contains(&column)).then_some(column))
360                .collect::<Vec<_>>();
361            let scores =
362                RowSchema::remap_typed_identities_without_input_aliases(child.row_schema(), &[]);
363            let scores = RowSchema::without_internal_attributes(&scores, &retired);
364            let (scores, score_slots) = scores.canonical_projection();
365            schema = RowSchema::join(&schema, &scores, []);
366            slots.extend(score_slots.into_iter().map(Some));
367        }
368        let identity_layout = child.row_schema().physical_width() == slots.len()
369            && slots
370                .iter()
371                .enumerate()
372                .all(|(position, slot)| *slot == Some(position));
373        Ok(Self {
374            child,
375            schema,
376            ordering,
377            rebind_lock_qualifier: None,
378            discard_lock_origins: false,
379            _memory: None,
380            compact_slots: (!identity_layout).then_some(slots),
381        })
382    }
383
384    /// Establish a source namespace even when the selected row has no named columns.
385    #[must_use]
386    pub(crate) fn with_relation_qualifier(mut self, qualifier: &str) -> Self {
387        self.schema = RowSchema::with_relation_qualifier(&self.schema, qualifier);
388        self
389    }
390
391    /// Attribute inner lock origins to this source qualifier so `FOR UPDATE OF` a view, CTE, or subquery does not lock sibling join inputs.
392    #[must_use]
393    pub fn rebinding_lock_origins(mut self, qualifier: impl Into<String>) -> Self {
394        let qualifier = qualifier.into();
395        if !qualifier.is_empty() {
396            self.rebind_lock_qualifier = Some(qualifier.into());
397        }
398        self
399    }
400
401    /// Attribute every structurally carried retrieval score to this relation alias without introducing a hidden SQL column name.
402    #[must_use]
403    pub fn rebinding_score_sources(mut self, qualifier: impl Into<String>) -> Self {
404        let qualifier = qualifier.into();
405        self.schema = RowSchema::with_rebound_score_sources(
406            &self.schema,
407            (!qualifier.is_empty()).then_some(qualifier.as_str()),
408        );
409        self
410    }
411
412    /// Remove row-lock identities at a relational row-identity barrier.
413    #[must_use]
414    pub fn discarding_lock_origins(mut self) -> Self {
415        self.rebind_lock_qualifier = None;
416        self.discard_lock_origins = true;
417        self
418    }
419
420    /// A derived-table demand boundary changes its physical layout without retiring row-lock identities needed by an enclosing locking clause.
421    #[must_use]
422    pub fn preserving_lock_origins(mut self) -> Self {
423        self.discard_lock_origins = false;
424        self
425    }
426
427    fn select_batch(&self, batch: Batch) -> Batch {
428        let mut rows = match self.compact_slots.as_ref() {
429            Some(slots) => batch
430                .rows
431                .into_iter()
432                .map(|row| {
433                    row.project_with_values(slots.iter().map(|slot| {
434                        slot.map_or(
435                            RowProjectionValue::Owned(Value::Null),
436                            RowProjectionValue::InputSlot,
437                        )
438                    }))
439                })
440                .collect(),
441            None => batch.rows,
442        };
443        if self.discard_lock_origins {
444            for row in &mut rows {
445                row.discard_lock_origins_mut();
446            }
447        } else if let Some(qualifier) = self.rebind_lock_qualifier.as_ref() {
448            for row in &mut rows {
449                row.rebind_lock_origin_qualifiers_mut(qualifier.clone());
450            }
451        }
452        Batch::from_physical_rows(self.schema.clone(), rows)
453    }
454}
455
456impl ColumnSelection<'_> {
457    fn pull(&mut self, demand: Option<std::num::NonZeroUsize>) -> ExecResult<Option<Batch>> {
458        let Some(batch) = (match demand {
459            Some(rows) => self.child.next_with_demand(rows)?,
460            None => self.child.next()?,
461        }) else {
462            return Ok(None);
463        };
464        Ok(Some(self.select_batch(batch)))
465    }
466}
467
468impl PhysicalOperator for ColumnSelection<'_> {
469    fn row_schema(&self) -> &RowSchema {
470        &self.schema
471    }
472
473    fn supports_batched_consumption(&self) -> bool {
474        self.child.supports_batched_consumption()
475    }
476
477    fn collect_materialized(
478        &mut self,
479        sink: &mut crate::query::output::MaterializedResultSink<'_>,
480    ) -> ExecResult<bool> {
481        if self.compact_slots.is_some()
482            || self.rebind_lock_qualifier.is_some()
483            || self.discard_lock_origins
484            || self.schema.physical_width() != self.child.row_schema().physical_width()
485        {
486            return Ok(false);
487        }
488        self.child.collect_materialized(sink)
489    }
490
491    fn estimated_cardinality(&self) -> Option<u64> {
492        self.child.estimated_cardinality()
493    }
494
495    fn output_ordering(&self) -> &[PhysicalOrder] {
496        &self.ordering
497    }
498
499    fn backward_scan_support(&self) -> crate::BackwardScanSupport {
500        self.child.backward_scan_support()
501    }
502
503    fn open(&mut self) -> ExecResult<()> {
504        self.child.open()
505    }
506
507    fn next(&mut self) -> ExecResult<Option<Batch>> {
508        self.pull(None)
509    }
510
511    fn next_with_demand(&mut self, rows: std::num::NonZeroUsize) -> ExecResult<Option<Batch>> {
512        self.pull(Some(rows))
513    }
514
515    fn next_direction(
516        &mut self,
517        direction: crate::PhysicalScanDirection,
518    ) -> ExecResult<Option<Batch>> {
519        let Some(batch) = self.child.next_direction(direction)? else {
520            return Ok(None);
521        };
522        Ok(Some(self.select_batch(batch)))
523    }
524
525    fn rewind(&mut self) -> ExecResult<()> {
526        self.child.rewind()
527    }
528
529    fn close(&mut self) -> ExecResult<()> {
530        self.child.close()
531    }
532}
533
534#[cfg(test)]
535mod tests {
536    use crate::RowSchemaExecution;
537    use std::collections::BTreeMap;
538
539    use uqa_core::Value;
540
541    use super::*;
542    use crate::physical::run_to_rows;
543    use crate::scan::TableScan;
544
545    #[test]
546    fn selects_computed_columns_without_leaking_sort_inputs() {
547        let row = BTreeMap::from([
548            ("source".to_string(), Value::Int(1)),
549            ("alias".to_string(), Value::Int(2)),
550        ]);
551        let scan = TableScan::from_rows(vec!["source".into(), "alias".into()], vec![row]);
552        let mut selection = ColumnSelection::new(Box::new(scan), vec!["alias".into()]);
553        let (schema, rows) = run_to_rows(&mut selection).unwrap();
554        assert_eq!(schema, vec!["alias"]);
555        assert_eq!(rows[0], BTreeMap::from([("alias".into(), Value::Int(2))]));
556    }
557
558    #[test]
559    fn renames_collision_free_physical_columns() {
560        let row = BTreeMap::from([
561            ("source".to_string(), Value::Int(1)),
562            ("__projection_0".to_string(), Value::Int(2)),
563        ]);
564        let scan = TableScan::from_rows(vec!["source".into(), "__projection_0".into()], vec![row]);
565        let mut selection = ColumnSelection::with_mapping(
566            Box::new(scan),
567            vec![("source".into(), "__projection_0".into())],
568        );
569        let (schema, rows) = run_to_rows(&mut selection).unwrap();
570        assert_eq!(schema, vec!["source"]);
571        assert_eq!(rows[0], BTreeMap::from([("source".into(), Value::Int(2))]));
572    }
573
574    #[test]
575    fn renames_repeated_columns_by_position() {
576        let scan = TableScan::from_rows(
577            vec!["left.value".into(), "right.value".into()],
578            vec![BTreeMap::from([
579                ("left.value".into(), Value::Int(1)),
580                ("right.value".into(), Value::Int(2)),
581            ])],
582        );
583        let mut selection = ColumnSelection::with_positions(
584            Box::new(scan),
585            vec![("value".into(), 0), ("value".into(), 1)],
586        );
587        let batches = crate::physical::run_to_batches(&mut selection).unwrap();
588        assert_eq!(batches[0].schema.columns(), ["value", "value"]);
589        let row = batches[0].schema.view(&batches[0].rows[0]);
590        assert_eq!(row.value_at(0), Some(&Value::Int(1)));
591        assert_eq!(row.value_at(1), Some(&Value::Int(2)));
592    }
593
594    #[test]
595    fn ordering_is_remapped_by_selected_position() {
596        let ordering = vec![PhysicalOrder {
597            position: 2,
598            descending: false,
599            nulls_first: None,
600            nullable: false,
601        }];
602        let remapped = remap_ordering(&ordering, &[Some(2), Some(0)]);
603        assert_eq!(remapped[0].position, 0);
604        assert_eq!(remap_ordering(&ordering, &[Some(0), Some(1)]).len(), 0);
605    }
606
607    #[test]
608    fn row_identity_barrier_discards_lock_origins_in_place() {
609        let schema = RowSchema::new(vec!["id".into()]);
610        let row = crate::PhysicalRow::from_values(vec![Value::Int(1)])
611            .with_lock_origin(crate::RowLockOrigin::new("accounts", "public.accounts", 1));
612        let scan = TableScan::from_physical_rows(schema, vec![row]);
613        let mut barrier = ColumnSelection::with_positions(Box::new(scan), vec![("id".into(), 0)])
614            .discarding_lock_origins();
615
616        let batches = crate::physical::run_to_batches(&mut barrier).unwrap();
617        assert_eq!(batches[0].rows[0].lock_origins().len(), 0);
618    }
619
620    #[test]
621    fn explicit_compaction_canonicalizes_a_wider_physical_layout() {
622        let source = RowSchema::new(vec!["unused".into(), "value".into()]);
623        let selected = RowSchema::select(&source, &[("value".into(), "value".into())]);
624        let row = crate::PhysicalRow::from_values(vec![Value::Int(1), Value::Int(7)]);
625        let scan = TableScan::from_physical_rows(selected, vec![row]);
626        let mut compact =
627            ColumnSelection::compacting_with_positions(Box::new(scan), vec![("value".into(), 0)]);
628
629        compact.open().unwrap();
630        let batch = compact.next().unwrap().unwrap();
631        compact.close().unwrap();
632
633        assert_eq!(batch.schema.physical_width(), 1);
634        assert_eq!(
635            batch.schema.view(&batch.rows[0]).get("value"),
636            Some(&Value::Int(7))
637        );
638    }
639
640    #[test]
641    fn captured_output_shares_values_without_exposing_input_names() {
642        let relation = uqa_sql::ast::InternalRelationId::allocate();
643        let source = RowSchema::with_types(
644            vec!["unused".into(), "payload".into()],
645            vec![
646                Some(uqa_sql::ColumnType::Integer),
647                Some(uqa_sql::ColumnType::Text),
648            ],
649        );
650        let selected = RowSchema::select(&source, &[("payload".into(), "payload".into())]);
651        let row =
652            crate::PhysicalRow::from_values(vec![Value::Int(7), Value::Str("shared".repeat(1000))]);
653        let retained = row.clone();
654        let mut output = ColumnSelection::capturing_output(
655            Box::new(TableScan::from_physical_rows(selected, vec![row])),
656            relation,
657        );
658        let batches = crate::physical::run_to_batches(&mut output).unwrap();
659        assert_eq!(batches.len(), 1);
660        let batch = &batches[0];
661        assert!(batch.schema.is_empty());
662        assert_eq!(batch.schema.position("payload"), None);
663        let slot = batch.schema.internal_slot(relation.column(0)).unwrap();
664        assert!(std::ptr::eq(
665            batch.rows[0].value(slot).unwrap(),
666            retained.value(1).unwrap()
667        ));
668        assert_eq!(
669            batch.schema.internal_type(relation.column(0)),
670            Some(&uqa_sql::ColumnType::Text)
671        );
672        let selected = RowSchema::select(&source, &[("missing".into(), "absent".into())]);
673        let mut output = ColumnSelection::capturing_output(
674            Box::new(TableScan::from_physical_rows(selected, vec![retained])),
675            relation,
676        );
677        let batches = crate::physical::run_to_batches(&mut output).unwrap();
678        let slot = batches[0].schema.internal_slot(relation.column(0)).unwrap();
679        assert_eq!(batches[0].rows[0].value(slot), Some(&Value::Null));
680    }
681
682    #[test]
683    fn bound_output_preserves_metadata_shared_values_and_lock_origins() {
684        use uqa_sql::ColumnType;
685
686        let fields: uqa_sql::schema::RecordFields =
687            vec![Some(ColumnType::SmallInteger), Some(ColumnType::Text)].into();
688        let bound = RowSchema::with_types(
689            vec!["first".into(), "second".into(), "record".into()],
690            vec![
691                Some(ColumnType::Text),
692                Some(ColumnType::Text),
693                Some(ColumnType::Record),
694            ],
695        )
696        .with_column_collations([Some(950), Some(951), None])
697        .with_record_fields([(2, fields.clone())]);
698        let input = RowSchema::with_types(
699            vec!["hidden".into(), "text".into(), "record".into()],
700            vec![
701                Some(ColumnType::Integer),
702                Some(ColumnType::Text),
703                Some(ColumnType::Record),
704            ],
705        );
706        let row = crate::PhysicalRow::from_values(vec![
707            Value::Int(99),
708            Value::Str("shared".repeat(1_000)),
709            Value::Record(vec![("n".into(), Value::Int(7)), ("s".into(), Value::Null)].into()),
710        ])
711        .with_lock_origin(crate::RowLockOrigin::new("items", "public.items", 1));
712        let retained = row.clone();
713        let mut output = ColumnSelection::with_bound_output(
714            Box::new(TableScan::from_physical_rows(input, vec![row])),
715            &[
716                ("first".into(), 1),
717                ("second".into(), 1),
718                ("record".into(), 2),
719            ],
720            bound.clone(),
721        )
722        .unwrap();
723        assert_eq!(output.row_schema(), &bound);
724        let batches = crate::physical::run_to_batches(&mut output).unwrap();
725        assert_eq!(batches.len(), 1);
726        assert_eq!(batches[0].schema, bound);
727        let result = &batches[0].rows[0];
728        for (output, input) in [(0, 1), (1, 1), (2, 2)] {
729            assert!(std::ptr::eq(
730                result.value(output).unwrap(),
731                retained.value(input).unwrap()
732            ));
733        }
734        assert_eq!(result.lock_origins().len(), 1);
735        assert!(std::ptr::eq(
736            &raw const **batches[0].schema.record_fields(2).unwrap(),
737            &raw const *fields
738        ));
739    }
740
741    #[test]
742    fn bound_output_rejects_changed_columns_types_and_nonpositional_descriptors() {
743        use uqa_sql::ColumnType;
744
745        let input = RowSchema::with_types(vec!["value".into()], vec![Some(ColumnType::Integer)]);
746        let other_type = RowSchema::with_types(vec!["value".into()], vec![Some(ColumnType::Text)]);
747        let wider = RowSchema::with_types(
748            vec!["hidden".into(), "value".into()],
749            vec![Some(ColumnType::Integer); 2],
750        );
751        let nonpositional = RowSchema::select(&wider, &[("value".into(), "value".into())]);
752        for (columns, bound) in [
753            (vec![("renamed".into(), 0)], input.clone()),
754            (vec![], input.clone()),
755            (vec![("value".into(), 1)], input.clone()),
756            (vec![("value".into(), 0)], other_type),
757            (vec![("value".into(), 0)], nonpositional),
758        ] {
759            let child = Box::new(TableScan::from_physical_rows(input.clone(), vec![]));
760            assert!(ColumnSelection::with_bound_output(child, &columns, bound).is_err());
761        }
762    }
763
764    #[test]
765    fn bound_output_preserves_scores_without_exposing_internal_columns() {
766        use uqa_sql::ColumnType;
767
768        let public = RowSchema::with_types(vec!["body".into()], vec![Some(ColumnType::Text)])
769            .with_column_collations([Some(950)]);
770        let first = uqa_sql::ast::InternalRelationId::allocate().column(0);
771        let second = uqa_sql::ast::InternalRelationId::allocate().column(0);
772        let physical = RowSchema::from_physical_layout(uqa_sql::schema::PhysicalLayout {
773            columns: vec!["unused".into(), "body".into()],
774            identities: vec![
775                ColumnIdentity::unqualified("unused"),
776                ColumnIdentity::unqualified("body"),
777            ],
778            types: vec![Some(ColumnType::Integer), Some(ColumnType::Text)],
779            slots: vec![Some(0), Some(1)],
780            physical_width: 4,
781            aliases: vec![],
782            internal: vec![
783                (first, Some(2), Some(ColumnType::DoublePrecision)),
784                (second, Some(3), Some(ColumnType::DoublePrecision)),
785            ],
786            score_sources: vec![
787                (Some("left".into()), first),
788                (Some("right".into()), second),
789                (Some("echo".into()), first),
790            ],
791            wildcard_hidden: std::collections::HashSet::new(),
792        })
793        .unwrap();
794        let row = crate::PhysicalRow::from_values(vec![
795            Value::Int(99),
796            Value::Str("rust".into()),
797            Value::Float(1.25),
798            Value::Float(2.5),
799        ]);
800        let retained = row.clone();
801        let mut output = ColumnSelection::with_bound_output(
802            Box::new(TableScan::from_physical_rows(physical, vec![row])),
803            &[("body".into(), 1)],
804            public,
805        )
806        .unwrap();
807        assert_eq!(output.schema(), ["body"]);
808        assert_eq!(output.row_schema().column_collation(0), Some(950));
809        assert_eq!(
810            output.row_schema().score_sources().collect::<Vec<_>>(),
811            [
812                (Some("left"), first),
813                (Some("right"), second),
814                (Some("echo"), first)
815            ]
816        );
817        assert_eq!(output.row_schema().physical_width(), 3);
818        let batches = crate::physical::run_to_batches(&mut output).unwrap();
819        for (column, slot) in [(first, 2), (second, 3)] {
820            let output_slot = batches[0].schema.internal_slot(column).unwrap();
821            assert!(std::ptr::eq(
822                batches[0].rows[0].value(output_slot).unwrap(),
823                retained.value(slot).unwrap(),
824            ));
825        }
826    }
827}
828
829mod production;