uqa_sql/schema/production/
rebinding.rs1use super::{optional_type, parts};
10use crate::{
11 schema::{ColumnIdentity, RowSchema, SchemaBuildMetadata, SchemaMap},
12 SQLError,
13};
14use uqa_core::memory::{ProductionControl, ProductionMap, ProductionVec};
15
16impl RowSchema {
17 pub fn with_output_names_with_control<'a>(
19 input: &Self,
20 names: impl IntoIterator<Item = &'a str>,
21 control: &ProductionControl<'_>,
22 ) -> Result<Self, SQLError> {
23 output(
24 input,
25 names,
26 SchemaBuildMetadata::empty_with_control(control),
27 control,
28 )
29 }
30
31 pub fn with_recursive_input_with_control(
33 input: &Self,
34 visible: usize,
35 control: &ProductionControl<'_>,
36 ) -> Result<Self, SQLError> {
37 control.check()?;
38 let visible = visible.min(input.len());
39 let mut first = ProductionMap::new(*control);
40 for (position, name) in input.columns().iter().enumerate() {
41 control.check()?;
42 if first.get(&name.as_str()).is_none() {
43 first.insert_copy(name.as_str(), position)?;
44 }
45 }
46 let binding_only = SchemaMap::from_produced(
47 first
48 .iter()
49 .filter(|(_, position)| **position >= visible)
50 .map(|(name, position)| {
51 Ok((
52 ColumnIdentity::from_parts_with_control(None, name, control)?,
53 optional_type(input.column_type(*position), control)?,
54 ))
55 }),
56 control,
57 )?;
58 drop(first);
59 output(
60 input,
61 input.columns()[..visible].iter().map(String::as_str),
62 SchemaBuildMetadata {
63 binding_only,
64 ..SchemaBuildMetadata::empty_with_control(control)
65 },
66 control,
67 )
68 }
69}
70
71fn output<'a>(
72 input: &RowSchema,
73 names: impl IntoIterator<Item = &'a str>,
74 mut metadata: SchemaBuildMetadata,
75 control: &ProductionControl<'_>,
76) -> Result<RowSchema, SQLError> {
77 control.check()?;
78 let mut columns = ProductionVec::new(*control);
79 let mut identities = ProductionVec::new(*control);
80 let mut types = ProductionVec::new(*control);
81 let mut slots = ProductionVec::new(*control);
82 for (position, name) in names.into_iter().enumerate() {
83 control.check()?;
84 let ty = input.column_type(position);
85 columns.push_produced(control.copy_text(name)?)?;
86 identities.push_produced(ColumnIdentity::from_parts_with_control(
87 None, name, control,
88 )?)?;
89 types.push_produced(optional_type(ty, control)?)?;
90 slots.push_copy(position)?;
91 metadata.collations.column_with_control(
92 position,
93 input.column_collation(position),
94 ty,
95 control,
96 )?;
97 }
98 let width = columns.len();
99 metadata.record_fields = SchemaMap::from_produced(
100 (0..width.min(input.len()))
101 .filter_map(|position| {
102 input
103 .record_metadata(position)
104 .map(|record| (position, record))
105 })
106 .map(|(position, record)| {
107 Ok((
108 control.finish(position, control.empty_reservation())?,
109 control.finish(record.clone(), control.empty_reservation())?,
110 ))
111 }),
112 control,
113 )?;
114 parts::produce(
115 columns.finish()?,
116 identities.finish()?,
117 types.finish()?,
118 slots.finish()?,
119 width,
120 metadata,
121 control,
122 )
123}