Skip to main content

vortex_functions/
builtins.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Builder methods for this crate's scalar functions, complementing
5//! [`ExprBuiltins`](vortex_expr::ExprBuiltins) and
6//! [`ArrayBuiltins`](vortex_array::builtins::ArrayBuiltins).
7
8use vortex_array::ArrayRef;
9use vortex_array::IntoArray;
10use vortex_array::scalar_fn::EmptyOptions;
11use vortex_error::VortexResult;
12use vortex_expr::Expression;
13use vortex_expr::ScalarFnExprExt;
14
15use crate::between::Between;
16use crate::between::BetweenOptions;
17use crate::is_null::IsNull;
18use crate::list_contains::ListContains;
19use crate::list_contains::ListContainsOptions;
20use crate::zip::Zip;
21
22/// Builder methods on [`Expression`] for this crate's scalar functions.
23pub trait ExprFunctions: Sized {
24    /// Conditional selection: `result[i] = if mask[i] then if_true[i] else if_false[i]`.
25    fn zip(&self, if_true: Expression, if_false: Expression) -> VortexResult<Expression>;
26
27    /// Check if a list contains a value.
28    fn list_contains(&self, value: Expression) -> VortexResult<Expression>;
29
30    /// Is null check.
31    fn is_null(&self) -> VortexResult<Expression>;
32}
33
34impl ExprFunctions for Expression {
35    fn zip(&self, if_true: Expression, if_false: Expression) -> VortexResult<Expression> {
36        Zip.try_new_expr(EmptyOptions, [if_true, if_false, self.clone()])
37    }
38
39    fn list_contains(&self, value: Expression) -> VortexResult<Expression> {
40        ListContains.try_new_expr(ListContainsOptions::default(), [self.clone(), value])
41    }
42
43    fn is_null(&self) -> VortexResult<Expression> {
44        IsNull.try_new_expr(EmptyOptions, [self.clone()])
45    }
46}
47
48/// Builder methods on [`ArrayRef`] for this crate's scalar functions.
49pub trait ArrayFunctions: Sized {
50    /// Conditional selection: `result[i] = if mask[i] then if_true[i] else if_false[i]`.
51    fn zip(&self, if_true: ArrayRef, if_false: ArrayRef) -> VortexResult<ArrayRef>;
52
53    /// Check if a list contains a value.
54    fn list_contains(&self, value: ArrayRef) -> VortexResult<ArrayRef>;
55
56    /// Is null check.
57    fn is_null(&self) -> VortexResult<ArrayRef>;
58
59    /// Compare a values between lower </<= value </<= upper
60    fn between(
61        self,
62        lower: ArrayRef,
63        upper: ArrayRef,
64        options: BetweenOptions,
65    ) -> VortexResult<ArrayRef>;
66}
67
68impl ArrayFunctions for ArrayRef {
69    fn zip(&self, if_true: ArrayRef, if_false: ArrayRef) -> VortexResult<ArrayRef> {
70        Ok(Zip::try_new(if_true, if_false, self.clone())?.into_array())
71    }
72
73    fn list_contains(&self, value: ArrayRef) -> VortexResult<ArrayRef> {
74        Ok(ListContains::try_new(self.clone(), value)?.into_array())
75    }
76
77    fn is_null(&self) -> VortexResult<ArrayRef> {
78        Ok(IsNull::new(self.clone()).into_array())
79    }
80
81    fn between(
82        self,
83        lower: ArrayRef,
84        upper: ArrayRef,
85        options: BetweenOptions,
86    ) -> VortexResult<ArrayRef> {
87        Ok(Between::try_new(self, lower, upper, options)?.into_array())
88    }
89}