Skip to main content

uqa_engine/
hook.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Adapter from the stable SQL expression hook to engine-owned capabilities.
8
9use uqa_core::Value;
10use uqa_sql::SQLError;
11
12use super::Engine;
13
14impl uqa_sql::expr::EngineHook for Engine {
15    fn construct_range(
16        &self,
17        oid: u32,
18        bounds: uqa_sql::expr::RangeBoundsInput,
19        control: &uqa_core::memory::ProductionControl<'_>,
20    ) -> Result<uqa_core::memory::Produced<Value>, SQLError> {
21        uqa_execution::catalog::range_type::input::construct_range(
22            &self.range_input_context(&self.catalog_read_view(), self),
23            oid,
24            bounds,
25            control,
26        )
27    }
28
29    fn construct_multirange(
30        &self,
31        oid: u32,
32        members: &[Value],
33        control: &uqa_core::memory::ProductionControl<'_>,
34    ) -> Result<uqa_core::memory::Produced<Value>, SQLError> {
35        uqa_execution::catalog::range_type::input::construct_multirange(
36            &self.range_input_context(&self.catalog_read_view(), self),
37            oid,
38            members,
39            control,
40        )
41    }
42
43    fn sql_notice(&self, notice: uqa_sql::SQLNotice) {
44        self.push_sql_notice(notice);
45    }
46
47    fn read_range_input(
48        &self,
49        oid: u32,
50        text: &str,
51        control: &uqa_core::memory::ProductionControl<'_>,
52    ) -> Result<uqa_core::memory::Produced<Value>, SQLError> {
53        uqa_execution::catalog::range_type::input::read_input(
54            &self.range_input_context(&self.catalog_read_view(), self),
55            oid,
56            text,
57            control,
58        )
59    }
60
61    fn require_builtin_execute(
62        &self,
63        binding: &uqa_sql::ast::FunctionBinding,
64    ) -> Result<(), SQLError> {
65        use uqa_sql::catalog::security::builtin_routines::BuiltinRoutineExecution;
66        uqa_execution::catalog::security::builtin_routines::execution::BuiltinRoutinePermissions::capture(
67            &self.catalog_execution(),
68        ).require_execute(binding)
69    }
70
71    fn require_builtin_execute_with_control(
72        &self,
73        binding: &uqa_sql::ast::FunctionBinding,
74        control: &uqa_core::memory::ProductionControl<'_>,
75    ) -> Result<(), SQLError> {
76        use uqa_sql::catalog::security::builtin_routines::BuiltinRoutineExecution;
77        control.check()?;
78        uqa_execution::catalog::security::builtin_routines::execution::BuiltinRoutinePermissions::capture(
79            &self.catalog_execution(),
80        ).require_execute_with_control(binding, control)
81    }
82
83    fn transaction_timestamp_micros(&self) -> Option<i64> {
84        Some(Engine::transaction_timestamp_micros(self))
85    }
86
87    fn statement_timestamp_micros(&self) -> Option<i64> {
88        Some(Engine::statement_timestamp_micros(self))
89    }
90
91    fn resolve_oid_alias_input_with_control(
92        &self,
93        ty: &uqa_sql::ColumnType,
94        name: &str,
95        control: &uqa_core::memory::ProductionControl<'_>,
96    ) -> Result<Option<i64>, SQLError> {
97        uqa_execution::catalog::projection::resolve_oid_alias_input_with_control(
98            &self.catalog_execution(),
99            ty,
100            name,
101            control,
102        )
103    }
104
105    fn resolve_regtype_input(&self, name: &str) -> Result<Option<i64>, SQLError> {
106        uqa_execution::catalog::projection::resolve_regtype_oid(&self.catalog_execution(), name)?
107            .map(Some)
108            .ok_or_else(|| SQLError::Routine {
109                sqlstate: "42704".into(),
110                message: format!("type \"{name}\" does not exist"),
111            })
112    }
113    fn cast_domain(
114        &self,
115        value: &Value,
116        source: Option<&str>,
117        target: &uqa_sql::ast::ColumnType,
118    ) -> Result<Option<Value>, SQLError> {
119        uqa_sql::assignment::domain::cast_domain_value(self, value, source, target)
120    }
121    fn cast_domain_with_control(
122        &self,
123        value: &Value,
124        source: Option<&str>,
125        target: &uqa_sql::ColumnType,
126        control: &uqa_core::memory::ProductionControl<'_>,
127    ) -> Result<Option<uqa_core::memory::Produced<Value>>, SQLError> {
128        uqa_sql::assignment::domain::cast_domain_value_with_control(
129            self, value, source, target, control,
130        )
131    }
132    fn resolve_type_name(
133        &self,
134        name: &str,
135    ) -> std::result::Result<Option<uqa_sql::ast::ColumnType>, String> {
136        Ok(
137            uqa_execution::catalog::projection::resolve_catalog_column_type(
138                &self.catalog_execution(),
139                name,
140            ),
141        )
142    }
143
144    fn resolve_type_oid(&self, oid: u32) -> Result<Option<uqa_sql::ast::ColumnType>, String> {
145        Ok(
146            uqa_execution::catalog::projection::resolve_catalog_user_type_by_oid(
147                &self.catalog_execution(),
148                oid,
149            ),
150        )
151    }
152
153    fn resolve_type_name_with_control(
154        &self,
155        name: &str,
156        control: &uqa_core::memory::ProductionControl<'_>,
157    ) -> Result<Option<uqa_core::memory::Produced<uqa_sql::ColumnType>>, SQLError> {
158        uqa_execution::catalog::projection::resolve_catalog_column_type_with_control(
159            &self.catalog_execution(),
160            name,
161            control,
162        )
163    }
164
165    fn resolve_type_oid_with_control(
166        &self,
167        oid: u32,
168        control: &uqa_core::memory::ProductionControl<'_>,
169    ) -> Result<Option<uqa_core::memory::Produced<uqa_sql::ColumnType>>, SQLError> {
170        uqa_execution::catalog::projection::resolve_catalog_user_type_by_oid_with_control(
171            &self.catalog_execution(),
172            oid,
173            control,
174        )
175    }
176
177    fn type_oid_exists(&self, oid: u32) -> Result<bool, String> {
178        uqa_execution::catalog::projection::type_oid_exists(&self.catalog_execution(), oid)
179            .map_err(|error| error.to_string())
180    }
181
182    fn resolve_regclass_input(&self, name: &str) -> std::result::Result<Option<i64>, SQLError> {
183        uqa_execution::catalog::projection::resolve_regclass_oid(&self.catalog_execution(), name)
184    }
185
186    fn resolve_regproc(&self, name: &str) -> std::result::Result<Option<i64>, SQLError> {
187        uqa_execution::catalog::projection::resolve_regproc_input_oid(
188            &self.catalog_execution(),
189            name,
190        )
191        .map(Some)
192    }
193
194    fn resolve_regprocedure(&self, name: &str) -> std::result::Result<Option<i64>, String> {
195        uqa_execution::catalog::projection::resolve_regprocedure_oid(
196            &self.catalog_execution(),
197            name,
198        )
199    }
200
201    fn resolve_regprocedure_input(&self, name: &str) -> std::result::Result<Option<i64>, SQLError> {
202        uqa_execution::catalog::projection::resolve_regprocedure_input_oid(
203            &self.catalog_execution(),
204            name,
205        )
206        .map(Some)
207    }
208
209    fn resolve_regrole(&self, name: &str) -> std::result::Result<Option<i64>, SQLError> {
210        uqa_execution::catalog::projection::resolve_regrole_oid(&self.catalog_execution(), name)
211    }
212
213    fn resolve_regnamespace(&self, name: &str) -> std::result::Result<Option<i64>, SQLError> {
214        uqa_execution::catalog::projection::resolve_regnamespace_oid(
215            &self.catalog_execution(),
216            name,
217        )
218    }
219
220    fn resolve_regcollation(&self, name: &str) -> std::result::Result<Option<i64>, SQLError> {
221        uqa_execution::catalog::projection::resolve_regcollation_oid(
222            &self.catalog_execution(),
223            name,
224        )
225    }
226
227    fn resolve_regobject(
228        &self,
229        ty: &uqa_sql::ast::ColumnType,
230        name: &str,
231    ) -> std::result::Result<Option<i64>, SQLError> {
232        uqa_execution::catalog::projection::resolve_regobject_oid(
233            &self.catalog_execution(),
234            ty,
235            name,
236        )
237    }
238
239    fn resolve_regtype_output(
240        &self,
241        ty: &uqa_sql::ast::ColumnType,
242        oid: i64,
243    ) -> std::result::Result<Option<String>, String> {
244        uqa_execution::catalog::projection::resolve_regtype_output(
245            &self.catalog_execution(),
246            ty,
247            oid,
248        )
249    }
250
251    fn enum_labels(&self) -> Option<&dyn uqa_sql::expr::enums::EnumLabelCatalog> {
252        Some(self)
253    }
254
255    fn composite_types(&self) -> Option<&dyn uqa_sql::expr::composites::CompositeTypeCatalog> {
256        Some(self)
257    }
258
259    fn nextval(&self, name: &str) -> std::result::Result<i64, SQLError> {
260        self.nextval_sql(name)
261    }
262
263    fn currval(&self, name: &str) -> std::result::Result<i64, SQLError> {
264        self.currval_sql(name)
265    }
266
267    fn lastval(&self) -> std::result::Result<i64, SQLError> {
268        self.lastval_sql()
269    }
270
271    fn setval(
272        &self,
273        name: &str,
274        value: i64,
275        is_called: bool,
276    ) -> std::result::Result<i64, SQLError> {
277        self.setval_sql(name, value, is_called)
278    }
279
280    fn call_scalar_function(
281        &self,
282        name: &str,
283        args: &[Value],
284    ) -> Option<std::result::Result<Value, SQLError>> {
285        self.call_registered_scalar_function(name, args)
286    }
287
288    fn call_bound_builtin_function(
289        &self,
290        binding: &uqa_sql::ast::FunctionBinding,
291        args: &[(Option<String>, Value)],
292    ) -> Option<std::result::Result<Value, SQLError>> {
293        uqa_execution::query::scalar_functions::call_bound_builtin(
294            &self.scalar_function_context(),
295            binding,
296            args,
297        )
298    }
299
300    fn has_scalar_functions(&self) -> bool {
301        self.has_registered_scalar_functions()
302    }
303
304    fn call_bound_builtin_function_with_control(
305        &self,
306        binding: &uqa_sql::ast::FunctionBinding,
307        args: &[(Option<String>, Value)],
308        control: &uqa_core::memory::ProductionControl<'_>,
309    ) -> Option<Result<uqa_core::memory::Produced<Value>, SQLError>> {
310        uqa_execution::query::scalar_functions::call_bound_builtin_with_control(
311            &self.scalar_function_context(),
312            binding,
313            args,
314            control,
315        )
316    }
317
318    fn current_schema(&self) -> std::result::Result<Option<String>, String> {
319        self.current_schema_name_in_execution()
320            .map_err(|error| error.to_string())
321    }
322
323    fn current_user(&self) -> std::result::Result<Option<String>, SQLError> {
324        Ok(Some(
325            self.current_role()
326                .require_name(&self.durable.roles.read())?
327                .to_owned(),
328        ))
329    }
330
331    fn session_user(&self) -> std::result::Result<Option<String>, SQLError> {
332        Ok(Some(
333            self.session_role()
334                .require_name(&self.durable.roles.read())?
335                .to_owned(),
336        ))
337    }
338
339    fn runtime_parameter(&self, name: &str) -> std::result::Result<Option<String>, SQLError> {
340        Ok(self.session_execution_view().runtime_parameter(name))
341    }
342
343    fn temporal_interval_style(
344        &self,
345    ) -> std::result::Result<Option<uqa_core::TemporalIntervalStyle>, SQLError> {
346        Ok(Some(self.session.interval_style()))
347    }
348
349    fn temporal_time_zone(
350        &self,
351    ) -> std::result::Result<Option<uqa_core::TemporalTimeZone>, SQLError> {
352        Ok(Some(self.session.time_zone()))
353    }
354
355    fn sleep(&self, duration: std::time::Duration) -> std::result::Result<(), SQLError> {
356        Ok(self.runtime.cancellation.sleep(duration)?)
357    }
358
359    fn set_runtime_parameter(
360        &self,
361        name: &str,
362        value: Option<&str>,
363        local: bool,
364    ) -> std::result::Result<String, SQLError> {
365        self.set_config(name, value, local)
366    }
367
368    fn current_schemas(
369        &self,
370        include_implicit: bool,
371    ) -> std::result::Result<Option<Vec<String>>, String> {
372        self.current_schema_names_in_execution(include_implicit)
373            .map(Some)
374            .map_err(|error| error.to_string())
375    }
376
377    fn random_value(&self) -> std::result::Result<Option<f64>, String> {
378        Ok(Some(self.next_random_value()))
379    }
380
381    fn random_u64(&self) -> std::result::Result<Option<u64>, String> {
382        Ok(Some(self.next_random_u64()))
383    }
384
385    fn set_random_seed(&self, seed: f64) -> std::result::Result<bool, String> {
386        Engine::set_random_seed(self, seed)?;
387        Ok(true)
388    }
389
390    fn call_user_function(
391        &self,
392        name: &str,
393        args: &[(Option<String>, Value)],
394    ) -> Option<std::result::Result<Value, SQLError>> {
395        crate::capabilities::routine_invocation::call_user_scalar_function(self, name, args)
396    }
397}