Skip to main content

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
7pub(crate) mod binary_op;
8mod column_ref;
9pub(crate) mod conditional;
10mod context;
11mod function_call;
12pub(crate) mod hash;
13mod is_null;
14mod literal;
15pub(crate) mod numeric;
16pub(crate) mod pattern;
17pub mod registry;
18pub(crate) mod string;
19pub(crate) mod type_fn;
20mod unary_op;
21pub mod vector_ops;
22
23pub use vector_ops::{VectorError, VectorMetric, vector_distance, vector_similarity};
24
25pub use context::EvalContext;
26
27use crate::executor::{EvaluationError, ExecutorError, Result};
28use crate::planner::typed_expr::TypedExpr;
29use crate::planner::typed_expr::TypedExprKind;
30use crate::storage::SqlValue;
31
32/// Evaluate a typed expression against the provided evaluation context.
33pub fn evaluate(expr: &TypedExpr, ctx: &EvalContext<'_>) -> Result<SqlValue> {
34    match &expr.kind {
35        TypedExprKind::Literal(lit) => literal::eval_literal(lit, &expr.resolved_type),
36        TypedExprKind::ColumnRef { column_index, .. } => {
37            column_ref::eval_column_ref(*column_index, ctx)
38        }
39        TypedExprKind::BinaryOp { left, op, right } => {
40            binary_op::eval_binary_op(op, left, right, ctx)
41        }
42        TypedExprKind::UnaryOp { op, operand } => unary_op::eval_unary_op(op, operand, ctx),
43        TypedExprKind::IsNull { expr, negated } => is_null::eval_is_null(expr, *negated, ctx),
44        TypedExprKind::VectorLiteral(values) => {
45            Ok(SqlValue::Vector(values.iter().map(|v| *v as f32).collect()))
46        }
47        TypedExprKind::FunctionCall {
48            name,
49            args,
50            distinct,
51            star,
52        } => function_call::evaluate_function_call(name, args, *distinct, *star, ctx),
53        TypedExprKind::Like {
54            expr,
55            pattern,
56            escape,
57            negated,
58            kind,
59        } => pattern::evaluate_pattern(expr, pattern, escape.as_deref(), *negated, *kind, ctx),
60        // Unsupported expressions return a clear error message.
61        other => Err(ExecutorError::Evaluation(
62            EvaluationError::UnsupportedExpression(format!("{other:?}")),
63        )),
64    }
65}