Skip to main content

uqa_sql/plan/
source_metadata.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Declared metadata of SQL-invisible VALUES inputs, including empty carriers.
8
9use super::SourcePlan;
10use crate::{RowSchema, SQLError};
11
12#[cfg(test)]
13mod tests;
14
15impl SourcePlan {
16    /// Recover an internal VALUES schema from its declarations without sampling a row or exposing SQL-visible attributes.
17    pub fn internal_values_schema(&self) -> Result<Option<RowSchema>, SQLError> {
18        let Self::Values {
19            internal_relation: Some(relation),
20            internal_column_types: types,
21            internal_column_collations: collations,
22            ..
23        } = self
24        else {
25            return Ok(None);
26        };
27        if !collations.is_empty() && collations.len() != types.len() {
28            return Err(SQLError::Internal(
29                "internal VALUES collation width does not match its declared attributes".into(),
30            ));
31        }
32        for (ty, oid) in types.iter().zip(collations) {
33            if oid.is_some_and(|oid| oid != 0)
34                && !ty
35                    .as_ref()
36                    .is_some_and(crate::schema::composites::type_is_collatable)
37            {
38                return Err(SQLError::Internal(
39                    "internal VALUES collation requires a collatable attribute type".into(),
40                ));
41            }
42        }
43        Ok(Some(RowSchema::with_internal_relation_metadata(
44            *relation,
45            types
46                .iter()
47                .cloned()
48                .enumerate()
49                .map(|(position, ty)| (ty, collations.get(position).copied().flatten())),
50        )))
51    }
52}