22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
44use prost:: Message ;
5- use vortex_compute:: arithmetic_op;
6- use vortex_compute:: checked_arithmetic_op;
7- use vortex_compute:: compare_op;
5+ use vortex_compute:: arithmetic:: Add ;
6+ use vortex_compute:: arithmetic:: Arithmetic ;
7+ use vortex_compute:: arithmetic:: CheckedArithmetic ;
8+ use vortex_compute:: arithmetic:: Div ;
9+ use vortex_compute:: arithmetic:: Mul ;
10+ use vortex_compute:: arithmetic:: Sub ;
11+ use vortex_compute:: comparison:: Compare ;
12+ use vortex_compute:: comparison:: Equal ;
13+ use vortex_compute:: comparison:: GreaterThan ;
14+ use vortex_compute:: comparison:: GreaterThanOrEqual ;
15+ use vortex_compute:: comparison:: LessThan ;
16+ use vortex_compute:: comparison:: LessThanOrEqual ;
17+ use vortex_compute:: comparison:: NotEqual ;
818use vortex_compute:: logical:: KleeneAnd ;
919use vortex_compute:: logical:: KleeneOr ;
1020use vortex_compute:: logical:: LogicalOp ;
@@ -17,7 +27,6 @@ use vortex_vector::BoolDatum;
1727use vortex_vector:: Datum ;
1828use vortex_vector:: PrimitiveDatum ;
1929
20- use crate :: compute;
2130use crate :: expr:: ChildName ;
2231use crate :: expr:: Operator ;
2332use crate :: expr:: functions:: ArgName ;
@@ -45,7 +54,7 @@ impl VTable for BinaryFn {
4554 }
4655
4756 fn arity ( & self , _options : & Operator ) -> Arity {
48- Arity :: Fixed ( 2 )
57+ Arity :: Exact ( 2 )
4958 }
5059
5160 fn null_handling ( & self , options : & Operator ) -> NullHandling {
@@ -85,62 +94,56 @@ impl VTable for BinaryFn {
8594 let lhs: Datum = args. input_datums ( 0 ) . clone ( ) ;
8695 let rhs: Datum = args. input_datums ( 1 ) . clone ( ) ;
8796
88- if op. is_arithmetic ( ) {
89- execute_arithmetic_primitive ( & lhs. into_primitive ( ) , & rhs. into_primitive ( ) , * op)
90- } else if let Some ( comp) = op. maybe_cmp_operator ( ) {
91- let result = compare_op ! (
92- comp,
93- lhs,
94- rhs,
95- compute:: Operator :: Eq ,
96- compute:: Operator :: NotEq ,
97- compute:: Operator :: Lt ,
98- compute:: Operator :: Lte ,
99- compute:: Operator :: Gt ,
100- compute:: Operator :: Gte
101- ) ;
102- Ok ( result. into ( ) )
103- } else if matches ! ( op, Operator :: And ) {
104- Ok ( <BoolDatum as LogicalOp < KleeneAnd > >:: op ( lhs. into_bool ( ) , rhs. into_bool ( ) ) . into ( ) )
105- } else if matches ! ( op, Operator :: Or ) {
106- Ok ( <BoolDatum as LogicalOp < KleeneOr > >:: op ( lhs. into_bool ( ) , rhs. into_bool ( ) ) . into ( ) )
107- } else {
108- unreachable ! ( "unknown operator type" )
97+ match op {
98+ Operator :: Eq => Ok ( Compare :: < Equal > :: compare ( lhs, rhs) . into ( ) ) ,
99+ Operator :: NotEq => Ok ( Compare :: < NotEqual > :: compare ( lhs, rhs) . into ( ) ) ,
100+ Operator :: Lt => Ok ( Compare :: < LessThan > :: compare ( lhs, rhs) . into ( ) ) ,
101+ Operator :: Lte => Ok ( Compare :: < LessThanOrEqual > :: compare ( lhs, rhs) . into ( ) ) ,
102+ Operator :: Gt => Ok ( Compare :: < GreaterThan > :: compare ( lhs, rhs) . into ( ) ) ,
103+ Operator :: Gte => Ok ( Compare :: < GreaterThanOrEqual > :: compare ( lhs, rhs) . into ( ) ) ,
104+ Operator :: And => Ok ( <BoolDatum as LogicalOp < KleeneAnd > >:: op (
105+ lhs. into_bool ( ) ,
106+ rhs. into_bool ( ) ,
107+ )
108+ . into ( ) ) ,
109+ Operator :: Or => {
110+ Ok ( <BoolDatum as LogicalOp < KleeneOr > >:: op ( lhs. into_bool ( ) , rhs. into_bool ( ) ) . into ( ) )
111+ }
112+ Operator :: Add | Operator :: Sub | Operator :: Mul | Operator :: Div => {
113+ execute_arithmetic_primitive ( lhs. into_primitive ( ) , rhs. into_primitive ( ) , * op)
114+ }
109115 }
110116 }
111117}
112118
113119fn execute_arithmetic_primitive (
114- lhs : & PrimitiveDatum ,
115- rhs : & PrimitiveDatum ,
120+ lhs : PrimitiveDatum ,
121+ rhs : PrimitiveDatum ,
116122 op : Operator ,
117123) -> VortexResult < Datum > {
118124 // Float arithmetic - no overflow checking needed
119125 if lhs. ptype ( ) . is_float ( ) && lhs. ptype ( ) == rhs. ptype ( ) {
120- let result = arithmetic_op ! (
121- op,
122- lhs,
123- rhs,
124- Operator :: Add ,
125- Operator :: Sub ,
126- Operator :: Mul ,
127- Operator :: Div
128- ) ;
126+ let result: PrimitiveDatum = match op {
127+ Operator :: Add => Arithmetic :: < Add > :: eval ( lhs, rhs) ,
128+ Operator :: Sub => Arithmetic :: < Sub > :: eval ( lhs, rhs) ,
129+ Operator :: Mul => Arithmetic :: < Mul > :: eval ( lhs, rhs) ,
130+ Operator :: Div => Arithmetic :: < Div > :: eval ( lhs, rhs) ,
131+ _ => unreachable ! ( "Not an arithmetic operator" ) ,
132+ } ;
129133 return Ok ( result. into ( ) ) ;
130134 }
131135
132136 // Integer arithmetic - use checked operations
133- checked_arithmetic_op ! (
134- op,
135- lhs,
136- rhs,
137- Operator :: Add ,
138- Operator :: Sub ,
139- Operator :: Mul ,
140- Operator :: Div
141- )
142- . map ( |d| d. into ( ) )
143- . ok_or_else ( || vortex_err ! ( "Arithmetic overflow/underflow or type mismatch" ) )
137+ let result: Option < PrimitiveDatum > = match op {
138+ Operator :: Add => CheckedArithmetic :: < Add > :: checked_eval ( lhs, rhs) ,
139+ Operator :: Sub => CheckedArithmetic :: < Sub > :: checked_eval ( lhs, rhs) ,
140+ Operator :: Mul => CheckedArithmetic :: < Mul > :: checked_eval ( lhs, rhs) ,
141+ Operator :: Div => CheckedArithmetic :: < Div > :: checked_eval ( lhs, rhs) ,
142+ _ => unreachable ! ( "Not an arithmetic operator" ) ,
143+ } ;
144+ result
145+ . map ( |d| d. into ( ) )
146+ . ok_or_else ( || vortex_err ! ( "Arithmetic overflow/underflow or type mismatch" ) )
144147}
145148
146149#[ cfg( test) ]
0 commit comments