Skip to main content

uqa_sql/schema/production/
internal_aliases.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Trailing routine identities preserve physical slots and carry independent controlled lookup metadata.
8
9use crate::{
10    schema::{RowSchema, SchemaMap, NULL_SLOT},
11    SQLError,
12};
13use uqa_core::memory::ProductionControl;
14
15impl RowSchema {
16    pub fn with_trailing_internal_aliases_with_control(
17        input: &Self,
18        trailing: &Self,
19        control: &ProductionControl<'_>,
20    ) -> Result<Self, SQLError> {
21        control.check()?;
22        let offset = input
23            .physical_width()
24            .checked_sub(trailing.physical_width())
25            .expect("trailing internal-alias scope exceeds input row width");
26        for (column, slot) in &trailing.index.executor_attributes {
27            control.check()?;
28            if let Some(previous) = input.index.executor_attributes.get(column) {
29                assert_eq!(
30                    *previous,
31                    shifted(*slot, offset),
32                    "structural identity moved between row scopes"
33                );
34            }
35        }
36        for (column, ty) in &trailing.index.cold.executor_attribute_types {
37            control.check()?;
38            if let Some(previous) = input.index.cold.executor_attribute_types.get(column) {
39                assert_eq!(
40                    previous, ty,
41                    "structural identity type changed between row scopes"
42                );
43            }
44        }
45        let mut metadata = super::copy::metadata(input, control)?;
46        metadata.internal = SchemaMap::from_produced(
47            input
48                .index
49                .executor_attributes
50                .iter()
51                .map(|(column, slot)| {
52                    Ok((
53                        control.finish(*column, control.empty_reservation())?,
54                        control.finish(*slot, control.empty_reservation())?,
55                    ))
56                })
57                .chain(
58                    trailing
59                        .index
60                        .executor_attributes
61                        .iter()
62                        .filter(|(column, _)| {
63                            !input.index.executor_attributes.contains_key(*column)
64                        })
65                        .map(|(column, slot)| {
66                            Ok((
67                                control.finish(*column, control.empty_reservation())?,
68                                control
69                                    .finish(shifted(*slot, offset), control.empty_reservation())?,
70                            ))
71                        }),
72                ),
73            control,
74        )?;
75        metadata.internal_types = SchemaMap::from_produced(
76            input
77                .index
78                .cold
79                .executor_attribute_types
80                .iter()
81                .map(|(column, ty)| {
82                    Ok((
83                        control.finish(*column, control.empty_reservation())?,
84                        super::optional_type(ty.as_ref(), control)?,
85                    ))
86                })
87                .chain(
88                    trailing
89                        .index
90                        .cold
91                        .executor_attribute_types
92                        .iter()
93                        .filter(|(column, _)| {
94                            !input
95                                .index
96                                .cold
97                                .executor_attribute_types
98                                .contains_key(*column)
99                        })
100                        .map(|(column, ty)| {
101                            Ok((
102                                control.finish(*column, control.empty_reservation())?,
103                                super::optional_type(ty.as_ref(), control)?,
104                            ))
105                        }),
106                ),
107            control,
108        )?;
109        for (column, ty) in &trailing.index.cold.executor_attribute_types {
110            metadata.collations.internal_with_control(
111                *column,
112                trailing.internal_collation(*column),
113                ty.as_ref(),
114                control,
115            )?;
116        }
117        super::copy::with_metadata(input, metadata, control)
118    }
119}
120
121fn shifted(slot: usize, offset: usize) -> usize {
122    if slot == NULL_SLOT {
123        NULL_SLOT
124    } else {
125        offset + slot
126    }
127}