Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expr/arithmetic: implement div_real && div_decimal #2243

Merged
merged 10 commits into from Sep 4, 2017
79 changes: 78 additions & 1 deletion src/coprocessor/dag/expr/arithmetic.rs
Expand Up @@ -15,7 +15,7 @@ use std::{f64, i64, u64};
use std::borrow::Cow;
use std::ops::{Add, Mul, Sub};
use coprocessor::codec::{mysql, Datum};
use coprocessor::codec::mysql::Decimal;
use coprocessor::codec::mysql::{Decimal, Res};
use super::{Error, FnCall, Result, StatementContext};

impl FnCall {
Expand Down Expand Up @@ -144,6 +144,36 @@ impl FnCall {
};
res.ok_or(Error::Overflow).map(Some)
}

pub fn divide_real(&self, ctx: &StatementContext, row: &[Datum]) -> Result<Option<f64>> {
let lhs = try_opt!(self.children[0].eval_real(ctx, row));
let rhs = try_opt!(self.children[1].eval_real(ctx, row));
if rhs == 0f64 {
return Ok(None);
}
let res = lhs / rhs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if rhs is 0?

if res.is_infinite() {
Err(Error::Overflow)
} else {
Ok(Some(res))
}
}

pub fn divide_decimal<'a, 'b: 'a>(
&'b self,
ctx: &StatementContext,
row: &'a [Datum],
) -> Result<Option<Cow<'a, Decimal>>> {
let lhs = try_opt!(self.children[0].eval_decimal(ctx, row));
let rhs = try_opt!(self.children[1].eval_decimal(ctx, row));
match lhs.into_owned() / rhs.into_owned() {
Some(v) => match v {
Res::Ok(v) => Ok(Some(Cow::Owned(v))),
Res::Truncated(_) | Res::Overflow(_) => Err(Error::Overflow),
},
None => Ok(None),
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -249,6 +279,30 @@ mod test {
Datum::F64(-0.01),
Datum::F64(-0.0101001),
),
(
ScalarFuncSig::DivideReal,
Datum::F64(2.0),
Datum::F64(0.3),
Datum::F64(6.666666666666667),
),
(
ScalarFuncSig::DivideReal,
Datum::F64(44.3),
Datum::F64(0.000),
Datum::Null,
), // TODO: support precision in divide.
// (
// ScalarFuncSig::DivideReal,
// Datum::F64(-12.3),
// Datum::F64(41f64),
// Datum::F64(-0.3),
// ),
// (
// ScalarFuncSig::DivideReal,
// Datum::F64(12.3),
// Datum::F64(0.3),
// Datum::F64(41f64)
// )
];
let ctx = StatementContext::default();
for tt in tests {
Expand Down Expand Up @@ -282,6 +336,24 @@ mod test {
str2dec("2.2"),
str2dec("2.42"),
),
(
ScalarFuncSig::DivideDecimal,
str2dec("12.3"),
str2dec("-0.3"),
str2dec("-41"),
),
(
ScalarFuncSig::DivideDecimal,
str2dec("12.3"),
str2dec("0.3"),
str2dec("41"),
),
(
ScalarFuncSig::DivideDecimal,
str2dec("12.3"),
str2dec("0"),
Datum::Null,
),
];
let ctx = StatementContext::default();
for tt in tests {
Expand Down Expand Up @@ -375,6 +447,11 @@ mod test {
Datum::F64(f64::MIN),
Datum::F64(f64::MAX),
),
(
ScalarFuncSig::DivideReal,
Datum::F64(f64::MAX),
Datum::F64(0.00001),
),
];
let ctx = StatementContext::default();
for tt in tests {
Expand Down
6 changes: 5 additions & 1 deletion src/coprocessor/dag/expr/fncall.rs
Expand Up @@ -90,7 +90,9 @@ impl FnCall {
ScalarFuncSig::IfNullDuration |
ScalarFuncSig::LogicalAnd |
ScalarFuncSig::LogicalOr |
ScalarFuncSig::LogicalXor => (2, 2),
ScalarFuncSig::LogicalXor |
ScalarFuncSig::DivideDecimal |
ScalarFuncSig::DivideReal => (2, 2),

ScalarFuncSig::CastIntAsInt |
ScalarFuncSig::CastIntAsReal |
Expand Down Expand Up @@ -441,6 +443,7 @@ dispatch_call! {

CoalesceReal => coalesce_real,
CaseWhenReal => case_when_real,
DivideReal => divide_real,
}
DEC_CALLS {
CastIntAsDecimal => cast_int_as_decimal,
Expand All @@ -467,6 +470,7 @@ dispatch_call! {

CoalesceDecimal => coalesce_decimal,
CaseWhenDecimal => case_when_decimal,
DivideDecimal => divide_decimal,
}
BYTES_CALLS {
CastIntAsString => cast_int_as_str,
Expand Down