Skip to main content

uqa_sql/result/
production.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Admitted public result metadata and named rows consume already-produced positional cells.
8
9use super::{ColumnType, ResultRow, SQLResult, SQLResultKind};
10use crate::SQLError;
11use uqa_core::{
12    memory::{MemoryReservation, Produced, ProductionControl, ProductionVec},
13    Value,
14};
15
16// Completed rows and remaining cells are destroyed before their shared input lease on every failure path.
17struct Rows<'a> {
18    output: ProductionVec<'a, ResultRow>,
19    remaining: std::vec::IntoIter<Value>,
20    cells: Option<MemoryReservation>,
21}
22
23impl SQLResult {
24    /// Construct a single-column result under the cells' original allowance and cancellation owners.
25    ///
26    /// Column names, type metadata, vector capacities and live map entries acquire their leases before construction. Map accounting follows Core's exposed-payload contract: private B-tree node slack and allocator bookkeeping are excluded. The returned owner retains the admitted cells and metadata until an explicit public API handoff.
27    pub fn from_column_with_control(
28        name: &str,
29        ty: &ColumnType,
30        cells: Produced<Vec<Value>>,
31        control: &ProductionControl<'_>,
32    ) -> Result<Produced<Self>, SQLError> {
33        let mut columns = ProductionVec::new(*control);
34        columns.push_produced(control.copy_text(name)?)?;
35        let mut column_types = ProductionVec::new(*control);
36        let (ty, memory) = ty.clone_with_control(control)?.into_parts();
37        column_types.push_produced(control.finish(Some(ty), memory)?)?;
38        let (cells, cell_memory) = cells.into_parts();
39        let mut rows = Rows {
40            output: ProductionVec::new(*control),
41            remaining: cells.into_iter(),
42            cells: cell_memory,
43        };
44        for cell in rows.remaining.by_ref() {
45            let entry = control.reserve(size_of::<(String, Value)>())?;
46            let (key, key_memory) = control.copy_text(name)?.into_parts();
47            let row = ResultRow::from([(key, cell)]);
48            rows.output
49                .push_produced(control.finish(row, control.combine(entry, key_memory))?)?;
50        }
51        let columns = columns.finish()?;
52        let column_types = column_types.finish()?;
53        let output = rows.output.finish()?;
54        drop(rows.remaining);
55        let (columns, column_memory) = columns.into_parts();
56        let (column_types, type_memory) = column_types.into_parts();
57        let (output, row_memory) = output.into_parts();
58        let memory = control.combine(
59            control.combine(rows.cells, row_memory),
60            control.combine(column_memory, type_memory),
61        );
62        Ok(control.finish(
63            Self {
64                kind: SQLResultKind::Rows,
65                command_tag: None,
66                columns,
67                column_types,
68                row_schema: None,
69                rows: output,
70                positional_rows: None,
71                affected_rows: 0,
72            },
73            memory,
74        )?)
75    }
76}
77
78#[cfg(test)]
79mod tests;