Skip to main content

uqa_sql/schema/production/composition/
row_images.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Row-image names share selected target metadata while their hidden physical positions remain distinct.
8
9use super::{
10    copy, inline, optional_type, ColumnIdentity, Layout, ProductionControl, RowSchema, SQLError,
11    SchemaMap, SchemaSet,
12};
13
14impl RowSchema {
15    pub fn with_row_image_aliases_with_control(
16        target: &Self,
17        composite_width: usize,
18        old: &str,
19        new: &str,
20        control: &ProductionControl<'_>,
21    ) -> Result<Self, SQLError> {
22        let width = target.len();
23        assert_eq!(
24            width,
25            target.physical_width(),
26            "RETURNING target must be positional"
27        );
28        assert!(
29            composite_width <= width,
30            "RETURNING user columns must belong to the target"
31        );
32        let mut layout = Layout::new(control);
33        layout.append(target, 0, None, control)?;
34        let mut metadata = copy::metadata(target, control)?;
35        metadata.wildcard_hidden = SchemaSet::from_produced(
36            (composite_width..width).map(|slot| inline(slot, control)),
37            control,
38        )?;
39        let selected = || {
40            target
41                .columns()
42                .iter()
43                .enumerate()
44                .flat_map(|(position, column)| {
45                    [
46                        (old, column.as_str(), width + position, position),
47                        (new, column.as_str(), width * 2 + position, position),
48                    ]
49                })
50        };
51        metadata.aliases = SchemaMap::from_produced(
52            target
53                .index
54                .aliases
55                .iter()
56                .map(|(identity, slot)| {
57                    Ok((
58                        identity.clone_with_control(control)?,
59                        inline(*slot, control)?,
60                    ))
61                })
62                .chain(selected().map(|(qualifier, column, slot, _)| {
63                    Ok((
64                        ColumnIdentity::from_parts_with_control(Some(qualifier), column, control)?,
65                        inline(slot, control)?,
66                    ))
67                })),
68            control,
69        )?;
70        metadata.alias_types = SchemaMap::from_produced(
71            target
72                .index
73                .cold
74                .aliases
75                .iter()
76                .map(|(identity, ty)| {
77                    Ok((
78                        identity.clone_with_control(control)?,
79                        optional_type(ty.as_ref(), control)?,
80                    ))
81                })
82                .chain(selected().map(|(qualifier, column, _, position)| {
83                    Ok((
84                        ColumnIdentity::from_parts_with_control(Some(qualifier), column, control)?,
85                        optional_type(target.column_type(position), control)?,
86                    ))
87                })),
88            control,
89        )?;
90        let mut collations = crate::schema::collations::composition::Selection::new(
91            &target.index.cold.collations,
92            control,
93        )?;
94        for (qualifier, column, _, position) in selected() {
95            collations.alias(
96                (Some(qualifier), column),
97                target.column_collation(position),
98                target.column_type(position),
99            )?;
100        }
101        metadata.collations = collations.finish(control)?;
102        layout.finish(
103            width
104                .checked_mul(3)
105                .ok_or_else(|| SQLError::Internal("RETURNING row width overflow".into()))?,
106            metadata,
107            control,
108        )
109    }
110}