1use 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
34pub trait ArrayBuiltins: Sized {
36 fn cast(&self, dtype: DType) -> VortexResult<ArrayRef>;
38
39 fn fill_null(&self, fill_value: impl Into<Scalar>) -> VortexResult<ArrayRef>;
41
42 fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<ArrayRef>;
44
45 fn is_not_null(&self) -> VortexResult<ArrayRef>;
47
48 fn mask(self, mask: ArrayRef) -> VortexResult<ArrayRef>;
52
53 fn not(&self) -> VortexResult<ArrayRef>;
55
56 fn interleave(
61 &self,
62 values: impl IntoIterator<Item = ArrayRef>,
63 row_indices: ArrayRef,
64 ) -> VortexResult<ArrayRef>;
65
66 fn compare(&self, rhs: ArrayRef, op: CompareOperator) -> VortexResult<ArrayRef>;
68
69 fn boolean(&self, rhs: ArrayRef, op: BooleanOperator) -> VortexResult<ArrayRef>;
71
72 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}