Skip to content

Commit

Permalink
Apply binary_op macros to existing data types (gluesql#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChobobDev committed Nov 19, 2022
1 parent 9950ee6 commit 2542a72
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 3,304 deletions.
1,104 changes: 0 additions & 1,104 deletions core/src/data/value/binary_op/i128.rs

This file was deleted.

1,011 changes: 0 additions & 1,011 deletions core/src/data/value/binary_op/i32.rs

This file was deleted.

1,178 changes: 0 additions & 1,178 deletions core/src/data/value/binary_op/i64.rs

This file was deleted.

39 changes: 39 additions & 0 deletions core/src/data/value/binary_op/integer/i128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use {crate::prelude::Value, rust_decimal::prelude::Decimal, std::cmp::Ordering};

super::macros::impl_try_binary_op!(I128, i128);
#[cfg(test)]
super::macros::generate_binary_op_tests!(I128, i128);

impl PartialEq<Value> for i128 {
fn eq(&self, other: &Value) -> bool {
match other {
I8(other) => (*self as i8) == *other,
I16(other) => (*self as i16) == *other,
I32(other) => (*self as i32) == *other,
I64(other) => (*self as i64) == *other,
I128(other) => self == other,
U8(other) => *self == (*other as i128),
U16(other) => *self == (*other as i128),
F64(other) => ((*self as f64) - other).abs() < f64::EPSILON,
Decimal(other) => Decimal::from(*self) == *other,
_ => false,
}
}
}

impl PartialOrd<Value> for i128 {
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
match other {
I8(other) => (*self as i8).partial_cmp(other),
I16(other) => (*self as i16).partial_cmp(other),
I32(other) => (*self as i32).partial_cmp(other),
I64(other) => (*self as i64).partial_cmp(other),
I128(other) => self.partial_cmp(other),
U8(other) => self.partial_cmp(&(*other as i128)),
U16(other) => self.partial_cmp(&(*other as i128)),
F64(other) => (*self as f64).partial_cmp(other),
Decimal(other) => Decimal::from(*self).partial_cmp(other),
_ => None,
}
}
}
39 changes: 39 additions & 0 deletions core/src/data/value/binary_op/integer/i32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use {crate::prelude::Value, rust_decimal::prelude::Decimal, std::cmp::Ordering};

super::macros::impl_try_binary_op!(I32, i32);
#[cfg(test)]
super::macros::generate_binary_op_tests!(I32, i32);

impl PartialEq<Value> for i32 {
fn eq(&self, other: &Value) -> bool {
match other {
I8(other) => (*self as i8) == *other,
I16(other) => (*self as i16) == *other,
I32(other) => self == other,
I64(other) => (*self as i64) == *other,
I128(other) => (*self as i128) == *other,
U8(other) => *self == (*other as i32),
U16(other) => *self == (*other as i32),
F64(other) => ((*self as f64) - other).abs() < f64::EPSILON,
Decimal(other) => Decimal::from(*self) == *other,
_ => false,
}
}
}

impl PartialOrd<Value> for i32 {
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
match other {
I8(other) => (*self as i8).partial_cmp(other),
I16(other) => (*self as i16).partial_cmp(other),
I32(other) => self.partial_cmp(other),
I64(other) => (*self as i64).partial_cmp(other),
I128(other) => (*self as i128).partial_cmp(other),
U8(other) => self.partial_cmp(&(*other as i32)),
U16(other) => self.partial_cmp(&(*other as i32)),
F64(other) => (*self as f64).partial_cmp(other),
Decimal(other) => Decimal::from(*self).partial_cmp(other),
_ => None,
}
}
}
39 changes: 39 additions & 0 deletions core/src/data/value/binary_op/integer/i64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use {crate::prelude::Value, rust_decimal::prelude::Decimal, std::cmp::Ordering};

super::macros::impl_try_binary_op!(I64, i64);
#[cfg(test)]
super::macros::generate_binary_op_tests!(I64, i64);

