vortex_array/compute/
mod.rs
1use std::any::Any;
10use std::fmt::{Debug, Formatter};
11
12pub use between::{BetweenFn, BetweenOptions, StrictComparison, between};
13pub use binary_numeric::{
14 BinaryNumericFn, add, add_scalar, binary_numeric, div, div_scalar, mul, mul_scalar, sub,
15 sub_scalar,
16};
17pub use boolean::{
18 BinaryBooleanFn, BinaryOperator, and, and_kleene, binary_boolean, or, or_kleene,
19};
20pub use cast::{CastFn, try_cast};
21pub use compare::{CompareFn, Operator, compare, compare_lengths_to_empty, scalar_cmp};
22pub use fill_forward::{FillForwardFn, fill_forward};
23pub use fill_null::{FillNullFn, fill_null};
24pub use filter::*;
25pub use invert::{InvertFn, invert};
26pub use is_constant::*;
27pub use is_sorted::*;
28pub use like::{LikeFn, LikeOptions, like};
29pub use mask::{MaskFn, mask};
30pub use min_max::{MinMaxFn, MinMaxResult, min_max};
31pub use optimize::*;
32pub use scalar_at::{ScalarAtFn, scalar_at};
33pub use search_sorted::*;
34pub use slice::{SliceFn, slice};
35pub use sum::*;
36pub use take::{TakeFn, take, take_into};
37pub use take_from::TakeFromFn;
38pub use to_arrow::*;
39pub use uncompressed_size::*;
40use vortex_dtype::DType;
41use vortex_error::VortexResult;
42use vortex_mask::Mask;
43use vortex_scalar::Scalar;
44
45use crate::arcref::ArcRef;
46use crate::builders::ArrayBuilder;
47use crate::{Array, ArrayRef};
48
49#[cfg(feature = "arbitrary")]
50mod arbitrary;
51mod between;
52mod binary_numeric;
53mod boolean;
54mod cast;
55mod compare;
56#[cfg(feature = "test-harness")]
57pub mod conformance;
58mod fill_forward;
59mod fill_null;
60mod filter;
61mod invert;
62mod is_constant;
63mod is_sorted;
64mod like;
65mod mask;
66mod min_max;
67mod optimize;
68mod scalar_at;
69mod search_sorted;
70mod slice;
71mod sum;
72mod take;
73mod take_from;
74mod to_arrow;
75mod uncompressed_size;
76
77pub trait ComputeFn {
78 fn id(&self) -> ArcRef<str>;
80
81 fn as_any(&self) -> &dyn Any;
83
84 fn invoke<'a>(&self, args: &'a InvocationArgs<'a>) -> VortexResult<Output>;
90
91 fn return_type<'a>(&self, args: &'a InvocationArgs<'a>) -> VortexResult<DType>;
95
96 fn is_elementwise(&self) -> bool;
102}
103
104pub type ComputeFnRef = ArcRef<dyn ComputeFn>;
105
106pub struct InvocationArgs<'a> {
108 pub inputs: &'a [Input<'a>],
109 pub options: Option<&'a dyn Options>,
110}
111
112pub enum Input<'a> {
114 Scalar(&'a Scalar),
115 Array(&'a dyn Array),
116 Mask(&'a Mask),
117 Builder(&'a mut dyn ArrayBuilder),
118}
119
120impl Debug for Input<'_> {
121 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
122 let mut f = f.debug_struct("Input");
123 match self {
124 Input::Scalar(scalar) => f.field("Scalar", scalar),
125 Input::Array(array) => f.field("Array", array),
126 Input::Mask(mask) => f.field("Mask", mask),
127 Input::Builder(builder) => f.field("Builder", &builder.len()),
128 };
129 f.finish()
130 }
131}
132
133impl<'a> Input<'a> {
134 pub fn scalar(&self) -> Option<&'a Scalar> {
135 match self {
136 Input::Scalar(scalar) => Some(*scalar),
137 _ => None,
138 }
139 }
140
141 pub fn array(&self) -> Option<&'a dyn Array> {
142 match self {
143 Input::Array(array) => Some(*array),
144 _ => None,
145 }
146 }
147
148 pub fn mask(&self) -> Option<&'a Mask> {
149 match self {
150 Input::Mask(mask) => Some(*mask),
151 _ => None,
152 }
153 }
154
155 pub fn builder(&'a mut self) -> Option<&'a mut dyn ArrayBuilder> {
156 match self {
157 Input::Builder(builder) => Some(*builder),
158 _ => None,
159 }
160 }
161}
162
163#[derive(Debug)]
165pub enum Output {
166 Scalar(Scalar),
167 Array(ArrayRef),
168}
169
170impl Output {
171 pub fn into_scalar(self) -> Option<Scalar> {
172 match self {
173 Output::Scalar(scalar) => Some(scalar),
174 _ => None,
175 }
176 }
177
178 pub fn into_array(self) -> Option<ArrayRef> {
179 match self {
180 Output::Array(array) => Some(array),
181 _ => None,
182 }
183 }
184}
185
186impl From<ArrayRef> for Output {
187 fn from(value: ArrayRef) -> Self {
188 Output::Array(value)
189 }
190}
191
192impl From<Scalar> for Output {
193 fn from(value: Scalar) -> Self {
194 Output::Scalar(value)
195 }
196}
197
198pub trait Options {
200 fn as_any(&self) -> &dyn Any;
201}
202
203pub trait Kernel {
211 fn invoke<'a>(&self, args: &'a InvocationArgs<'a>) -> VortexResult<Option<Output>>;
212}
213
214pub type KernelRef = ArcRef<dyn Kernel>;