Skip to main content

uqa_execution/query/statement/
context.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Query read generation, physical sources, and directional execution services.
8
9use crate::query::{sources::SourceContext, CteScope};
10use crate::{PhysicalOperator, RowSchema};
11use uqa_sql::{plan::QueryPlan, SQLError, SQLParam};
12
13/// Select a read generation while keeping its owner alive only for the borrowed operation.
14pub trait SnapshotSource<S: Clone + 'static> {
15    fn capture(&self) -> Result<S, SQLError>;
16}
17
18pub trait QuerySnapshots<S: Clone + 'static>: SnapshotSource<S> {
19    fn with_snapshot(
20        &self,
21        snapshot: &S,
22        operation: &mut dyn ScopedQueryOperation<S>,
23    ) -> Result<(), SQLError>;
24}
25pub trait ScopedQueryOperation<S: Clone + 'static> {
26    fn run(&mut self, context: &QueryContext<'_, S>) -> Result<(), SQLError>;
27}
28/// Fork session state for a directional child. Physical traversal belongs to execution.
29pub trait DirectionalQueryFactory<S: Clone + 'static> {
30    fn query_operator(
31        &self,
32        plan: &QueryPlan,
33        params: &[SQLParam],
34        scope: &CteScope<S>,
35        schema: RowSchema,
36    ) -> Result<Box<dyn PhysicalOperator>, SQLError>;
37}
38pub trait CteFilterPlanning<S: Clone> {
39    fn output_filters(
40        &self,
41        plan: &QueryPlan,
42        scope: &CteScope<S>,
43    ) -> Result<uqa_sql::plan::source_projection::CteOutputFilters, SQLError>;
44}
45#[derive(Clone)]
46pub struct QueryContext<'a, S: Clone + 'static> {
47    pub generation: Option<&'a S>,
48    pub source: SourceContext<'a, S>,
49    pub snapshots: &'a dyn QuerySnapshots<S>,
50    pub directional: &'a dyn DirectionalQueryFactory<S>,
51    pub cte_filters: &'a dyn CteFilterPlanning<S>,
52}
53impl<S: Clone + 'static> Copy for QueryContext<'_, S> {}
54
55pub fn with_query_snapshot<S: Clone + 'static, T>(
56    snapshots: &dyn QuerySnapshots<S>,
57    snapshot: &S,
58    action: impl for<'scope> FnOnce(&QueryContext<'scope, S>) -> Result<T, SQLError>,
59) -> Result<T, SQLError> {
60    struct Operation<F, T> {
61        action: Option<F>,
62        result: Option<T>,
63    }
64    impl<S: Clone + 'static, F, T> ScopedQueryOperation<S> for Operation<F, T>
65    where
66        F: for<'scope> FnOnce(&QueryContext<'scope, S>) -> Result<T, SQLError>,
67    {
68        fn run(&mut self, context: &QueryContext<'_, S>) -> Result<(), SQLError> {
69            let action = self.action.take().ok_or_else(|| {
70                SQLError::Internal("statement snapshot operation was invoked more than once".into())
71            })?;
72            self.result = Some(action(context)?);
73            Ok(())
74        }
75    }
76    let mut operation = Operation {
77        action: Some(action),
78        result: None,
79    };
80    snapshots.with_snapshot(snapshot, &mut operation)?;
81    operation
82        .result
83        .ok_or_else(|| SQLError::Internal("statement snapshot operation was not invoked".into()))
84}