@@ -9,17 +9,20 @@ mod tests {
9
9
use std:: rc:: Rc ;
10
10
11
11
use partiql_logical as logical;
12
- use partiql_logical:: { BindingsExpr , LogicalPlan , PathComponent , ValueExpr } ;
12
+ use partiql_logical:: { BinaryOp , BindingsExpr , LogicalPlan , PathComponent , ValueExpr } ;
13
13
use partiql_value as value;
14
+
15
+ use crate :: env:: basic:: MapBindings ;
16
+ use crate :: plan;
17
+ use ordered_float:: OrderedFloat ;
14
18
use partiql_value:: {
15
19
partiql_bag, partiql_list, partiql_tuple, Bag , BindingsName , List , Tuple , Value ,
16
20
} ;
21
+ use rust_decimal:: Decimal as RustDecimal ;
17
22
18
- use crate :: env:: basic:: MapBindings ;
19
23
use crate :: eval:: {
20
24
DagEvaluator , EvalFromAt , EvalOutputAccumulator , Evaluable , Evaluator , Output ,
21
25
} ;
22
- use crate :: plan;
23
26
24
27
fn evaluate ( logical : BindingsExpr , bindings : MapBindings < Value > ) -> Bag {
25
28
let output = Rc :: new ( RefCell :: new ( EvalOutputAccumulator :: default ( ) ) ) ;
@@ -91,6 +94,23 @@ mod tests {
91
94
bindings
92
95
}
93
96
97
+ fn data_arithmetic_tuple ( ) -> MapBindings < Value > {
98
+ fn tuple_a_to_v ( v : Value ) -> value:: Value {
99
+ Tuple ( HashMap :: from ( [ ( "a" . into ( ) , v) ] ) ) . into ( )
100
+ }
101
+ // <<{'a': <int>}, {'a': <decimal>}, {'a': <float>}, {'a': NULL}, {'a': MISSING}>>
102
+ let data = partiql_list ! [
103
+ tuple_a_to_v( Value :: Integer ( 1 ) ) ,
104
+ tuple_a_to_v( Value :: Decimal ( RustDecimal :: from( 1 ) ) ) ,
105
+ tuple_a_to_v( Value :: Real ( OrderedFloat ( 1.5 ) ) ) ,
106
+ tuple_a_to_v( Value :: Null ) ,
107
+ tuple_a_to_v( Value :: Missing ) ,
108
+ ] ;
109
+ let mut bindings = MapBindings :: default ( ) ;
110
+ bindings. insert ( "data" , data. into ( ) ) ;
111
+ bindings
112
+ }
113
+
94
114
#[ test]
95
115
fn select ( ) {
96
116
// Plan for `select a as b from data`
@@ -116,6 +136,124 @@ mod tests {
116
136
assert_eq ! ( result. len( ) , 3 ) ;
117
137
}
118
138
139
+ #[ test]
140
+ fn arithmetic ( ) {
141
+ // Tests arithmetic ops using int, real, decimal, null, and missing with values defined from
142
+ // `data_arithmetic_tuple`
143
+ fn arithmetic_logical ( binary_op : BinaryOp , lit : Value ) -> BindingsExpr {
144
+ BindingsExpr :: From ( logical:: From {
145
+ expr : ValueExpr :: VarRef ( BindingsName :: CaseInsensitive ( "data" . into ( ) ) ) ,
146
+ as_key : "data" . to_string ( ) ,
147
+ at_key : None ,
148
+ out : Box :: new ( BindingsExpr :: Select ( logical:: Select {
149
+ exprs : HashMap :: from ( [ (
150
+ "b" . to_string ( ) ,
151
+ ValueExpr :: BinaryExpr (
152
+ binary_op,
153
+ Box :: new ( ValueExpr :: Path (
154
+ Box :: new ( ValueExpr :: VarRef ( BindingsName :: CaseInsensitive (
155
+ "data" . into ( ) ,
156
+ ) ) ) ,
157
+ vec ! [ PathComponent :: Key ( "a" . to_string( ) ) ] ,
158
+ ) ) ,
159
+ Box :: new ( ValueExpr :: Lit ( Box :: new ( lit) ) ) ,
160
+ ) ,
161
+ ) ] ) ,
162
+ out : Box :: new ( BindingsExpr :: Output ) ,
163
+ } ) ) ,
164
+ } )
165
+ }
166
+ // Plan for `select a + <lit> as b from data`
167
+ println ! ( "Add+++++++++++++++++++++++++++++" ) ;
168
+ let logical = arithmetic_logical ( BinaryOp :: Add , Value :: Integer ( 1 ) ) ;
169
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
170
+ println ! ( "{:?}" , result) ;
171
+ let logical = arithmetic_logical ( BinaryOp :: Add , Value :: Decimal ( RustDecimal :: new ( 11 , 1 ) ) ) ;
172
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
173
+ println ! ( "{:?}" , result) ;
174
+ let logical = arithmetic_logical ( BinaryOp :: Add , Value :: Real ( OrderedFloat ( 1.5 ) ) ) ;
175
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
176
+ println ! ( "{:?}" , result) ;
177
+ let logical = arithmetic_logical ( BinaryOp :: Add , Value :: Null ) ;
178
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
179
+ println ! ( "{:?}" , result) ;
180
+ let logical = arithmetic_logical ( BinaryOp :: Add , Value :: Missing ) ;
181
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
182
+ println ! ( "{:?}" , result) ;
183
+
184
+ // Plan for `select a - <lit> as b from data`
185
+ println ! ( "Sub-----------------------------" ) ;
186
+ let logical = arithmetic_logical ( BinaryOp :: Sub , Value :: Integer ( 1 ) ) ;
187
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
188
+ println ! ( "{:?}" , result) ;
189
+ let logical = arithmetic_logical ( BinaryOp :: Sub , Value :: Decimal ( RustDecimal :: new ( 11 , 1 ) ) ) ;
190
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
191
+ println ! ( "{:?}" , result) ;
192
+ let logical = arithmetic_logical ( BinaryOp :: Sub , Value :: Real ( OrderedFloat ( 1.5 ) ) ) ;
193
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
194
+ println ! ( "{:?}" , result) ;
195
+ let logical = arithmetic_logical ( BinaryOp :: Sub , Value :: Null ) ;
196
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
197
+ println ! ( "{:?}" , result) ;
198
+ let logical = arithmetic_logical ( BinaryOp :: Sub , Value :: Missing ) ;
199
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
200
+ println ! ( "{:?}" , result) ;
201
+
202
+ // Plan for `select a * <lit> as b from data`
203
+ println ! ( "Mul*****************************" ) ;
204
+ let logical = arithmetic_logical ( BinaryOp :: Mul , Value :: Integer ( 1 ) ) ;
205
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
206
+ println ! ( "{:?}" , result) ;
207
+ let logical = arithmetic_logical ( BinaryOp :: Mul , Value :: Decimal ( RustDecimal :: new ( 11 , 1 ) ) ) ;
208
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
209
+ println ! ( "{:?}" , result) ;
210
+ let logical = arithmetic_logical ( BinaryOp :: Mul , Value :: Real ( OrderedFloat ( 1.5 ) ) ) ;
211
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
212
+ println ! ( "{:?}" , result) ;
213
+ let logical = arithmetic_logical ( BinaryOp :: Mul , Value :: Null ) ;
214
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
215
+ println ! ( "{:?}" , result) ;
216
+ let logical = arithmetic_logical ( BinaryOp :: Mul , Value :: Missing ) ;
217
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
218
+ println ! ( "{:?}" , result) ;
219
+
220
+ // Plan for `select a / <lit> as b from data`
221
+ println ! ( "Div/////////////////////////////" ) ;
222
+ let logical = arithmetic_logical ( BinaryOp :: Div , Value :: Integer ( 1 ) ) ;
223
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
224
+ println ! ( "{:?}" , result) ;
225
+ let logical = arithmetic_logical ( BinaryOp :: Div , Value :: Decimal ( RustDecimal :: new ( 11 , 1 ) ) ) ;
226
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
227
+ println ! ( "{:?}" , result) ;
228
+ let logical = arithmetic_logical ( BinaryOp :: Div , Value :: Real ( OrderedFloat ( 1.5 ) ) ) ;
229
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
230
+ println ! ( "{:?}" , result) ;
231
+ let logical = arithmetic_logical ( BinaryOp :: Div , Value :: Null ) ;
232
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
233
+ println ! ( "{:?}" , result) ;
234
+ let logical = arithmetic_logical ( BinaryOp :: Div , Value :: Missing ) ;
235
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
236
+ println ! ( "{:?}" , result) ;
237
+
238
+ // Plan for `select a % <lit> as b from data`
239
+ println ! ( "Mod%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" ) ;
240
+ let logical = arithmetic_logical ( BinaryOp :: Mod , Value :: Integer ( 1 ) ) ;
241
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
242
+ println ! ( "{:?}" , result) ;
243
+ let logical = arithmetic_logical ( BinaryOp :: Mod , Value :: Decimal ( RustDecimal :: new ( 11 , 1 ) ) ) ;
244
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
245
+ println ! ( "{:?}" , result) ;
246
+ let logical = arithmetic_logical ( BinaryOp :: Mod , Value :: Real ( OrderedFloat ( 1.5 ) ) ) ;
247
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
248
+ println ! ( "{:?}" , result) ;
249
+ let logical = arithmetic_logical ( BinaryOp :: Mod , Value :: Null ) ;
250
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
251
+ println ! ( "{:?}" , result) ;
252
+ let logical = arithmetic_logical ( BinaryOp :: Mod , Value :: Missing ) ;
253
+ let result = evaluate ( logical, data_arithmetic_tuple ( ) ) ;
254
+ println ! ( "{:?}" , result) ;
255
+ }
256
+
119
257
#[ test]
120
258
fn select_dag ( ) {
121
259
// Plan for `select a as b from data`
0 commit comments