Skip to main content

reifydb_evaluate/expression/
context.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::sync::LazyLock;
5
6use arrow_array::RecordBatch;
7use reifydb_core::{
8	interface::evaluate::TargetColumn,
9	value::{
10		batch::empty_batch,
11		column::arith::context::{ArithContext, Overflow},
12	},
13};
14use reifydb_routine_abi::registry::Routines;
15use reifydb_runtime::context::{RuntimeContext, clock::Clock};
16use reifydb_value::{params::Params, value::identity::IdentityId};
17
18use crate::stack::SymbolTable;
19
20pub struct EvalContext<'a> {
21	pub target: Option<TargetColumn>,
22	pub batch: RecordBatch,
23	pub row_count: usize,
24	pub take: Option<usize>,
25	pub params: &'a Params,
26	pub symbols: &'a SymbolTable,
27	pub routines: &'a Routines,
28	pub runtime_context: &'a RuntimeContext,
29	pub identity: IdentityId,
30}
31
32impl<'a> EvalContext<'a> {
33	pub fn testing() -> EvalContext<'static> {
34		static EMPTY_PARAMS: LazyLock<Params> = LazyLock::new(|| Params::None);
35		static EMPTY_SYMBOL_TABLE: LazyLock<SymbolTable> = LazyLock::new(SymbolTable::new);
36		static EMPTY_ROUTINES: LazyLock<Routines> = LazyLock::new(Routines::empty);
37		static DEFAULT_RUNTIME_CONTEXT: LazyLock<RuntimeContext> =
38			LazyLock::new(|| RuntimeContext::with_clock(Clock::Real));
39
40		EvalContext {
41			target: None,
42			batch: empty_batch(),
43			row_count: 1,
44			take: None,
45			params: &EMPTY_PARAMS,
46			symbols: &EMPTY_SYMBOL_TABLE,
47			routines: &EMPTY_ROUTINES,
48			runtime_context: &DEFAULT_RUNTIME_CONTEXT,
49			identity: IdentityId::system(),
50		}
51	}
52
53	pub fn with_eval(&self, batch: RecordBatch, row_count: usize) -> EvalContext<'a> {
54		EvalContext {
55			target: None,
56			batch,
57			row_count,
58			take: None,
59			params: self.params,
60			symbols: self.symbols,
61			routines: self.routines,
62			runtime_context: self.runtime_context,
63			identity: self.identity,
64		}
65	}
66
67	pub fn with_eval_empty(&self) -> EvalContext<'a> {
68		self.with_eval(empty_batch(), 1)
69	}
70
71	pub fn with_eval_join(&self, batch: RecordBatch) -> EvalContext<'a> {
72		let mut ctx = self.with_eval(batch, 1);
73		ctx.take = Some(1);
74		ctx
75	}
76
77	pub fn arith(&self) -> ArithContext {
78		ArithContext {
79			target: self.target.clone(),
80			overflow: Overflow::Target,
81		}
82	}
83}