1use {
2 super::{Interval, Key, StringExt},
3 crate::{
4 ast::{DataType, DateTimeField},
5 data::{Tribool, point::Point},
6 result::Result,
7 },
8 binary_op::TryBinaryOperator,
9 chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike},
10 core::ops::Sub,
11 rust_decimal::Decimal,
12 serde::{Deserialize, Serialize},
13 std::{
14 cmp::Ordering,
15 collections::BTreeMap,
16 fmt::Debug,
17 hash::{Hash, Hasher},
18 mem::discriminant,
19 net::IpAddr,
20 },
21};
22
23mod binary_op;
24mod convert;
25mod date;
26mod error;
27mod json;
28mod selector;
29mod to_sql;
30mod uuid;
31
32pub use {
33 error::{NumericBinaryOperator, ValueError},
34 json::BTreeMapJsonExt,
35};
36
37pub(crate) use {
38 date::{parse_date, parse_time, parse_timestamp},
39 uuid::parse_uuid,
40};
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub enum Value {
44 Bool(bool),
45 I8(i8),
46 I16(i16),
47 I32(i32),
48 I64(i64),
49 I128(i128),
50 U8(u8),
51 U16(u16),
52 U32(u32),
53 U64(u64),
54 U128(u128),
55 F32(f32),
56 F64(f64),
57 Decimal(Decimal),
58 Str(String),
59 Bytea(Vec<u8>),
60 Inet(IpAddr),
61 Date(NaiveDate),
62 Timestamp(NaiveDateTime),
63 Time(NaiveTime),
64 Interval(Interval),
65 Uuid(u128),
66 Map(BTreeMap<String, Value>),
67 List(Vec<Value>),
68 Point(Point),
69 Null,
70}
71
72impl From<Tribool> for Value {
73 fn from(t: Tribool) -> Self {
74 match t {
75 Tribool::True => Value::Bool(true),
76 Tribool::False => Value::Bool(false),
77 Tribool::Null => Value::Null,
78 }
79 }
80}
81
82impl Value {
83 pub fn evaluate_eq(&self, other: &Value) -> Tribool {
84 use Value::*;
85 match (self, other) {
86 (Null, _) | (_, Null) => Tribool::Null,
87 (I8(l), _) => Tribool::from(l == other),
88 (I16(l), _) => Tribool::from(l == other),
89 (I32(l), _) => Tribool::from(l == other),
90 (I64(l), _) => Tribool::from(l == other),
91 (I128(l), _) => Tribool::from(l == other),
92 (U8(l), _) => Tribool::from(l == other),
93 (U16(l), _) => Tribool::from(l == other),
94 (U32(l), _) => Tribool::from(l == other),
95 (U64(l), _) => Tribool::from(l == other),
96 (U128(l), _) => Tribool::from(l == other),
97 (F32(l), _) => Tribool::from(l == other),
98 (F64(l), _) => Tribool::from(l == other),
99 (Date(l), Timestamp(r)) => Tribool::from(
100 l.and_hms_opt(0, 0, 0)
101 .is_some_and(|date_time| &date_time == r),
102 ),
103 (Timestamp(l), Date(r)) => Tribool::from(
104 r.and_hms_opt(0, 0, 0)
105 .is_some_and(|date_time| l == &date_time),
106 ),
107 _ => Tribool::from(self == other),
108 }
109 }
110
111 pub fn evaluate_cmp(&self, other: &Value) -> Option<Ordering> {
112 match (self, other) {
113 (Value::I8(l), _) => l.partial_cmp(other),
114 (Value::I16(l), _) => l.partial_cmp(other),
115 (Value::I32(l), _) => l.partial_cmp(other),
116 (Value::I64(l), _) => l.partial_cmp(other),
117 (Value::I128(l), _) => l.partial_cmp(other),
118 (Value::U8(l), _) => l.partial_cmp(other),
119 (Value::U16(l), _) => l.partial_cmp(other),
120 (Value::U32(l), _) => l.partial_cmp(other),
121 (Value::U64(l), _) => l.partial_cmp(other),
122 (Value::U128(l), _) => l.partial_cmp(other),
123 (Value::F32(l), _) => l.partial_cmp(other),
124 (Value::F64(l), _) => l.partial_cmp(other),
125 (Value::Decimal(l), Value::Decimal(r)) => Some(l.cmp(r)),
126 (Value::Bool(l), Value::Bool(r)) => Some(l.cmp(r)),
127 (Value::Str(l), Value::Str(r)) => Some(l.cmp(r)),
128 (Value::Bytea(l), Value::Bytea(r)) => Some(l.cmp(r)),
129 (Value::Inet(l), Value::Inet(r)) => Some(l.cmp(r)),
130 (Value::Date(l), Value::Date(r)) => Some(l.cmp(r)),
131 (Value::Date(l), Value::Timestamp(r)) => {
132 l.and_hms_opt(0, 0, 0).map(|date_time| date_time.cmp(r))
133 }
134 (Value::Timestamp(l), Value::Date(r)) => {
135 r.and_hms_opt(0, 0, 0).map(|date_time| l.cmp(&date_time))
136 }
137 (Value::Timestamp(l), Value::Timestamp(r)) => Some(l.cmp(r)),
138 (Value::Time(l), Value::Time(r)) => Some(l.cmp(r)),
139 (Value::Interval(l), Value::Interval(r)) => l.partial_cmp(r),
140 (Value::Uuid(l), Value::Uuid(r)) => Some(l.cmp(r)),
141 _ => None,
142 }
143 }
144
145 pub fn is_zero(&self) -> bool {
146 match self {
147 Value::I8(v) => *v == 0,
148 Value::I16(v) => *v == 0,
149 Value::I32(v) => *v == 0,
150 Value::I64(v) => *v == 0,
151 Value::I128(v) => *v == 0,
152 Value::U8(v) => *v == 0,
153 Value::U16(v) => *v == 0,
154 Value::U32(v) => *v == 0,
155 Value::U64(v) => *v == 0,
156 Value::U128(v) => *v == 0,
157 Value::F32(v) => *v == 0.0,
158 Value::F64(v) => *v == 0.0,
159 Value::Decimal(v) => *v == Decimal::ZERO,
160 _ => false,
161 }
162 }
163
164 pub fn get_type(&self) -> Option<DataType> {
165 match self {
166 Value::I8(_) => Some(DataType::Int8),
167 Value::I16(_) => Some(DataType::Int16),
168 Value::I32(_) => Some(DataType::Int32),
169 Value::I64(_) => Some(DataType::Int),
170 Value::I128(_) => Some(DataType::Int128),
171 Value::U8(_) => Some(DataType::Uint8),
172 Value::U16(_) => Some(DataType::Uint16),
173 Value::U32(_) => Some(DataType::Uint32),
174 Value::U64(_) => Some(DataType::Uint64),
175 Value::U128(_) => Some(DataType::Uint128),
176 Value::F32(_) => Some(DataType::Float32),
177 Value::F64(_) => Some(DataType::Float),
178 Value::Decimal(_) => Some(DataType::Decimal),
179 Value::Bool(_) => Some(DataType::Boolean),
180 Value::Str(_) => Some(DataType::Text),
181 Value::Bytea(_) => Some(DataType::Bytea),
182 Value::Inet(_) => Some(DataType::Inet),
183 Value::Date(_) => Some(DataType::Date),
184 Value::Timestamp(_) => Some(DataType::Timestamp),
185 Value::Time(_) => Some(DataType::Time),
186 Value::Interval(_) => Some(DataType::Interval),
187 Value::Uuid(_) => Some(DataType::Uuid),
188 Value::Map(_) => Some(DataType::Map),
189 Value::List(_) => Some(DataType::List),
190 Value::Point(_) => Some(DataType::Point),
191 Value::Null => None,
192 }
193 }
194
195 pub fn validate_type(&self, data_type: &DataType) -> Result<()> {
196 let valid = self.get_type().is_none_or(|t| t == *data_type);
197
198 if !valid {
199 return Err(ValueError::IncompatibleDataType {
200 data_type: data_type.clone(),
201 value: self.clone(),
202 }
203 .into());
204 }
205
206 Ok(())
207 }
208
209 pub fn validate_null(&self, nullable: bool) -> Result<()> {
210 if !nullable && matches!(self, Value::Null) {
211 return Err(ValueError::NullValueOnNotNullField.into());
212 }
213
214 Ok(())
215 }
216
217 pub fn cast(&self, data_type: &DataType) -> Result<Self> {
218 match (data_type, self) {
219 (DataType::Int8, Value::I8(_))
220 | (DataType::Int16, Value::I16(_))
221 | (DataType::Int32, Value::I32(_))
222 | (DataType::Int, Value::I64(_))
223 | (DataType::Int128, Value::I128(_))
224 | (DataType::Uint8, Value::U8(_))
225 | (DataType::Uint16, Value::U16(_))
226 | (DataType::Uint32, Value::U32(_))
227 | (DataType::Uint64, Value::U64(_))
228 | (DataType::Uint128, Value::U128(_))
229 | (DataType::Float32, Value::F32(_))
230 | (DataType::Float, Value::F64(_))
231 | (DataType::Decimal, Value::Decimal(_))
232 | (DataType::Boolean, Value::Bool(_))
233 | (DataType::Text, Value::Str(_))
234 | (DataType::Bytea, Value::Bytea(_))
235 | (DataType::Inet, Value::Inet(_))
236 | (DataType::Point, Value::Point(_))
237 | (DataType::Date, Value::Date(_))
238 | (DataType::Timestamp, Value::Timestamp(_))
239 | (DataType::Time, Value::Time(_))
240 | (DataType::Interval, Value::Interval(_))
241 | (DataType::Uuid, Value::Uuid(_)) => Ok(self.clone()),
242
243 (_, Value::Null) => Ok(Value::Null),
244
245 (DataType::Boolean, value) => Ok(value.try_into().map(Value::Bool)?),
246 (DataType::Int8, value) => Ok(value.try_into().map(Value::I8)?),
247 (DataType::Int16, value) => Ok(value.try_into().map(Value::I16)?),
248 (DataType::Int32, value) => Ok(value.try_into().map(Value::I32)?),
249 (DataType::Int, value) => Ok(value.try_into().map(Value::I64)?),
250 (DataType::Int128, value) => Ok(value.try_into().map(Value::I128)?),
251 (DataType::Uint8, value) => Ok(value.try_into().map(Value::U8)?),
252 (DataType::Uint16, value) => Ok(value.try_into().map(Value::U16)?),
253 (DataType::Uint32, value) => Ok(value.try_into().map(Value::U32)?),
254 (DataType::Uint64, value) => Ok(value.try_into().map(Value::U64)?),
255 (DataType::Uint128, value) => Ok(value.try_into().map(Value::U128)?),
256 (DataType::Float32, value) => Ok(value.try_into().map(Value::F32)?),
257 (DataType::Float, value) => Ok(value.try_into().map(Value::F64)?),
258 (DataType::Decimal, value) => Ok(value.try_into().map(Value::Decimal)?),
259
260 (DataType::Text, value) => Ok(Value::Str(value.into())),
261
262 (DataType::Date, value) => Ok(value.try_into().map(Value::Date)?),
263 (DataType::Time, value) => Ok(value.try_into().map(Value::Time)?),
264 (DataType::Timestamp, value) => Ok(value.try_into().map(Value::Timestamp)?),
265
266 (DataType::Interval, Value::Str(value)) => Interval::parse(value).map(Value::Interval),
267 (DataType::Uuid, Value::Str(value)) => uuid::parse_uuid(value).map(Value::Uuid),
268
269 (DataType::Uuid, value) => Ok(value.try_into().map(Value::Uuid)?),
270 (DataType::Inet, value) => Ok(value.try_into().map(Value::Inet)?),
271 (DataType::Point, value) => Ok(value.try_into().map(Value::Point)?),
272
273 (DataType::Bytea, Value::Str(value)) => hex::decode(value)
274 .map_err(|_| ValueError::CastFromHexToByteaFailed(value.clone()).into())
275 .map(Value::Bytea),
276 (DataType::List, Value::Str(value)) => Self::parse_json_list(value),
277 (DataType::Map, Value::Str(value)) => Self::parse_json_map(value),
278
279 _ => Err(ValueError::UnimplementedCast {
280 value: self.clone(),
281 data_type: data_type.clone(),
282 }
283 .into()),
284 }
285 }
286
287 #[must_use]
288 pub fn concat(self, other: Value) -> Value {
289 match (self, other) {
290 (Value::Null, _) | (_, Value::Null) => Value::Null,
291 (Value::List(l), Value::List(r)) => Value::List([l, r].concat()),
292 (l, r) => Value::Str(String::from(l) + &String::from(r)),
293 }
294 }
295
296 pub fn add(&self, other: &Value) -> Result<Value> {
297 use Value::*;
298
299 match (self, other) {
300 (I8(a), b) => a.try_add(b),
301 (I16(a), b) => a.try_add(b),
302 (I32(a), b) => a.try_add(b),
303 (I64(a), b) => a.try_add(b),
304 (I128(a), b) => a.try_add(b),
305 (U8(a), b) => a.try_add(b),
306 (U16(a), b) => a.try_add(b),
307 (U32(a), b) => a.try_add(b),
308 (U64(a), b) => a.try_add(b),
309 (U128(a), b) => a.try_add(b),
310 (F32(a), b) => a.try_add(b),
311 (F64(a), b) => a.try_add(b),
312 (Decimal(a), b) => a.try_add(b),
313 (Date(a), Time(b)) => Ok(Timestamp(NaiveDateTime::new(*a, *b))),
314 (Date(a), Interval(b)) => b.add_date(a).map(Timestamp),
315 (Timestamp(a), Interval(b)) => b.add_timestamp(a).map(Timestamp),
316 (Time(a), Interval(b)) => b.add_time(a).map(Time),
317 (Interval(a), Interval(b)) => a.add(b).map(Interval),
318 (
319 Null,
320 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
321 | U128(_) | F32(_) | F64(_) | Decimal(_) | Date(_) | Timestamp(_) | Interval(_)
322 | Null,
323 )
324 | (Date(_) | Timestamp(_) | Time(_) | Interval(_), Null) => Ok(Null),
325 _ => Err(ValueError::NonNumericMathOperation {
326 lhs: self.clone(),
327 operator: NumericBinaryOperator::Add,
328 rhs: other.clone(),
329 }
330 .into()),
331 }
332 }
333
334 pub fn subtract(&self, other: &Value) -> Result<Value> {
335 use {super::Interval as I, Value::*};
336
337 match (self, other) {
338 (I8(a), _) => a.try_subtract(other),
339 (I16(a), _) => a.try_subtract(other),
340 (I32(a), _) => a.try_subtract(other),
341 (I64(a), _) => a.try_subtract(other),
342 (I128(a), _) => a.try_subtract(other),
343 (U8(a), _) => a.try_subtract(other),
344 (U16(a), _) => a.try_subtract(other),
345 (U32(a), _) => a.try_subtract(other),
346 (U64(a), _) => a.try_subtract(other),
347 (U128(a), _) => a.try_subtract(other),
348 (F32(a), _) => a.try_subtract(other),
349 (F64(a), _) => a.try_subtract(other),
350 (Decimal(a), _) => a.try_subtract(other),
351 (Date(a), Date(b)) => Ok(Interval(I::days((*a - *b).num_days() as i32))),
352 (Date(a), Interval(b)) => b.subtract_from_date(a).map(Timestamp),
353 (Timestamp(a), Interval(b)) => b.subtract_from_timestamp(a).map(Timestamp),
354 (Timestamp(a), Timestamp(b)) => a
355 .sub(*b)
356 .num_microseconds()
357 .ok_or_else(|| {
358 ValueError::UnreachableIntegerOverflow(format!("{a:?} - {b:?}")).into()
359 })
360 .map(|v| Interval(I::microseconds(v))),
361 (Time(a), Time(b)) => a
362 .sub(*b)
363 .num_microseconds()
364 .ok_or_else(|| {
365 ValueError::UnreachableIntegerOverflow(format!("{a:?} - {b:?}")).into()
366 })
367 .map(|v| Interval(I::microseconds(v))),
368 (Time(a), Interval(b)) => b.subtract_from_time(a).map(Time),
369 (Interval(a), Interval(b)) => a.subtract(b).map(Interval),
370 (
371 Null,
372 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
373 | U128(_) | F32(_) | F64(_) | Decimal(_) | Date(_) | Timestamp(_) | Time(_)
374 | Interval(_) | Null,
375 )
376 | (Date(_) | Timestamp(_) | Time(_) | Interval(_), Null) => Ok(Null),
377 _ => Err(ValueError::NonNumericMathOperation {
378 lhs: self.clone(),
379 operator: NumericBinaryOperator::Subtract,
380 rhs: other.clone(),
381 }
382 .into()),
383 }
384 }
385
386 pub fn multiply(&self, other: &Value) -> Result<Value> {
387 use Value::*;
388
389 match (self, other) {
390 (I8(a), _) => a.try_multiply(other),
391 (I16(a), _) => a.try_multiply(other),
392 (I32(a), _) => a.try_multiply(other),
393 (I64(a), _) => a.try_multiply(other),
394 (I128(a), _) => a.try_multiply(other),
395 (U8(a), _) => a.try_multiply(other),
396 (U16(a), _) => a.try_multiply(other),
397 (U32(a), _) => a.try_multiply(other),
398 (U64(a), _) => a.try_multiply(other),
399 (U128(a), _) => a.try_multiply(other),
400 (F32(a), _) => a.try_multiply(other),
401 (F64(a), _) => a.try_multiply(other),
402 (Decimal(a), _) => a.try_multiply(other),
403 (Interval(a), I8(b)) => Ok(Interval(*a * *b)),
404 (Interval(a), I16(b)) => Ok(Interval(*a * *b)),
405 (Interval(a), I32(b)) => Ok(Interval(*a * *b)),
406 (Interval(a), I64(b)) => Ok(Interval(*a * *b)),
407 (Interval(a), I128(b)) => Ok(Interval(*a * *b)),
408 (Interval(a), F32(b)) => Ok(Interval(*a * *b)),
409 (Interval(a), F64(b)) => Ok(Interval(*a * *b)),
410 (
411 Null,
412 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
413 | U128(_) | F32(_) | F64(_) | Decimal(_) | Interval(_) | Null,
414 )
415 | (Interval(_), Null) => Ok(Null),
416 _ => Err(ValueError::NonNumericMathOperation {
417 lhs: self.clone(),
418 operator: NumericBinaryOperator::Multiply,
419 rhs: other.clone(),
420 }
421 .into()),
422 }
423 }
424
425 pub fn divide(&self, other: &Value) -> Result<Value> {
426 use Value::*;
427
428 if other.is_zero() {
429 return Err(ValueError::DivisorShouldNotBeZero.into());
430 }
431
432 match (self, other) {
433 (I8(a), _) => a.try_divide(other),
434 (I16(a), _) => a.try_divide(other),
435 (I32(a), _) => a.try_divide(other),
436 (I64(a), _) => a.try_divide(other),
437 (I128(a), _) => a.try_divide(other),
438 (U8(a), _) => a.try_divide(other),
439 (U16(a), _) => a.try_divide(other),
440 (U32(a), _) => a.try_divide(other),
441 (U64(a), _) => a.try_divide(other),
442 (U128(a), _) => a.try_divide(other),
443 (F32(a), _) => a.try_divide(other),
444 (F64(a), _) => a.try_divide(other),
445 (Decimal(a), _) => a.try_divide(other),
446 (Interval(a), I8(b)) => Ok(Interval(*a / *b)),
447 (Interval(a), I16(b)) => Ok(Interval(*a / *b)),
448 (Interval(a), I32(b)) => Ok(Interval(*a / *b)),
449 (Interval(a), I64(b)) => Ok(Interval(*a / *b)),
450 (Interval(a), I128(b)) => Ok(Interval(*a / *b)),
451 (Interval(a), U8(b)) => Ok(Interval(*a / *b)),
452 (Interval(a), U16(b)) => Ok(Interval(*a / *b)),
453 (Interval(a), U32(b)) => Ok(Interval(*a / *b)),
454 (Interval(a), U64(b)) => Ok(Interval(*a / *b)),
455 (Interval(a), U128(b)) => Ok(Interval(*a / *b)),
456 (Interval(a), F32(b)) => Ok(Interval(*a / *b)),
457 (Interval(a), F64(b)) => Ok(Interval(*a / *b)),
458 (
459 Null,
460 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
461 | U128(_) | F32(_) | F64(_) | Decimal(_) | Null,
462 )
463 | (Interval(_), Null) => Ok(Null),
464 _ => Err(ValueError::NonNumericMathOperation {
465 lhs: self.clone(),
466 operator: NumericBinaryOperator::Divide,
467 rhs: other.clone(),
468 }
469 .into()),
470 }
471 }
472
473 pub fn bitwise_and(&self, other: &Value) -> Result<Value> {
474 use Value::*;
475
476 match (self, other) {
477 (I8(a), I8(b)) => Ok(I8(a & b)),
478 (I16(a), I16(b)) => Ok(I16(a & b)),
479 (I32(a), I32(b)) => Ok(I32(a & b)),
480 (I64(a), I64(b)) => Ok(I64(a & b)),
481 (I128(a), I128(b)) => Ok(I128(a & b)),
482 (U8(a), U8(b)) => Ok(U8(a & b)),
483 (U16(a), U16(b)) => Ok(U16(a & b)),
484 (U32(a), U32(b)) => Ok(U32(a & b)),
485 (U64(a), U64(b)) => Ok(U64(a & b)),
486 (U128(a), U128(b)) => Ok(U128(a & b)),
487 (
488 Null,
489 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
490 | U128(_) | Null,
491 )
492 | (
493 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
494 | U128(_),
495 Null,
496 ) => Ok(Null),
497 _ => Err(ValueError::NonNumericMathOperation {
498 lhs: self.clone(),
499 rhs: other.clone(),
500 operator: NumericBinaryOperator::BitwiseAnd,
501 }
502 .into()),
503 }
504 }
505
506 pub fn modulo(&self, other: &Value) -> Result<Value> {
507 use Value::*;
508
509 if other.is_zero() {
510 return Err(ValueError::DivisorShouldNotBeZero.into());
511 }
512
513 match (self, other) {
514 (I8(a), _) => a.try_modulo(other),
515 (I16(a), _) => a.try_modulo(other),
516 (I32(a), _) => a.try_modulo(other),
517 (I64(a), _) => a.try_modulo(other),
518 (I128(a), _) => a.try_modulo(other),
519 (U8(a), _) => a.try_modulo(other),
520 (U16(a), _) => a.try_modulo(other),
521 (U32(a), _) => a.try_modulo(other),
522 (U64(a), _) => a.try_modulo(other),
523 (U128(a), _) => a.try_modulo(other),
524 (F32(a), _) => a.try_modulo(other),
525 (F64(a), _) => a.try_modulo(other),
526 (Decimal(a), _) => a.try_modulo(other),
527 (
528 Null,
529 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
530 | U128(_) | F32(_) | F64(_) | Decimal(_) | Null,
531 ) => Ok(Null),
532 _ => Err(ValueError::NonNumericMathOperation {
533 lhs: self.clone(),
534 operator: NumericBinaryOperator::Modulo,
535 rhs: other.clone(),
536 }
537 .into()),
538 }
539 }
540
541 pub fn bitwise_shift_left(&self, rhs: &Value) -> Result<Value> {
542 use Value::*;
543
544 if *rhs == Null {
545 return Ok(Null);
546 }
547 let rhs = u32::try_from(rhs)?;
548 match self {
549 I8(lhs) => lhs.checked_shl(rhs).map(I8),
550 I16(lhs) => lhs.checked_shl(rhs).map(I16),
551 I32(lhs) => lhs.checked_shl(rhs).map(I32),
552 I64(lhs) => lhs.checked_shl(rhs).map(I64),
553 I128(lhs) => lhs.checked_shl(rhs).map(I128),
554 U8(lhs) => lhs.checked_shl(rhs).map(U8),
555 U16(lhs) => lhs.checked_shl(rhs).map(U16),
556 U32(lhs) => lhs.checked_shl(rhs).map(U32),
557 U64(lhs) => lhs.checked_shl(rhs).map(U64),
558 U128(lhs) => lhs.checked_shl(rhs).map(U128),
559 Null => Some(Null),
560 _ => {
561 return Err(ValueError::NonNumericMathOperation {
562 lhs: self.clone(),
563 rhs: U32(rhs),
564 operator: NumericBinaryOperator::BitwiseShiftLeft,
565 }
566 .into());
567 }
568 }
569 .ok_or_else(|| {
570 ValueError::BinaryOperationOverflow {
571 lhs: self.clone(),
572 rhs: U32(rhs),
573 operator: NumericBinaryOperator::BitwiseShiftLeft,
574 }
575 .into()
576 })
577 }
578
579 pub fn bitwise_shift_right(&self, rhs: &Value) -> Result<Value> {
580 use Value::*;
581
582 if *rhs == Null {
583 return Ok(Null);
584 }
585 let rhs = u32::try_from(rhs)?;
586 match self {
587 I8(lhs) => lhs.checked_shr(rhs).map(I8),
588 I16(lhs) => lhs.checked_shr(rhs).map(I16),
589 I32(lhs) => lhs.checked_shr(rhs).map(I32),
590 I64(lhs) => lhs.checked_shr(rhs).map(I64),
591 I128(lhs) => lhs.checked_shr(rhs).map(I128),
592 U8(lhs) => lhs.checked_shr(rhs).map(U8),
593 U16(lhs) => lhs.checked_shr(rhs).map(U16),
594 U32(lhs) => lhs.checked_shr(rhs).map(U32),
595 U64(lhs) => lhs.checked_shr(rhs).map(U64),
596 U128(lhs) => lhs.checked_shr(rhs).map(U128),
597 Null => Some(Null),
598 _ => {
599 return Err(ValueError::NonNumericMathOperation {
600 lhs: self.clone(),
601 rhs: U32(rhs),
602 operator: NumericBinaryOperator::BitwiseShiftRight,
603 }
604 .into());
605 }
606 }
607 .ok_or_else(|| {
608 ValueError::BinaryOperationOverflow {
609 lhs: self.clone(),
610 rhs: U32(rhs),
611 operator: NumericBinaryOperator::BitwiseShiftRight,
612 }
613 .into()
614 })
615 }
616
617 pub fn is_null(&self) -> bool {
618 matches!(self, Value::Null)
619 }
620
621 pub fn unary_plus(&self) -> Result<Value> {
622 use Value::*;
623
624 match self {
625 I8(_) | I16(_) | I32(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_)
626 | U128(_) | F32(_) | F64(_) | Interval(_) | Decimal(_) => Ok(self.clone()),
627 Null => Ok(Null),
628 _ => Err(ValueError::UnaryPlusOnNonNumeric.into()),
629 }
630 }
631
632 pub fn unary_minus(&self) -> Result<Value> {
633 use Value::*;
634
635 match self {
636 I8(a) => Ok(I8(-a)),
637 I16(a) => Ok(I16(-a)),
638 I32(a) => Ok(I32(-a)),
639 I64(a) => Ok(I64(-a)),
640 I128(a) => Ok(I128(-a)),
641 F32(a) => Ok(F32(-a)),
642 F64(a) => Ok(F64(-a)),
643 Decimal(a) => Ok(Decimal(-a)),
644 Interval(a) => Ok(Interval(a.unary_minus())),
645 Null => Ok(Null),
646 _ => Err(ValueError::UnaryMinusOnNonNumeric.into()),
647 }
648 }
649
650 pub fn unary_factorial(&self) -> Result<Value> {
651 use Value::*;
652
653 fn factorial_function(a: i128) -> Result<i128> {
654 if a.is_negative() {
655 return Err(ValueError::FactorialOnNegativeNumeric.into());
656 }
657
658 (1_i128..=a)
659 .try_fold(1_i128, i128::checked_mul)
660 .ok_or_else(|| ValueError::FactorialOverflow.into())
661 }
662
663 match self {
664 I8(a) => factorial_function(i128::from(*a)).map(I128),
665 I16(a) => factorial_function(i128::from(*a)).map(I128),
666 I32(a) => factorial_function(i128::from(*a)).map(I128),
667 I64(a) => factorial_function(i128::from(*a)).map(I128),
668 I128(a) => factorial_function(*a).map(I128),
669 U8(a) => factorial_function(i128::from(*a)).map(I128),
670 U16(a) => factorial_function(i128::from(*a)).map(I128),
671 U32(a) => factorial_function(i128::from(*a)).map(I128),
672 U64(a) => factorial_function(i128::from(*a)).map(I128),
673 U128(a) => factorial_function(*a as i128).map(I128),
674 F32(_) | F64(_) => Err(ValueError::FactorialOnNonInteger.into()),
675 Null => Ok(Null),
676 _ => Err(ValueError::FactorialOnNonNumeric.into()),
677 }
678 }
679
680 pub fn unary_bitwise_not(&self) -> Result<Value> {
681 use Value::*;
682
683 match self {
684 I8(v) => Ok(Value::I8(!v)),
685 I16(v) => Ok(Value::I16(!v)),
686 I32(v) => Ok(Value::I32(!v)),
687 I64(v) => Ok(Value::I64(!v)),
688 I128(v) => Ok(Value::I128(!v)),
689 U8(v) => Ok(Value::U8(!v)),
690 U16(v) => Ok(Value::U16(!v)),
691 U32(v) => Ok(Value::U32(!v)),
692 U64(v) => Ok(Value::U64(!v)),
693 U128(v) => Ok(Value::U128(!v)),
694 F32(_) | F64(_) => Err(ValueError::UnaryBitwiseNotOnNonInteger.into()),
695 Null => Ok(Null),
696 _ => Err(ValueError::UnaryBitwiseNotOnNonNumeric.into()),
697 }
698 }
699
700 pub fn like(&self, other: &Value, case_sensitive: bool) -> Result<Value> {
701 use Value::*;
702
703 match (self, other) {
704 (Str(a), Str(b)) => a.like(b, case_sensitive).map(Bool),
705 _ => Err(ValueError::LikeOnNonString {
706 base: self.clone(),
707 pattern: other.clone(),
708 case_sensitive,
709 }
710 .into()),
711 }
712 }
713
714 pub fn regex(&self, other: &Value, negated: bool, case_sensitive: bool) -> Result<Value> {
715 use Value::*;
716
717 match (self, other) {
718 (Str(a), Str(b)) => a
719 .regex(b, case_sensitive)
720 .map(|matched| Bool(matched ^ negated)),
721 (Null, Str(_) | Null) | (Str(_), Null) => Ok(Null),
724 _ => Err(ValueError::RegexOnNonString {
725 base: self.clone(),
726 pattern: other.clone(),
727 operator: match (negated, case_sensitive) {
728 (false, true) => "~",
729 (false, false) => "~*",
730 (true, true) => "!~",
731 (true, false) => "!~*",
732 }
733 .to_owned(),
734 }
735 .into()),
736 }
737 }
738
739 pub fn extract(&self, date_type: &DateTimeField) -> Result<Value> {
740 let value = match (self, date_type) {
741 (Value::Date(v), DateTimeField::Year) => v.year().into(),
742 (Value::Date(v), DateTimeField::Month) => v.month().into(),
743 (Value::Date(v), DateTimeField::Day) => v.day().into(),
744 (Value::Time(v), DateTimeField::Hour) => v.hour().into(),
745 (Value::Time(v), DateTimeField::Minute) => v.minute().into(),
746 (Value::Time(v), DateTimeField::Second) => v.second().into(),
747 (Value::Timestamp(v), DateTimeField::Year) => v.year().into(),
748 (Value::Timestamp(v), DateTimeField::Month) => v.month().into(),
749 (Value::Timestamp(v), DateTimeField::Day) => v.day().into(),
750 (Value::Timestamp(v), DateTimeField::Hour) => v.hour().into(),
751 (Value::Timestamp(v), DateTimeField::Minute) => v.minute().into(),
752 (Value::Timestamp(v), DateTimeField::Second) => v.second().into(),
753 (Value::Interval(v), _) => {
754 return v.extract(date_type);
755 }
756 _ => {
757 return Err(ValueError::ExtractFormatNotMatched {
758 value: self.clone(),
759 field: *date_type,
760 }
761 .into());
762 }
763 };
764
765 Ok(Value::I64(value))
766 }
767
768 pub fn sqrt(&self) -> Result<Value> {
769 use Value::*;
770 match self {
771 I8(_) | I16(_) | I64(_) | I128(_) | U8(_) | U16(_) | U32(_) | U64(_) | U128(_)
772 | F32(_) | F64(_) => {
773 let a: f64 = self.try_into()?;
774 Ok(Value::F64(a.sqrt()))
775 }
776 Null => Ok(Value::Null),
777 _ => Err(ValueError::SqrtOnNonNumeric(self.clone()).into()),
778 }
779 }
780
781 pub fn to_cmp_be_bytes(&self) -> Result<Vec<u8>> {
783 self.try_into().and_then(|key: Key| key.to_cmp_be_bytes())
784 }
785
786 pub fn position(&self, other: &Value) -> Result<Value> {
811 use Value::*;
812
813 match (self, other) {
814 (Str(from), Str(sub)) => Ok(I64(str_position(from, sub) as i64)),
815 (Null, _) | (_, Null) => Ok(Null),
816 _ => Err(ValueError::NonStringParameterInPosition {
817 from: self.clone(),
818 sub: other.clone(),
819 }
820 .into()),
821 }
822 }
823
824 pub fn find_idx(&self, sub_val: &Value, start: &Value) -> Result<Value> {
825 let start: i64 = start.try_into()?;
826 if start <= 0 {
827 return Err(ValueError::NonPositiveIntegerOffsetInFindIdx(start.to_string()).into());
828 }
829 let from = &String::from(self);
830 let sub = &String::from(sub_val);
831 let position = str_position(&from[(start - 1) as usize..], sub) as i64;
832 let position = match position {
833 0 => 0,
834 _ => position + start - 1,
835 };
836 Ok(Value::I64(position))
837 }
838}
839
840impl PartialEq for Value {
841 fn eq(&self, other: &Self) -> bool {
842 match (self, other) {
843 (Value::Bool(a), Value::Bool(b)) => a == b,
844 (Value::I8(a), Value::I8(b)) => a == b,
845 (Value::I16(a), Value::I16(b)) => a == b,
846 (Value::I32(a), Value::I32(b)) => a == b,
847 (Value::I64(a), Value::I64(b)) => a == b,
848 (Value::I128(a), Value::I128(b)) => a == b,
849 (Value::U8(a), Value::U8(b)) => a == b,
850 (Value::U16(a), Value::U16(b)) => a == b,
851 (Value::U32(a), Value::U32(b)) => a == b,
852 (Value::U64(a), Value::U64(b)) => a == b,
853 (Value::U128(a), Value::U128(b)) | (Value::Uuid(a), Value::Uuid(b)) => a == b,
854 (Value::F32(a), Value::F32(b)) => (a.is_nan() && b.is_nan()) || a == b,
855 (Value::F64(a), Value::F64(b)) => (a.is_nan() && b.is_nan()) || a == b,
856 (Value::Decimal(a), Value::Decimal(b)) => a == b,
857 (Value::Str(a), Value::Str(b)) => a == b,
858 (Value::Bytea(a), Value::Bytea(b)) => a == b,
859 (Value::Inet(a), Value::Inet(b)) => a == b,
860 (Value::Date(a), Value::Date(b)) => a == b,
861 (Value::Timestamp(a), Value::Timestamp(b)) => a == b,
862 (Value::Time(a), Value::Time(b)) => a == b,
863 (Value::Interval(a), Value::Interval(b)) => a == b,
864 (Value::Map(a), Value::Map(b)) => a == b,
865 (Value::List(a), Value::List(b)) => a == b,
866 (Value::Point(a), Value::Point(b)) => a == b,
867 (Value::Null, Value::Null) => true,
868 _ => false,
869 }
870 }
871}
872
873impl Eq for Value {}
874
875impl Hash for Value {
876 fn hash<H: Hasher>(&self, state: &mut H) {
877 const CANONICAL_F32_NAN_BITS: u32 = 0x7fc0_0000;
878 const CANONICAL_F64_NAN_BITS: u64 = 0x7ff8_0000_0000_0000;
879 const CANONICAL_F32_ZERO_BITS: u32 = 0;
880 const CANONICAL_F64_ZERO_BITS: u64 = 0;
881
882 discriminant(self).hash(state);
883
884 match self {
885 Value::Bool(v) => v.hash(state),
886 Value::I8(v) => v.hash(state),
887 Value::I16(v) => v.hash(state),
888 Value::I32(v) => v.hash(state),
889 Value::I64(v) => v.hash(state),
890 Value::I128(v) => v.hash(state),
891 Value::U8(v) => v.hash(state),
892 Value::U16(v) => v.hash(state),
893 Value::U32(v) => v.hash(state),
894 Value::U64(v) => v.hash(state),
895 Value::U128(v) | Value::Uuid(v) => v.hash(state),
896 Value::F32(v) => {
897 if v.is_nan() {
898 CANONICAL_F32_NAN_BITS.hash(state);
899 } else if *v == 0.0f32 {
900 CANONICAL_F32_ZERO_BITS.hash(state);
901 } else {
902 v.to_bits().hash(state);
903 }
904 }
905 Value::F64(v) => {
906 if v.is_nan() {
907 CANONICAL_F64_NAN_BITS.hash(state);
908 } else if *v == 0.0f64 {
909 CANONICAL_F64_ZERO_BITS.hash(state);
910 } else {
911 v.to_bits().hash(state);
912 }
913 }
914 Value::Decimal(v) => v.hash(state),
915 Value::Str(v) => v.hash(state),
916 Value::Bytea(v) => v.hash(state),
917 Value::Inet(v) => v.hash(state),
918 Value::Date(v) => v.hash(state),
919 Value::Timestamp(v) => v.hash(state),
920 Value::Time(v) => v.hash(state),
921 Value::Interval(v) => v.hash(state),
922 Value::Map(map) => {
923 map.hash(state);
924 }
925 Value::List(list) => list.hash(state),
926 Value::Point(p) => p.hash(state),
927 Value::Null => {
928 }
931 }
932 }
933}
934
935fn str_position(from_str: &str, sub_str: &str) -> usize {
936 if from_str.is_empty() || sub_str.is_empty() {
937 return 0;
938 }
939 from_str.find(sub_str).map_or(0, |position| position + 1)
940}
941
942#[cfg(test)]
943mod tests {
944 use {
945 super::{Interval, Value, Value::*},
946 crate::data::{NumericBinaryOperator, ValueError, point::Point, value::uuid::parse_uuid},
947 chrono::{NaiveDate, NaiveTime},
948 rust_decimal::Decimal,
949 std::{collections::HashMap, net::IpAddr, str::FromStr},
950 };
951
952 fn time(hour: u32, min: u32, sec: u32) -> NaiveTime {
953 NaiveTime::from_hms_opt(hour, min, sec).unwrap()
954 }
955
956 fn date(year: i32, month: u32, day: u32) -> NaiveDate {
957 NaiveDate::from_ymd_opt(year, month, day).unwrap()
958 }
959
960 #[allow(clippy::eq_op)]
961 #[test]
962 fn evaluate_eq() {
963 use crate::data::Tribool;
964 use {
965 super::Interval,
966 chrono::{NaiveDateTime, NaiveTime},
967 };
968 let decimal = |n: i32| Decimal(n.into());
969 let bytea = |v: &str| Bytea(hex::decode(v).unwrap());
970 let inet = |v: &str| Inet(IpAddr::from_str(v).unwrap());
971
972 assert_eq!(Tribool::Null, Tribool::Null); assert_eq!(Tribool::Null, Null.evaluate_eq(&Null));
974 assert_eq!(Tribool::True, Bool(true).evaluate_eq(&Bool(true)));
975 assert_eq!(Tribool::True, I8(1).evaluate_eq(&I8(1)));
976 assert_eq!(Tribool::True, I16(1).evaluate_eq(&I16(1)));
977 assert_eq!(Tribool::True, I32(1).evaluate_eq(&I32(1)));
978 assert_eq!(Tribool::True, I64(1).evaluate_eq(&I64(1)));
979 assert_eq!(Tribool::True, I128(1).evaluate_eq(&I128(1)));
980 assert_eq!(Tribool::True, U8(1).evaluate_eq(&U8(1)));
981 assert_eq!(Tribool::True, U16(1).evaluate_eq(&U16(1)));
982 assert_eq!(Tribool::True, U32(1).evaluate_eq(&U32(1)));
983 assert_eq!(Tribool::True, U64(1).evaluate_eq(&U64(1)));
984 assert_eq!(Tribool::True, U128(1).evaluate_eq(&U128(1)));
985 assert_eq!(Tribool::True, I64(1).evaluate_eq(&F64(1.0)));
986 assert_eq!(Tribool::True, F32(1.0_f32).evaluate_eq(&I64(1)));
987 assert_eq!(Tribool::True, F32(6.11_f32).evaluate_eq(&F64(6.11)));
988 assert_eq!(Tribool::True, F64(1.0).evaluate_eq(&I64(1)));
989 assert_eq!(Tribool::True, F64(6.11).evaluate_eq(&F64(6.11)));
990 assert_eq!(
991 Tribool::True,
992 Str("Glue".to_owned()).evaluate_eq(&Str("Glue".to_owned()))
993 );
994 assert_eq!(Tribool::True, bytea("1004").evaluate_eq(&bytea("1004")));
995 assert_eq!(Tribool::True, inet("::1").evaluate_eq(&inet("::1")));
996 assert_eq!(
997 Tribool::True,
998 Interval(Interval::Month(1)).evaluate_eq(&Interval(Interval::Month(1)))
999 );
1000 assert_eq!(
1001 Tribool::True,
1002 Time(NaiveTime::from_hms_opt(12, 30, 11).unwrap())
1003 .evaluate_eq(&Time(NaiveTime::from_hms_opt(12, 30, 11).unwrap()))
1004 );
1005 assert_eq!(Tribool::True, decimal(1).evaluate_eq(&decimal(1)));
1006 assert_eq!(
1007 Tribool::True,
1008 Date("2020-05-01".parse().unwrap()).evaluate_eq(&Date("2020-05-01".parse().unwrap()))
1009 );
1010 assert_eq!(
1011 Tribool::True,
1012 Timestamp("2020-05-01T00:00:00".parse::<NaiveDateTime>().unwrap()).evaluate_eq(
1013 &Timestamp("2020-05-01T00:00:00".parse::<NaiveDateTime>().unwrap())
1014 )
1015 );
1016 assert_eq!(
1017 Tribool::True,
1018 Uuid(parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap()).evaluate_eq(&Uuid(
1019 parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap()
1020 ))
1021 );
1022 assert_eq!(
1023 Tribool::True,
1024 Point(Point::new(1.0, 2.0)).evaluate_eq(&Point(Point::new(1.0, 2.0)))
1025 );
1026
1027 let date = Date("2020-05-01".parse().unwrap());
1028 let timestamp = Timestamp("2020-05-01T00:00:00".parse::<NaiveDateTime>().unwrap());
1029
1030 assert_eq!(Tribool::True, date.evaluate_eq(×tamp));
1031 assert_eq!(Tribool::True, timestamp.evaluate_eq(&date));
1032 }
1033
1034 #[test]
1035 fn cmp() {
1036 use {
1037 chrono::{NaiveDate, NaiveTime},
1038 std::cmp::Ordering,
1039 };
1040
1041 assert_eq!(
1042 Bool(true).evaluate_cmp(&Bool(false)),
1043 Some(Ordering::Greater)
1044 );
1045 assert_eq!(Bool(true).evaluate_cmp(&Bool(true)), Some(Ordering::Equal));
1046 assert_eq!(
1047 Bool(false).evaluate_cmp(&Bool(false)),
1048 Some(Ordering::Equal)
1049 );
1050 assert_eq!(Bool(false).evaluate_cmp(&Bool(true)), Some(Ordering::Less));
1051
1052 let date = Date(NaiveDate::from_ymd_opt(2020, 5, 1).unwrap());
1053 let timestamp = Timestamp(
1054 NaiveDate::from_ymd_opt(2020, 3, 1)
1055 .unwrap()
1056 .and_hms_opt(0, 0, 0)
1057 .unwrap(),
1058 );
1059
1060 assert_eq!(date.evaluate_cmp(×tamp), Some(Ordering::Greater));
1061 assert_eq!(timestamp.evaluate_cmp(&date), Some(Ordering::Less));
1062
1063 assert_eq!(
1064 Time(NaiveTime::from_hms_opt(23, 0, 1).unwrap())
1065 .evaluate_cmp(&Time(NaiveTime::from_hms_opt(10, 59, 59).unwrap())),
1066 Some(Ordering::Greater)
1067 );
1068 assert_eq!(
1069 Interval(Interval::Month(1)).evaluate_cmp(&Interval(Interval::Month(2))),
1070 Some(Ordering::Less)
1071 );
1072
1073 let one = Decimal(rust_decimal::Decimal::ONE);
1074 let two = Decimal(rust_decimal::Decimal::TWO);
1075 assert_eq!(one.evaluate_cmp(&two), Some(Ordering::Less));
1076 assert_eq!(two.evaluate_cmp(&one), Some(Ordering::Greater));
1077
1078 assert_eq!(
1079 F32(1.0_f32).evaluate_cmp(&F32(1.0_f32)),
1080 Some(Ordering::Equal)
1081 );
1082 assert_eq!(F64(1.0).evaluate_cmp(&F64(1.0)), Some(Ordering::Equal));
1083
1084 assert_eq!(
1085 Interval(Interval::Month(1)).evaluate_cmp(&Interval(Interval::Month(1))),
1086 Some(Ordering::Equal)
1087 );
1088
1089 assert_eq!(
1090 Uuid(parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap()).evaluate_cmp(&Uuid(
1091 parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap()
1092 )),
1093 Some(Ordering::Equal)
1094 );
1095
1096 assert_eq!(Null.evaluate_cmp(&Null), None);
1097
1098 let bytea = |v: &str| Bytea(hex::decode(v).unwrap());
1099 assert_eq!(bytea("12").evaluate_cmp(&bytea("20")), Some(Ordering::Less));
1100 assert_eq!(
1101 bytea("9123").evaluate_cmp(&bytea("9122")),
1102 Some(Ordering::Greater)
1103 );
1104 assert_eq!(
1105 bytea("10").evaluate_cmp(&bytea("10")),
1106 Some(Ordering::Equal)
1107 );
1108
1109 let inet = |v: &str| Inet(IpAddr::from_str(v).unwrap());
1110 assert_eq!(
1111 inet("0.0.0.0").evaluate_cmp(&inet("127.0.0.1")),
1112 Some(Ordering::Less)
1113 );
1114 assert_eq!(
1115 inet("192.168.0.1").evaluate_cmp(&inet("127.0.0.1")),
1116 Some(Ordering::Greater)
1117 );
1118 assert_eq!(
1119 inet("::1").evaluate_cmp(&inet("::1")),
1120 Some(Ordering::Equal)
1121 );
1122 }
1123
1124 #[test]
1125 fn cmp_ints() {
1126 use std::cmp::Ordering;
1127
1128 assert_eq!(I8(0).evaluate_cmp(&I8(-1)), Some(Ordering::Greater));
1129 assert_eq!(I8(0).evaluate_cmp(&I8(0)), Some(Ordering::Equal));
1130 assert_eq!(I8(0).evaluate_cmp(&I8(1)), Some(Ordering::Less));
1131
1132 assert_eq!(I16(0).evaluate_cmp(&I8(-1)), Some(Ordering::Greater));
1133 assert_eq!(I16(0).evaluate_cmp(&I8(0)), Some(Ordering::Equal));
1134 assert_eq!(I16(0).evaluate_cmp(&I8(1)), Some(Ordering::Less));
1135
1136 assert_eq!(I32(0).evaluate_cmp(&I8(-1)), Some(Ordering::Greater));
1137 assert_eq!(I32(0).evaluate_cmp(&I8(0)), Some(Ordering::Equal));
1138 assert_eq!(I32(0).evaluate_cmp(&I8(1)), Some(Ordering::Less));
1139
1140 assert_eq!(I64(0).evaluate_cmp(&I8(-1)), Some(Ordering::Greater));
1141 assert_eq!(I64(0).evaluate_cmp(&I8(0)), Some(Ordering::Equal));
1142 assert_eq!(I64(0).evaluate_cmp(&I8(1)), Some(Ordering::Less));
1143
1144 assert_eq!(I128(0).evaluate_cmp(&I8(-1)), Some(Ordering::Greater));
1145 assert_eq!(I128(0).evaluate_cmp(&I8(0)), Some(Ordering::Equal));
1146 assert_eq!(I128(0).evaluate_cmp(&I8(1)), Some(Ordering::Less));
1147
1148 assert_eq!(U8(1).evaluate_cmp(&U8(0)), Some(Ordering::Greater));
1149 assert_eq!(U8(0).evaluate_cmp(&U8(0)), Some(Ordering::Equal));
1150 assert_eq!(U8(0).evaluate_cmp(&U8(1)), Some(Ordering::Less));
1151
1152 assert_eq!(U16(1).evaluate_cmp(&U16(0)), Some(Ordering::Greater));
1153 assert_eq!(U16(0).evaluate_cmp(&U16(0)), Some(Ordering::Equal));
1154 assert_eq!(U16(0).evaluate_cmp(&U16(1)), Some(Ordering::Less));
1155
1156 assert_eq!(U32(1).evaluate_cmp(&U32(0)), Some(Ordering::Greater));
1157 assert_eq!(U32(0).evaluate_cmp(&U32(0)), Some(Ordering::Equal));
1158 assert_eq!(U32(0).evaluate_cmp(&U32(1)), Some(Ordering::Less));
1159
1160 assert_eq!(U64(1).evaluate_cmp(&U64(0)), Some(Ordering::Greater));
1161 assert_eq!(U64(0).evaluate_cmp(&U64(0)), Some(Ordering::Equal));
1162 assert_eq!(U64(0).evaluate_cmp(&U64(1)), Some(Ordering::Less));
1163
1164 assert_eq!(U128(1).evaluate_cmp(&U128(0)), Some(Ordering::Greater));
1165 assert_eq!(U128(0).evaluate_cmp(&U128(0)), Some(Ordering::Equal));
1166 assert_eq!(U128(0).evaluate_cmp(&U128(1)), Some(Ordering::Less));
1167 }
1168
1169 #[test]
1170 fn is_zero() {
1171 for i in -1..2 {
1172 assert_eq!(I8(i).is_zero(), i == 0);
1173 assert_eq!(I16(i.into()).is_zero(), i == 0);
1174 assert_eq!(I32(i.into()).is_zero(), i == 0);
1175 assert_eq!(I64(i.into()).is_zero(), i == 0);
1176 assert_eq!(I128(i.into()).is_zero(), i == 0);
1177 assert_eq!(F32(i.into()).is_zero(), i == 0);
1178 assert_eq!(F64(i.into()).is_zero(), i == 0);
1179 assert_eq!(Decimal(i.into()).is_zero(), i == 0);
1180 }
1181 assert!(U8(0).is_zero());
1182 assert!(!U8(1).is_zero());
1183 assert!(U16(0).is_zero());
1184 assert!(!U16(1).is_zero());
1185 assert!(U32(0).is_zero());
1186 assert!(!U32(1).is_zero());
1187 assert!(U64(0).is_zero());
1188 assert!(!U64(1).is_zero());
1189 assert!(U128(0).is_zero());
1190 assert!(!U128(1).is_zero());
1191 }
1192
1193 #[test]
1194 fn arithmetic() {
1195 use chrono::{NaiveDate, NaiveTime};
1196
1197 use crate::data::Tribool::True;
1198 macro_rules! test {
1199 ($op: ident $a: expr, $b: expr => $c: expr) => {
1200 assert_eq!(True, $a.$op(&$b).unwrap().evaluate_eq(&$c));
1201 };
1202 }
1203
1204 macro_rules! mon {
1205 ($n: expr) => {
1206 Interval(Interval::Month($n))
1207 };
1208 }
1209
1210 let decimal = |n: i32| Decimal(n.into());
1211
1212 test!(add I8(1), I8(2) => I8(3));
1213 test!(add I8(1), I16(2) => I16(3));
1214 test!(add I8(1), I32(2) => I32(3));
1215 test!(add I8(1), I64(2) => I64(3));
1216 test!(add I8(1), I128(2) => I128(3));
1217 test!(add I8(1), U8(2) => I64(3));
1218
1219 test!(add I16(1), I8(2) => I16(3));
1220 test!(add I16(1), I16(2) => I16(3));
1221 test!(add I16(1), I32(2) => I32(3));
1222 test!(add I16(1), I64(2) => I64(3));
1223 test!(add I16(1), I128(2) => I128(3));
1224 test!(add I16(1), U8(2) => I16(3));
1225
1226 test!(add I32(1), I8(2) => I32(3));
1227 test!(add I32(1), I16(2) => I32(3));
1228 test!(add I32(1), I32(2) => I32(3));
1229 test!(add I32(1), I64(2) => I64(3));
1230 test!(add I32(1), I128(2) => I128(3));
1231 test!(add I32(1), U8(2) => I32(3));
1232
1233 test!(add I64(1), I8(2) => I64(3));
1234 test!(add I64(1), I16(2) => I64(3));
1235 test!(add I64(1), I32(2) => I64(3));
1236 test!(add I64(1), I64(2) => I64(3));
1237 test!(add I64(1), I128(2) => I128(3));
1238 test!(add I64(1), U8(2) => I64(3));
1239
1240 test!(add I128(1), I8(2) => I128(3));
1241 test!(add I128(1), I16(2) => I128(3));
1242 test!(add I128(1), I32(2) => I128(3));
1243 test!(add I128(1), I64(2) => I128(3));
1244 test!(add I128(1), I128(2) => I128(3));
1245 test!(add I128(1), U8(2) => I128(3));
1246
1247 test!(add I8(1), F64(2.0) => F64(3.0));
1248
1249 test!(add I32(1), I8(2) => I32(3));
1250 test!(add I32(1), I16(2) => I32(3));
1251 test!(add I32(1), I32(2) => I32(3));
1252 test!(add I32(1), I64(2) => I64(3));
1253 test!(add I32(1), F32(2.0_f32) => F32(3.0_f32));
1254 test!(add I32(1), F64(2.0) => F64(3.0));
1255
1256 test!(add I64(1), I8(2) => I64(3));
1257 test!(add I64(1), I16(2) => I64(3));
1258 test!(add I64(1), I32(2) => I64(3));
1259 test!(add I64(1), I64(2) => I64(3));
1260 test!(add I64(1), F32(2.0_f32) => F32(3.0_f32));
1261 test!(add I64(1), F64(2.0) => F64(3.0));
1262
1263 test!(add I128(1), I8(2) => I128(3));
1264 test!(add I128(1), I16(2) => I128(3));
1265 test!(add I128(1), I32(2) => I128(3));
1266 test!(add I128(1), I64(2) => I128(3));
1267 test!(add I128(1), F32(2.0_f32) => F32(3.0_f32));
1268 test!(add I128(1), F64(2.0) => F64(3.0));
1269
1270 test!(add U8(1), I8(2) => I64(3));
1271 test!(add U8(1), I16(2) => I16(3));
1272 test!(add U8(1), I32(2) => I32(3));
1273 test!(add U8(1), I64(2) => I64(3));
1274 test!(add U8(1), I128(2) => I128(3));
1275 test!(add U8(1), U8(2) => U8(3));
1276 test!(add U8(1), F32(2.0_f32) => F32(3.0_f32));
1277 test!(add U8(1), F64(2.0) => F64(3.0));
1278
1279 test!(add U16(1), I8(2) => U16(3));
1280 test!(add U16(1), I16(2) => U16(3));
1281 test!(add U16(1), I32(2) => U16(3));
1282 test!(add U16(1), I64(2) => U16(3));
1283 test!(add U16(1), I128(2) => U16(3));
1284 test!(add U16(1), U8(2) => U16(3));
1285 test!(add U16(1), F32(2.0_f32) => F32(3.0_f32));
1286 test!(add U16(1), F64(2.0) => F64(3.0));
1287
1288 test!(add U32(1), I8(2) => U32(3));
1289 test!(add U32(1), I16(2) => U32(3));
1290 test!(add U32(1), I32(2) => U32(3));
1291 test!(add U32(1), I64(2) => U32(3));
1292 test!(add U32(1), I128(2) => U32(3));
1293 test!(add U32(1), U8(2) => U32(3));
1294 test!(add U32(1), U16(2) => U32(3));
1295 test!(add U32(1), U32(2) => U32(3));
1296 test!(add U32(1), F32(2.0_f32) => F32(3.0_f32));
1297 test!(add U32(1), F64(2.0) => F64(3.0));
1298
1299 test!(add U64(1), I8(2) => U64(3));
1300 test!(add U64(1), I16(2) => U64(3));
1301 test!(add U64(1), I32(2) => U64(3));
1302 test!(add U64(1), I64(2) => U64(3));
1303 test!(add U64(1), I128(2) => U64(3));
1304 test!(add U64(1), U8(2) => U64(3));
1305 test!(add U64(1), U16(2) => U64(3));
1306 test!(add U64(1), U32(2) => U64(3));
1307 test!(add U64(1), F32(2.0_f32) => F32(3.0_f32));
1308 test!(add U64(1), F64(2.0) => F64(3.0));
1309
1310 test!(add U128(1), I8(2) => U128(3));
1311 test!(add U128(1), I16(2) => U128(3));
1312 test!(add U128(1), I32(2) => U128(3));
1313 test!(add U128(1), I64(2) => U128(3));
1314 test!(add U128(1), I128(2) => U128(3));
1315 test!(add U128(1), U8(2) => U128(3));
1316 test!(add U128(1), U16(2) => U128(3));
1317 test!(add U128(1), U32(2) => U128(3));
1318 test!(add U128(1), F32(2.0_f32) => F32(3.0_f32));
1319 test!(add U128(1), F64(2.0) => F64(3.0));
1320
1321 test!(add F32(1.0_f32), F32(2.0_f32) => F32(3.0_f32));
1322 test!(add F32(1.0_f32), F64(2.0) => F64(3.0));
1323 test!(add F32(1.0_f32), I8(2) => F32(3.0_f32));
1324 test!(add F32(1.0_f32), I32(2) => F32(3.0_f32));
1325 test!(add F32(1.0_f32), I64(2) => F32(3.0_f32));
1326 test!(add F32(1.0_f32), U8(2) => F32(3.0_f32));
1327 test!(add F32(1.0_f32), U16(2) => F32(3.0_f32));
1328 test!(add F32(1.0_f32), U32(2) => F32(3.0_f32));
1329 test!(add F32(1.0_f32), U64(2) => F32(3.0_f32));
1330 test!(add F32(1.0_f32), U128(2) => F32(3.0_f32));
1331
1332 test!(add F64(1.0), F64(2.0) => F64(3.0));
1333 test!(add F64(1.0), F32(2.0_f32) => F32(3.0_f32));
1334 test!(add F64(1.0), I8(2) => F64(3.0));
1335 test!(add F64(1.0), I32(2) => F64(3.0));
1336 test!(add F64(1.0), I64(2) => F64(3.0));
1337 test!(add F64(1.0), U8(2) => F64(3.0));
1338
1339 test!(add decimal(1), decimal(2) => decimal(3));
1340
1341 test!(add
1342 Date(date(2021, 11, 11)),
1343 mon!(14)
1344 =>
1345 Timestamp(date(2023, 1, 11).and_hms_opt(0, 0, 0).unwrap())
1346 );
1347 test!(add
1348 Date(date(2021, 5, 7)),
1349 Time(time(12, 0, 0))
1350 =>
1351 Timestamp(date(2021, 5, 7).and_hms_opt(12, 0, 0).unwrap())
1352 );
1353 test!(add
1354 Timestamp(date(2021, 11, 11).and_hms_opt(0, 0, 0).unwrap()),
1355 mon!(14)
1356 =>
1357 Timestamp(date(2023, 1, 11).and_hms_opt(0, 0, 0).unwrap())
1358 );
1359 test!(add
1360 Time(time(1, 4, 6)),
1361 Interval(Interval::hours(20))
1362 =>
1363 Time(time(21, 4, 6))
1364 );
1365 test!(add
1366 Time(time(23, 10, 0)),
1367 Interval(Interval::hours(5))
1368 =>
1369 Time(time(4, 10, 0))
1370 );
1371 test!(add mon!(1), mon!(2) => mon!(3));
1372
1373 test!(subtract I8(3), I8(2) => I8(1));
1374 test!(subtract I8(3), I16(2) => I8(1));
1375 test!(subtract I8(3), I32(2) => I32(1));
1376 test!(subtract I8(3), I64(2) => I64(1));
1377 test!(subtract I8(3), I128(2) => I128(1));
1378 test!(subtract I8(3), U8(2) => I64(1));
1379
1380 test!(subtract I32(3), I8(2) => I32(1));
1381 test!(subtract I32(3), I16(2) => I32(1));
1382 test!(subtract I32(3), I32(2) => I32(1));
1383 test!(subtract I32(3), I64(2) => I64(1));
1384 test!(subtract I32(3), I128(2) => I128(1));
1385 test!(subtract I32(3), U8(2) => I32(1));
1386
1387 test!(subtract I64(3), I8(2) => I64(1));
1388 test!(subtract I64(3), I16(2) => I64(1));
1389 test!(subtract I64(3), I32(2) => I64(1));
1390 test!(subtract I64(3), I64(2) => I64(1));
1391 test!(subtract I64(3), I128(2) => I128(1));
1392 test!(subtract I64(3), U8(2) => I64(1));
1393
1394 test!(subtract I128(3), I8(2) => I128(1));
1395 test!(subtract I128(3), I16(2) => I128(1));
1396 test!(subtract I128(3), I32(2) => I128(1));
1397 test!(subtract I128(3), I64(2) => I128(1));
1398 test!(subtract I128(3), I128(2) => I128(1));
1399 test!(subtract I128(3), U8(2) => I128(1));
1400
1401 test!(subtract U8(3), I8(2) => I64(1));
1402 test!(subtract U8(3), I16(2) => I16(1));
1403 test!(subtract U8(3), I32(2) => I32(1));
1404 test!(subtract U8(3), I64(2) => I64(1));
1405 test!(subtract U8(3), I128(2) => I128(1));
1406 test!(subtract U8(3), U8(2) => U8(1));
1407 test!(subtract U8(3), F32(2.0_f32) => F32(1.0_f32));
1408 test!(subtract U8(3), F64(2.0) => F64(1.0));
1409
1410 test!(subtract U16(3), I8(2) => U16(1));
1411 test!(subtract U16(3), I16(2) => U16(1));
1412 test!(subtract U16(3), I32(2) => U16(1));
1413 test!(subtract U16(3), I64(2) => U16(1));
1414 test!(subtract U16(3), I128(2) => U16(1));
1415 test!(subtract U16(3), U8(2) => U16(1));
1416 test!(subtract U16(3), F32(2.0_f32) => F32(1.0_f32));
1417 test!(subtract U16(3), F64(2.0) => F64(1.0));
1418
1419 test!(subtract U32(3), I8(2) => U32(1));
1420 test!(subtract U32(3), I16(2) => U32(1));
1421 test!(subtract U32(3), I32(2) => U32(1));
1422 test!(subtract U32(3), I64(2) => U32(1));
1423 test!(subtract U32(3), I128(2) => U32(1));
1424 test!(subtract U32(3), U8(2) => U32(1));
1425 test!(subtract U32(3), F32(2.0_f32) => F32(1.0_f32));
1426 test!(subtract U32(3), F64(2.0) => F64(1.0));
1427
1428 test!(subtract U64(3), I8(2) => U64(1));
1429 test!(subtract U64(3), I16(2) => U64(1));
1430 test!(subtract U64(3), I32(2) => U64(1));
1431 test!(subtract U64(3), I64(2) => U64(1));
1432 test!(subtract U64(3), I128(2) => U64(1));
1433 test!(subtract U64(3), U8(2) => U64(1));
1434 test!(subtract U64(3), F32(2.0_f32) => F32(1.0_f32));
1435 test!(subtract U64(3), F64(2.0) => F64(1.0));
1436
1437 test!(subtract U128(3), I8(2) => U128(1));
1438 test!(subtract U128(3), I16(2) => U128(1));
1439 test!(subtract U128(3), I32(2) => U128(1));
1440 test!(subtract U128(3), I64(2) => U128(1));
1441 test!(subtract U128(3), I128(2) => U128(1));
1442 test!(subtract U128(3), U8(2) => U128(1));
1443 test!(subtract U128(3), F32(2.0_f32) => F32(1.0_f32));
1444 test!(subtract U128(3), F64(2.0) => F64(1.0));
1445
1446 test!(subtract I8(3), F32(2.0_f32) => F32(1.0_f32));
1447 test!(subtract I32(3), F32(2.0_f32) => F32(1.0_f32));
1448 test!(subtract I64(3), F32(2.0_f32) => F32(1.0_f32));
1449 test!(subtract I128(3), F32(2.0_f32) => F32(1.0_f32));
1450 test!(subtract U8(3), F32(2.0_f32) => F32(1.0_f32));
1451 test!(subtract U32(3), F32(2.0_f32) => F32(1.0_f32));
1452 test!(subtract U64(3), F32(2.0_f32) => F32(1.0_f32));
1453 test!(subtract U128(3), F32(2.0_f32) => F32(1.0_f32));
1454
1455 test!(subtract I8(3), F64(2.0) => F64(1.0));
1456 test!(subtract I32(3), F64(2.0) => F64(1.0));
1457 test!(subtract I64(3), F64(2.0) => F64(1.0));
1458 test!(subtract I128(3), F64(2.0) => F64(1.0));
1459
1460 test!(subtract I32(3), I8(2) => I64(1));
1461 test!(subtract I32(3), I16(2) => I64(1));
1462 test!(subtract I32(3), I32(2) => I32(1));
1463 test!(subtract I32(3), I64(2) => I64(1));
1464 test!(subtract I32(3), I128(2) => I128(1));
1465
1466 test!(subtract I32(3), F32(2.0_f32) => F32(1.0_f32));
1467 test!(subtract I32(3), F64(2.0) => F64(1.0));
1468
1469 test!(subtract I64(3), I8(2) => I64(1));
1470 test!(subtract I64(3), I16(2) => I64(1));
1471 test!(subtract I64(3), I32(2) => I64(1));
1472 test!(subtract I64(3), I64(2) => I64(1));
1473 test!(subtract I64(3), I128(2) => I64(1));
1474 test!(subtract I64(3), F32(2.0_f32) => F32(1.0_f32));
1475 test!(subtract I64(3), F64(2.0) => F64(1.0));
1476
1477 test!(subtract F32(3.0_f32), F32(2.0_f32) => F32(1.0_f32));
1478 test!(subtract F32(3.0_f32), F64(2.0) => F64(1.0));
1479 test!(subtract F32(3.0_f32), I8(2) => F32(1.0_f32));
1480 test!(subtract F32(3.0_f32), I64(2) => F32(1.0_f32));
1481
1482 test!(subtract F64(3.0), F32(2.0_f32) => F32(1.0_f32));
1483 test!(subtract F64(3.0), F64(2.0) => F64(1.0));
1484 test!(subtract F64(3.0), I8(2) => F64(1.0));
1485 test!(subtract F64(3.0), I64(2) => F64(1.0));
1486 test!(subtract F64(3.0), U8(2) => F64(1.0));
1487
1488 test!(subtract decimal(3), decimal(2) => decimal(1));
1489
1490 test!(subtract
1491 Date(NaiveDate::from_ymd_opt(2021, 11, 11).unwrap()),
1492 Date(NaiveDate::from_ymd_opt(2021, 6, 11).unwrap())
1493 =>
1494 Interval(Interval::days(153))
1495 );
1496 test!(subtract
1497 Date(NaiveDate::from_ymd_opt(2021, 1, 1).unwrap()),
1498 Interval(Interval::days(365))
1499 =>
1500 Timestamp(NaiveDate::from_ymd_opt(2020, 1, 2).unwrap().and_hms_opt(0, 0, 0).unwrap())
1501 );
1502 test!(subtract
1503 Timestamp(NaiveDate::from_ymd_opt(2021, 1, 1).unwrap().and_hms_opt(15, 0, 0).unwrap()),
1504 Timestamp(NaiveDate::from_ymd_opt(2021, 1, 1).unwrap().and_hms_opt(12, 0, 0).unwrap())
1505 =>
1506 Interval(Interval::hours(3))
1507 );
1508 test!(subtract
1509 Timestamp(NaiveDate::from_ymd_opt(2021, 1, 1).unwrap().and_hms_opt(0, 3, 0).unwrap()),
1510 Interval(Interval::days(365))
1511 =>
1512 Timestamp(NaiveDate::from_ymd_opt(2020, 1, 2).unwrap().and_hms_opt(0, 3, 0).unwrap())
1513 );
1514 test!(subtract
1515 Time(time(1, 4, 6)),
1516 Interval(Interval::hours(20))
1517 =>
1518 Time(time(5, 4, 6))
1519 );
1520 test!(subtract
1521 Time(time(23, 10, 0)),
1522 Interval(Interval::hours(5))
1523 =>
1524 Time(time(18, 10, 0))
1525 );
1526 test!(subtract mon!(1), mon!(2) => mon!(-1));
1527
1528 test!(multiply I8(3), I8(2) => I8(6));
1529 test!(multiply I8(3), I16(2) => I8(6));
1530 test!(multiply I8(3), I32(2) => I32(6));
1531 test!(multiply I8(3), I64(2) => I64(6));
1532 test!(multiply I8(3), I128(2) => I128(6));
1533 test!(multiply I8(3), U8(2) => I64(6));
1534
1535 test!(multiply I64(3), I8(2) => I64(6));
1536 test!(multiply I64(3), I16(2) => I64(6));
1537 test!(multiply I64(3), I32(2) => I64(6));
1538 test!(multiply I64(3), I64(2) => I64(6));
1539 test!(multiply I64(3), I128(2) => I128(6));
1540 test!(multiply I64(3), U8(2) => I64(6));
1541
1542 test!(multiply I128(3), I8(2) => I128(6));
1543 test!(multiply I128(3), I16(2) => I128(6));
1544 test!(multiply I128(3), I32(2) => I128(6));
1545 test!(multiply I128(3), I64(2) => I128(6));
1546 test!(multiply I128(3), I128(2) => I128(6));
1547 test!(multiply I128(3), U8(2) => I128(6));
1548
1549 test!(multiply I8(3), F32(2.0_f32) => F32(6.0_f32));
1550 test!(multiply I16(3), F32(2.0_f32) => F32(6.0_f32));
1551 test!(multiply I32(3), F32(2.0_f32) => F32(6.0_f32));
1552 test!(multiply I64(3), F32(2.0_f32) => F32(6.0_f32));
1553 test!(multiply I128(3), F32(2.0_f32) => F32(6.0_f32));
1554 test!(multiply I128(3), U8(2) => I128(6));
1555
1556 test!(multiply I8(3), F64(2.0) => F64(6.0));
1557 test!(multiply I16(3), F64(2.0) => F64(6.0));
1558 test!(multiply I32(3), F64(2.0) => F64(6.0));
1559 test!(multiply I64(3), F64(2.0) => F64(6.0));
1560 test!(multiply I128(3), F64(2.0) => F64(6.0));
1561 test!(multiply I128(3), U8(2) => I128(6));
1562
1563 test!(multiply U8(3), I8(2) => I64(6));
1564 test!(multiply U8(3), I16(2) => I16(6));
1565 test!(multiply U8(3), I32(2) => I32(6));
1566 test!(multiply U8(3), I64(2) => I64(6));
1567 test!(multiply U8(3), I128(2) => I128(6));
1568 test!(multiply U8(3), U8(2) => U8(6));
1569 test!(multiply U8(3), F32(2.0_f32) => F32(6.0_f32));
1570 test!(multiply U8(3), F64(2.0) => F64(6.0));
1571
1572 test!(multiply U16(3), I8(2) => U16(6));
1573 test!(multiply U16(3), I16(2) => U16(6));
1574 test!(multiply U16(3), I32(2) => U16(6));
1575 test!(multiply U16(3), I64(2) => U16(6));
1576 test!(multiply U16(3), I128(2) => U16(6));
1577 test!(multiply U16(3), U8(2) => U16(6));
1578 test!(multiply U16(3), F32(2.0_f32) => F64(6.0));
1579 test!(multiply U16(3), F64(2.0) => F64(6.0));
1580
1581 test!(multiply U32(3), I8(2) => U32(6));
1582 test!(multiply U32(3), I16(2) => U32(6));
1583 test!(multiply U32(3), I32(2) => U32(6));
1584 test!(multiply U32(3), I64(2) => U32(6));
1585 test!(multiply U32(3), I128(2) => U32(6));
1586 test!(multiply U32(3), U8(2) => U32(6));
1587 test!(multiply U32(3), F32(2.0_f32) => F64(6.0));
1588 test!(multiply U32(3), F64(2.0) => F64(6.0));
1589
1590 test!(multiply U64(3), I8(2) => U64(6));
1591 test!(multiply U64(3), I16(2) => U64(6));
1592 test!(multiply U64(3), I32(2) => U64(6));
1593 test!(multiply U64(3), I64(2) => U64(6));
1594 test!(multiply U64(3), I128(2) => U64(6));
1595 test!(multiply U64(3), U8(2) => U64(6));
1596 test!(multiply U64(3), F32(2.0_f32) => F64(6.0));
1597 test!(multiply U64(3), F64(2.0) => F64(6.0));
1598
1599 test!(multiply U128(3), I8(2) => U128(6));
1600 test!(multiply U128(3), I16(2) => U128(6));
1601 test!(multiply U128(3), I32(2) => U128(6));
1602 test!(multiply U128(3), I64(2) => U128(6));
1603 test!(multiply U128(3), I128(2) => U128(6));
1604 test!(multiply U128(3), U8(2) => U128(6));
1605 test!(multiply U128(3), F32(2.0_f32) => F32(6.0_f32));
1606 test!(multiply U128(3), F64(2.0) => F64(6.0));
1607
1608 test!(multiply F32(3.0_f32), F32(2.0_f32) => F32(6.0_f32));
1609 test!(multiply F32(3.0_f32), F64(2.0) => F64(6.0));
1610 test!(multiply F32(3.0_f32), I8(2) => F32(6.0_f32));
1611 test!(multiply F32(3.0_f32), I32(2) => F32(6.0_f32));
1612 test!(multiply F32(3.0_f32), I64(2) => F32(6.0_f32));
1613 test!(multiply F32(3.0_f32), I128(2) => F32(6.0_f32));
1614 test!(multiply F32(3.0_f32), U8(2) => F32(6.0_f32));
1615
1616 test!(multiply F64(3.0), F64(2.0) => F64(6.0));
1617 test!(multiply F64(3.0), F32(2.0_f32) => F32(6.0_f32));
1618 test!(multiply F64(3.0), I8(2) => F64(6.0));
1619 test!(multiply F64(3.0), I32(2) => F64(6.0));
1620 test!(multiply F64(3.0), I64(2) => F64(6.0));
1621 test!(multiply F64(3.0), I128(2) => F64(6.0));
1622 test!(multiply F64(3.0), U8(2) => F64(6.0));
1623
1624 test!(multiply decimal(3), decimal(2) => decimal(6));
1625
1626 test!(multiply I8(3), mon!(3) => mon!(9));
1627 test!(multiply I16(3), mon!(3) => mon!(9));
1628 test!(multiply I32(3), mon!(3) => mon!(9));
1629 test!(multiply I64(3), mon!(3) => mon!(9));
1630 test!(multiply I128(3), mon!(3) => mon!(9));
1631 test!(multiply F32(3.0_f32), mon!(3) => mon!(9));
1632 test!(multiply F64(3.0), mon!(3) => mon!(9));
1633 test!(multiply mon!(3), I8(2) => mon!(6));
1634 test!(multiply mon!(3), I16(2) => mon!(6));
1635 test!(multiply mon!(3), I32(2) => mon!(6));
1636 test!(multiply mon!(3), I64(2) => mon!(6));
1637 test!(multiply mon!(3), I128(2) => mon!(6));
1638 test!(multiply mon!(3), F32(2.0_f32) => mon!(6));
1639 test!(multiply mon!(3), F32(2.0_f32) => mon!(6));
1640 test!(multiply mon!(3), F64(2.0) => mon!(6));
1641
1642 test!(divide I8(0), I8(5) => I8(0));
1643 test!(divide I8(0), I16(5) => I8(0));
1644 test!(divide I8(0), I32(5) => I32(0));
1645 test!(divide I8(0), I64(5) => I64(0));
1646 test!(divide I8(0), I128(5) => I128(0));
1647 test!(divide I8(0), U8(5) => I64(0));
1648 assert_eq!(
1649 I8(5).divide(&I8(0)),
1650 Err(ValueError::DivisorShouldNotBeZero.into())
1651 );
1652
1653 test!(divide I8(6), I8(2) => I8(3));
1654 test!(divide I8(6), I16(2) => I8(3));
1655 test!(divide I8(6), I32(2) => I8(3));
1656 test!(divide I8(6), I64(2) => I64(3));
1657 test!(divide I8(6), I128(2) => I128(3));
1658 test!(divide I8(6), U8(2) => I64(3));
1659
1660 test!(divide I64(6), I8(2) => I64(3));
1661 test!(divide I64(6), I16(2) => I64(3));
1662 test!(divide I64(6), I32(2) => I64(3));
1663 test!(divide I64(6), I64(2) => I64(3));
1664 test!(divide I64(6), I128(2) => I128(3));
1665 test!(divide I64(6), U8(2) => I64(3));
1666
1667 test!(divide I128(6), I8(2) => I128(3));
1668 test!(divide I128(6), I16(2) => I128(3));
1669 test!(divide I128(6), I32(2) => I128(3));
1670 test!(divide I128(6), I64(2) => I128(3));
1671 test!(divide I128(6), I128(2) => I128(3));
1672 test!(divide I128(6), U8(2) => I64(3));
1673
1674 test!(divide I128(6), I8(2) => I128(3));
1675 test!(divide I128(6), I16(2) => I128(3));
1676 test!(divide I128(6), I32(2) => I128(3));
1677 test!(divide I128(6), I64(2) => I128(3));
1678 test!(divide I128(6), I128(2) => I128(3));
1679
1680 test!(divide U8(6), I8(2) => I64(3));
1681 test!(divide U8(6), I16(2) => I16(3));
1682 test!(divide U8(6), I32(2) => I32(3));
1683 test!(divide U8(6), I64(2) => I64(3));
1684 test!(divide U8(6), I128(2) => I128(3));
1685 test!(divide U8(6), U8(2) => U8(3));
1686 test!(divide U8(6), F32(2.0_f32) => F64(3.0));
1687 test!(divide U8(6), F64(2.0) => F64(3.0));
1688
1689 test!(divide U16(6), I8(2) => U16(3));
1690 test!(divide U16(6), I16(2) => U16(3));
1691 test!(divide U16(6), I32(2) => U16(3));
1692 test!(divide U16(6), I64(2) => U16(3));
1693 test!(divide U16(6), I128(2) => U16(3));
1694 test!(divide U16(6), U8(2) => U16(3));
1695 test!(divide U16(6), F32(2.0_f32) => F64(3.0));
1696 test!(divide U16(6), F64(2.0) => F64(3.0));
1697
1698 test!(divide U32(6), I8(2) => U32(3));
1699 test!(divide U32(6), I16(2) => U32(3));
1700 test!(divide U32(6), I32(2) => U32(3));
1701 test!(divide U32(6), I64(2) => U32(3));
1702 test!(divide U32(6), I128(2) => U32(3));
1703 test!(divide U32(6), U8(2) => U32(3));
1704 test!(divide U32(6), F32(2.0_f32) => F64(3.0));
1705 test!(divide U32(6), F64(2.0) => F64(3.0));
1706
1707 test!(divide U64(6), I8(2) => U64(3));
1708 test!(divide U64(6), I16(2) => U64(3));
1709 test!(divide U64(6), I32(2) => U64(3));
1710 test!(divide U64(6), I64(2) => U64(3));
1711 test!(divide U64(6), I128(2) => U64(3));
1712 test!(divide U64(6), U8(2) => U64(3));
1713 test!(divide U64(6), F32(2.0_f32) => F64(3.0));
1714 test!(divide U64(6), F64(2.0) => F64(3.0));
1715
1716 test!(divide U128(6), I8(2) => U128(3));
1717 test!(divide U128(6), I16(2) => U128(3));
1718 test!(divide U128(6), I32(2) => U128(3));
1719 test!(divide U128(6), I64(2) => U128(3));
1720 test!(divide U128(6), I128(2) => U128(3));
1721 test!(divide U128(6), U8(2) => U128(3));
1722 test!(divide U128(6), F64(2.0) => F64(3.0));
1723
1724 test!(divide I8(6), F64(2.0) => F64(3.0));
1725 test!(divide I32(6), F64(2.0) => F64(3.0));
1726 test!(divide I64(6), F64(2.0) => F64(3.0));
1727 test!(divide I128(6), F64(2.0) => F64(3.0));
1728 test!(divide F32(6.0_f32), F64(2.0) => F64(3.0));
1729
1730 test!(divide I8(6), F32(2.0_f32) => F32(3.0_f32));
1731 test!(divide I32(6), F32(2.0_f32) => F32(3.0_f32));
1732 test!(divide I64(6), F32(2.0_f32) => F32(3.0_f32));
1733 test!(divide I128(6), F32(2.0_f32) => F32(3.0_f32));
1734 test!(divide F64(6.0), F32(2.0_f32) => F32(3.0_f32));
1735
1736 test!(divide F32(6.0_f32), I8(2) => F32(3.0_f32));
1737 test!(divide F32(6.0_f32), I16(2) => F32(3.0_f32));
1738 test!(divide F32(6.0_f32), I32(2) => F32(3.0_f32));
1739 test!(divide F32(6.0_f32), I64(2) => F32(3.0_f32));
1740 test!(divide F32(6.0_f32), I128(2) => F32(3.0_f32));
1741 test!(divide F64(6.0), F32(2.0_f32) => F32(3.0_f32));
1742
1743 test!(divide F64(6.0), I8(2) => F64(3.0));
1744 test!(divide F64(6.0), I16(2) => F64(3.0));
1745 test!(divide F64(6.0), I32(2) => F64(3.0));
1746 test!(divide F64(6.0), I64(2) => F64(3.0));
1747 test!(divide F64(6.0), I128(2) => F64(3.0));
1748 test!(divide F64(6.0), U8(2) => F64(3.0));
1749 test!(divide F64(6.0), F32(2.0_f32) => F32(3.0_f32));
1750
1751 test!(divide mon!(6), I8(2) => mon!(3));
1752 test!(divide mon!(6), I16(2) => mon!(3));
1753 test!(divide mon!(6), I32(2) => mon!(3));
1754 test!(divide mon!(6), I64(2) => mon!(3));
1755 test!(divide mon!(6), I128(2) => mon!(3));
1756 test!(divide mon!(6), U8(2) => mon!(3));
1757 test!(divide mon!(6), U16(2) => mon!(3));
1758 test!(divide mon!(6), U32(2) => mon!(3));
1759 test!(divide mon!(6), U64(2) => mon!(3));
1760 test!(divide mon!(6), U128(2) => mon!(3));
1761 test!(divide mon!(6), F32(2.0_f32) => mon!(3));
1762 test!(divide mon!(6), F64(2.0) => mon!(3));
1763
1764 test!(modulo I8(6), I8(4) => I8(2));
1765 test!(modulo I8(6), I16(4) => I8(2));
1766 test!(modulo I8(6), I32(4) => I8(2));
1767 test!(modulo I8(6), I64(4) => I64(2));
1768 test!(modulo I8(6), I128(4) => I128(2));
1769
1770 assert_eq!(
1771 I8(5).modulo(&I8(0)),
1772 Err(ValueError::DivisorShouldNotBeZero.into())
1773 );
1774
1775 test!(modulo I64(6), I8(4) => I64(2));
1776 test!(modulo I64(6), I16(4) => I64(2));
1777 test!(modulo I64(6), I32(4) => I64(2));
1778 test!(modulo I64(6), I64(4) => I64(2));
1779 test!(modulo I64(6), I128(4) => I128(2));
1780
1781 test!(modulo I128(6), I8(4) => I128(2));
1782 test!(modulo I128(6), I16(4) => I128(2));
1783 test!(modulo I128(6), I32(4) => I128(2));
1784 test!(modulo I128(6), I64(4) => I128(2));
1785 test!(modulo I128(6), I128(4) => I128(2));
1786
1787 test!(modulo I8(6), I8(2) => I8(0));
1788 test!(modulo I8(6), F32(2.0_f32) => F32(0.0_f32));
1789 test!(modulo I8(6), F64(2.0) => F64(0.0));
1790 test!(modulo I32(6), I32(2) => I32(0));
1791 test!(modulo I32(6), F64(2.0) => F64(0.0));
1792 test!(modulo I64(6), I32(2) => I32(0));
1793 test!(modulo I64(6), F64(2.0) => F64(0.0));
1794 test!(modulo F32(6.0_f32), I64(2) => F32(0.0_f32));
1795 test!(modulo F32(6.0_f32), F32(2.0_f32) => F32(0.0_f32));
1796 test!(modulo F64(6.0), I64(2) => F64(0.0));
1797 test!(modulo F64(6.0), F64(2.0) => F64(0.0));
1798 test!(modulo I128(6), I8(2) => I128(0));
1799 test!(modulo I128(6), I16(2) => I128(0));
1800 test!(modulo I128(6), I32(2) => I128(0));
1801 test!(modulo I128(6), I64(2) => I128(0));
1802 test!(modulo I128(6), I128(2) => I128(0));
1803 test!(modulo I128(6), F64(2.0) => F64(0.0));
1804 test!(modulo I128(6), F32(2.0_f32) => F32(0.0_f32));
1805
1806 macro_rules! null_test {
1807 ($op: ident $a: expr, $b: expr) => {
1808 assert!($a.$op(&$b).unwrap().is_null());
1809 };
1810 }
1811
1812 let date = || Date(NaiveDate::from_ymd_opt(1989, 3, 1).unwrap());
1813 let time = || Time(NaiveTime::from_hms_opt(6, 1, 1).unwrap());
1814 let ts = || {
1815 Timestamp(
1816 NaiveDate::from_ymd_opt(1989, 1, 1)
1817 .unwrap()
1818 .and_hms_opt(0, 0, 0)
1819 .unwrap(),
1820 )
1821 };
1822
1823 null_test!(add I8(1), Null);
1824 null_test!(add I16(1), Null);
1825 null_test!(add I32(1), Null);
1826 null_test!(add I64(1), Null);
1827 null_test!(add I128(1), Null);
1828 null_test!(add U8(1), Null);
1829 null_test!(add U16(1), Null);
1830 null_test!(add U32(1), Null);
1831 null_test!(add U64(1), Null);
1832 null_test!(add U128(1), Null);
1833 null_test!(add F32(1.0_f32), Null);
1834 null_test!(add F64(1.0), Null);
1835 null_test!(add decimal(1), Null);
1836 null_test!(add date(), Null);
1837 null_test!(add ts(), Null);
1838 null_test!(add time(), Null);
1839 null_test!(add mon!(1), Null);
1840 null_test!(subtract I8(1), Null);
1841 null_test!(subtract I16(1), Null);
1842 null_test!(subtract I32(1), Null);
1843 null_test!(subtract I64(1), Null);
1844 null_test!(subtract I128(1), Null);
1845 null_test!(subtract U8(1), Null);
1846 null_test!(subtract U16(1), Null);
1847 null_test!(subtract U32(1), Null);
1848 null_test!(subtract U64(1), Null);
1849 null_test!(subtract U128(1), Null);
1850 null_test!(subtract F32(1.0_f32), Null);
1851 null_test!(subtract F64(1.0), Null);
1852 null_test!(subtract decimal(1), Null);
1853 null_test!(subtract date(), Null);
1854 null_test!(subtract ts(), Null);
1855 null_test!(subtract time(), Null);
1856 null_test!(subtract mon!(1), Null);
1857 null_test!(multiply I8(1), Null);
1858 null_test!(multiply I16(1), Null);
1859 null_test!(multiply I32(1), Null);
1860 null_test!(multiply I64(1), Null);
1861 null_test!(multiply I128(1), Null);
1862 null_test!(multiply U8(1), Null);
1863 null_test!(multiply U16(1), Null);
1864 null_test!(multiply U32(1), Null);
1865 null_test!(multiply U64(1), Null);
1866 null_test!(multiply U128(1), Null);
1867 null_test!(multiply F32(1.0_f32), Null);
1868 null_test!(multiply F64(1.0), Null);
1869 null_test!(multiply decimal(1), Null);
1870 null_test!(multiply mon!(1), Null);
1871 null_test!(divide I8(1), Null);
1872 null_test!(divide I16(1), Null);
1873 null_test!(divide I32(1), Null);
1874 null_test!(divide I64(1), Null);
1875 null_test!(divide I128(1), Null);
1876 null_test!(divide U8(1), Null);
1877 null_test!(divide U16(1), Null);
1878 null_test!(divide U32(1), Null);
1879 null_test!(divide U64(1), Null);
1880 null_test!(divide U128(1), Null);
1881 null_test!(divide F32(1.0_f32), Null);
1882 null_test!(divide F64(1.0), Null);
1883 null_test!(divide decimal(1), Null);
1884 null_test!(divide mon!(1), Null);
1885 null_test!(modulo I8(1), Null);
1886 null_test!(modulo I16(1), Null);
1887 null_test!(modulo I32(1), Null);
1888 null_test!(modulo I64(1), Null);
1889 null_test!(modulo I128(1), Null);
1890 null_test!(modulo U8(1), Null);
1891 null_test!(modulo U16(1), Null);
1892 null_test!(modulo U32(1), Null);
1893 null_test!(modulo U64(1), Null);
1894 null_test!(modulo U128(1), Null);
1895 null_test!(modulo F32(1.0_f32), Null);
1896 null_test!(modulo F64(1.0), Null);
1897 null_test!(modulo decimal(1), Null);
1898
1899 null_test!(add Null, I8(1));
1900 null_test!(add Null, I16(1));
1901 null_test!(add Null, I32(1));
1902 null_test!(add Null, I64(1));
1903 null_test!(add Null, I128(1));
1904 null_test!(add Null, U8(1));
1905 null_test!(add Null, U16(1));
1906 null_test!(add Null, U32(1));
1907 null_test!(add Null, U64(1));
1908 null_test!(add Null, U128(1));
1909 null_test!(add Null, F32(1.0_f32));
1910 null_test!(add Null, F64(1.0));
1911 null_test!(add Null, decimal(1));
1912 null_test!(add Null, mon!(1));
1913 null_test!(add Null, date());
1914 null_test!(add Null, ts());
1915 null_test!(subtract Null, I8(1));
1916 null_test!(subtract Null, I16(1));
1917 null_test!(subtract Null, I32(1));
1918 null_test!(subtract Null, I64(1));
1919 null_test!(subtract Null, I128(1));
1920 null_test!(subtract Null, U8(1));
1921 null_test!(subtract Null, U16(1));
1922 null_test!(subtract Null, U32(1));
1923 null_test!(subtract Null, U64(1));
1924 null_test!(subtract Null, U128(1));
1925 null_test!(subtract Null, F32(1.0_f32));
1926 null_test!(subtract Null, F64(1.0));
1927 null_test!(subtract Null, decimal(1));
1928 null_test!(subtract Null, date());
1929 null_test!(subtract Null, ts());
1930 null_test!(subtract Null, time());
1931 null_test!(subtract Null, mon!(1));
1932 null_test!(multiply Null, I8(1));
1933 null_test!(multiply Null, I16(1));
1934 null_test!(multiply Null, I32(1));
1935 null_test!(multiply Null, I64(1));
1936 null_test!(multiply Null, I128(1));
1937 null_test!(multiply Null, U8(1));
1938 null_test!(multiply Null, U16(1));
1939 null_test!(multiply Null, U32(1));
1940 null_test!(multiply Null, U64(1));
1941 null_test!(multiply Null, U128(1));
1942 null_test!(multiply Null, F32(1.0_f32));
1943 null_test!(multiply Null, F64(1.0));
1944 null_test!(multiply Null, decimal(1));
1945 null_test!(divide Null, I8(1));
1946 null_test!(divide Null, I16(1));
1947 null_test!(divide Null, I32(1));
1948 null_test!(divide Null, I64(1));
1949 null_test!(divide Null, I128(1));
1950 null_test!(divide Null, U8(1));
1951 null_test!(divide Null, U16(1));
1952 null_test!(divide Null, U32(1));
1953 null_test!(divide Null, U64(1));
1954 null_test!(divide Null, U128(1));
1955 null_test!(divide Null, F32(1.0_f32));
1956 null_test!(divide Null, F64(1.0));
1957 null_test!(divide Null, decimal(1));
1958 null_test!(modulo Null, I8(1));
1959 null_test!(modulo Null, I32(1));
1960 null_test!(modulo Null, I64(1));
1961 null_test!(modulo Null, I128(1));
1962 null_test!(modulo Null, U8(1));
1963 null_test!(modulo Null, U16(1));
1964 null_test!(modulo Null, U32(1));
1965 null_test!(modulo Null, U64(1));
1966 null_test!(modulo Null, U128(1));
1967 null_test!(modulo Null, F32(1.0_f32));
1968 null_test!(modulo Null, F64(1.0));
1969 null_test!(modulo Null, decimal(1));
1970
1971 null_test!(add Null, Null);
1972 null_test!(subtract Null, Null);
1973 null_test!(multiply Null, Null);
1974 null_test!(divide Null, Null);
1975 null_test!(modulo Null, Null);
1976 }
1977
1978 #[test]
1979 fn bitwise_shift_left() {
1980 use {super::error::ValueError, crate::ast::DataType};
1981
1982 use crate::data::Tribool::True;
1983 macro_rules! test {
1984 ($op: ident $a: expr, $b: expr => $c: expr) => {
1985 assert_eq!(True, $a.$op(&$b).unwrap().evaluate_eq(&$c));
1986 };
1987 }
1988
1989 macro_rules! mon {
1990 ($n: expr) => {
1991 Interval(Interval::Month($n))
1992 };
1993 }
1994
1995 test!(bitwise_shift_left I8(1), I64(2) => I8(4));
1997 test!(bitwise_shift_left I16(1), I64(2) => I16(4));
1998 test!(bitwise_shift_left I32(1), I64(2) => I32(4));
1999 test!(bitwise_shift_left I64(1), I64(2) => I64(4));
2000 test!(bitwise_shift_left I128(1), I64(2) => I128(4));
2001 test!(bitwise_shift_left U8(1), I64(2) => U8(4));
2002 test!(bitwise_shift_left U16(1), I64(2) => U16(4));
2003 test!(bitwise_shift_left U32(1), I64(2) => U32(4));
2004 test!(bitwise_shift_left U64(1), I64(2) => U64(4));
2005 test!(bitwise_shift_left U128(1), I64(2) => U128(4));
2006 test!(bitwise_shift_left I8(1), U32(2) => I8(4));
2007 test!(bitwise_shift_left I16(1), U32(2) => I16(4));
2008 test!(bitwise_shift_left I32(1), U32(2) => I32(4));
2009 test!(bitwise_shift_left I64(1), U32(2) => I64(4));
2010 test!(bitwise_shift_left I128(1), U32(2) => I128(4));
2011 test!(bitwise_shift_left U8(1), U32(2) => U8(4));
2012 test!(bitwise_shift_left U16(1), U32(2) => U16(4));
2013 test!(bitwise_shift_left U32(1), U32(2) => U32(4));
2014 test!(bitwise_shift_left U64(1), U32(2) => U64(4));
2015 test!(bitwise_shift_left U128(1), U32(2) => U128(4));
2016
2017 assert_eq!(
2019 I8(1).bitwise_shift_left(&I64(100)),
2020 Err(ValueError::BinaryOperationOverflow {
2021 lhs: I8(1),
2022 rhs: U32(100),
2023 operator: NumericBinaryOperator::BitwiseShiftLeft
2024 }
2025 .into())
2026 );
2027 assert_eq!(
2028 I16(1).bitwise_shift_left(&I64(100)),
2029 Err(ValueError::BinaryOperationOverflow {
2030 lhs: I16(1),
2031 rhs: U32(100),
2032 operator: NumericBinaryOperator::BitwiseShiftLeft
2033 }
2034 .into())
2035 );
2036 assert_eq!(
2037 I32(1).bitwise_shift_left(&I64(100)),
2038 Err(ValueError::BinaryOperationOverflow {
2039 lhs: I32(1),
2040 rhs: U32(100),
2041 operator: NumericBinaryOperator::BitwiseShiftLeft
2042 }
2043 .into())
2044 );
2045 assert_eq!(
2046 I64(1).bitwise_shift_left(&I64(100)),
2047 Err(ValueError::BinaryOperationOverflow {
2048 lhs: I64(1),
2049 rhs: U32(100),
2050 operator: NumericBinaryOperator::BitwiseShiftLeft
2051 }
2052 .into())
2053 );
2054 assert_eq!(
2055 I128(1).bitwise_shift_left(&I64(150)),
2056 Err(ValueError::BinaryOperationOverflow {
2057 lhs: I128(1),
2058 rhs: U32(150),
2059 operator: NumericBinaryOperator::BitwiseShiftLeft
2060 }
2061 .into())
2062 );
2063 assert_eq!(
2064 U8(1).bitwise_shift_left(&I64(100)),
2065 Err(ValueError::BinaryOperationOverflow {
2066 lhs: U8(1),
2067 rhs: U32(100),
2068 operator: NumericBinaryOperator::BitwiseShiftLeft
2069 }
2070 .into())
2071 );
2072 assert_eq!(
2073 U16(1).bitwise_shift_left(&I64(100)),
2074 Err(ValueError::BinaryOperationOverflow {
2075 lhs: U16(1),
2076 rhs: U32(100),
2077 operator: NumericBinaryOperator::BitwiseShiftLeft
2078 }
2079 .into())
2080 );
2081 assert_eq!(
2082 U32(1).bitwise_shift_left(&I64(100)),
2083 Err(ValueError::BinaryOperationOverflow {
2084 lhs: U32(1),
2085 rhs: U32(100),
2086 operator: NumericBinaryOperator::BitwiseShiftLeft
2087 }
2088 .into())
2089 );
2090 assert_eq!(
2091 U64(1).bitwise_shift_left(&I64(100)),
2092 Err(ValueError::BinaryOperationOverflow {
2093 lhs: U64(1),
2094 rhs: U32(100),
2095 operator: NumericBinaryOperator::BitwiseShiftLeft
2096 }
2097 .into())
2098 );
2099 assert_eq!(
2100 U128(1).bitwise_shift_left(&I64(150)),
2101 Err(ValueError::BinaryOperationOverflow {
2102 lhs: U128(1),
2103 rhs: U32(150),
2104 operator: NumericBinaryOperator::BitwiseShiftLeft
2105 }
2106 .into())
2107 );
2108
2109 assert_eq!(
2111 I64(1).bitwise_shift_left(&I64(-2)),
2112 Err(ValueError::ConvertFailed {
2113 value: I64(-2),
2114 data_type: DataType::Uint32,
2115 }
2116 .into())
2117 );
2118
2119 assert_eq!(
2121 mon!(3).bitwise_shift_left(&I64(2)),
2122 Err(ValueError::NonNumericMathOperation {
2123 lhs: mon!(3),
2124 rhs: U32(2),
2125 operator: NumericBinaryOperator::BitwiseShiftLeft,
2126 }
2127 .into())
2128 );
2129
2130 macro_rules! null_test {
2132 ($op: ident $a: expr, $b: expr) => {
2133 assert!($a.$op(&$b).unwrap().is_null());
2134 };
2135 }
2136
2137 null_test!(bitwise_shift_left I64(1), Null);
2138 null_test!(bitwise_shift_left Null, I64(1));
2139 }
2140
2141 #[test]
2142 fn bitwise_shift_right() {
2143 use {super::error::ValueError, crate::ast::DataType};
2144
2145 use crate::data::Tribool::True;
2146 macro_rules! test {
2147 ($op: ident $a: expr, $b: expr => $c: expr) => {
2148 assert_eq!(True, $a.$op(&$b).unwrap().evaluate_eq(&$c));
2149 };
2150 }
2151
2152 macro_rules! mon {
2153 ($n: expr) => {
2154 Interval(Interval::Month($n))
2155 };
2156 }
2157
2158 test!(bitwise_shift_right I8(1), I64(2) => I8(0));
2160 test!(bitwise_shift_right I16(1), I64(2) => I16(0));
2161 test!(bitwise_shift_right I32(1), I64(2) => I32(0));
2162 test!(bitwise_shift_right I64(1), I64(2) => I64(0));
2163 test!(bitwise_shift_right I128(1), I64(2) => I128(0));
2164 test!(bitwise_shift_right U8(1), I64(2) => U8(0));
2165 test!(bitwise_shift_right U16(1), I64(2) => U16(0));
2166 test!(bitwise_shift_right U32(1), I64(2) => U32(0));
2167 test!(bitwise_shift_right U64(1), I64(2) => U64(0));
2168 test!(bitwise_shift_right U128(1), I64(2) => U128(0));
2169 test!(bitwise_shift_right I8(1), U32(2) => I8(0));
2170 test!(bitwise_shift_right I16(1), U32(2) => I16(0));
2171 test!(bitwise_shift_right I32(1), U32(2) => I32(0));
2172 test!(bitwise_shift_right I64(1), U32(2) => I64(0));
2173 test!(bitwise_shift_right I128(1), U32(2) => I128(0));
2174 test!(bitwise_shift_right U8(1), U32(2) => U8(0));
2175 test!(bitwise_shift_right U16(1), U32(2) => U16(0));
2176 test!(bitwise_shift_right U32(1), U32(2) => U32(0));
2177 test!(bitwise_shift_right U64(1), U32(2) => U64(0));
2178 test!(bitwise_shift_right U128(1), U32(2) => U128(0));
2179
2180 assert_eq!(
2182 I8(1).bitwise_shift_right(&I64(100)),
2183 Err(ValueError::BinaryOperationOverflow {
2184 lhs: I8(1),
2185 rhs: U32(100),
2186 operator: NumericBinaryOperator::BitwiseShiftRight
2187 }
2188 .into())
2189 );
2190 assert_eq!(
2191 I16(1).bitwise_shift_right(&I64(100)),
2192 Err(ValueError::BinaryOperationOverflow {
2193 lhs: I16(1),
2194 rhs: U32(100),
2195 operator: NumericBinaryOperator::BitwiseShiftRight
2196 }
2197 .into())
2198 );
2199 assert_eq!(
2200 I32(1).bitwise_shift_right(&I64(100)),
2201 Err(ValueError::BinaryOperationOverflow {
2202 lhs: I32(1),
2203 rhs: U32(100),
2204 operator: NumericBinaryOperator::BitwiseShiftRight
2205 }
2206 .into())
2207 );
2208 assert_eq!(
2209 I64(1).bitwise_shift_right(&I64(100)),
2210 Err(ValueError::BinaryOperationOverflow {
2211 lhs: I64(1),
2212 rhs: U32(100),
2213 operator: NumericBinaryOperator::BitwiseShiftRight
2214 }
2215 .into())
2216 );
2217 assert_eq!(
2218 I128(1).bitwise_shift_right(&I64(150)),
2219 Err(ValueError::BinaryOperationOverflow {
2220 lhs: I128(1),
2221 rhs: U32(150),
2222 operator: NumericBinaryOperator::BitwiseShiftRight
2223 }
2224 .into())
2225 );
2226 assert_eq!(
2227 U8(1).bitwise_shift_right(&I64(100)),
2228 Err(ValueError::BinaryOperationOverflow {
2229 lhs: U8(1),
2230 rhs: U32(100),
2231 operator: NumericBinaryOperator::BitwiseShiftRight
2232 }
2233 .into())
2234 );
2235 assert_eq!(
2236 U16(1).bitwise_shift_right(&I64(100)),
2237 Err(ValueError::BinaryOperationOverflow {
2238 lhs: U16(1),
2239 rhs: U32(100),
2240 operator: NumericBinaryOperator::BitwiseShiftRight
2241 }
2242 .into())
2243 );
2244 assert_eq!(
2245 U32(1).bitwise_shift_right(&I64(100)),
2246 Err(ValueError::BinaryOperationOverflow {
2247 lhs: U32(1),
2248 rhs: U32(100),
2249 operator: NumericBinaryOperator::BitwiseShiftRight
2250 }
2251 .into())
2252 );
2253 assert_eq!(
2254 U64(1).bitwise_shift_right(&I64(100)),
2255 Err(ValueError::BinaryOperationOverflow {
2256 lhs: U64(1),
2257 rhs: U32(100),
2258 operator: NumericBinaryOperator::BitwiseShiftRight
2259 }
2260 .into())
2261 );
2262 assert_eq!(
2263 U128(1).bitwise_shift_right(&I64(150)),
2264 Err(ValueError::BinaryOperationOverflow {
2265 lhs: U128(1),
2266 rhs: U32(150),
2267 operator: NumericBinaryOperator::BitwiseShiftRight
2268 }
2269 .into())
2270 );
2271
2272 assert_eq!(
2274 I64(1).bitwise_shift_right(&I64(-2)),
2275 Err(ValueError::ConvertFailed {
2276 value: I64(-2),
2277 data_type: DataType::Uint32,
2278 }
2279 .into())
2280 );
2281
2282 assert_eq!(
2284 mon!(3).bitwise_shift_right(&I64(2)),
2285 Err(ValueError::NonNumericMathOperation {
2286 lhs: mon!(3),
2287 rhs: U32(2),
2288 operator: NumericBinaryOperator::BitwiseShiftRight,
2289 }
2290 .into())
2291 );
2292
2293 macro_rules! null_test {
2295 ($op: ident $a: expr, $b: expr) => {
2296 assert!($a.$op(&$b).unwrap().is_null());
2297 };
2298 }
2299
2300 null_test!(bitwise_shift_right I64(1), Null);
2301 null_test!(bitwise_shift_right Null, I64(1));
2302 }
2303
2304 #[test]
2305 fn cast() {
2306 use {
2307 crate::{ast::DataType::*, data::Point, prelude::Value},
2308 chrono::{NaiveDate, NaiveTime},
2309 };
2310
2311 macro_rules! cast {
2312 ($input: expr => $data_type: expr, $expected: expr) => {
2313 let found = $input.cast(&$data_type).unwrap();
2314
2315 match ($expected, found) {
2316 (Null, Null) => {}
2317 (expected, found) => {
2318 assert_eq!(expected, found);
2319 }
2320 }
2321 };
2322 }
2323
2324 let bytea = Value::Bytea(hex::decode("0abc").unwrap());
2325 let inet = |v| Value::Inet(IpAddr::from_str(v).unwrap());
2326 let point = |x, y| Value::Point(Point::new(x, y));
2327
2328 cast!(Bool(true) => Boolean , Bool(true));
2330 cast!(Str("a".to_owned()) => Text , Str("a".to_owned()));
2331 cast!(bytea => Bytea , bytea);
2332 cast!(inet("::1") => Inet , inet("::1"));
2333 cast!(I8(1) => Int8 , I8(1));
2334 cast!(I16(1) => Int16 , I16(1));
2335 cast!(I32(1) => Int32 , I32(1));
2336 cast!(I64(1) => Int , I64(1));
2337 cast!(I128(1) => Int128 , I128(1));
2338 cast!(U8(1) => Uint8 , U8(1));
2339 cast!(U16(1) => Uint16 , U16(1));
2340 cast!(U32(1) => Uint32 , U32(1));
2341 cast!(U64(1) => Uint64 , U64(1));
2342 cast!(U128(1) => Uint128 , U128(1));
2343 cast!(F32(1.0_f32) => Float32 , F32(1.0_f32));
2344 cast!(F64(1.0) => Float , F64(1.0));
2345 cast!(Value::Uuid(123) => Uuid , Value::Uuid(123));
2346
2347 cast!(Str("TRUE".to_owned()) => Boolean, Bool(true));
2349 cast!(Str("FALSE".to_owned()) => Boolean, Bool(false));
2350 cast!(I8(1) => Boolean, Bool(true));
2351 cast!(I8(0) => Boolean, Bool(false));
2352 cast!(I16(0) => Boolean, Bool(false));
2353 cast!(I32(1) => Boolean, Bool(true));
2354 cast!(I32(0) => Boolean, Bool(false));
2355 cast!(I64(1) => Boolean, Bool(true));
2356 cast!(I64(0) => Boolean, Bool(false));
2357 cast!(I128(1) => Boolean, Bool(true));
2358 cast!(I128(0) => Boolean, Bool(false));
2359 cast!(U8(1) => Boolean, Bool(true));
2360 cast!(U8(0) => Boolean, Bool(false));
2361 cast!(U16(1) => Boolean, Bool(true));
2362 cast!(U16(0) => Boolean, Bool(false));
2363 cast!(U32(1) => Boolean, Bool(true));
2364 cast!(U32(1) => Boolean, Bool(true));
2365 cast!(U64(1) => Boolean, Bool(true));
2366 cast!(U64(0) => Boolean, Bool(false));
2367 cast!(U128(0) => Boolean, Bool(false));
2368 cast!(U128(0) => Boolean, Bool(false));
2369 cast!(F32(1.0_f32) => Boolean, Bool(true));
2370 cast!(F32(0.0_f32) => Boolean, Bool(false));
2371 cast!(F64(1.0) => Boolean, Bool(true));
2372 cast!(F64(0.0) => Boolean, Bool(false));
2373 cast!(Null => Boolean, Null);
2374
2375 cast!(Bool(true) => Int8, I8(1));
2377 cast!(Bool(false) => Int8, I8(0));
2378 cast!(F32(1.1_f32) => Int8, I8(1));
2379 cast!(F64(1.1) => Int8, I8(1));
2380 cast!(Str("11".to_owned()) => Int8, I8(11));
2381 cast!(Null => Int8, Null);
2382
2383 cast!(Bool(true) => Int32, I32(1));
2384 cast!(Bool(false) => Int32, I32(0));
2385 cast!(F32(1.1_f32) => Int32, I32(1));
2386 cast!(F64(1.1) => Int32, I32(1));
2387 cast!(Str("11".to_owned()) => Int32, I32(11));
2388 cast!(Null => Int32, Null);
2389
2390 cast!(Bool(true) => Int, I64(1));
2391 cast!(Bool(false) => Int, I64(0));
2392 cast!(F32(1.1_f32) => Int, I64(1));
2393 cast!(F64(1.1) => Int, I64(1));
2394 cast!(Str("11".to_owned()) => Int, I64(11));
2395 cast!(Null => Int, Null);
2396
2397 cast!(Bool(true) => Int128, I128(1));
2398 cast!(Bool(false) => Int128, I128(0));
2399 cast!(F32(1.1_f32) => Int128, I128(1));
2400 cast!(F64(1.1) => Int128, I128(1));
2401 cast!(Str("11".to_owned()) => Int128, I128(11));
2402 cast!(Null => Int128, Null);
2403
2404 cast!(Bool(true) => Uint8, U8(1));
2405 cast!(Bool(false) => Uint8, U8(0));
2406 cast!(F32(1.1_f32) => Uint8, U8(1));
2407 cast!(F64(1.1) => Uint8, U8(1));
2408 cast!(Str("11".to_owned()) => Uint8, U8(11));
2409 cast!(Null => Uint8, Null);
2410
2411 cast!(Bool(true) => Uint16, U16(1));
2412 cast!(Bool(false) => Uint16, U16(0));
2413 cast!(F32(1.1_f32) => Uint16, U16(1));
2414 cast!(F64(1.1) => Uint16, U16(1));
2415 cast!(Str("11".to_owned()) => Uint16, U16(11));
2416 cast!(Null => Uint16, Null);
2417
2418 cast!(Bool(true) => Uint32, U32(1));
2419 cast!(Bool(false) => Uint32, U32(0));
2420 cast!(F32(1.1_f32) => Uint32, U32(1));
2421 cast!(F64(1.1) => Uint32, U32(1));
2422 cast!(Str("11".to_owned()) => Uint32, U32(11));
2423 cast!(Null => Uint32, Null);
2424
2425 cast!(Bool(true) => Uint64, U64(1));
2426 cast!(Bool(false) => Uint64, U64(0));
2427 cast!(F32(1.1_f32) => Uint64, U64(1));
2428 cast!(F64(1.1) => Uint64, U64(1));
2429 cast!(Str("11".to_owned()) => Uint64, U64(11));
2430 cast!(Null => Uint64, Null);
2431
2432 cast!(Bool(true) => Uint128, U128(1));
2433 cast!(Bool(false) => Uint128, U128(0));
2434 cast!(F32(1.1_f32) => Uint128, U128(1));
2435 cast!(F64(1.1) => Uint128, U128(1));
2436 cast!(Str("11".to_owned()) => Uint128, U128(11));
2437 cast!(Null => Uint128, Null);
2438
2439 cast!(Bool(true) => Float32, F32(1.0_f32));
2441 cast!(Bool(false) => Float32, F32(0.0_f32));
2442 cast!(I8(1) => Float32, F32(1.0_f32));
2443 cast!(I16(1) => Float32, F32(1.0_f32));
2444 cast!(I32(1) => Float32, F32(1.0_f32));
2445 cast!(I64(1) => Float32, F32(1.0_f32));
2446 cast!(I128(1) => Float32, F32(1.0_f32));
2447 cast!(F64(1.0) => Float32, F32(1.0_f32));
2448
2449 cast!(Bool(true) => Float, F64(1.0));
2451 cast!(Bool(false) => Float, F64(0.0));
2452 cast!(I8(1) => Float, F64(1.0));
2453 cast!(I16(1) => Float, F64(1.0));
2454 cast!(I32(1) => Float, F64(1.0));
2455 cast!(I64(1) => Float, F64(1.0));
2456 cast!(I128(1) => Float, F64(1.0));
2457 cast!(F32(1_f32) => Float, F64(1.0));
2458
2459 cast!(U8(1) => Float, F64(1.0));
2460 cast!(U16(1) => Float, F64(1.0));
2461 cast!(U32(1) => Float, F64(1.0));
2462 cast!(U64(1) => Float, F64(1.0));
2463 cast!(U128(1) => Float, F64(1.0));
2464 cast!(Str("11".to_owned()) => Float, F64(11.0));
2465 cast!(Null => Float, Null);
2466
2467 cast!(Bool(true) => Text, Str("TRUE".to_owned()));
2469 cast!(Bool(false) => Text, Str("FALSE".to_owned()));
2470 cast!(I8(11) => Text, Str("11".to_owned()));
2471 cast!(I16(11) => Text, Str("11".to_owned()));
2472 cast!(I32(11) => Text, Str("11".to_owned()));
2473 cast!(I64(11) => Text, Str("11".to_owned()));
2474 cast!(I128(11) => Text, Str("11".to_owned()));
2475 cast!(U8(11) => Text, Str("11".to_owned()));
2476 cast!(U16(11) => Text, Str("11".to_owned()));
2477 cast!(U32(11) => Text, Str("11".to_owned()));
2478 cast!(U64(11) => Text, Str("11".to_owned()));
2479 cast!(U128(11) => Text, Str("11".to_owned()));
2480 cast!(F32(1.0_f32) => Text, Str("1".to_owned()));
2481 cast!(F64(1.0) => Text, Str("1".to_owned()));
2482 cast!(inet("::1") => Text, Str("::1".to_owned()));
2483
2484 let date = Value::Date(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap());
2485 cast!(date => Text, Str("2021-05-01".to_owned()));
2486
2487 let timestamp = Value::Timestamp(
2488 NaiveDate::from_ymd_opt(2021, 5, 1)
2489 .unwrap()
2490 .and_hms_opt(12, 34, 50)
2491 .unwrap(),
2492 );
2493 cast!(timestamp => Text, Str("2021-05-01 12:34:50".to_owned()));
2494 cast!(Null => Text, Null);
2495
2496 let date = Value::Date(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap());
2498 let timestamp = Value::Timestamp(
2499 NaiveDate::from_ymd_opt(2021, 5, 1)
2500 .unwrap()
2501 .and_hms_opt(12, 34, 50)
2502 .unwrap(),
2503 );
2504
2505 cast!(Str("2021-05-01".to_owned()) => Date, date.clone());
2506 cast!(timestamp => Date, date);
2507 cast!(Null => Date, Null);
2508
2509 cast!(Str("08:05:30".to_owned()) => Time, Value::Time(NaiveTime::from_hms_opt(8, 5, 30).unwrap()));
2511 cast!(Null => Time, Null);
2512
2513 cast!(Value::Date(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap()) => Timestamp, Value::Timestamp(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap().and_hms_opt(0, 0, 0).unwrap()));
2515 cast!(Str("2021-05-01 08:05:30".to_owned()) => Timestamp, Value::Timestamp(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap().and_hms_opt(8, 5, 30).unwrap()));
2516 cast!(Null => Timestamp, Null);
2517
2518 cast!(Value::Str("0abc".to_owned()) => Bytea, Value::Bytea(hex::decode("0abc").unwrap()));
2520 assert_eq!(
2521 Value::Str("!@#$5".to_owned()).cast(&Bytea),
2522 Err(ValueError::CastFromHexToByteaFailed("!@#$5".to_owned()).into()),
2523 );
2524
2525 cast!(inet("::1") => Inet, inet("::1"));
2527 cast!(Str("::1".to_owned()) => Inet, inet("::1"));
2528 cast!(Str("0.0.0.0".to_owned()) => Inet, inet("0.0.0.0"));
2529
2530 cast!(point(0.32, 0.52) => Point, point(0.32, 0.52));
2532 cast!(Str("POINT(0.32 0.52)".to_owned()) => Point, point(0.32, 0.52));
2533
2534 cast!(
2536 Str(r#"{"a": 1}"#.to_owned()) => Map,
2537 Value::parse_json_map(r#"{"a": 1}"#).unwrap()
2538 );
2539
2540 cast!(
2542 Str(r"[1, 2, 3]".to_owned()) => List,
2543 Value::parse_json_list(r"[1, 2, 3]").unwrap()
2544 );
2545
2546 assert_eq!(
2548 Value::Uuid(123).cast(&List),
2549 Err(ValueError::UnimplementedCast {
2550 value: Value::Uuid(123),
2551 data_type: List,
2552 }
2553 .into())
2554 );
2555 }
2556
2557 #[test]
2558 fn concat() {
2559 assert_eq!(
2560 Str("A".to_owned()).concat(Str("B".to_owned())),
2561 Str("AB".to_owned())
2562 );
2563 assert_eq!(
2564 Str("A".to_owned()).concat(Bool(true)),
2565 Str("ATRUE".to_owned())
2566 );
2567 assert_eq!(Str("A".to_owned()).concat(I8(1)), Str("A1".to_owned()));
2568 assert_eq!(Str("A".to_owned()).concat(I16(1)), Str("A1".to_owned()));
2569 assert_eq!(Str("A".to_owned()).concat(I32(1)), Str("A1".to_owned()));
2570 assert_eq!(Str("A".to_owned()).concat(I64(1)), Str("A1".to_owned()));
2571 assert_eq!(Str("A".to_owned()).concat(I128(1)), Str("A1".to_owned()));
2572 assert_eq!(Str("A".to_owned()).concat(U8(1)), Str("A1".to_owned()));
2573 assert_eq!(Str("A".to_owned()).concat(U16(1)), Str("A1".to_owned()));
2574 assert_eq!(Str("A".to_owned()).concat(U32(1)), Str("A1".to_owned()));
2575 assert_eq!(Str("A".to_owned()).concat(U64(1)), Str("A1".to_owned()));
2576 assert_eq!(Str("A".to_owned()).concat(U128(1)), Str("A1".to_owned()));
2577 assert_eq!(
2578 Str("A".to_owned()).concat(F32(1.0_f32)),
2579 Str("A1".to_owned())
2580 );
2581 assert_eq!(Str("A".to_owned()).concat(F64(1.0)), Str("A1".to_owned()));
2582 assert_eq!(
2583 List(vec![I64(1)]).concat(List(vec![I64(2)])),
2584 List(vec![I64(1), I64(2)])
2585 );
2586 assert_eq!(I64(2).concat(I64(1)), Str("21".to_owned()));
2587 assert!(Str("A".to_owned()).concat(Null).is_null());
2588 }
2589
2590 #[test]
2591 fn validate_type() {
2592 use {
2593 super::{Value, ValueError},
2594 crate::{ast::DataType as D, data::Interval as I, data::Point},
2595 chrono::{NaiveDate, NaiveTime},
2596 };
2597
2598 let date = Date(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap());
2599 let timestamp = Timestamp(
2600 NaiveDate::from_ymd_opt(2021, 5, 1)
2601 .unwrap()
2602 .and_hms_opt(12, 34, 50)
2603 .unwrap(),
2604 );
2605 let time = Time(NaiveTime::from_hms_opt(12, 30, 11).unwrap());
2606 let interval = Interval(I::hours(5));
2607 let uuid = Uuid(parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap());
2608 let point = Point(Point::new(1.0, 2.0));
2609 let map = Value::parse_json_map(r#"{ "a": 10 }"#).unwrap();
2610 let list = Value::parse_json_list(r"[ true ]").unwrap();
2611 let bytea = Bytea(hex::decode("9001").unwrap());
2612 let inet = Inet(IpAddr::from_str("::1").unwrap());
2613
2614 assert!(Bool(true).validate_type(&D::Boolean).is_ok());
2615 assert!(Bool(true).validate_type(&D::Int).is_err());
2616 assert!(I8(1).validate_type(&D::Int8).is_ok());
2617 assert!(I8(1).validate_type(&D::Text).is_err());
2618 assert!(I16(1).validate_type(&D::Text).is_err());
2619 assert!(I32(1).validate_type(&D::Int32).is_ok());
2620 assert!(I32(1).validate_type(&D::Text).is_err());
2621 assert!(I64(1).validate_type(&D::Int).is_ok());
2622 assert!(I64(1).validate_type(&D::Text).is_err());
2623 assert!(I128(1).validate_type(&D::Int128).is_ok());
2624 assert!(I128(1).validate_type(&D::Text).is_err());
2625 assert!(U8(1).validate_type(&D::Uint8).is_ok());
2626 assert!(U8(1).validate_type(&D::Text).is_err());
2627 assert!(U16(1).validate_type(&D::Uint16).is_ok());
2628 assert!(U16(1).validate_type(&D::Text).is_err());
2629 assert!(U32(1).validate_type(&D::Uint32).is_ok());
2630 assert!(U32(1).validate_type(&D::Text).is_err());
2631 assert!(U64(1).validate_type(&D::Uint64).is_ok());
2632 assert!(U64(1).validate_type(&D::Text).is_err());
2633 assert!(U128(1).validate_type(&D::Uint128).is_ok());
2634 assert!(U128(1).validate_type(&D::Text).is_err());
2635 assert!(F32(1.0_f32).validate_type(&D::Float32).is_ok());
2636 assert!(F32(1.0_f32).validate_type(&D::Int).is_err());
2637 assert!(F64(1.0).validate_type(&D::Float).is_ok());
2638 assert!(F64(1.0).validate_type(&D::Int).is_err());
2639 assert!(
2640 Decimal(rust_decimal::Decimal::ONE)
2641 .validate_type(&D::Decimal)
2642 .is_ok()
2643 );
2644 assert!(
2645 Decimal(rust_decimal::Decimal::ONE)
2646 .validate_type(&D::Int)
2647 .is_err()
2648 );
2649 assert!(Str("a".to_owned()).validate_type(&D::Text).is_ok());
2650 assert!(Str("a".to_owned()).validate_type(&D::Int).is_err());
2651 assert!(bytea.validate_type(&D::Bytea).is_ok());
2652 assert!(bytea.validate_type(&D::Uuid).is_err());
2653 assert!(inet.validate_type(&D::Inet).is_ok());
2654 assert!(inet.validate_type(&D::Uuid).is_err());
2655 assert!(inet.validate_type(&D::Inet).is_ok());
2656 assert!(inet.validate_type(&D::Uuid).is_err());
2657 assert!(date.validate_type(&D::Date).is_ok());
2658 assert!(date.validate_type(&D::Text).is_err());
2659 assert!(timestamp.validate_type(&D::Timestamp).is_ok());
2660 assert!(timestamp.validate_type(&D::Boolean).is_err());
2661 assert!(time.validate_type(&D::Time).is_ok());
2662 assert!(time.validate_type(&D::Date).is_err());
2663 assert!(interval.validate_type(&D::Interval).is_ok());
2664 assert!(interval.validate_type(&D::Date).is_err());
2665 assert!(uuid.validate_type(&D::Uuid).is_ok());
2666 assert!(uuid.validate_type(&D::Boolean).is_err());
2667 assert!(point.validate_type(&D::Point).is_ok());
2668 assert!(point.validate_type(&D::Boolean).is_err());
2669 assert!(map.validate_type(&D::Map).is_ok());
2670 assert!(map.validate_type(&D::Int).is_err());
2671 assert!(list.validate_type(&D::List).is_ok());
2672 assert!(list.validate_type(&D::Int).is_err());
2673 assert!(Null.validate_type(&D::Time).is_ok());
2674 assert!(Null.validate_type(&D::Boolean).is_ok());
2675
2676 assert_eq!(
2677 Bool(true).validate_type(&D::Text),
2678 Err(ValueError::IncompatibleDataType {
2679 data_type: D::Text,
2680 value: Bool(true),
2681 }
2682 .into()),
2683 );
2684 }
2685
2686 #[test]
2687 fn unary_minus() {
2688 use crate::data::Interval as I;
2689 assert_eq!(I8(1).unary_minus(), Ok(I8(-1)));
2690 assert_eq!(I16(1).unary_minus(), Ok(I16(-1)));
2691 assert_eq!(I32(1).unary_minus(), Ok(I32(-1)));
2692 assert_eq!(I64(1).unary_minus(), Ok(I64(-1)));
2693 assert_eq!(I128(1).unary_minus(), Ok(I128(-1)));
2694
2695 assert_eq!(F32(1.0_f32).unary_minus(), Ok(F32(-1.0)));
2696 assert_eq!(F64(1.0).unary_minus(), Ok(F64(-1.0)));
2697 assert_eq!(
2698 Interval(I::hours(5)).unary_minus(),
2699 Ok(Interval(I::hours(-5)))
2700 );
2701 assert_eq!(Null.unary_minus(), Ok(Null));
2702 assert_eq!(
2703 Decimal(Decimal::ONE).unary_minus(),
2704 Ok(Decimal(-Decimal::ONE))
2705 );
2706
2707 assert_eq!(
2708 Str("abc".to_owned()).unary_minus(),
2709 Err(ValueError::UnaryMinusOnNonNumeric.into())
2710 );
2711 }
2712
2713 #[test]
2714 fn unary_plus() {
2715 assert_eq!(U8(1).unary_plus(), Ok(U8(1)));
2716 assert!(Null.unary_plus().unwrap().is_null());
2717 }
2718
2719 #[test]
2720 fn factorial() {
2721 assert_eq!(I8(5).unary_factorial(), Ok(I128(120)));
2722 assert_eq!(I16(5).unary_factorial(), Ok(I128(120)));
2723 assert_eq!(I32(5).unary_factorial(), Ok(I128(120)));
2724 assert_eq!(I64(5).unary_factorial(), Ok(I128(120)));
2725 assert_eq!(I128(5).unary_factorial(), Ok(I128(120)));
2726 assert_eq!(U8(5).unary_factorial(), Ok(I128(120)));
2727 assert_eq!(U16(5).unary_factorial(), Ok(I128(120)));
2728 assert_eq!(U32(5).unary_factorial(), Ok(I128(120)));
2729 assert_eq!(U64(5).unary_factorial(), Ok(I128(120)));
2730 assert_eq!(U128(5).unary_factorial(), Ok(I128(120)));
2731 assert_eq!(
2732 F32(5.0_f32).unary_factorial(),
2733 Err(ValueError::FactorialOnNonInteger.into())
2734 );
2735 assert_eq!(
2736 F64(5.0).unary_factorial(),
2737 Err(ValueError::FactorialOnNonInteger.into())
2738 );
2739 assert!(Null.unary_factorial().unwrap().is_null());
2740 assert_eq!(
2741 Str("5".to_owned()).unary_factorial(),
2742 Err(ValueError::FactorialOnNonNumeric.into())
2743 );
2744 }
2745
2746 #[test]
2747 fn sqrt() {
2748 assert_eq!(I8(9).sqrt(), Ok(F64(3.0)));
2749 assert_eq!(I16(9).sqrt(), Ok(F64(3.0)));
2750 assert_eq!(I64(9).sqrt(), Ok(F64(3.0)));
2751 assert_eq!(I128(9).sqrt(), Ok(F64(3.0)));
2752 assert_eq!(U8(9).sqrt(), Ok(F64(3.0)));
2753 assert_eq!(U16(9).sqrt(), Ok(F64(3.0)));
2754 assert_eq!(U32(9).sqrt(), Ok(F64(3.0)));
2755 assert_eq!(U64(9).sqrt(), Ok(F64(3.0)));
2756 assert_eq!(U128(9).sqrt(), Ok(F64(3.0)));
2757 assert_eq!(F32(9.0_f32).sqrt(), Ok(F64(3.0)));
2758 assert_eq!(F64(9.0).sqrt(), Ok(F64(3.0)));
2759 assert!(Null.sqrt().unwrap().is_null());
2760 assert_eq!(
2761 Str("9".to_owned()).sqrt(),
2762 Err(ValueError::SqrtOnNonNumeric(Str("9".to_owned())).into())
2763 );
2764 }
2765
2766 #[test]
2767 fn bitwise_and() {
2768 use crate::data::Tribool::True;
2769 macro_rules! test {
2770 ($op: ident $a: expr, $b: expr => $c: expr) => {
2771 assert_eq!(True, $a.$op(&$b).unwrap().evaluate_eq(&$c));
2772 };
2773 }
2774
2775 macro_rules! test_bitwise_and {
2776 ($($vt: ident $pt: ident);*;) => {
2777 $(
2778 test!(bitwise_and $vt($pt::MIN), $vt($pt::MIN) => $vt($pt::MIN & $pt::MIN));
2779 test!(bitwise_and $vt($pt::MIN), $vt($pt::MAX) => $vt($pt::MIN & $pt::MAX));
2780 test!(bitwise_and $vt($pt::MAX), $vt($pt::MAX) => $vt($pt::MAX & $pt::MAX));
2781 test!(bitwise_and $vt(0), $vt(0) => $vt(0 & 0));
2782 test!(bitwise_and $vt(0), $vt(1) => $vt(0 & 1));
2783 test!(bitwise_and $vt(1), $vt(0) => $vt(1 & 0));
2784 test!(bitwise_and $vt(1), $vt(1) => $vt(1 & 1));
2785 )*
2786 };
2787 }
2788
2789 test_bitwise_and!(
2790 I8 i8;
2791 I16 i16;
2792 I32 i32;
2793 I64 i64;
2794 I128 i128;
2795 U8 u8;
2796 U16 u16;
2797 U32 u32;
2798 U64 u64;
2799 U128 u128;
2800 );
2801
2802 macro_rules! null_test {
2803 ($op: ident $a: expr, $b: expr) => {
2804 assert!($a.$op(&$b).unwrap().is_null());
2805 };
2806 }
2807
2808 macro_rules! null_test_bitwise_and {
2809 ($($vt: ident)*) => {
2810 $(
2811 null_test!(bitwise_and $vt(1), Null);
2812 null_test!(bitwise_and Null, $vt(1));
2813 null_test!(bitwise_and Null, Null);
2814 )*
2815 };
2816 }
2817
2818 null_test_bitwise_and!(
2819 I8 I16 I32 I64 I128 U8 U16 U32 U64 U128
2820 );
2821
2822 let lhs = I8(3);
2823 let rhs = I16(12);
2824 assert_eq!(
2825 lhs.bitwise_and(&rhs),
2826 Err(ValueError::NonNumericMathOperation {
2827 lhs,
2828 rhs,
2829 operator: NumericBinaryOperator::BitwiseAnd
2830 }
2831 .into())
2832 );
2833 }
2834
2835 #[test]
2836 fn position() {
2837 let str1 = Str("ramen".to_owned());
2838 let str2 = Str("men".to_owned());
2839 let empty_str = Str(String::new());
2840
2841 assert_eq!(str1.position(&str2), Ok(I64(3)));
2842 assert_eq!(str2.position(&str1), Ok(I64(0)));
2843 assert!(Null.position(&str2).unwrap().is_null());
2844 assert!(str1.position(&Null).unwrap().is_null());
2845 assert_eq!(empty_str.position(&str2), Ok(I64(0)));
2846 assert_eq!(str1.position(&empty_str), Ok(I64(0)));
2847 assert_eq!(
2848 str1.position(&I64(1)),
2849 Err(ValueError::NonStringParameterInPosition {
2850 from: str1,
2851 sub: I64(1)
2852 }
2853 .into())
2854 );
2855 }
2856
2857 #[test]
2858 fn get_type() {
2859 use {
2860 super::Value,
2861 crate::{ast::DataType as D, data::Interval as I, data::Point},
2862 chrono::{NaiveDate, NaiveTime},
2863 };
2864
2865 let decimal = Decimal(rust_decimal::Decimal::ONE);
2866 let date = Date(NaiveDate::from_ymd_opt(2021, 5, 1).unwrap());
2867 let timestamp = Timestamp(
2868 NaiveDate::from_ymd_opt(2021, 5, 1)
2869 .unwrap()
2870 .and_hms_opt(12, 34, 50)
2871 .unwrap(),
2872 );
2873 let time = Time(NaiveTime::from_hms_opt(12, 30, 11).unwrap());
2874 let interval = Interval(I::hours(5));
2875 let uuid = Uuid(parse_uuid("936DA01F9ABD4d9d80C702AF85C822A8").unwrap());
2876 let point = Point(Point::new(1.0, 2.0));
2877 let map = Value::parse_json_map(r#"{ "a": 10 }"#).unwrap();
2878 let list = Value::parse_json_list(r"[ true ]").unwrap();
2879 let bytea = Bytea(hex::decode("9001").unwrap());
2880 let inet = Inet(IpAddr::from_str("::1").unwrap());
2881
2882 assert_eq!(I8(1).get_type(), Some(D::Int8));
2883 assert_eq!(I16(1).get_type(), Some(D::Int16));
2884 assert_eq!(I32(1).get_type(), Some(D::Int32));
2885 assert_eq!(I64(1).get_type(), Some(D::Int));
2886 assert_eq!(I128(1).get_type(), Some(D::Int128));
2887 assert_eq!(U8(1).get_type(), Some(D::Uint8));
2888 assert_eq!(U16(1).get_type(), Some(D::Uint16));
2889 assert_eq!(U32(1).get_type(), Some(D::Uint32));
2890 assert_eq!(U64(1).get_type(), Some(D::Uint64));
2891 assert_eq!(U128(1).get_type(), Some(D::Uint128));
2892 assert_eq!(F32(1.1_f32).get_type(), Some(D::Float32));
2893 assert_eq!(F64(1.1).get_type(), Some(D::Float));
2894 assert_eq!(decimal.get_type(), Some(D::Decimal));
2895 assert_eq!(Bool(true).get_type(), Some(D::Boolean));
2896 assert_eq!(Str('1'.into()).get_type(), Some(D::Text));
2897 assert_eq!(bytea.get_type(), Some(D::Bytea));
2898 assert_eq!(inet.get_type(), Some(D::Inet));
2899 assert_eq!(date.get_type(), Some(D::Date));
2900 assert_eq!(timestamp.get_type(), Some(D::Timestamp));
2901 assert_eq!(time.get_type(), Some(D::Time));
2902 assert_eq!(interval.get_type(), Some(D::Interval));
2903 assert_eq!(uuid.get_type(), Some(D::Uuid));
2904 assert_eq!(point.get_type(), Some(D::Point));
2905 assert_eq!(map.get_type(), Some(D::Map));
2906 assert_eq!(list.get_type(), Some(D::List));
2907 assert_eq!(Null.get_type(), None);
2908 }
2909
2910 #[test]
2911 fn hash() {
2912 use {
2913 super::Interval,
2914 crate::data::point::Point,
2915 chrono::{NaiveDate, NaiveTime},
2916 rust_decimal::Decimal,
2917 std::{
2918 collections::BTreeMap,
2919 collections::hash_map::DefaultHasher,
2920 f32::consts::PI as PI_F32,
2921 f64::consts::PI as PI_F64,
2922 hash::{Hash, Hasher},
2923 net::IpAddr,
2924 str::FromStr,
2925 },
2926 };
2927
2928 fn hash_value<T: Hash>(t: &T) -> u64 {
2929 let mut hasher = DefaultHasher::new();
2930 t.hash(&mut hasher);
2931 hasher.finish()
2932 }
2933
2934 const CANONICAL_F64_NAN_BITS: u64 = 0x7ff8_0000_0000_0000;
2935 const CANONICAL_F32_NAN_BITS: u32 = 0x7fc0_0000;
2936 const CANONICAL_F32_ZERO_BITS: u32 = 0;
2937 const CANONICAL_F64_ZERO_BITS: u64 = 0;
2938
2939 let zero_pos_f32 = F32(0.0);
2941 let zero_neg_f32 = F32(-0.0);
2942 assert_eq!(hash_value(&zero_pos_f32), hash_value(&zero_neg_f32),);
2943
2944 let zero_pos_f64 = F64(0.0);
2945 let zero_neg_f64 = F64(-0.0);
2946 assert_eq!(hash_value(&zero_pos_f64), hash_value(&zero_neg_f64),);
2947
2948 let one_f32 = F32(1.0);
2950 let neg_one_f32 = F32(-1.0);
2951 assert_ne!(hash_value(&one_f32), hash_value(&neg_one_f32),);
2952
2953 let one_f64 = F64(1.0);
2954 let neg_one_f64 = F64(-1.0);
2955 assert_ne!(hash_value(&one_f64), hash_value(&neg_one_f64),);
2956
2957 let values_equal = [
2958 (I8(42), I8(42)),
2959 (I16(42), I16(42)),
2960 (I32(42), I32(42)),
2961 (I64(42), I64(42)),
2962 (I128(42), I128(42)),
2963 (U8(42), U8(42)),
2964 (U16(42), U16(42)),
2965 (U32(42), U32(42)),
2966 (U64(42), U64(42)),
2967 (U128(42), U128(42)),
2968 (F32(PI_F32), F32(PI_F32)),
2969 (F64(PI_F64), F64(PI_F64)),
2970 (Decimal(Decimal::new(314, 2)), Decimal(Decimal::new(314, 2))),
2971 (Bool(true), Bool(true)),
2972 (Bool(false), Bool(false)),
2973 (Str("test".to_owned()), Str("test".to_owned())),
2974 (Bytea(vec![1, 2, 3]), Bytea(vec![1, 2, 3])),
2975 (
2976 Inet(IpAddr::from_str("127.0.0.1").unwrap()),
2977 Inet(IpAddr::from_str("127.0.0.1").unwrap()),
2978 ),
2979 (
2980 Inet(IpAddr::from_str("::1").unwrap()),
2981 Inet(IpAddr::from_str("::1").unwrap()),
2982 ),
2983 (
2984 Date(NaiveDate::from_ymd_opt(2025, 8, 6).unwrap()),
2985 Date(NaiveDate::from_ymd_opt(2025, 8, 6).unwrap()),
2986 ),
2987 (
2988 Timestamp(
2989 NaiveDate::from_ymd_opt(2025, 8, 6)
2990 .unwrap()
2991 .and_hms_opt(10, 30, 0)
2992 .unwrap(),
2993 ),
2994 Timestamp(
2995 NaiveDate::from_ymd_opt(2025, 8, 6)
2996 .unwrap()
2997 .and_hms_opt(10, 30, 0)
2998 .unwrap(),
2999 ),
3000 ),
3001 (
3002 Time(NaiveTime::from_hms_opt(10, 30, 0).unwrap()),
3003 Time(NaiveTime::from_hms_opt(10, 30, 0).unwrap()),
3004 ),
3005 (Interval(Interval::hours(5)), Interval(Interval::hours(5))),
3006 (Uuid(123_456_789), Uuid(123_456_789)),
3007 (List(vec![I64(1), I64(2)]), List(vec![I64(1), I64(2)])),
3008 (Null, Null),
3009 ];
3010
3011 for (a, b) in values_equal {
3012 assert_eq!(a, b);
3013 assert_eq!(hash_value(&a), hash_value(&b), "{a:?} vs {b:?}");
3014 }
3015
3016 let map_test_cases = [{
3017 let mut map1 = BTreeMap::new();
3018 map1.insert("b".to_owned(), I64(2));
3019 map1.insert("a".to_owned(), I64(1));
3020
3021 let mut map2 = BTreeMap::new();
3022 map2.insert("a".to_owned(), I64(1));
3023 map2.insert("b".to_owned(), I64(2));
3024
3025 (super::Value::Map(map1), super::Value::Map(map2))
3026 }];
3027
3028 for (value_map1, value_map2) in map_test_cases {
3029 assert_eq!(value_map1, value_map2);
3030 assert_eq!(hash_value(&value_map1), hash_value(&value_map2));
3031 }
3032
3033 let point_test_cases = [
3034 (Point(Point::new(1.0, 2.0)), Point(Point::new(1.0, 2.0))),
3035 (Point(Point::new(0.0, 1.0)), Point(Point::new(-0.0, 1.0))),
3036 (Point(Point::new(1.0, 0.0)), Point(Point::new(1.0, -0.0))),
3037 (
3038 Point(Point::new(1.0, f64::NAN)),
3039 Point(Point::new(1.0, f64::from_bits(CANONICAL_F64_NAN_BITS))),
3040 ),
3041 (
3042 Point(Point::new(f64::NAN, 1.0)),
3043 Point(Point::new(f64::from_bits(CANONICAL_F64_NAN_BITS), 1.0)),
3044 ),
3045 (
3046 Point(Point::new(f64::NAN, f64::NAN)),
3047 Point(Point::new(
3048 f64::from_bits(CANONICAL_F64_NAN_BITS),
3049 f64::from_bits(CANONICAL_F64_NAN_BITS),
3050 )),
3051 ),
3052 (
3053 Point(Point::new(1.0, 0.0_f64)),
3054 Point(Point::new(1.0, f64::from_bits(CANONICAL_F64_ZERO_BITS))),
3055 ),
3056 (
3057 Point(Point::new(1.0, -0.0_f64)),
3058 Point(Point::new(1.0, f64::from_bits(CANONICAL_F64_ZERO_BITS))),
3059 ),
3060 ];
3061
3062 for (point1, point2) in point_test_cases {
3063 assert_eq!(hash_value(&point1), hash_value(&point2));
3064 }
3065
3066 assert_eq!(hash_value(&F32(0.0)), hash_value(&F32(-0.0)));
3067 assert_eq!(hash_value(&F64(0.0)), hash_value(&F64(-0.0)));
3068 assert_eq!(
3069 hash_value(&F32(f32::NAN)),
3070 hash_value(&F32(f32::from_bits(CANONICAL_F32_NAN_BITS)))
3071 );
3072 assert_eq!(
3073 hash_value(&F64(f64::NAN)),
3074 hash_value(&F64(f64::from_bits(CANONICAL_F64_NAN_BITS)))
3075 );
3076 assert_eq!(
3077 hash_value(&F32(f32::from_bits(CANONICAL_F32_ZERO_BITS))),
3078 hash_value(&F32(0.0))
3079 );
3080 assert_eq!(
3081 hash_value(&F64(f64::from_bits(CANONICAL_F64_ZERO_BITS))),
3082 hash_value(&F64(0.0))
3083 );
3084
3085 let mut map = HashMap::new();
3087 map.insert(F32(f32::NAN), "test");
3088 assert_eq!(
3089 map.get(&F32(f32::from_bits(CANONICAL_F32_NAN_BITS))),
3090 Some(&"test")
3091 );
3092 }
3093
3094 #[test]
3095 fn eq() {
3096 use {
3097 super::Interval,
3098 crate::data::point::Point,
3099 chrono::{NaiveDate, NaiveTime},
3100 rust_decimal::Decimal,
3101 std::{collections::BTreeMap, net::IpAddr, str::FromStr},
3102 };
3103
3104 let test_cases = [
3105 (Bool(true), Bool(true), true),
3106 (Bool(true), Bool(false), false),
3107 (I8(42), I8(42), true),
3108 (I16(42), I16(42), true),
3109 (I32(42), I32(42), true),
3110 (I64(42), I64(42), true),
3111 (I128(42), I128(42), true),
3112 (U8(42), U8(42), true),
3113 (U16(42), U16(42), true),
3114 (U32(42), U32(42), true),
3115 (U64(42), U64(42), true),
3116 (U128(42), U128(42), true),
3117 (F32(1.5), F32(1.5), true),
3118 (F64(1.5), F64(1.5), true),
3119 (
3120 Decimal(Decimal::new(314, 2)),
3121 Decimal(Decimal::new(314, 2)),
3122 true,
3123 ),
3124 (Str("test".to_owned()), Str("test".to_owned()), true),
3125 (Bytea(vec![1, 2, 3]), Bytea(vec![1, 2, 3]), true),
3126 (
3127 Inet(IpAddr::from_str("127.0.0.1").unwrap()),
3128 Inet(IpAddr::from_str("127.0.0.1").unwrap()),
3129 true,
3130 ),
3131 (
3132 Date(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap()),
3133 Date(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap()),
3134 true,
3135 ),
3136 (
3137 Time(NaiveTime::from_hms_opt(10, 30, 0).unwrap()),
3138 Time(NaiveTime::from_hms_opt(10, 30, 0).unwrap()),
3139 true,
3140 ),
3141 (
3142 Interval(Interval::hours(5)),
3143 Interval(Interval::hours(5)),
3144 true,
3145 ),
3146 (Uuid(123_456_789), Uuid(123_456_789), true),
3147 (List(vec![I64(1), I64(2)]), List(vec![I64(1), I64(2)]), true),
3148 (
3149 Point(Point::new(1.0, 2.0)),
3150 Point(Point::new(1.0, 2.0)),
3151 true,
3152 ),
3153 (Null, Null, true),
3154 ];
3155
3156 for (a, b, expected) in test_cases {
3157 assert_eq!(a == b, expected);
3158 }
3159
3160 assert_ne!(Bool(true), I32(1));
3162 assert_ne!(F32(1.0), F64(1.0));
3163 assert_ne!(Null, Bool(false));
3164
3165 assert_eq!(F32(f32::NAN), F32(f32::NAN));
3167 assert_eq!(F64(f64::NAN), F64(f64::NAN));
3168 assert_eq!(F32(0.0), F32(-0.0));
3169 assert_eq!(F64(0.0), F64(-0.0));
3170 assert_eq!(F32(f32::from_bits(0x7fc0_0001)), F32(f32::NAN));
3171
3172 assert_eq!(
3173 Point(Point::new(f64::NAN, 1.0)),
3174 Point(Point::new(f64::NAN, 1.0))
3175 );
3176 assert_eq!(
3177 Point(Point::new(1.0, f64::NAN)),
3178 Point(Point::new(1.0, f64::NAN))
3179 );
3180 assert_eq!(Point(Point::new(0.0, 1.0)), Point(Point::new(-0.0, 1.0)));
3181 assert_eq!(Point(Point::new(1.0, 0.0)), Point(Point::new(1.0, -0.0)));
3182
3183 let mut map1 = BTreeMap::new();
3184 map1.insert("a".to_owned(), I64(1));
3185 let mut map2 = BTreeMap::new();
3186 map2.insert("a".to_owned(), I64(1));
3187 assert_eq!(Map(map1), Map(map2));
3188 }
3189
3190 #[test]
3191 fn regex() {
3192 let str = |value: &str| Value::Str(value.to_owned());
3193
3194 assert_eq!(
3195 str("Hello").regex(&str("ell"), false, true).unwrap(),
3196 Value::Bool(true)
3197 );
3198 assert_eq!(
3199 str("Hello").regex(&str("^hello$"), false, false).unwrap(),
3200 Value::Bool(true)
3201 );
3202 assert_eq!(
3203 str("Hello").regex(&str("ell"), true, true).unwrap(),
3204 Value::Bool(false)
3205 );
3206 assert_eq!(
3207 str("Hello").regex(&str("^hello$"), true, false).unwrap(),
3208 Value::Bool(false)
3209 );
3210 assert_eq!(
3211 Value::Null.regex(&str("."), false, true).unwrap(),
3212 Value::Null
3213 );
3214 assert_eq!(
3215 str("Hello").regex(&Value::Null, false, true).unwrap(),
3216 Value::Null
3217 );
3218 assert_eq!(
3219 Value::Null.regex(&Value::Null, false, true).unwrap(),
3220 Value::Null
3221 );
3222 assert_eq!(
3224 Value::Null.regex(&str("."), true, true).unwrap(),
3225 Value::Null
3226 );
3227
3228 for (negated, case_sensitive, operator) in [
3230 (false, true, "~"),
3231 (false, false, "~*"),
3232 (true, true, "!~"),
3233 (true, false, "!~*"),
3234 ] {
3235 assert_eq!(
3236 Value::Bool(true)
3237 .regex(&str("."), negated, case_sensitive)
3238 .unwrap_err(),
3239 ValueError::RegexOnNonString {
3240 base: Value::Bool(true),
3241 pattern: str("."),
3242 operator: operator.to_owned(),
3243 }
3244 .into(),
3245 );
3246 }
3247 assert_eq!(
3248 Value::Bool(true)
3249 .regex(&Value::Null, false, true)
3250 .unwrap_err(),
3251 ValueError::RegexOnNonString {
3252 base: Value::Bool(true),
3253 pattern: Value::Null,
3254 operator: "~".to_owned(),
3255 }
3256 .into(),
3257 );
3258 assert_eq!(
3259 Value::Null
3260 .regex(&Value::Bool(true), false, true)
3261 .unwrap_err(),
3262 ValueError::RegexOnNonString {
3263 base: Value::Null,
3264 pattern: Value::Bool(true),
3265 operator: "~".to_owned(),
3266 }
3267 .into(),
3268 );
3269 }
3270
3271 #[test]
3272 fn test_conversion_from_tribool() {
3273 use {super::Value, crate::data::Tribool};
3274
3275 assert_eq!(Value::from(Tribool::True), Value::Bool(true));
3276 assert_eq!(Value::from(Tribool::False), Value::Bool(false));
3277 assert_eq!(Value::from(Tribool::Null), Value::Null);
3278 }
3279}