Skip to main content

uqa_sql/schema/record_fields/
ownership.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Field arrays and selectable record descriptors retain their own leases independently of every schema that references them.
8
9use super::{ColumnType, RecordDescriptor, RecordFieldMetadata};
10use crate::SQLError;
11use std::sync::Arc;
12use uqa_core::memory::{MemoryReservation, Produced, ProductionControl, ProductionVec};
13
14#[derive(Clone)]
15pub struct RecordFields(Storage);
16#[derive(Clone)]
17enum Storage {
18    Ordinary(Arc<[Option<ColumnType>]>),
19    Produced(Arc<Produced<Vec<Option<ColumnType>>>>),
20}
21impl RecordFields {
22    pub fn from_produced(
23        types: Produced<Vec<Option<ColumnType>>>,
24        control: &ProductionControl<'_>,
25    ) -> Result<Self, SQLError> {
26        Ok(Self(Storage::Produced(types.into_shared(control)?)))
27    }
28    pub fn from_types_with_control<'a>(
29        types: impl IntoIterator<Item = Option<&'a ColumnType>>,
30        control: &ProductionControl<'_>,
31    ) -> Result<Self, SQLError> {
32        control.check()?;
33        let mut output = ProductionVec::new(*control);
34        for ty in types {
35            let ty = if let Some(ty) = ty {
36                let (ty, memory) = ty.clone_with_control(control)?.into_parts();
37                control.finish(Some(ty), memory)?
38            } else {
39                control.finish(None, control.empty_reservation())?
40            };
41            output.push_produced(ty)?;
42        }
43        Self::from_produced(output.finish()?, control)
44    }
45    pub fn shares_storage(&self, other: &Self) -> bool {
46        match (&self.0, &other.0) {
47            (Storage::Ordinary(left), Storage::Ordinary(right)) => Arc::ptr_eq(left, right),
48            (Storage::Produced(left), Storage::Produced(right)) => Arc::ptr_eq(left, right),
49            _ => false,
50        }
51    }
52}
53impl std::ops::Deref for RecordFields {
54    type Target = [Option<ColumnType>];
55    fn deref(&self) -> &Self::Target {
56        match &self.0 {
57            Storage::Ordinary(types) => types,
58            Storage::Produced(types) => types,
59        }
60    }
61}
62impl AsRef<[Option<ColumnType>]> for RecordFields {
63    fn as_ref(&self) -> &[Option<ColumnType>] {
64        self
65    }
66}
67impl std::fmt::Debug for RecordFields {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        self.as_ref().fmt(f)
70    }
71}
72impl PartialEq for RecordFields {
73    fn eq(&self, other: &Self) -> bool {
74        self.as_ref() == other.as_ref()
75    }
76}
77impl Eq for RecordFields {}
78impl From<Vec<Option<ColumnType>>> for RecordFields {
79    fn from(types: Vec<Option<ColumnType>>) -> Self {
80        Self(Storage::Ordinary(types.into()))
81    }
82}
83impl From<Arc<[Option<ColumnType>]>> for RecordFields {
84    fn from(types: Arc<[Option<ColumnType>]>) -> Self {
85        Self(Storage::Ordinary(types))
86    }
87}
88impl FromIterator<Option<ColumnType>> for RecordFields {
89    fn from_iter<I: IntoIterator<Item = Option<ColumnType>>>(types: I) -> Self {
90        types.into_iter().collect::<Vec<_>>().into()
91    }
92}
93
94#[derive(Debug, Default)]
95pub(crate) struct RecordMemory {
96    _memory: Option<MemoryReservation>,
97}
98impl PartialEq for RecordMemory {
99    fn eq(&self, _: &Self) -> bool {
100        true
101    }
102}
103impl Eq for RecordMemory {}
104
105impl RecordDescriptor {
106    pub(crate) fn from_parts_with_control(
107        types: RecordFields,
108        names: Option<Produced<Vec<String>>>,
109        fields: Produced<Vec<RecordFieldMetadata>>,
110        control: &ProductionControl<'_>,
111    ) -> Result<Arc<Self>, SQLError> {
112        control.check()?;
113        let names = names
114            .map(|names| crate::schema::production::lease::boxed_slice(names, control))
115            .transpose()?;
116        let fields = crate::schema::production::lease::boxed_slice(fields, control)?;
117        let header = control.reserve(size_of::<Self>())?;
118        let (names, names_memory) = match names {
119            Some(names) => {
120                let (names, memory) = names.into_parts();
121                (Some(names), memory)
122            }
123            None => (None, control.empty_reservation()),
124        };
125        let (fields, fields_memory) = fields.into_parts();
126        Ok(Arc::new(Self {
127            types,
128            names,
129            fields,
130            memory: RecordMemory {
131                _memory: control.combine(header, control.combine(names_memory, fields_memory)),
132            },
133        }))
134    }
135}