uqa_sql/schema/production/
captured_output.rs1use super::{copy, lease::SchemaMemory, optional_type, parts};
10use crate::{
11 ast::InternalRelationId,
12 schema::{RowSchema, SchemaBuildMetadata, SchemaMap},
13 SQLError,
14};
15use uqa_core::memory::ProductionControl;
16
17impl RowSchema {
18 pub fn capture_output_with_control(
19 input: &Self,
20 relation: InternalRelationId,
21 control: &ProductionControl<'_>,
22 ) -> Result<Self, SQLError> {
23 control.check()?;
24 let mut collations = input.index.cold.collations.copy_with_control(control)?;
25 collations.clear_columns();
26 let mut has_null = false;
27 for position in 0..input.len() {
28 control.check()?;
29 has_null |= input.physical_slot(position).is_none();
30 collations.internal_with_control(
31 relation.column(position),
32 input.column_collation(position),
33 input.column_type(position),
34 control,
35 )?;
36 }
37 let internal = SchemaMap::from_produced(
38 input
39 .index
40 .executor_attributes
41 .iter()
42 .map(|(column, slot)| {
43 Ok((
44 control.finish(*column, control.empty_reservation())?,
45 control.finish(*slot, control.empty_reservation())?,
46 ))
47 })
48 .chain((0..input.len()).map(|position| {
49 Ok((
50 control.finish(relation.column(position), control.empty_reservation())?,
51 control.finish(
52 input
53 .physical_slot(position)
54 .unwrap_or(input.physical_width()),
55 control.empty_reservation(),
56 )?,
57 ))
58 })),
59 control,
60 )?;
61 let internal_types = SchemaMap::from_produced(
62 input
63 .index
64 .cold
65 .executor_attribute_types
66 .iter()
67 .map(|(column, ty)| {
68 Ok((
69 control.finish(*column, control.empty_reservation())?,
70 optional_type(ty.as_ref(), control)?,
71 ))
72 })
73 .chain((0..input.len()).map(|position| {
74 Ok((
75 control.finish(relation.column(position), control.empty_reservation())?,
76 optional_type(input.column_type(position), control)?,
77 ))
78 })),
79 control,
80 )?;
81 let record_fields = SchemaMap::from_produced(
82 input.index.cold.record_fields.iter().map(|(slot, record)| {
83 Ok((
84 control.finish(*slot, control.empty_reservation())?,
85 control.finish(record.clone(), control.empty_reservation())?,
86 ))
87 }),
88 control,
89 )?;
90 let (score_sources, score_memory) = copy::score_sources(input, control)?.into_parts();
91 let metadata = SchemaBuildMetadata {
92 collations,
93 internal,
94 internal_types,
95 record_fields,
96 score_sources,
97 memory: SchemaMemory::new(score_memory),
98 ..SchemaBuildMetadata::empty_with_control(control)
99 };
100 parts::produce(
101 control.finish(Vec::new(), control.empty_reservation())?,
102 control.finish(Vec::new(), control.empty_reservation())?,
103 control.finish(Vec::new(), control.empty_reservation())?,
104 control.finish(Vec::new(), control.empty_reservation())?,
105 input.physical_width() + usize::from(has_null),
106 metadata,
107 control,
108 )
109 }
110}