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(&name.to_ascii_lowercase())
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(&name.to_ascii_lowercase())
69    }
70
71    pub fn lookup_table_function(
72        &self,
73        name: &str,
74    ) -> Option<RegisteredSQLFunction<dyn SQLTableFunction>> {
75        self.table_functions
76            .read()
77            .get(&name.to_ascii_lowercase())
78            .cloned()
79    }
80
81    pub fn has_table_function(&self, name: &str) -> bool {
82        self.table_functions
83            .read()
84            .contains_key(&name.to_ascii_lowercase())
85    }
86
87    pub fn lookup_aggregate_function(
88        &self,
89        name: &str,
90    ) -> Option<RegisteredSQLFunction<dyn SQLAggregateFactory>> {
91        self.aggregate_functions
92            .read()
93            .get(&name.to_ascii_lowercase())
94            .cloned()
95    }
96
97    pub fn has_aggregate_function(&self, name: &str) -> bool {
98        self.aggregate_functions
99            .read()
100            .contains_key(&name.to_ascii_lowercase())
101    }
102
103    pub fn registered_function_options(&self, name: &str) -> [Option<SQLFunctionOptions>; 3] {
104        let name = name.to_ascii_lowercase();
105        [
106            self.scalar_functions
107                .read()
108                .get(&name)
109                .map(|registration| registration.options),
110            self.table_functions
111                .read()
112                .get(&name)
113                .map(|registration| registration.options),
114            self.aggregate_functions
115                .read()
116                .get(&name)
117                .map(|registration| registration.options),
118        ]
119    }
120
121    /// Queue a notice for the client, after the notices queued before it.
122    pub fn push_notice(&self, notice: uqa_sql::SQLNotice) {
123        self.notices.push(notice);
124    }
125}