Skip to main content

vortex_array/
builtins.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! A collection of built-in common scalar functions.
5//!
6//! It is expected that each Vortex integration may provide its own set of scalar functions with
7//! semantics that exactly match the underlying system (e.g. SQL engine, DataFrame library, etc).
8//!
9//! This set of functions should cover the basics, and in general leans towards the semantics of
10//! the equivalent Arrow compute function.
11
12use vortex_error::VortexResult;
13
14use crate::ArrayRef;
15use crate::IntoArray;
16use crate::arrays::ConstantArray;
17use crate::arrays::InterleaveArray;
18use crate::dtype::DType;
19use crate::dtype::FieldName;
20use crate::scalar::NumericOperator;
21use crate::scalar::Scalar;
22use crate::scalar_fn::builtin::boolean::Boolean;
23use crate::scalar_fn::builtin::cast::Cast;
24use crate::scalar_fn::builtin::compare::Compare;
25use crate::scalar_fn::builtin::fill_null::FillNull;
26use crate::scalar_fn::builtin::get_item::GetItem;
27use crate::scalar_fn::builtin::is_not_null::IsNotNull;
28use crate::scalar_fn::builtin::mask::Mask;
29use crate::scalar_fn::builtin::not::Not;
30use crate::scalar_fn::builtin::numeric::Numeric;
31use crate::scalar_fn::builtin::operators::BooleanOperator;
32use crate::scalar_fn::builtin::operators::CompareOperator;
33
34/// A collection of built-in scalar functions that can be applied to expressions or arrays.
35pub trait ArrayBuiltins: Sized {
36    /// Cast to the given data type.
37    fn cast(&self, dtype: DType) -> VortexResult<ArrayRef>;
38
39    /// Replace null values with the given fill value.
40    fn fill_null(&self, fill_value: impl Into<Scalar>) -> VortexResult<ArrayRef>;
41
42    /// Get item by field name (for struct types).
43    fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<ArrayRef>;
44
45    /// Is not null check.
46    fn is_not_null(&self) -> VortexResult<ArrayRef>;
47
48    /// Mask the array using the given boolean mask.
49    /// The resulting array's validity is the intersection of the original array's validity
50    /// and the mask's validity.
51    fn mask(self, mask: ArrayRef) -> VortexResult<ArrayRef>;
52
53    /// Boolean negation.
54    fn not(&self) -> VortexResult<ArrayRef>;
55
56    /// Random-access gather by `(array_index, row_index)`: output row `i` is taken from
57    /// `values[array_indices[i]][row_indices[i]]`, where `self` is the (non-nullable)
58    /// `array_indices` selector and `row_indices` names the position within the selected value.
59    /// See [`InterleaveArray`].
60    fn interleave(
61        &self,
62        values: impl IntoIterator<Item = ArrayRef>,
63        row_indices: ArrayRef,
64    ) -> VortexResult<ArrayRef>;
65
66    /// Compare this array with another.
67    fn compare(&self, rhs: ArrayRef, op: CompareOperator) -> VortexResult<ArrayRef>;
68
69    /// Apply a Kleene boolean operator to this array and another.
70    fn boolean(&self, rhs: ArrayRef, op: BooleanOperator) -> VortexResult<ArrayRef>;
71
72    /// Apply an arithmetic operator to this array and another.
73    fn numeric(&self, rhs: ArrayRef, op: NumericOperator) -> VortexResult<ArrayRef>;
74}
75
76impl ArrayBuiltins for ArrayRef {
77    fn cast(&self, dtype: DType) -> VortexResult<ArrayRef> {
78        if self.dtype() == &dtype {
79            return Ok(self.clone());
80        }
81        Ok(Cast::try_new(self.clone(), dtype)?.into_array())
82    }
83
84    fn fill_null(&self, fill_value: impl Into<Scalar>) -> VortexResult<ArrayRef> {
85        let fill_value = fill_value.into();
86        if !self.dtype().is_nullable() {
87            return self.cast(FillNull::output_dtype(self.dtype(), fill_value.dtype())?);
88        }
89        Ok(FillNull::try_new(
90            self.clone(),
91            ConstantArray::new(fill_value, self.len()).into_array(),
92        )?
93        .into_array())
94    }
95
96    fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<ArrayRef> {
97        Ok(GetItem::try_new(self.clone(), field_name)?.into_array())
98    }
99
100    fn is_not_null(&self) -> VortexResult<ArrayRef> {
101        Ok(IsNotNull::new(self.clone()).into_array())
102    }
103
104    fn mask(self, mask: ArrayRef) -> VortexResult<ArrayRef> {
105        Ok(Mask::try_new(self, mask)?.into_array())
106    }
107
108    fn not(&self) -> VortexResult<ArrayRef> {
109        Ok(Not::try_new(self.clone())?.into_array())
110    }
111
112    fn interleave(
113        &self,
114        values: impl IntoIterator<Item = ArrayRef>,
115        row_indices: ArrayRef,
116    ) -> VortexResult<ArrayRef> {
117        Ok(
118            InterleaveArray::try_new(values.into_iter().collect(), self.clone(), row_indices)?
119                .into_array(),
120        )
121    }
122
123    fn compare(&self, rhs: ArrayRef, op: CompareOperator) -> VortexResult<ArrayRef> {
124        Ok(Compare::try_new(self.clone(), rhs, op)?.into_array())
125    }
126
127    fn boolean(&self, rhs: ArrayRef, op: BooleanOperator) -> VortexResult<ArrayRef> {
128        Ok(Boolean::try_new(self.clone(), rhs, op)?.into_array())
129    }
130
131    fn numeric(&self, rhs: ArrayRef, op: NumericOperator) -> VortexResult<ArrayRef> {
132        Ok(Numeric::try_new(self.clone(), rhs, op)?.into_array())
133    }
134}