Skip to main content

uqa_sql/schema/
join_output.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Static layouts shared by qualified joins and physical output shaping.
8
9use super::SchemaLayoutError;
10use crate::{ColumnIdentity, ColumnType, RowSchema};
11
12/// Source of one visible or hidden join-output identity.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum JoinOutputSource {
15    /// Reuse an existing logical input position without copying its value.
16    Input(usize),
17    /// Apply an implicit binder-selected coercion to one input position.
18    Cast { input: usize, ty: ColumnType },
19    /// SQL `COALESCE(left::type, right::type)` over two logical input
20    /// positions. This is required only for a merged column of `FULL JOIN`.
21    Coalesce {
22        left: usize,
23        right: usize,
24        ty: ColumnType,
25    },
26}
27
28pub fn compile_layout(
29    input: &RowSchema,
30    columns: &[(String, ColumnIdentity, JoinOutputSource)],
31    aliases: &[(ColumnIdentity, JoinOutputSource)],
32) -> Result<(RowSchema, Vec<JoinOutputSource>), SchemaLayoutError> {
33    let (schema, computed) = compile_layout_with_control(
34        input,
35        columns,
36        aliases,
37        &uqa_core::memory::ProductionControl::uncontrolled(),
38    )
39    .map_err(|error| match error {
40        crate::SQLError::Internal(message) => SchemaLayoutError(message),
41        error => SchemaLayoutError(error.to_string()),
42    })?;
43    Ok((
44        schema,
45        computed
46            .into_uncontrolled()
47            .expect("ordinary JOIN output has no reservation"),
48    ))
49}
50
51mod production;
52pub use production::compile_layout_with_control;
53
54pub fn source_type(source: &JoinOutputSource) -> Option<ColumnType> {
55    match source {
56        JoinOutputSource::Input(_) => None,
57        JoinOutputSource::Cast { ty, .. } | JoinOutputSource::Coalesce { ty, .. } => {
58            Some(ty.clone())
59        }
60    }
61}