alopex_sql/executor/evaluator/
mod.rs

1//! Expression evaluator for typed expressions.
2//!
3//! Provides a lightweight, zero-allocation evaluator over typed expressions
4//! emitted by the planner. The evaluator operates on a borrowed row slice
5//! via [`EvalContext`] and returns [`SqlValue`] results or [`ExecutorError`].
6
7mod binary_op;
8mod column_ref;
9mod context;
10mod function_call;
11mod is_null;
12mod literal;
13mod unary_op;
14pub mod vector_ops;
15
16pub use vector_ops::{VectorError, VectorMetric, vector_distance, vector_similarity};
17
18pub use context::EvalContext;
19
20use crate::executor::{EvaluationError, ExecutorError, Result};
21use crate::planner::typed_expr::TypedExpr;
22use crate::planner::typed_expr::TypedExprKind;
23use crate::storage::SqlValue;
24
25/// Evaluate a typed expression against the provided evaluation context.
26pub fn evaluate(expr: &TypedExpr, ctx: &EvalContext<'_>) -> Result<SqlValue> {
27    match &expr.kind {
28        TypedExprKind::Literal(lit) => literal::eval_literal(lit, &expr.resolved_type),
29        TypedExprKind::ColumnRef { column_index, .. } => {
30            column_ref::eval_column_ref(*column_index, ctx)
31        }
32        TypedExprKind::BinaryOp { left, op, right } => {
33            binary_op::eval_binary_op(op, left, right, ctx)
34        }
35        TypedExprKind::UnaryOp { op, operand } => unary_op::eval_unary_op(op, operand, ctx),
36        TypedExprKind::IsNull { expr, negated } => is_null::eval_is_null(expr, *negated, ctx),
37        TypedExprKind::VectorLiteral(values) => {
38            Ok(SqlValue::Vector(values.iter().map(|v| *v as f32).collect()))
39        }
40        TypedExprKind::FunctionCall { name, args } => {
41            function_call::evaluate_function_call(name, args, ctx)
42        }
43        // Unsupported expressions return a clear error message.
44        other => Err(ExecutorError::Evaluation(
45            EvaluationError::UnsupportedExpression(format!("{other:?}")),
46        )),
47    }
48}