Skip to main content

uqa_sql/catalog/collations/
declarations.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Declaration collation selection and retained attribute identities.
8
9use crate::{ast::ColumnDef, ColumnType, FunctionTypeResolver, SQLError};
10
11/// `GetColumnDefCollation` resolves an explicit name before rejecting a noncollatable type. Expression COLLATE validation has a different diagnostic order.
12pub fn select(
13    types: &dyn FunctionTypeResolver,
14    ty: &ColumnType,
15    name: Option<&str>,
16) -> Result<u32, SQLError> {
17    let Some(name) = name else {
18        return Ok(
19            u32::try_from(crate::catalog::type_metadata::pg_type_collation_oid(ty))
20                .expect("predefined collation identity"),
21        );
22    };
23    let oid = super::expression::select(name, None, None, Some(types))?;
24    super::expression::select(name, Some(oid), Some(ty), Some(types))
25}
26
27/// An unmodified legacy attribute retains the collation it actually used before explicit declarations were preserved.
28#[must_use]
29pub fn column_oid(column: &ColumnDef) -> i64 {
30    column.collation_oid.map_or_else(
31        || crate::catalog::type_metadata::pg_type_collation_oid(&column.ty),
32        i64::from,
33    )
34}
35
36#[cfg(test)]
37mod tests;