Skip to main content

uqa_engine/capabilities/
table_functions.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind table-function runtime services to retained Engine state and public command boundaries.
8
9use crate::Engine;
10use uqa_core::Value;
11use uqa_execution::query::table_functions::context::{
12    AnalyzerTableFunctions, OperatorJoinBinding, TableFunctionContext, TableFunctionSession,
13};
14use uqa_execution::scalar::plan::PhysicalSubqueryRunner;
15use uqa_operators::OperatorTree;
16use uqa_sql::semantics::graph_functions::GraphNameCatalog;
17use uqa_sql::{
18    ast::OperatorJoinRelations, expr::EngineHook, plan::QueryPlan, SQLError, SQLParam, ScalarExpr,
19};
20use uqa_storage::FtsIndexStat;
21
22impl GraphNameCatalog for Engine {
23    fn list_graphs(&self) -> Result<Vec<String>, SQLError> {
24        self.graph_names_in_execution()
25            .map_err(|err| SQLError::Internal(format!("read graph catalog: {err}")))
26    }
27}
28impl TableFunctionSession for Engine {
29    fn listening_channels(&self) -> Vec<String> {
30        self.listening_channels()
31    }
32    fn sequence_data(&self, args: &[Value]) -> Result<Value, SQLError> {
33        self.sequence_introspection_context()
34            .pg_get_sequence_data_value(args)
35    }
36    fn sequence_parameters(&self, args: &[Value]) -> Result<Value, SQLError> {
37        self.sequence_introspection_context()
38            .pg_sequence_parameters_value(args)
39    }
40    fn role_oid(&self, name: &str) -> Result<Option<i64>, SQLError> {
41        Ok(
42            uqa_execution::catalog::security::roles::RoleCatalogGuards::inquiry_role_definitions(
43                self,
44            )?
45            .get(name)
46            .map(|role| role.oid),
47        )
48    }
49}
50impl AnalyzerTableFunctions for Engine {
51    fn register_named_analyzer(&self, name: &str, config: &str) -> Result<(), String> {
52        self.register_named_analyzer(name, config)
53    }
54    fn drop_named_analyzer(&self, name: &str) -> Result<bool, String> {
55        self.drop_named_analyzer(name)
56    }
57    fn list_named_analyzers(&self) -> Result<Vec<String>, String> {
58        self.named_analyzers_in_execution()
59    }
60    fn set_table_field_analyzer(
61        &self,
62        table: &str,
63        field: &str,
64        analyzer: &str,
65        phase: &str,
66    ) -> Result<(), SQLError> {
67        self.with_implicit_transaction(|engine| {
68            engine.set_table_field_analyzer_inner(table, field, analyzer, phase)
69        })
70    }
71    fn fts_index_stats(&self, table: Option<&str>) -> Result<Vec<FtsIndexStat>, SQLError> {
72        self.fts_index_stats_in_execution(table)
73    }
74}
75impl OperatorJoinBinding for Engine {
76    fn lower(
77        &self,
78        name: &str,
79        relations: Option<&OperatorJoinRelations>,
80        args: &[ScalarExpr],
81        params: &[SQLParam],
82    ) -> Result<(OperatorJoinRelations, OperatorTree), SQLError> {
83        self.retrieval_binding()
84            .lower_join(name, relations, args, params)
85    }
86}
87impl Engine {
88    pub(crate) fn table_function_context<'a>(
89        &'a self,
90        params: &'a [SQLParam],
91        eval_hook: &'a dyn EngineHook,
92        subquery_runner: &'a dyn PhysicalSubqueryRunner,
93        subqueries: &'a [QueryPlan],
94    ) -> TableFunctionContext<'a> {
95        TableFunctionContext {
96            runtime: self.query_runtime_view(),
97            session: self,
98            analyzers: self,
99            graph_names: self,
100            cypher: self,
101            routines: self.routine_invocation_context(),
102            retrieval: self.tree_execution_context(),
103            joins: self,
104            params,
105            eval_hook,
106            resolver: self,
107            subquery_runner,
108            subqueries,
109        }
110    }
111}