gluesql_core/data/value/
mod.rs

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