Skip to main content

uqa_sql/result/
labels.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Host-facing results observe deferred physical datums and carry enums as their current labels, through SQL's type-output owners.
8
9use super::SQLResult;
10use crate::expr::enums::{contains_enum_carrier, render_enum_labels, EnumLabelCatalog};
11use crate::expr::observations::output_carriers;
12use crate::SQLError;
13use uqa_core::Value;
14
15#[cfg(test)]
16mod tests;
17
18/// Read deferred datums and replace enum carriers in named and positional results by their current labels. This output boundary preserves declared column types and recursive carrier metadata.
19pub fn render_result_enum_labels(
20    catalog: Option<&dyn EnumLabelCatalog>,
21    result: &mut SQLResult,
22) -> Result<(), SQLError> {
23    for row in &mut result.rows {
24        for value in row.values_mut() {
25            render_value(catalog, value)?;
26        }
27    }
28    for row in result.positional_rows.iter_mut().flatten() {
29        for value in row {
30            render_value(catalog, value)?;
31        }
32    }
33    Ok(())
34}
35
36fn render_value(catalog: Option<&dyn EnumLabelCatalog>, value: &mut Value) -> Result<(), SQLError> {
37    let carriers = output_carriers(value);
38    if carriers.datum {
39        *value = read_datums(value, catalog)?;
40    }
41    // Datum decoding can reveal an enum. Preserve the original complete datum
42    // phase before any enum-label lookup, including its first-error boundary.
43    if carriers.enumeration || (carriers.datum && contains_enum_carrier(value)) {
44        *value = render_enum_labels(catalog, value)?;
45    }
46    Ok(())
47}
48
49fn read_datums(value: &Value, catalog: Option<&dyn EnumLabelCatalog>) -> Result<Value, SQLError> {
50    Ok(match value {
51        Value::Datum(datum) => read_datums(
52            &*crate::expr::datums::read_with_value_catalog_and_control(
53                datum,
54                catalog,
55                &uqa_core::memory::ProductionControl::uncontrolled(),
56            )?,
57            catalog,
58        )?,
59        Value::Array(array) => Value::Array(
60            uqa_core::ArrayValue::with_lower_bounds(
61                array
62                    .elements()
63                    .iter()
64                    .map(|value| read_datums(value, catalog))
65                    .collect::<Result<_, _>>()?,
66                array.lower_bounds().to_vec(),
67            )
68            .ok_or_else(|| SQLError::Internal("datum output changed array shape".into()))?
69            .with_element_type_oid(array.element_type_oid()),
70        ),
71        Value::List(values) => Value::List(
72            values
73                .iter()
74                .map(|value| read_datums(value, catalog))
75                .collect::<Result<_, _>>()?,
76        ),
77        Value::Row(values) => Value::Row(
78            values.clone().with_values(
79                values
80                    .iter()
81                    .map(|value| read_datums(value, catalog))
82                    .collect::<Result<_, _>>()?,
83            )?,
84        ),
85        Value::Record(fields) => Value::Record(uqa_core::RecordValue::from_shared_parts(
86            fields
87                .values()
88                .iter()
89                .map(|value| read_datums(value, catalog))
90                .collect::<Result<_, SQLError>>()?,
91            fields.descriptor().clone(),
92        )?),
93        Value::Map(values) => Value::Map(
94            values
95                .iter()
96                .map(|(name, value)| Ok((name.clone(), read_datums(value, catalog)?)))
97                .collect::<Result<_, SQLError>>()?,
98        ),
99        other => other.clone(),
100    })
101}