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, SQLAggregateFunction, SQLFunctionOptions, SQLScalarFunction,
11    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 settings: &'a dyn QueryMemorySettings,
29    pub scalar_functions:
30        &'a RwLock<BTreeMap<String, RegisteredSQLFunction<dyn SQLScalarFunction>>>,
31    pub table_functions: &'a RwLock<BTreeMap<String, RegisteredSQLFunction<dyn SQLTableFunction>>>,
32    pub aggregate_functions:
33        &'a RwLock<BTreeMap<String, RegisteredSQLFunction<dyn SQLAggregateFunction>>>,
34    pub notices: &'a super::NoticeQueue,
35}
36
37impl QueryRuntimeView<'_> {
38    pub fn check_cancelled(&self) -> Result<(), SQLError> {
39        Ok(self.cancellation.check()?)
40    }
41
42    pub fn cancellation_token(&self) -> uqa_core::CancellationToken {
43        self.cancellation.clone()
44    }
45
46    pub fn work_mem_bytes(&self) -> Result<usize, SQLError> {
47        self.settings.work_mem_bytes()
48    }
49
50    pub fn lookup_scalar_function(
51        &self,
52        name: &str,
53    ) -> Option<RegisteredSQLFunction<dyn SQLScalarFunction>> {
54        self.scalar_functions
55            .read()
56            .get(&name.to_ascii_lowercase())
57            .cloned()
58    }
59
60    pub fn has_scalar_functions(&self) -> bool {
61        !self.scalar_functions.read().is_empty()
62    }
63
64    pub fn has_scalar_function(&self, name: &str) -> bool {
65        self.scalar_functions
66            .read()
67            .contains_key(&name.to_ascii_lowercase())
68    }
69
70    pub fn lookup_table_function(
71        &self,
72        name: &str,
73    ) -> Option<RegisteredSQLFunction<dyn SQLTableFunction>> {
74        self.table_functions
75            .read()
76            .get(&name.to_ascii_lowercase())
77            .cloned()
78    }
79
80    pub fn has_table_function(&self, name: &str) -> bool {
81        self.table_functions
82            .read()
83            .contains_key(&name.to_ascii_lowercase())
84    }
85
86    pub fn lookup_aggregate_function(
87        &self,
88        name: &str,
89    ) -> Option<RegisteredSQLFunction<dyn SQLAggregateFunction>> {
90        self.aggregate_functions
91            .read()
92            .get(&name.to_ascii_lowercase())
93            .cloned()
94    }
95
96    pub fn has_aggregate_function(&self, name: &str) -> bool {
97        self.aggregate_functions
98            .read()
99            .contains_key(&name.to_ascii_lowercase())
100    }
101
102    pub fn registered_function_options(&self, name: &str) -> [Option<SQLFunctionOptions>; 3] {
103        let name = name.to_ascii_lowercase();
104        [
105            self.scalar_functions
106                .read()
107                .get(&name)
108                .map(|registration| registration.options),
109            self.table_functions
110                .read()
111                .get(&name)
112                .map(|registration| registration.options),
113            self.aggregate_functions
114                .read()
115                .get(&name)
116                .map(|registration| registration.options),
117        ]
118    }
119
120    /// Queue a notice for the client, after the notices queued before it.
121    pub fn push_notice(&self, notice: uqa_sql::SQLNotice) {
122        self.notices.push(notice);
123    }
124}