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