Skip to main content

reifydb_engine/expression/
context.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::sync::LazyLock;
5
6use reifydb_core::{
7	interface::{
8		catalog::property::{ColumnPropertyKind, ColumnSaturationPolicy, DEFAULT_COLUMN_SATURATION_POLICY},
9		evaluate::TargetColumn,
10	},
11	value::column::columns::Columns,
12};
13use reifydb_function::registry::Functions;
14use reifydb_runtime::clock::Clock;
15use reifydb_type::{params::Params, value::identity::IdentityId};
16
17use crate::{arena::QueryArena, vm::stack::SymbolTable};
18
19pub struct EvalContext<'a> {
20	pub target: Option<TargetColumn>,
21	pub columns: Columns,
22	pub row_count: usize,
23	pub take: Option<usize>,
24	pub params: &'a Params,
25	pub symbol_table: &'a SymbolTable,
26	// TODO: This is a temporary hack to support aggregate functions in StandardColumnEvaluator
27	// Should be replaced with proper function detection or separate aggregation methods
28	pub is_aggregate_context: bool,
29	pub functions: &'a Functions,
30	pub clock: &'a Clock,
31	pub arena: Option<&'a QueryArena>,
32	pub identity: IdentityId,
33}
34
35impl<'a> EvalContext<'a> {
36	pub fn testing() -> Self {
37		static EMPTY_PARAMS: LazyLock<Params> = LazyLock::new(|| Params::None);
38		static EMPTY_SYMBOL_TABLE: LazyLock<SymbolTable> = LazyLock::new(|| SymbolTable::new());
39		static EMPTY_FUNCTIONS: LazyLock<Functions> = LazyLock::new(|| Functions::empty());
40		static DEFAULT_CLOCK: LazyLock<Clock> = LazyLock::new(|| Clock::default());
41		Self {
42			target: None,
43			columns: Columns::empty(),
44			row_count: 1,
45			take: None,
46			params: &EMPTY_PARAMS,
47			symbol_table: &EMPTY_SYMBOL_TABLE,
48			is_aggregate_context: false,
49			functions: &EMPTY_FUNCTIONS,
50			clock: &DEFAULT_CLOCK,
51			arena: None,
52			identity: IdentityId::root(),
53		}
54	}
55
56	pub(crate) fn saturation_policy(&self) -> ColumnSaturationPolicy {
57		self.target
58			.as_ref()
59			.and_then(|t| {
60				t.properties().into_iter().find_map(|p| match p {
61					ColumnPropertyKind::Saturation(policy) => Some(policy),
62				})
63			})
64			.unwrap_or(DEFAULT_COLUMN_SATURATION_POLICY.clone())
65	}
66}
67
68/// Compile-time context for resolving functions and UDFs.
69pub struct CompileContext<'a> {
70	pub functions: &'a Functions,
71	pub symbol_table: &'a SymbolTable,
72}