uqa_sql/schema/production/
captured_aliases.rs1use crate::{
10 ast::InternalColumnRef,
11 schema::{ColumnIdentity, RowSchema, SchemaMap, NULL_SLOT},
12 ColumnType, SQLError,
13};
14use uqa_core::memory::{ProductionControl, ProductionMap};
15
16type Alias<'a> = (usize, Option<&'a ColumnType>, Option<u32>);
17
18impl RowSchema {
19 pub fn with_physical_internal_aliases_with_control<'a>(
20 input: &'a Self,
21 aliases: impl IntoIterator<Item = (InternalColumnRef, usize, Option<&'a ColumnType>)>,
22 control: &ProductionControl<'_>,
23 ) -> Result<Self, SQLError> {
24 let mut selected = ProductionMap::new(*control);
25 for (column, slot, ty) in aliases {
26 assert!(
27 slot < input.physical_width(),
28 "physical internal alias is outside row width"
29 );
30 selected.insert_copy(column, (slot, ty, input.inherited_physical_collation(slot)))?;
31 }
32 publish(input, &selected, control, |_| Ok(()))
33 }
34
35 pub fn with_named_internal_aliases_with_control(
36 input: &Self,
37 aliases: &[(InternalColumnRef, &ColumnIdentity)],
38 control: &ProductionControl<'_>,
39 ) -> Result<Self, SQLError> {
40 let mut selected = ProductionMap::new(*control);
41 for (column, identity) in aliases {
42 control.check()?;
43 if input.has_internal_column(*column) || identity.qualifier().is_none() {
44 continue;
45 }
46 let exists = input.index.qualified.contains_key(*identity)
47 || input.index.aliases.contains_key(*identity)
48 || input.index.cold.binding_only.contains_key(*identity);
49 if !exists {
50 continue;
51 }
52 let (slot, ty) = if input.index.ambiguous_qualified.contains(*identity) {
53 (NULL_SLOT, None)
54 } else {
55 (
56 input
57 .index
58 .qualified
59 .get(*identity)
60 .and_then(|logical| input.slot(*logical))
61 .or_else(|| input.index.aliases.get(*identity).copied())
62 .unwrap_or(NULL_SLOT),
63 input
64 .index
65 .qualified
66 .get(*identity)
67 .and_then(|logical| input.column_type(*logical))
68 .or_else(|| {
69 input
70 .index
71 .cold
72 .aliases
73 .get(*identity)
74 .and_then(Option::as_ref)
75 })
76 .or_else(|| {
77 input
78 .index
79 .cold
80 .binding_only
81 .get(*identity)
82 .and_then(Option::as_ref)
83 }),
84 )
85 };
86 selected.insert_copy(
87 *column,
88 (slot, ty, input.qualified_identity_collation(identity)),
89 )?;
90 }
91 publish(input, &selected, control, |metadata| {
92 for (column, identity) in aliases {
93 if !input.has_internal_column(*column) && identity.qualifier().is_some() {
94 metadata.collations.internal_with_control(
95 *column,
96 input.qualified_identity_collation(identity),
97 input.qualified_identity_type(identity),
98 control,
99 )?;
100 }
101 }
102 Ok(())
103 })
104 }
105}
106
107fn publish(
108 input: &RowSchema,
109 selected: &ProductionMap<'_, InternalColumnRef, Alias<'_>>,
110 control: &ProductionControl<'_>,
111 complete: impl FnOnce(&mut crate::schema::SchemaBuildMetadata) -> Result<(), SQLError>,
112) -> Result<RowSchema, SQLError> {
113 let mut metadata = super::copy::metadata(input, control)?;
114 metadata.internal = SchemaMap::from_produced(
115 input
116 .index
117 .executor_attributes
118 .iter()
119 .filter(|(column, _)| selected.get(column).is_none())
120 .map(|(column, slot)| {
121 Ok((
122 control.finish(*column, control.empty_reservation())?,
123 control.finish(*slot, control.empty_reservation())?,
124 ))
125 })
126 .chain(selected.iter().map(|(column, (slot, _, _))| {
127 Ok((
128 control.finish(*column, control.empty_reservation())?,
129 control.finish(*slot, control.empty_reservation())?,
130 ))
131 })),
132 control,
133 )?;
134 metadata.internal_types = SchemaMap::from_produced(
135 input
136 .index
137 .cold
138 .executor_attribute_types
139 .iter()
140 .filter(|(column, _)| selected.get(column).is_none())
141 .map(|(column, ty)| {
142 Ok((
143 control.finish(*column, control.empty_reservation())?,
144 super::optional_type(ty.as_ref(), control)?,
145 ))
146 })
147 .chain(selected.iter().map(|(column, (_, ty, _))| {
148 Ok((
149 control.finish(*column, control.empty_reservation())?,
150 super::optional_type(*ty, control)?,
151 ))
152 })),
153 control,
154 )?;
155 for (column, (_, ty, oid)) in selected {
156 metadata
157 .collations
158 .internal_with_control(*column, *oid, *ty, control)?;
159 }
160 complete(&mut metadata)?;
161 super::copy::with_metadata(input, metadata, control)
162}