Skip to main content

reifydb_engine/expression/
eval.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::Column;
5use reifydb_rql::expression::Expression;
6
7use crate::{
8	Result,
9	expression::{
10		cast::cast_column_data,
11		compile::compile_expression,
12		context::{CompileContext, EvalContext},
13	},
14};
15
16pub fn evaluate(ctx: &EvalContext, expr: &Expression) -> Result<Column> {
17	let compile_ctx = CompileContext {
18		functions: ctx.functions,
19		symbols: ctx.symbols,
20	};
21	let compiled = compile_expression(&compile_ctx, expr)?;
22	let column = compiled.execute(ctx)?;
23
24	// Ensures that result column data type matches the expected target column type
25	if let Some(ty) = ctx.target.as_ref().map(|c| c.column_type()) {
26		let data = cast_column_data(ctx, &column.data(), ty, &expr.lazy_fragment())?;
27		Ok(Column {
28			name: column.name,
29			data,
30		})
31	} else {
32		Ok(column)
33	}
34}