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    /// Physical I/O belongs to the invocation executing this work, including explicit absence for unrelated sessions.
17    pub io: uqa_core::io_usage::IOContext,
18    /// The session-owned compiled-pattern allowance, or `None` outside a session.
19    pub regex_cache: Option<std::sync::Arc<uqa_sql::expr::RegexCache>>,
20    pub temporal_input: uqa_sql::expr::TemporalInputContext,
21    /// The running statement's cancellation token, or `None` for work dispatched from outside any statement.
22    pub cancellation: Option<uqa_core::CancellationToken>,
23}
24
25impl StatementThreadContext {
26    /// Capture the invoking thread's context for work it dispatches to another thread.
27    #[must_use]
28    pub fn current() -> Self {
29        Self {
30            retained_rows: crate::query::sources::retained_source::RetainedRowContext::current(),
31            io: uqa_core::io_usage::IOContext::current(),
32            regex_cache: uqa_sql::expr::current_regex_cache(),
33            temporal_input: uqa_sql::expr::TemporalInputContext::current(),
34            cancellation: uqa_sql::expr::statement_cancellation(),
35        }
36    }
37
38    /// Run the invoking thread under this context until the returned scope drops, which restores the enclosing context.
39    #[must_use]
40    pub fn enter(&self) -> StatementThreadScope {
41        StatementThreadScope::enter(self)
42    }
43}