Skip to main content

uqa_sql/routines/inlining/
table.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Typed SQL set-function bodies eligible to become a caller-owned subquery.
8
9use super::{CreateFunction, FunctionBinding, RoutineInliningContext, SQLError, ScalarExpr};
10use crate::ast::FunctionVolatility;
11use crate::plan::QueryPlan;
12
13mod owned;
14mod production;
15mod relations;
16mod scopes_owned;
17pub use owned::InlineOwnedRoutineQuery;
18
19pub struct InlineRoutineQuery {
20    pub identity: [u8; 16],
21    pub query: Box<QueryPlan>,
22    /// Declared OUT names or composite attributes; a scalar result is named by its FROM alias.
23    pub declared_columns: Option<Vec<String>>,
24}
25
26impl RoutineInliningContext<'_> {
27    /// Describe a selected scalar FROM call without evaluating its body or arguments.
28    pub fn scalar_from_columns(
29        &self,
30        binding: &FunctionBinding,
31        arguments: &[ScalarExpr],
32        output_name: &str,
33        alias: Option<&str>,
34        columns: &[String],
35        ordinality: bool,
36    ) -> Result<Option<Vec<String>>, SQLError> {
37        self.scalar_from_columns_with_control(
38            binding,
39            arguments,
40            output_name,
41            alias,
42            columns,
43            ordinality,
44            &uqa_core::memory::ProductionControl::uncontrolled(),
45        )
46        .map(|columns| {
47            columns.map(|columns| {
48                columns
49                    .into_uncontrolled()
50                    .expect("ordinary scalar source columns")
51            })
52        })
53    }
54
55    /// Expand an ordinary SQL set function under the caller's snapshot. Volatile inputs, subplans, security boundaries and ordinality retain the function scan.
56    pub fn prepare_table(
57        &self,
58        binding: &FunctionBinding,
59        arguments: &[ScalarExpr],
60        active: &[[u8; 16]],
61        column_names: &[String],
62        column_types: &[String],
63    ) -> Result<Option<InlineRoutineQuery>, SQLError> {
64        let control = uqa_core::memory::ProductionControl::uncontrolled();
65        self.prepare_table_owned(
66            binding,
67            arguments,
68            active,
69            column_names,
70            column_types,
71            &control,
72        )?
73        .map(|expanded| {
74            Ok(InlineRoutineQuery {
75                identity: expanded.identity,
76                query: Box::new(
77                    expanded
78                        .query
79                        .finish(&control)?
80                        .into_uncontrolled()
81                        .expect("ordinary inline table body"),
82                ),
83                declared_columns: expanded.declared_columns.map(|columns| {
84                    columns
85                        .into_uncontrolled()
86                        .expect("ordinary declared columns")
87                }),
88            })
89        })
90        .transpose()
91    }
92}
93
94pub(super) fn eligible_definition(definition: &CreateFunction) -> bool {
95    definition.language == "sql"
96        && definition.returns_set()
97        && definition.volatility != FunctionVolatility::Volatile
98        && !definition.strict
99        && !definition.security.security_definer
100        && definition.config.is_empty()
101}