Skip to main content

uqa_sql/schema/relation_presence/
production.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use super::{PresenceBindings, RelationPresence};
8use crate::{
9    schema::{
10        production::lease::{boxed_text, SchemaMemory},
11        RowSchema, SchemaMap,
12    },
13    SQLError,
14};
15use std::sync::Arc;
16use uqa_core::memory::ProductionControl;
17
18impl RelationPresence {
19    pub(in crate::schema) fn compose_with_control(
20        left: &RowSchema,
21        right: &RowSchema,
22        outer: bool,
23        control: &ProductionControl<'_>,
24    ) -> Result<Self, SQLError> {
25        let bindings = SchemaMap::from_produced(
26            left.whole_row_presences()
27                .filter(|(name, _)| outer || !right.has_qualifier(name))
28                .chain(
29                    right
30                        .whole_row_presences()
31                        .filter(|(name, _)| !left.has_qualifier(name)),
32                )
33                .map(|(name, reference)| {
34                    Ok((
35                        boxed_text(name, control)?,
36                        control.finish(reference, control.empty_reservation())?,
37                    ))
38                }),
39            control,
40        )?;
41        if bindings.is_empty() {
42            return Ok(Self::default());
43        }
44        let memory = control.reserve(size_of::<PresenceBindings>())?;
45        Ok(Self(Some(Arc::new(PresenceBindings {
46            bindings,
47            _memory: SchemaMemory::new(memory),
48        }))))
49    }
50}
51
52impl RowSchema {
53    /// Admit the new presence map and complete physical layout before publishing either. Existing nested markers remain attached to their original relation scopes.
54    pub fn with_nullable_relations_with_control(
55        &self,
56        marker: crate::ast::InternalColumnRef,
57        control: &ProductionControl<'_>,
58    ) -> Result<Option<Self>, SQLError> {
59        control.check()?;
60        let mut unmarked = false;
61        for qualifier in self.relation_qualifiers() {
62            control.check()?;
63            if self.whole_row_presence(qualifier).is_none() {
64                unmarked = true;
65                break;
66            }
67        }
68        if !unmarked {
69            return Ok(None);
70        }
71        let bindings = SchemaMap::from_produced(
72            self.whole_row_presences()
73                .chain(
74                    self.relation_qualifiers()
75                        .filter(|qualifier| self.whole_row_presence(qualifier).is_none())
76                        .map(|qualifier| (qualifier, marker)),
77                )
78                .map(|(name, reference)| {
79                    Ok((
80                        boxed_text(name, control)?,
81                        control.finish(reference, control.empty_reservation())?,
82                    ))
83                }),
84            control,
85        )?;
86        let memory = control.reserve(size_of::<PresenceBindings>())?;
87        let presence = RelationPresence(Some(Arc::new(PresenceBindings {
88            bindings,
89            _memory: SchemaMemory::new(memory),
90        })));
91        let mut result = Self::project_with_sources_with_control(
92            self,
93            std::iter::empty(),
94            &[(
95                marker,
96                Some(&crate::ColumnType::Boolean),
97                crate::schema::ProjectedSlot::Computed {
98                    position: 0,
99                    collation: None,
100                },
101            )],
102            1,
103            true,
104            control,
105        )?;
106        Arc::get_mut(&mut result.index)
107            .expect("new nullable layout is not shared")
108            .cold
109            .row_presence = presence;
110        Ok(Some(result))
111    }
112}