Skip to main content

uqa_sql/routines/result_check/
retention.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Admit result descriptions before copying their owned names and type payloads.
8
9use super::{ColumnType, SQLFunctionResultColumn, SQLFunctionResultLayout};
10use uqa_core::{
11    memory::{Produced, ProductionControl, ProductionVec},
12    ValueRetentionError,
13};
14
15pub(crate) fn copy_types(
16    types: &[Option<ColumnType>],
17    control: &ProductionControl<'_>,
18) -> Result<Produced<Vec<Option<ColumnType>>>, ValueRetentionError> {
19    let mut result = ProductionVec::new(*control);
20    result.reserve(types.len())?;
21    for ty in types {
22        let (ty, memory) = match ty {
23            Some(ty) => {
24                let (ty, memory) = ty.clone_with_control(control)?.into_parts();
25                (Some(ty), memory)
26            }
27            None => (None, control.empty_reservation()),
28        };
29        result.push_produced(control.finish(ty, memory)?)?;
30    }
31    result.finish()
32}
33
34fn copy_columns(
35    columns: &[SQLFunctionResultColumn],
36    control: &ProductionControl<'_>,
37) -> Result<Produced<Vec<SQLFunctionResultColumn>>, ValueRetentionError> {
38    let mut result = ProductionVec::new(*control);
39    result.reserve(columns.len())?;
40    for column in columns {
41        let name = control.copy_text(&column.name)?;
42        let ty = column.ty.clone_with_control(control)?;
43        let (name, names) = name.into_parts();
44        let (ty, types) = ty.into_parts();
45        result.push_produced(control.finish(
46            SQLFunctionResultColumn { name, ty },
47            control.combine(names, types),
48        )?)?;
49    }
50    result.finish()
51}
52
53impl SQLFunctionResultLayout {
54    pub fn clone_with_control(
55        &self,
56        control: &ProductionControl<'_>,
57    ) -> Result<Produced<Self>, ValueRetentionError> {
58        let declared = self.declared_type.clone_with_control(control)?;
59        let source = self
60            .source_record
61            .as_ref()
62            .map(|types| copy_types(types, control))
63            .transpose()?;
64        let columns = self
65            .columns
66            .as_ref()
67            .map(|columns| copy_columns(columns, control))
68            .transpose()?;
69        let (declared_type, mut memory) = declared.into_parts();
70        let source_record = source.map(|source| {
71            let (source, lease) = source.into_parts();
72            memory = control.combine(memory.take(), lease);
73            source
74        });
75        let columns = columns.map(|columns| {
76            let (columns, lease) = columns.into_parts();
77            memory = control.combine(memory.take(), lease);
78            columns
79        });
80        control.finish(
81            Self {
82                kind: self.kind,
83                declared_type,
84                source_record,
85                columns,
86            },
87            memory,
88        )
89    }
90}