Skip to main content

uqa_execution/query/
runtime.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Runtime services consumed by physical query operators and host callbacks.
8
9use crate::functions::{
10    RegisteredSQLFunction, SQLAggregateFactory, SQLFunctionContext, SQLFunctionOptions,
11    SQLScalarFunction, SQLTableFunction,
12};
13use parking_lot::RwLock;
14use std::collections::BTreeMap;
15use uqa_core::CancellationToken;
16use uqa_sql::SQLError;
17
18/// Live memory settings are read at the same execution boundaries as the session setting.
19pub trait QueryMemorySettings: Send + Sync {
20    fn work_mem_bytes(&self) -> Result<usize, SQLError>;
21}
22
23/// Borrowed runtime services with no catalog mutation, transaction, or storage publication access.
24#[derive(Clone, Copy)]
25pub struct QueryRuntimeView<'a> {
26    pub diagnostics: &'a super::diagnostics::QueryDiagnostics,
27    pub cancellation: &'a CancellationToken,
28    pub function_context: &'a SQLFunctionContext,
29    pub settings: &'a dyn QueryMemorySettings,
30    pub scalar_functions:
31        &'a RwLock<BTreeMap<String, RegisteredSQLFunction<dyn SQLScalarFunction>>>,
32    pub table_functions: &'a RwLock<BTreeMap<String, RegisteredSQLFunction<dyn SQLTableFunction>>>,
33    pub aggregate_functions:
34        &'a RwLock<BTreeMap<String, RegisteredSQLFunction<dyn SQLAggregateFactory>>>,
35    pub notices: &'a super::NoticeQueue,
36}
37
38impl QueryRuntimeView<'_> {
39    pub fn check_cancelled(&self) -> Result<(), SQLError> {
40        Ok(self.cancellation.check()?)
41    }
42
43    pub fn cancellation_token(&self) -> uqa_core::CancellationToken {
44        self.cancellation.clone()
45    }
46
47    pub fn work_mem_bytes(&self) -> Result<usize, SQLError> {
48        self.settings.work_mem_bytes()
49    }
50
51    pub fn lookup_scalar_function(
52        &self,
53        name: &str,
54    ) -> Option<RegisteredSQLFunction<dyn SQLScalarFunction>> {
55        self.scalar_functions
56            .read()
57            .get(runtime_function_name(name).as_str())
58            .cloned()
59    }
60
61    pub fn has_scalar_functions(&self) -> bool {
62        !self.scalar_functions.read().is_empty()
63    }
64
65    pub fn has_scalar_function(&self, name: &str) -> bool {
66        self.scalar_functions
67            .read()
68            .contains_key(runtime_function_name(name).as_str())
69    }
70
71    pub fn has_scalar_function_with_control(
72        &self,
73        name: &str,
74        control: &uqa_core::memory::ProductionControl<'_>,
75    ) -> Result<bool, SQLError> {
76        let name = uqa_sql::semantics::lowercase_function_name_with_control(name, control)?;
77        control.check()?;
78        Ok(self.scalar_functions.read().contains_key(name.as_str()))
79    }
80
81    pub fn lookup_table_function(
82        &self,
83        name: &str,
84    ) -> Option<RegisteredSQLFunction<dyn SQLTableFunction>> {
85        self.table_functions
86            .read()
87            .get(runtime_function_name(name).as_str())
88            .cloned()
89    }
90
91    pub fn has_table_function(&self, name: &str) -> bool {
92        self.table_functions
93            .read()
94            .contains_key(runtime_function_name(name).as_str())
95    }
96
97    pub fn lookup_aggregate_function(
98        &self,
99        name: &str,
100    ) -> Option<RegisteredSQLFunction<dyn SQLAggregateFactory>> {
101        self.aggregate_functions
102            .read()
103            .get(runtime_function_name(name).as_str())
104            .cloned()
105    }
106
107    pub fn has_aggregate_function(&self, name: &str) -> bool {
108        self.aggregate_functions
109            .read()
110            .contains_key(runtime_function_name(name).as_str())
111    }
112
113    pub fn registered_function_options(&self, name: &str) -> [Option<SQLFunctionOptions>; 3] {
114        let name = runtime_function_name(name);
115        [
116            self.scalar_functions
117                .read()
118                .get(name.as_str())
119                .map(|registration| registration.options),
120            self.table_functions
121                .read()
122                .get(name.as_str())
123                .map(|registration| registration.options),
124            self.aggregate_functions
125                .read()
126                .get(name.as_str())
127                .map(|registration| registration.options),
128        ]
129    }
130
131    pub fn has_aggregate_function_with_control(
132        &self,
133        name: &str,
134        control: &uqa_core::memory::ProductionControl<'_>,
135    ) -> Result<bool, SQLError> {
136        let name = uqa_sql::semantics::lowercase_function_name_with_control(name, control)?;
137        control.check()?;
138        Ok(self.aggregate_functions.read().contains_key(name.as_str()))
139    }
140
141    pub fn has_table_function_with_control(
142        &self,
143        name: &str,
144        control: &uqa_core::memory::ProductionControl<'_>,
145    ) -> Result<bool, SQLError> {
146        let name = uqa_sql::semantics::lowercase_function_name_with_control(name, control)?;
147        control.check()?;
148        Ok(self.table_functions.read().contains_key(name.as_str()))
149    }
150
151    /// Queue a notice for the client, after the notices queued before it.
152    pub fn push_notice(&self, notice: uqa_sql::SQLNotice) {
153        self.notices.push(notice);
154    }
155}
156
157fn runtime_function_name(name: &str) -> uqa_sql::semantics::FunctionName<'_> {
158    uqa_sql::semantics::lowercase_function_name_with_control(
159        name,
160        &uqa_core::memory::ProductionControl::uncontrolled(),
161    )
162    .expect("ordinary runtime identity normalization")
163}