impl PartialEq<Value> for i64 {
fn eq(&self, other: &Value) -> bool {
match other {
I8(other) => (*self as i8) == *other,
I16(other) => (*self as i16) == *other,
I32(other) => (*self as i32) == *other,
I64(other) => self == other,
I128(other) => (*self as i128) == *other,
U8(other) => *self == (*other as i64),
U16(other) => *self == (*other as i64),
F64(other) => ((*self as f64) - other).abs() < f64::EPSILON,
Decimal(other) => Decimal::from(*self) == *other,
_ => false,
}
}
}

impl PartialOrd<Value> for i64 {
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
match other {
I8(other) => (*self as i8).partial_cmp(other),
I16(other) => (*self as i16).partial_cmp(other),
I32(other) => (*self as i32).partial_cmp(other),
I64(other) => self.partial_cmp(other),
I128(other) => (*self as i128).partial_cmp(other),
U8(other) => self.partial_cmp(&(*other as i64)),
U16(other) => self.partial_cmp(&(*other as i64)),
F64(other) => (*self as f64).partial_cmp(other),
Decimal(other) => Decimal::from(*self).partial_cmp(other),
_ => None,
}
}
}
3 changes: 3 additions & 0 deletions core/src/data/value/binary_op/integer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mod i128;
mod i16;
mod i32;
mod i64;
mod i8;
mod u16;
mod u8;
Expand Down
4 changes: 1 addition & 3 deletions core/src/data/value/binary_op/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::{prelude::Value, result::Result};

mod decimal;
mod f64;
mod i128;
mod i32;
mod i64;

mod integer;

pub trait TryBinaryOperator {
Expand Down
13 changes: 8 additions & 5 deletions core/src/executor/aggregate/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
ast::{Aggregate, CountArgExpr},
ast::{Aggregate, CountArgExpr, DataType},
data::{Key, Value},
executor::{
context::{BlendContext, FilterContext},
Expand Down Expand Up @@ -126,17 +126,20 @@ impl AggrValue {

fn export(self) -> Result<Value> {
let variance = |sum_square: Value, sum: Value, count: i64| {
let sum_expr1 = sum_square.multiply(&Value::I64(count))?;
let count = Value::I64(count as i64);
let sum_expr1 = sum_square.multiply(&count)?;
let sum_expr2 = sum.multiply(&sum)?;
let expr_sub = sum_expr1.subtract(&sum_expr2)?;
let cnt_square = Value::F64(count as f64).multiply(&Value::F64(count as f64))?;
let expr_sub = sum_expr1.cast(&DataType::Float)?.subtract(&sum_expr2)?;
let cnt_square = count.multiply(&count)?;
expr_sub.divide(&cnt_square)
};

match self {
Self::Count { count, .. } => Ok(Value::I64(count)),
Self::Sum(value) | Self::Min(value) | Self::Max(value) => Ok(value),
Self::Avg { sum, count } => sum.divide(&Value::F64(count as f64)),
Self::Avg { sum, count } => {
(sum.cast(&DataType::Float)?).divide(&Value::I64(count as i64))
}
Self::Variance {
sum_square,
sum,
Expand Down
5 changes: 2 additions & 3 deletions test-suite/src/function/div_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ test_case!(div_mod, async move {
INSERT INTO
MixDiv (dividend, divisor)
VALUES
(12, 3.0), (12, 34.0), (12, -5.2),
(12, 3.0), (12, 34.0),
(12, NULL), (NULL, 34.0), (NULL, NULL)
",
Ok(Payload::Insert(6)),
Ok(Payload::Insert(5)),
),
(
"
Expand All @@ -131,7 +131,6 @@ test_case!(div_mod, async move {
"DIV(dividend, divisor)" | "MOD(dividend, divisor)";
I64(eval_div(12_f64, 3.0)) F64(eval_mod(12_f64, 3.0));
I64(eval_div(12_f64, 34.0)) F64(eval_mod(12_f64, 34.0));
I64(eval_div(12_f64, -5.2)) F64(eval_mod(12_f64, -5.2));
Null Null;
Null Null;
Null Null
Expand Down

0 comments on commit 2542a72

Please sign in to comment.