Skip to main content

uqa_engine/capabilities/
stored_relations.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind stored relation analysis to catalog lookup and retained sequence registry state.
8
9use crate::Engine;
10use uqa_sql::{
11    binding::stored_relations::{
12        self as analysis, StoredQueryBindingContext, StoredQuerySequences, StoredRelationCatalog,
13    },
14    catalog::resolution::RelationResolution,
15    plan::QueryPlan,
16    SQLError,
17};
18
19impl StoredRelationCatalog for Engine {
20    fn resolve_age_label_relation_name(&self, reference: &str) -> Result<Option<String>, SQLError> {
21        uqa_execution::catalog::projection::resolve_age_label_relation_name(
22            &self.catalog_execution(),
23            reference,
24        )
25    }
26    fn resolve_visible_relation_kind(
27        &self,
28        reference: &str,
29    ) -> Result<RelationResolution, SQLError> {
30        Engine::resolve_visible_relation_kind(self, reference)
31    }
32    fn resolve_loaded_visible_relation_kind(
33        &self,
34        reference: &str,
35    ) -> Result<RelationResolution, SQLError> {
36        Engine::resolve_loaded_visible_relation_kind(self, reference)
37    }
38    fn resolve_bound_relation_kind(&self, reference: &str) -> Result<RelationResolution, SQLError> {
39        Engine::resolve_bound_relation_kind(self, reference)
40    }
41    fn resolve_visible_relation_kind_with_control(
42        &self,
43        reference: &str,
44        control: &uqa_core::memory::ProductionControl<'_>,
45    ) -> Result<uqa_core::memory::Produced<RelationResolution>, SQLError> {
46        self.resolve_relation_kind_for_query_with_control(reference, false, control)
47    }
48    fn resolve_loaded_visible_relation_kind_with_control(
49        &self,
50        reference: &str,
51        control: &uqa_core::memory::ProductionControl<'_>,
52    ) -> Result<uqa_core::memory::Produced<RelationResolution>, SQLError> {
53        self.resolve_loaded_relation_kind_with_control(reference, false, control)
54    }
55    fn resolve_bound_relation_kind_with_control(
56        &self,
57        reference: &str,
58        control: &uqa_core::memory::ProductionControl<'_>,
59    ) -> Result<uqa_core::memory::Produced<RelationResolution>, SQLError> {
60        self.resolve_loaded_relation_kind_with_control(reference, true, control)
61    }
62}
63impl StoredQuerySequences for Engine {
64    fn query_sequence(&self, reference: &str) -> Result<String, String> {
65        self.resolve_sequence_reference_for_binding(reference)
66            .map_err(|error| error.to_string())
67    }
68    fn loaded_query_sequence(&self, reference: &str) -> Result<String, String> {
69        let sequences = self.durable.sequences.read();
70        let candidates = self
71            .relation_lookup_candidates(reference)
72            .map_err(|error| error.to_string())?;
73        analysis::resolve_loaded_query_sequence(reference, candidates, |candidate| {
74            sequences.contains_key(candidate)
75        })
76    }
77}
78impl Engine {
79    pub(crate) fn bind_stored_query_relations(
80        &self,
81        plan: &mut QueryPlan,
82        context: &str,
83        reject_transition_relations: bool,
84    ) -> Result<bool, SQLError> {
85        let temporary_schema = self.temporary_schema_name();
86        let transition_relations =
87            uqa_execution::mutation::triggers::current_transition_relation_names();
88        analysis::bind_stored_query_relations(
89            &StoredQueryBindingContext {
90                relations: self,
91                lookup_mode: uqa_sql::catalog::resolution::RelationLookupMode::Dynamic,
92                sequences: self,
93                temporary_schema: &temporary_schema,
94                transition_relations: &transition_relations,
95            },
96            plan,
97            context,
98            reject_transition_relations,
99            false,
100        )
101    }
102}