Skip to main content

vortex_expr/
builtins.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Expression constructors for the scalar functions vortex-array is built on, as methods.
5
6use vortex_array::dtype::DType;
7use vortex_array::dtype::FieldName;
8use vortex_array::scalar::NumericOperator;
9use vortex_array::scalar_fn::EmptyOptions;
10use vortex_array::scalar_fn::builtin::boolean::Boolean;
11use vortex_array::scalar_fn::builtin::cast::Cast;
12use vortex_array::scalar_fn::builtin::compare::Compare;
13use vortex_array::scalar_fn::builtin::fill_null::FillNull;
14use vortex_array::scalar_fn::builtin::get_item::GetItem;
15use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull;
16use vortex_array::scalar_fn::builtin::mask::Mask;
17use vortex_array::scalar_fn::builtin::not::Not;
18use vortex_array::scalar_fn::builtin::numeric::Numeric;
19use vortex_array::scalar_fn::builtin::operators::BooleanOperator;
20use vortex_array::scalar_fn::builtin::operators::CompareOperator;
21use vortex_error::VortexResult;
22
23use crate::Expression;
24use crate::ScalarFnExprExt;
25
26/// Builds expressions over the core scalar functions.
27pub trait ExprBuiltins: Sized {
28    /// Cast to the given data type.
29    fn cast(&self, dtype: DType) -> VortexResult<Expression>;
30
31    /// Replace null values with the given fill value.
32    fn fill_null(&self, fill_value: Expression) -> VortexResult<Expression>;
33
34    /// Get item by field name (for struct types).
35    fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<Expression>;
36
37    /// Is not null check.
38    fn is_not_null(&self) -> VortexResult<Expression>;
39
40    /// Mask the expression using the given boolean mask.
41    /// The resulting expression's validity is the intersection of the original expression's
42    /// validity.
43    fn mask(&self, mask: Expression) -> VortexResult<Expression>;
44
45    /// Boolean negation.
46    fn not(&self) -> VortexResult<Expression>;
47
48    // TODO(joe): add an `interleave` expression builtin mirroring `ArrayBuiltins::interleave`.
49
50    /// Compare this expression with another.
51    fn compare(&self, rhs: Expression, op: CompareOperator) -> VortexResult<Expression>;
52
53    /// Apply a Kleene boolean operator to this expression and another.
54    fn boolean(&self, rhs: Expression, op: BooleanOperator) -> VortexResult<Expression>;
55
56    /// Apply an arithmetic operator to this expression and another.
57    fn numeric(&self, rhs: Expression, op: NumericOperator) -> VortexResult<Expression>;
58}
59
60impl ExprBuiltins for Expression {
61    fn cast(&self, dtype: DType) -> VortexResult<Expression> {
62        Cast.try_new_expr(dtype, [self.clone()])
63    }
64
65    fn fill_null(&self, fill_value: Expression) -> VortexResult<Expression> {
66        FillNull.try_new_expr(EmptyOptions, [self.clone(), fill_value])
67    }
68
69    fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<Expression> {
70        GetItem.try_new_expr(field_name.into(), [self.clone()])
71    }
72
73    fn is_not_null(&self) -> VortexResult<Expression> {
74        IsNotNull.try_new_expr(EmptyOptions, [self.clone()])
75    }
76
77    fn mask(&self, mask: Expression) -> VortexResult<Expression> {
78        Mask.try_new_expr(EmptyOptions, [self.clone(), mask])
79    }
80
81    fn not(&self) -> VortexResult<Expression> {
82        Not.try_new_expr(EmptyOptions, [self.clone()])
83    }
84
85    fn compare(&self, rhs: Expression, op: CompareOperator) -> VortexResult<Expression> {
86        Compare.try_new_expr(op, [self.clone(), rhs])
87    }
88
89    fn boolean(&self, rhs: Expression, op: BooleanOperator) -> VortexResult<Expression> {
90        Boolean.try_new_expr(op, [self.clone(), rhs])
91    }
92
93    fn numeric(&self, rhs: Expression, op: NumericOperator) -> VortexResult<Expression> {
94        Numeric.try_new_expr(op, [self.clone(), rhs])
95    }
96}