vortex_array/scalar_fn/builtin/numeric/
mod.rs1mod checked;
15mod decimal;
16mod primitive;
17mod row;
18
19use std::fmt::Formatter;
20
21use decimal::execute_numeric_decimal;
22use prost::Message;
23use row::execute_numeric_primitive;
24use vortex_error::VortexResult;
25use vortex_error::vortex_bail;
26use vortex_error::vortex_ensure;
27use vortex_error::vortex_ensure_eq;
28use vortex_session::VortexSession;
29use vortex_session::registry::CachedId;
30
31use crate::ArrayRef;
32use crate::Canonical;
33use crate::ExecutionCtx;
34use crate::IntoArray;
35use crate::arrays::ScalarFnArray;
36use crate::dtype::DType;
37use crate::proto::expr as pb;
38use crate::scalar::NumericOperator;
39pub(crate) use crate::scalar::decimal_numeric_result_dtype as numeric_op_result_decimal_dtype;
40use crate::scalar_fn::Arity;
41use crate::scalar_fn::ChildName;
42use crate::scalar_fn::ExecutionArgs;
43use crate::scalar_fn::ScalarFnExecutionResult;
44use crate::scalar_fn::ScalarFnId;
45use crate::scalar_fn::ScalarFnVTable;
46use crate::scalar_fn::ScalarFnVTableExt;
47use crate::scalar_fn::builtin::binary::binary_child_name;
48use crate::scalar_fn::builtin::binary::execute_strict_step;
49use crate::scalar_fn::builtin::binary::fmt_infix;
50use crate::scalar_fn::display::ExprDisplay;
51
52#[derive(Clone)]
57pub struct Numeric;
58
59impl Numeric {
60 pub fn try_new(
66 lhs: ArrayRef,
67 rhs: ArrayRef,
68 operator: NumericOperator,
69 ) -> VortexResult<ScalarFnArray> {
70 ScalarFnArray::try_new(Numeric.bind(operator), [lhs, rhs])
71 }
72}
73
74impl ScalarFnVTable for Numeric {
75 type Options = NumericOperator;
76
77 fn id(&self) -> ScalarFnId {
78 static ID: CachedId = CachedId::new("vortex.numeric");
79 *ID
80 }
81
82 fn serialize(&self, operator: &NumericOperator) -> VortexResult<Option<Vec<u8>>> {
83 Ok(Some(
84 pb::NumericOpts {
85 op: pb::numeric_opts::NumericOp::from(*operator).into(),
86 }
87 .encode_to_vec(),
88 ))
89 }
90
91 fn deserialize(
92 &self,
93 metadata: &[u8],
94 _session: &VortexSession,
95 ) -> VortexResult<NumericOperator> {
96 let opts = pb::NumericOpts::decode(metadata)?;
97
98 Ok(pb::numeric_opts::NumericOp::try_from(opts.op)?.into())
99 }
100
101 fn arity(&self, _operator: &NumericOperator) -> Arity {
102 Arity::Exact(2)
103 }
104
105 fn child_name(&self, _operator: &NumericOperator, child_idx: usize) -> ChildName {
106 binary_child_name(child_idx)
107 }
108
109 fn fmt_sql(
110 &self,
111 operator: &NumericOperator,
112 expr: &dyn ExprDisplay,
113 f: &mut Formatter<'_>,
114 ) -> std::fmt::Result {
115 fmt_infix(operator, expr, f)
116 }
117
118 fn return_dtype(
119 &self,
120 operator: &NumericOperator,
121 arg_dtypes: &[DType],
122 ) -> VortexResult<DType> {
123 let lhs = &arg_dtypes[0];
124 let rhs = &arg_dtypes[1];
125
126 if lhs.is_primitive() && lhs.eq_ignore_nullability(rhs) {
127 return Ok(lhs.with_nullability(lhs.nullability() | rhs.nullability()));
128 }
129
130 if let DType::Decimal(decimal_dtype, _) = lhs
131 && lhs.eq_ignore_nullability(rhs)
132 {
133 return Ok(DType::Decimal(
134 numeric_op_result_decimal_dtype(*decimal_dtype, *operator)?,
135 lhs.nullability() | rhs.nullability(),
136 ));
137 }
138
139 vortex_bail!(
140 "incompatible types for arithmetic operation: {} {}",
141 lhs,
142 rhs
143 );
144 }
145
146 fn execute(
147 &self,
148 operator: &NumericOperator,
149 args: &dyn ExecutionArgs,
150 ctx: &mut ExecutionCtx,
151 ) -> VortexResult<ArrayRef> {
152 let lhs = args.get(0)?;
153 let rhs = args.get(1)?;
154
155 execute_numeric(&lhs, &rhs, *operator, ctx)
156 }
157
158 fn execute_step(
159 &self,
160 operator: &NumericOperator,
161 args: &dyn ExecutionArgs,
162 ctx: &mut ExecutionCtx,
163 ) -> VortexResult<ScalarFnExecutionResult> {
164 execute_strict_step(self, operator, args, ctx)
165 }
166
167 fn is_strict(&self, _operator: &NumericOperator) -> bool {
168 true
169 }
170}
171
172pub(crate) fn execute_numeric(
174 lhs: &ArrayRef,
175 rhs: &ArrayRef,
176 op: NumericOperator,
177 ctx: &mut ExecutionCtx,
178) -> VortexResult<ArrayRef> {
179 vortex_ensure!(
180 lhs.dtype().eq_ignore_nullability(rhs.dtype()),
181 "numeric operator requires matching types, got {} and {}",
182 lhs.dtype(),
183 rhs.dtype()
184 );
185
186 let dtype = lhs.dtype();
187 vortex_ensure!(
188 matches!(dtype, DType::Primitive(..) | DType::Decimal(..)),
189 "numeric operator is not supported for dtype {}",
190 dtype
191 );
192
193 vortex_ensure_eq!(
194 lhs.len(),
195 rhs.len(),
196 "numeric operator requires equal lengths"
197 );
198
199 if lhs.is_empty() {
200 return build_empty_result(lhs, rhs, op);
201 }
202
203 match dtype {
204 DType::Primitive(..) => execute_numeric_primitive(lhs, rhs, op, ctx),
205 DType::Decimal(..) => execute_numeric_decimal(lhs, rhs, op, ctx),
206 _ => unreachable!("dtype is either Primitive or Decimal"),
207 }
208}
209
210fn build_empty_result(
211 lhs: &ArrayRef,
212 rhs: &ArrayRef,
213 op: NumericOperator,
214) -> VortexResult<ArrayRef> {
215 let nullability = lhs.dtype().nullability() | rhs.dtype().nullability();
216 let result_dtype = match lhs.dtype() {
217 DType::Primitive(..) => lhs.dtype().with_nullability(nullability),
218 DType::Decimal(decimal_dtype, _) => DType::Decimal(
219 numeric_op_result_decimal_dtype(*decimal_dtype, op)?,
220 nullability,
221 ),
222 _ => unreachable!("dtype is either Primitive or Decimal"),
223 };
224
225 Ok(Canonical::empty(&result_dtype).into_array())
226}
227
228#[cfg(test)]
229mod tests;