1use 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
36pub struct ColumnSelection<'a> {
44 child: Box<dyn PhysicalOperator + 'a>,
45 schema: RowSchema,
46 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 pub fn dropping_internal_attributes(
59 child: Box<dyn PhysicalOperator + 'a>,
60 columns: &[uqa_sql::ast::InternalColumnRef],
61 ) -> Self {
62 let schema = RowSchema::without_internal_attributes(child.row_schema(), columns);
63 let ordering = child.output_ordering().to_vec();
64 Self {
65 child,
66 schema,
67 ordering,
68 rebind_lock_qualifier: None,
69 discard_lock_origins: false,
70 compact_slots: None,
71 }
72 }
73
74 pub fn new(child: Box<dyn PhysicalOperator + 'a>, columns: Vec<String>) -> Self {
75 let columns = columns
76 .into_iter()
77 .map(|column| (column.clone(), column))
78 .collect();
79 Self::with_mapping(child, columns)
80 }
81
82 pub fn with_mapping(
83 child: Box<dyn PhysicalOperator + 'a>,
84 columns: Vec<(String, String)>,
85 ) -> Self {
86 let input_positions = columns
87 .iter()
88 .map(|(_, input)| child.row_schema().position(input))
89 .collect::<Vec<_>>();
90 let ordering = remap_ordering(child.output_ordering(), &input_positions);
91 let schema = RowSchema::select(child.row_schema(), &columns);
92 Self {
93 child,
94 schema,
95 ordering,
96 rebind_lock_qualifier: None,
97 discard_lock_origins: false,
98 compact_slots: None,
99 }
100 }
101
102 pub fn with_positions(
106 child: Box<dyn PhysicalOperator + 'a>,
107 columns: Vec<(String, usize)>,
108 ) -> Self {
109 let input_positions = columns
110 .iter()
111 .map(|(_, position)| Some(*position))
112 .collect::<Vec<_>>();
113 let ordering = remap_ordering(child.output_ordering(), &input_positions);
114 let schema = RowSchema::remap_positions(child.row_schema(), &columns, &[]);
115 Self {
116 child,
117 schema,
118 ordering,
119 rebind_lock_qualifier: None,
120 discard_lock_origins: false,
121 compact_slots: None,
122 }
123 }
124
125 pub fn with_physical_positions(
128 child: Box<dyn PhysicalOperator + 'a>,
129 columns: Vec<(String, usize)>,
130 ) -> Self {
131 let input_positions = columns
132 .iter()
133 .map(|(_, physical)| {
134 (0..child.row_schema().len())
135 .find(|logical| child.row_schema().physical_slot(*logical) == Some(*physical))
136 })
137 .collect::<Vec<_>>();
138 let ordering = remap_ordering(child.output_ordering(), &input_positions);
139 let columns = columns
140 .into_iter()
141 .map(|(label, physical)| {
142 let ty = child.row_schema().physical_type(physical).cloned();
143 (
144 label.clone(),
145 ColumnIdentity::unqualified(label),
146 physical,
147 ty,
148 )
149 })
150 .collect::<Vec<_>>();
151 let schema = RowSchema::remap_typed_physical_identities(child.row_schema(), &columns, &[]);
152 Self {
153 child,
154 schema,
155 ordering,
156 rebind_lock_qualifier: None,
157 discard_lock_origins: false,
158 compact_slots: None,
159 }
160 }
161
162 pub fn with_identities(
164 child: Box<dyn PhysicalOperator + 'a>,
165 columns: Vec<(String, ColumnIdentity, usize)>,
166 ) -> Self {
167 let input_positions = columns
168 .iter()
169 .map(|(_, _, position)| Some(*position))
170 .collect::<Vec<_>>();
171 let ordering = remap_ordering(child.output_ordering(), &input_positions);
172 let columns = columns
173 .into_iter()
174 .map(|(label, identity, position)| {
175 let ty = child.row_schema().column_type(position).cloned();
176 (label, identity, position, ty)
177 })
178 .collect::<Vec<_>>();
179 let schema = RowSchema::remap_typed_identities(child.row_schema(), &columns, &[]);
180 Self {
181 child,
182 schema,
183 ordering,
184 rebind_lock_qualifier: None,
185 discard_lock_origins: false,
186 compact_slots: None,
187 }
188 }
189
190 pub fn hiding_trailing_columns(
192 child: Box<dyn PhysicalOperator + 'a>,
193 visible: usize,
194 qualifier: &str,
195 ) -> Self {
196 let visible = visible.min(child.row_schema().len());
197 let input_positions = (0..visible).map(Some).collect::<Vec<_>>();
198 let ordering = remap_ordering(child.output_ordering(), &input_positions);
199 let columns = (0..visible)
200 .map(|position| {
201 let label = child.row_schema().columns()[position].clone();
202 let identity = if qualifier.is_empty() {
203 ColumnIdentity::unqualified(&label)
204 } else {
205 ColumnIdentity::qualified(qualifier, &label)
206 };
207 let ty = child.row_schema().column_type(position).cloned();
208 (label, identity, position, ty)
209 })
210 .collect::<Vec<_>>();
211 let mut aliases = Vec::new();
212 for position in visible..child.row_schema().len() {
213 let name = child.row_schema().columns()[position].clone();
214 aliases.push((ColumnIdentity::unqualified(&name), position));
215 if !qualifier.is_empty() {
216 aliases.push((ColumnIdentity::qualified(qualifier, name), position));
217 }
218 }
219 let schema = RowSchema::remap_typed_identities(child.row_schema(), &columns, &aliases);
220 Self {
221 child,
222 schema,
223 ordering,
224 rebind_lock_qualifier: None,
225 discard_lock_origins: true,
226 compact_slots: None,
227 }
228 }
229
230 pub fn with_fresh_identities(
232 child: Box<dyn PhysicalOperator + 'a>,
233 columns: Vec<(String, ColumnIdentity, usize)>,
234 ) -> Self {
235 let input_positions = columns
236 .iter()
237 .map(|(_, _, position)| Some(*position))
238 .collect::<Vec<_>>();
239 let ordering = remap_ordering(child.output_ordering(), &input_positions);
240 let columns = columns
241 .into_iter()
242 .map(|(label, identity, position)| {
243 let ty = child.row_schema().column_type(position).cloned();
244 (label, identity, position, ty)
245 })
246 .collect::<Vec<_>>();
247 let schema =
248 RowSchema::remap_typed_identities_without_input_aliases(child.row_schema(), &columns);
249 Self {
250 child,
251 schema,
252 ordering,
253 rebind_lock_qualifier: None,
254 discard_lock_origins: false,
255 compact_slots: None,
256 }
257 }
258
259 pub fn compacting_with_positions(
261 child: Box<dyn PhysicalOperator + 'a>,
262 columns: Vec<(String, usize)>,
263 ) -> Self {
264 let input_positions = columns
265 .iter()
266 .map(|(_, position)| Some(*position))
267 .collect::<Vec<_>>();
268 let ordering = remap_ordering(child.output_ordering(), &input_positions);
269 let selected = RowSchema::remap_positions(child.row_schema(), &columns, &[]);
270 let (schema, source_slots) = selected.canonical_projection();
271 let identity_layout = child.row_schema().physical_width() == source_slots.len()
272 && source_slots
273 .iter()
274 .enumerate()
275 .all(|(position, slot)| *slot == position);
276 Self {
277 child,
278 schema,
279 ordering,
280 rebind_lock_qualifier: None,
281 discard_lock_origins: true,
282 compact_slots: (!identity_layout).then(|| source_slots.into_iter().map(Some).collect()),
283 }
284 }
285
286 pub(crate) fn with_bound_output(
288 child: Box<dyn PhysicalOperator + 'a>,
289 columns: &[(String, usize)],
290 mut schema: RowSchema,
291 ) -> ExecResult<Self> {
292 if !schema.is_identity_layout()
293 || schema.physical_width() != schema.len()
294 || columns.len() != schema.len()
295 || columns.iter().enumerate().any(|(output, (name, input))| {
296 schema.columns()[output] != *name
297 || *input >= child.row_schema().len()
298 || schema.column_type(output) != child.row_schema().column_type(*input)
299 })
300 {
301 return Err(uqa_sql::SQLError::Internal(
302 "query output does not match its analyzed columns and types".into(),
303 )
304 .into());
305 }
306 let input_positions = columns
307 .iter()
308 .map(|(_, position)| Some(*position))
309 .collect::<Vec<_>>();
310 let ordering = remap_ordering(child.output_ordering(), &input_positions);
311 let mut slots = columns
312 .iter()
313 .map(|(_, position)| child.row_schema().physical_slot(*position))
314 .collect::<Vec<_>>();
315 if child.row_schema().score_sources().next().is_some() {
316 let score_columns = child
317 .row_schema()
318 .score_sources()
319 .map(|(_, column)| column)
320 .collect::<std::collections::BTreeSet<_>>();
321 let retired = child
322 .row_schema()
323 .internal_columns_with_types()
324 .into_iter()
325 .filter_map(|(column, _, _)| (!score_columns.contains(&column)).then_some(column))
326 .collect::<Vec<_>>();
327 let scores =
328 RowSchema::remap_typed_identities_without_input_aliases(child.row_schema(), &[]);
329 let scores = RowSchema::without_internal_attributes(&scores, &retired);
330 let (scores, score_slots) = scores.canonical_projection();
331 schema = RowSchema::join(&schema, &scores, []);
332 slots.extend(score_slots.into_iter().map(Some));
333 }
334 let identity_layout = child.row_schema().physical_width() == slots.len()
335 && slots
336 .iter()
337 .enumerate()
338 .all(|(position, slot)| *slot == Some(position));
339 Ok(Self {
340 child,
341 schema,
342 ordering,
343 rebind_lock_qualifier: None,
344 discard_lock_origins: false,
345 compact_slots: (!identity_layout).then_some(slots),
346 })
347 }
348
349 #[must_use]
351 pub fn rebinding_lock_origins(mut self, qualifier: impl Into<String>) -> Self {
352 let qualifier = qualifier.into();
353 if !qualifier.is_empty() {
354 self.rebind_lock_qualifier = Some(Arc::from(qualifier));
355 }
356 self
357 }
358
359 #[must_use]
361 pub fn rebinding_score_sources(mut self, qualifier: impl Into<String>) -> Self {
362 let qualifier = qualifier.into();
363 self.schema = RowSchema::with_rebound_score_sources(
364 &self.schema,
365 (!qualifier.is_empty()).then_some(qualifier.as_str()),
366 );
367 self
368 }
369
370 #[must_use]
372 pub fn discarding_lock_origins(mut self) -> Self {
373 self.rebind_lock_qualifier = None;
374 self.discard_lock_origins = true;
375 self
376 }
377
378 #[must_use]
380 pub fn preserving_lock_origins(mut self) -> Self {
381 self.discard_lock_origins = false;
382 self
383 }
384
385 fn select_batch(&self, batch: Batch) -> Batch {
386 let mut rows = match self.compact_slots.as_ref() {
387 Some(slots) => batch
388 .rows
389 .into_iter()
390 .map(|row| {
391 row.project_with_values(slots.iter().map(|slot| {
392 slot.map_or(
393 RowProjectionValue::Owned(Value::Null),
394 RowProjectionValue::InputSlot,
395 )
396 }))
397 })
398 .collect(),
399 None => batch.rows,
400 };
401 if self.discard_lock_origins {
402 for row in &mut rows {
403 row.discard_lock_origins_mut();
404 }
405 } else if let Some(qualifier) = self.rebind_lock_qualifier.as_ref() {
406 for row in &mut rows {
407 row.rebind_lock_origin_qualifiers_mut(Arc::clone(qualifier));
408 }
409 }
410 Batch::from_physical_rows(self.schema.clone(), rows)
411 }
412}
413
414impl PhysicalOperator for ColumnSelection<'_> {
415 fn row_schema(&self) -> &RowSchema {
416 &self.schema
417 }
418
419 fn estimated_cardinality(&self) -> Option<u64> {
420 self.child.estimated_cardinality()
421 }
422
423 fn output_ordering(&self) -> &[PhysicalOrder] {
424 &self.ordering
425 }
426
427 fn backward_scan_support(&self) -> crate::BackwardScanSupport {
428 self.child.backward_scan_support()
429 }
430
431 fn open(&mut self) -> ExecResult<()> {
432 self.child.open()
433 }
434
435 fn next(&mut self) -> ExecResult<Option<Batch>> {
436 let Some(batch) = self.child.next()? else {
437 return Ok(None);
438 };
439 Ok(Some(self.select_batch(batch)))
440 }
441
442 fn next_direction(
443 &mut self,
444 direction: crate::PhysicalScanDirection,
445 ) -> ExecResult<Option<Batch>> {
446 let Some(batch) = self.child.next_direction(direction)? else {
447 return Ok(None);
448 };
449 Ok(Some(self.select_batch(batch)))
450 }
451
452 fn rewind(&mut self) -> ExecResult<()> {
453 self.child.rewind()
454 }
455
456 fn close(&mut self) -> ExecResult<()> {
457 self.child.close()
458 }
459}
460
461#[cfg(test)]
462mod tests {
463 use crate::RowSchemaExecution;
464 use std::collections::BTreeMap;
465
466 use uqa_core::Value;
467
468 use super::*;
469 use crate::physical::run_to_rows;
470 use crate::scan::TableScan;
471
472 #[test]
473 fn selects_computed_columns_without_leaking_sort_inputs() {
474 let row = BTreeMap::from([
475 ("source".to_string(), Value::Int(1)),
476 ("alias".to_string(), Value::Int(2)),
477 ]);
478 let scan = TableScan::from_rows(vec!["source".into(), "alias".into()], vec![row]);
479 let mut selection = ColumnSelection::new(Box::new(scan), vec!["alias".into()]);
480 let (schema, rows) = run_to_rows(&mut selection).unwrap();
481 assert_eq!(schema, vec!["alias"]);
482 assert_eq!(rows[0], BTreeMap::from([("alias".into(), Value::Int(2))]));
483 }
484
485 #[test]
486 fn renames_collision_free_physical_columns() {
487 let row = BTreeMap::from([
488 ("source".to_string(), Value::Int(1)),
489 ("__projection_0".to_string(), Value::Int(2)),
490 ]);
491 let scan = TableScan::from_rows(vec!["source".into(), "__projection_0".into()], vec![row]);
492 let mut selection = ColumnSelection::with_mapping(
493 Box::new(scan),
494 vec![("source".into(), "__projection_0".into())],
495 );
496 let (schema, rows) = run_to_rows(&mut selection).unwrap();
497 assert_eq!(schema, vec!["source"]);
498 assert_eq!(rows[0], BTreeMap::from([("source".into(), Value::Int(2))]));
499 }
500
501 #[test]
502 fn renames_repeated_columns_by_position() {
503 let scan = TableScan::from_rows(
504 vec!["left.value".into(), "right.value".into()],
505 vec![BTreeMap::from([
506 ("left.value".into(), Value::Int(1)),
507 ("right.value".into(), Value::Int(2)),
508 ])],
509 );
510 let mut selection = ColumnSelection::with_positions(
511 Box::new(scan),
512 vec![("value".into(), 0), ("value".into(), 1)],
513 );
514 let batches = crate::physical::run_to_batches(&mut selection).unwrap();
515 assert_eq!(batches[0].schema.columns(), ["value", "value"]);
516 let row = batches[0].schema.view(&batches[0].rows[0]);
517 assert_eq!(row.value_at(0), Some(&Value::Int(1)));
518 assert_eq!(row.value_at(1), Some(&Value::Int(2)));
519 }
520
521 #[test]
522 fn ordering_is_remapped_by_selected_position() {
523 let ordering = vec![PhysicalOrder {
524 position: 2,
525 descending: false,
526 nulls_first: None,
527 nullable: false,
528 }];
529 let remapped = remap_ordering(&ordering, &[Some(2), Some(0)]);
530 assert_eq!(remapped[0].position, 0);
531 assert_eq!(remap_ordering(&ordering, &[Some(0), Some(1)]).len(), 0);
532 }
533
534 #[test]
535 fn row_identity_barrier_discards_lock_origins_in_place() {
536 let schema = RowSchema::new(vec!["id".into()]);
537 let row = crate::PhysicalRow::from_values(vec![Value::Int(1)])
538 .with_lock_origin(crate::RowLockOrigin::new("accounts", "public.accounts", 1));
539 let scan = TableScan::from_physical_rows(schema, vec![row]);
540 let mut barrier = ColumnSelection::with_positions(Box::new(scan), vec![("id".into(), 0)])
541 .discarding_lock_origins();
542
543 let batches = crate::physical::run_to_batches(&mut barrier).unwrap();
544 assert_eq!(batches[0].rows[0].lock_origins().len(), 0);
545 }
546
547 #[test]
548 fn explicit_compaction_canonicalizes_a_wider_physical_layout() {
549 let source = RowSchema::new(vec!["unused".into(), "value".into()]);
550 let selected = RowSchema::select(&source, &[("value".into(), "value".into())]);
551 let row = crate::PhysicalRow::from_values(vec![Value::Int(1), Value::Int(7)]);
552 let scan = TableScan::from_physical_rows(selected, vec![row]);
553 let mut compact =
554 ColumnSelection::compacting_with_positions(Box::new(scan), vec![("value".into(), 0)]);
555
556 compact.open().unwrap();
557 let batch = compact.next().unwrap().unwrap();
558 compact.close().unwrap();
559
560 assert_eq!(batch.schema.physical_width(), 1);
561 assert_eq!(
562 batch.schema.view(&batch.rows[0]).get("value"),
563 Some(&Value::Int(7))
564 );
565 }
566
567 #[test]
568 fn bound_output_preserves_metadata_shared_values_and_lock_origins() {
569 use uqa_sql::ColumnType;
570
571 let fields: uqa_sql::schema::RecordFields =
572 Arc::from([Some(ColumnType::SmallInteger), Some(ColumnType::Text)]);
573 let bound = RowSchema::with_types(
574 vec!["first".into(), "second".into(), "record".into()],
575 vec![
576 Some(ColumnType::Text),
577 Some(ColumnType::Text),
578 Some(ColumnType::Record),
579 ],
580 )
581 .with_column_collations([Some(950), Some(951), None])
582 .with_record_fields([(2, Arc::clone(&fields))]);
583 let input = RowSchema::with_types(
584 vec!["hidden".into(), "text".into(), "record".into()],
585 vec![
586 Some(ColumnType::Integer),
587 Some(ColumnType::Text),
588 Some(ColumnType::Record),
589 ],
590 );
591 let row = crate::PhysicalRow::from_values(vec![
592 Value::Int(99),
593 Value::Str("shared".repeat(1_000)),
594 Value::Record(vec![("n".into(), Value::Int(7)), ("s".into(), Value::Null)].into()),
595 ])
596 .with_lock_origin(crate::RowLockOrigin::new("items", "public.items", 1));
597 let retained = row.clone();
598 let mut output = ColumnSelection::with_bound_output(
599 Box::new(TableScan::from_physical_rows(input, vec![row])),
600 &[
601 ("first".into(), 1),
602 ("second".into(), 1),
603 ("record".into(), 2),
604 ],
605 bound.clone(),
606 )
607 .unwrap();
608 assert_eq!(output.row_schema(), &bound);
609 let batches = crate::physical::run_to_batches(&mut output).unwrap();
610 assert_eq!(batches.len(), 1);
611 assert_eq!(batches[0].schema, bound);
612 let result = &batches[0].rows[0];
613 for (output, input) in [(0, 1), (1, 1), (2, 2)] {
614 assert!(std::ptr::eq(
615 result.value(output).unwrap(),
616 retained.value(input).unwrap()
617 ));
618 }
619 assert_eq!(result.lock_origins().len(), 1);
620 assert!(Arc::ptr_eq(
621 batches[0].schema.record_fields(2).unwrap(),
622 &fields
623 ));
624 }
625
626 #[test]
627 fn bound_output_rejects_changed_columns_types_and_nonpositional_descriptors() {
628 use uqa_sql::ColumnType;
629
630 let input = RowSchema::with_types(vec!["value".into()], vec![Some(ColumnType::Integer)]);
631 let other_type = RowSchema::with_types(vec!["value".into()], vec![Some(ColumnType::Text)]);
632 let wider = RowSchema::with_types(
633 vec!["hidden".into(), "value".into()],
634 vec![Some(ColumnType::Integer); 2],
635 );
636 let nonpositional = RowSchema::select(&wider, &[("value".into(), "value".into())]);
637 for (columns, bound) in [
638 (vec![("renamed".into(), 0)], input.clone()),
639 (vec![], input.clone()),
640 (vec![("value".into(), 1)], input.clone()),
641 (vec![("value".into(), 0)], other_type),
642 (vec![("value".into(), 0)], nonpositional),
643 ] {
644 let child = Box::new(TableScan::from_physical_rows(input.clone(), vec![]));
645 assert!(ColumnSelection::with_bound_output(child, &columns, bound).is_err());
646 }
647 }
648
649 #[test]
650 fn bound_output_preserves_scores_without_exposing_internal_columns() {
651 use uqa_sql::ColumnType;
652
653 let public = RowSchema::with_types(vec!["body".into()], vec![Some(ColumnType::Text)])
654 .with_column_collations([Some(950)]);
655 let first = uqa_sql::ast::InternalRelationId::allocate().column(0);
656 let second = uqa_sql::ast::InternalRelationId::allocate().column(0);
657 let physical = RowSchema::from_physical_layout(uqa_sql::schema::PhysicalLayout {
658 columns: vec!["unused".into(), "body".into()],
659 identities: vec![
660 ColumnIdentity::unqualified("unused"),
661 ColumnIdentity::unqualified("body"),
662 ],
663 types: vec![Some(ColumnType::Integer), Some(ColumnType::Text)],
664 slots: vec![Some(0), Some(1)],
665 physical_width: 4,
666 aliases: vec![],
667 internal: vec![
668 (first, Some(2), Some(ColumnType::DoublePrecision)),
669 (second, Some(3), Some(ColumnType::DoublePrecision)),
670 ],
671 score_sources: vec![
672 (Some("left".into()), first),
673 (Some("right".into()), second),
674 (Some("echo".into()), first),
675 ],
676 wildcard_hidden: std::collections::HashSet::new(),
677 })
678 .unwrap();
679 let row = crate::PhysicalRow::from_values(vec![
680 Value::Int(99),
681 Value::Str("rust".into()),
682 Value::Float(1.25),
683 Value::Float(2.5),
684 ]);
685 let retained = row.clone();
686 let mut output = ColumnSelection::with_bound_output(
687 Box::new(TableScan::from_physical_rows(physical, vec![row])),
688 &[("body".into(), 1)],
689 public,
690 )
691 .unwrap();
692 assert_eq!(output.schema(), ["body"]);
693 assert_eq!(output.row_schema().column_collation(0), Some(950));
694 assert_eq!(
695 output.row_schema().score_sources().collect::<Vec<_>>(),
696 [
697 (Some("left"), first),
698 (Some("right"), second),
699 (Some("echo"), first)
700 ]
701 );
702 assert_eq!(output.row_schema().physical_width(), 3);
703 let batches = crate::physical::run_to_batches(&mut output).unwrap();
704 for (column, slot) in [(first, 2), (second, 3)] {
705 let output_slot = batches[0].schema.internal_slot(column).unwrap();
706 assert!(std::ptr::eq(
707 batches[0].rows[0].value(output_slot).unwrap(),
708 retained.value(slot).unwrap(),
709 ));
710 }
711 }
712}