uqa_sql/schema/join_output/
production.rs1use super::{ColumnIdentity, ColumnType, JoinOutputSource, RowSchema};
8use crate::{schema::production::remapping, SQLError};
9use uqa_core::memory::{Produced, ProductionControl, ProductionVec};
10
11impl JoinOutputSource {
12 pub fn clone_with_control(
13 &self,
14 control: &ProductionControl<'_>,
15 ) -> Result<Produced<Self>, SQLError> {
16 let (source, memory) = match self {
17 Self::Input(position) => (Self::Input(*position), control.empty_reservation()),
18 Self::Cast { input, ty } => {
19 let (ty, memory) = ty.clone_with_control(control)?.into_parts();
20 (Self::Cast { input: *input, ty }, memory)
21 }
22 Self::Coalesce { left, right, ty } => {
23 let (ty, memory) = ty.clone_with_control(control)?.into_parts();
24 (
25 Self::Coalesce {
26 left: *left,
27 right: *right,
28 ty,
29 },
30 memory,
31 )
32 }
33 };
34 Ok(control.finish(source, memory)?)
35 }
36 fn selected_type<'a>(&'a self, input: &'a RowSchema) -> Option<&'a ColumnType> {
37 match self {
38 Self::Input(position) => input.column_type(*position),
39 Self::Cast { ty, .. } | Self::Coalesce { ty, .. } => Some(ty),
40 }
41 }
42 fn validate(&self, width: usize) -> Result<(), SQLError> {
43 let message = match self {
44 Self::Input(position) if *position >= width => {
45 format!("join output input position {position} is outside width {width}")
46 }
47 Self::Cast { input, .. } if *input >= width => {
48 format!("join output cast position {input} is outside width {width}")
49 }
50 Self::Coalesce { left, right, .. } if *left >= width || *right >= width => format!(
51 "join output coalesce positions ({left}, {right}) are outside width {width}"
52 ),
53 _ => return Ok(()),
54 };
55 Err(SQLError::Internal(message))
56 }
57}
58
59pub fn compile_layout_with_control(
61 input: &RowSchema,
62 columns: &[(String, ColumnIdentity, JoinOutputSource)],
63 aliases: &[(ColumnIdentity, JoinOutputSource)],
64 control: &ProductionControl<'_>,
65) -> Result<(RowSchema, Produced<Vec<JoinOutputSource>>), SQLError> {
66 let mut computed = ProductionVec::<JoinOutputSource>::new(*control);
67 for source in columns
68 .iter()
69 .map(|(_, _, source)| source)
70 .chain(aliases.iter().map(|(_, source)| source))
71 {
72 control.check()?;
73 source.validate(input.len())?;
74 if !matches!(source, JoinOutputSource::Input(_)) && !computed.contains(source) {
75 computed.push_produced(source.clone_with_control(control)?)?;
76 }
77 }
78 let source_position = |source: &JoinOutputSource| match source {
79 JoinOutputSource::Input(position) => input
80 .physical_slot(*position)
81 .expect("validated join output input position has a physical slot"),
82 JoinOutputSource::Cast { .. } | JoinOutputSource::Coalesce { .. } => {
83 input.physical_width()
84 + computed
85 .iter()
86 .position(|candidate| candidate == source)
87 .expect("computed join output source was registered")
88 }
89 };
90 let schema = remapping::physical(
91 input,
92 columns.iter().map(|(name, identity, source)| {
93 (
94 name.as_str(),
95 identity,
96 source_position(source),
97 source.selected_type(input),
98 )
99 }),
100 aliases.iter().map(|(identity, source)| {
101 (
102 identity,
103 source_position(source),
104 source.selected_type(input),
105 )
106 }),
107 input.physical_width() + computed.len(),
108 control,
109 )?;
110 Ok((schema, computed.finish()?))
111}