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    /// Explicit LATERAL permission retained by a range item independently of its parent join.
17    pub fn is_explicitly_lateral(&self) -> bool {
18        matches!(
19            self,
20            Self::Values { lateral: true, .. } | Self::Subquery { lateral: true, .. }
21        )
22    }
23
24    /// Recover an internal VALUES schema from its declarations without sampling a row or exposing SQL-visible attributes.
25    pub fn internal_values_schema(&self) -> Result<Option<RowSchema>, SQLError> {
26        self.internal_values_schema_with_inference(None)
27    }
28
29    /// Fill undeclared captured attributes from their caller scope, retaining explicit declarations and collations unchanged.
30    pub fn internal_values_schema_with_inference(
31        &self,
32        inferred: Option<&RowSchema>,
33    ) -> Result<Option<RowSchema>, SQLError> {
34        let Self::Values {
35            internal_relation: Some(relation),
36            internal_column_types: types,
37            internal_column_collations: collations,
38            ..
39        } = self
40        else {
41            return Ok(None);
42        };
43        if !collations.is_empty() && collations.len() != types.len() {
44            return Err(SQLError::Internal(
45                "internal VALUES collation width does not match its declared attributes".into(),
46            ));
47        }
48        let metadata = types
49            .iter()
50            .enumerate()
51            .map(|(position, declared)| {
52                let inferred_type = inferred
53                    .and_then(|schema| schema.column_types().get(position))
54                    .cloned()
55                    .flatten();
56                let ty = declared.clone().or(inferred_type);
57                let collation = collations.get(position).copied().flatten().or_else(|| {
58                    declared
59                        .is_none()
60                        .then(|| inferred.and_then(|schema| schema.column_collation(position)))
61                        .flatten()
62                });
63                (ty, collation)
64            })
65            .collect::<Vec<_>>();
66        for (ty, oid) in &metadata {
67            if oid.is_some_and(|oid| oid != 0)
68                && !ty
69                    .as_ref()
70                    .is_some_and(crate::schema::composites::type_is_collatable)
71            {
72                return Err(SQLError::Internal(
73                    "internal VALUES collation requires a collatable attribute type".into(),
74                ));
75            }
76        }
77        Ok(Some(RowSchema::with_internal_relation_metadata(
78            *relation, metadata,
79        )))
80    }
81}