22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
44use vortex_dtype:: DType ;
5- use vortex_error:: VortexResult ;
5+ use vortex_error:: { VortexResult , vortex_ensure } ;
66
77use crate :: expr:: Expression ;
88use crate :: stats:: ArrayStats ;
99use crate :: { Array , ArrayRef } ;
1010
11+ /// A array that represents an expression to be evaluated over a child array.
12+ ///
13+ /// `ExprArray` enables deferred evaluation of expressions by wrapping a child array
14+ /// with an expression that operates on it. The expression is not evaluated until the
15+ /// array is canonicalized/executed.
16+ ///
17+ /// # Examples
18+ ///
19+ /// ```ignore
20+ /// // Create an expression that filters an integer array
21+ /// let data = PrimitiveArray::from_iter([1, 2, 3, 4, 5]);
22+ /// let expr = gt(root(), lit(3)); // $ > 3
23+ /// let expr_array = ExprArray::new_infer_dtype(data.into_array(), expr)?;
24+ ///
25+ /// // The expression is evaluated when canonicalized
26+ /// let result = expr_array.to_canonical(); // Returns BoolArray([false, false, false, true, true])
27+ /// ```
28+ ///
29+ /// # Type Safety
30+ ///
31+ /// The `dtype` field must match `expr.return_dtype(child.dtype())`. This invariant
32+ /// is enforced by the safe constructors ([`try_new`](ExprArray::try_new) and
33+ /// [`new_infer_dtype`](ExprArray::new_infer_dtype)) but can be bypassed
34+ /// with [`unchecked_new`](ExprArray::unchecked_new) for performance-critical code.
1135#[ derive( Clone , Debug ) ]
1236pub struct ExprArray {
37+ /// The underlying array that the expression will operate on.
1338 pub ( super ) child : ArrayRef ,
39+ /// The expression to evaluate over the child array.
1440 pub ( super ) expr : Expression ,
41+ /// The data type of the result after evaluating the expression.
1542 pub ( super ) dtype : DType ,
43+ /// Statistics about the resulting array (may be computed lazily).
1644 pub ( super ) stats : ArrayStats ,
1745}
1846
1947impl ExprArray {
2048 /// Creates a new ExprArray with the dtype validated to match the expression's return type.
2149 pub fn try_new ( child : ArrayRef , expr : Expression , dtype : DType ) -> VortexResult < Self > {
22- assert_eq ! ( dtype, expr. return_dtype( child. dtype( ) ) ?) ;
50+ let expected_dtype = expr. return_dtype ( child. dtype ( ) ) ?;
51+ vortex_ensure ! (
52+ dtype == expected_dtype,
53+ "ExprArray dtype mismatch: expected {}, got {}" ,
54+ expected_dtype,
55+ dtype
56+ ) ;
2357 Ok ( unsafe { Self :: unchecked_new ( child, expr, dtype) } )
2458 }
2559
@@ -34,6 +68,7 @@ impl ExprArray {
3468 child,
3569 expr,
3670 dtype,
71+ // TODO(joe): Propagate or compute statistics from the child array and expression.
3772 stats : ArrayStats :: default ( ) ,
3873 }
3974 }
0 commit comments