Skip to main content

uqa_execution/statement/
thread_context.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The thread-local inputs and cancellation of one running statement, which the statement enters where it runs and each thread that runs part of it enters before that work.
8
9use super::thread_scope::StatementThreadScope;
10
11/// The temporal input, cancellation and retained regex cache a statement's expressions use. A statement enters the context it runs under for its analysis, planning and execution; work dispatched to another thread captures the dispatching thread's context and enters it on that thread.
12#[derive(Clone)]
13pub struct StatementThreadContext {
14    /// SQL-invisible immutable row sources owned by this invocation, also shared by its workers.
15    pub retained_rows: crate::query::sources::retained_source::RetainedRowContext,
16    /// The session-owned compiled-pattern allowance, or `None` outside a session.
17    pub regex_cache: Option<std::sync::Arc<uqa_sql::expr::RegexCache>>,
18    pub temporal_input: uqa_sql::expr::TemporalInputContext,
19    /// The running statement's cancellation token, or `None` for work dispatched from outside any statement.
20    pub cancellation: Option<uqa_core::CancellationToken>,
21}
22
23impl StatementThreadContext {
24    /// Capture the invoking thread's context for work it dispatches to another thread.
25    #[must_use]
26    pub fn current() -> Self {
27        Self {
28            retained_rows: crate::query::sources::retained_source::RetainedRowContext::current(),
29            regex_cache: uqa_sql::expr::current_regex_cache(),
30            temporal_input: uqa_sql::expr::TemporalInputContext::current(),
31            cancellation: uqa_sql::expr::statement_cancellation(),
32        }
33    }
34
35    /// Run the invoking thread under this context until the returned scope drops, which restores the enclosing context.
36    #[must_use]
37    pub fn enter(&self) -> StatementThreadScope {
38        StatementThreadScope::enter(self)
39    }
40}