Skip to main content

uqa_sql/catalog/view/
materialized.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Materialized relation columns keep their creation-time collation independently of query execution and refresh.
8
9use crate::{ColumnType, RowSchema, SQLError};
10
11pub fn materialized_view_schema(
12    columns: &[String],
13    types: &[Option<ColumnType>],
14    collations: Option<&[u32]>,
15) -> Result<RowSchema, SQLError> {
16    let collations = collations.ok_or_else(|| {
17        SQLError::Internal("stored materialized view has no durable collation metadata".into())
18    })?;
19    if columns.len() != types.len() || columns.len() != collations.len() {
20        return Err(SQLError::Internal(
21            "stored materialized view column metadata is inconsistent".into(),
22        ));
23    }
24    for (ty, oid) in types.iter().zip(collations) {
25        if ty
26            .as_ref()
27            .is_some_and(crate::schema::composites::type_is_collatable)
28            != (*oid != 0)
29        {
30            return Err(SQLError::Internal(
31                "stored materialized view collation does not match its column type".into(),
32            ));
33        }
34    }
35    Ok(RowSchema::with_types(columns.to_vec(), types.to_vec())
36        .with_column_collations(collations.iter().copied().map(Some)))
37}