Skip to main content

uqa_engine/capabilities/
scalar_functions.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Compose scalar execution services from current session and catalog state.
8use crate::{Engine, TableState};
9use std::{collections::BTreeMap, sync::Arc};
10use uqa_core::DocId;
11use uqa_execution::query::{
12    model_training::{ModelTrainingContext, TrainedModels, TrainingTable, TrainingTables},
13    scalar_functions::{ScalarFunctionContext, ScalarSession},
14    scalar_projection::AnalyzerRevisions,
15};
16use uqa_ml::DeepModel;
17use uqa_sql::SQLError;
18use uqa_storage::{document_store::Document, StorageBackendResult};
19impl ScalarSession for Engine {
20    fn backend_process_id(&self) -> i32 {
21        self.backend_process_id()
22    }
23    fn notify(&self, channel: &str, payload: &str) -> Result<(), SQLError> {
24        self.notify(channel, payload)
25    }
26    fn notification_queue_usage(&self) -> Result<f64, SQLError> {
27        self.notification_queue_usage()
28    }
29}
30impl AnalyzerRevisions for Engine {
31    fn analyzer_revision(&self, name: &str) -> Result<Arc<uqa_analysis::CompiledAnalyzer>, String> {
32        self.resolve_analyzer_revision(name)
33            .map_err(|error| error.to_string())
34    }
35}
36
37struct TrainingTableState(Arc<TableState>);
38
39impl TrainingTable for TrainingTableState {
40    fn doc_ids(&self) -> StorageBackendResult<Vec<DocId>> {
41        self.0.document_store.read().doc_ids()
42    }
43}
44
45impl TrainingTables for Engine {
46    fn training_table(
47        &self,
48        name: &str,
49    ) -> StorageBackendResult<Option<Box<dyn TrainingTable + '_>>> {
50        self.try_table(name).map(|table| {
51            table.map(|state| Box::new(TrainingTableState(state)) as Box<dyn TrainingTable>)
52        })
53    }
54
55    fn training_documents(
56        &self,
57        table: &str,
58        doc_ids: &[DocId],
59        projection: &[String],
60    ) -> Result<BTreeMap<DocId, Document>, SQLError> {
61        self.get_documents_with_materialized_projection(table, doc_ids, projection)
62    }
63}
64
65impl TrainedModels for Engine {
66    fn save_model(&self, name: &str, model: &DeepModel) -> Result<(), SQLError> {
67        self.save_model(name, model)
68    }
69}
70
71impl Engine {
72    pub(crate) fn model_training_context(&self) -> ModelTrainingContext<'_> {
73        ModelTrainingContext {
74            tables: self,
75            models: self,
76        }
77    }
78
79    pub(crate) fn scalar_function_context(&self) -> ScalarFunctionContext<'_> {
80        ScalarFunctionContext {
81            catalog: self.catalog_execution(),
82            sequences: self.sequence_introspection_context(),
83            names: self,
84            roles: self,
85            database: self.database_privilege_inquiry(),
86            schemas: self.schema_privilege_inquiry(),
87            sequence_privileges: self.sequence_privilege_read_context(),
88            tables: self.table_privilege_context(),
89            session: self,
90            graphs: self,
91            models: self.model_training_context(),
92            analyzers: self,
93            runtime: self.query_runtime_view(),
94        }
95    }
96}