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