forked from tikv/tikv
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cop: add rpn function ABS (tikv#4982)
Signed-off-by: MWish <1506118561@qq.com>
- Loading branch information
1 parent
e4fe49d
commit cc1f0af
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use cop_codegen::rpn_fn; | ||
|
||
use crate::coprocessor::codec::data_type::*; | ||
use crate::coprocessor::codec::{self, Error}; | ||
use crate::coprocessor::Result; | ||
|
||
#[rpn_fn] | ||
#[inline] | ||
fn abs_int(arg: &Option<Int>) -> Result<Option<Int>> { | ||
match arg { | ||
None => Ok(None), | ||
Some(arg) => match (*arg).checked_abs() { | ||
None => Err(Error::overflow("BIGINT", &format!("abs({})", *arg)))?, | ||
Some(arg_abs) => Ok(Some(arg_abs)), | ||
}, | ||
} | ||
} | ||
|
||
#[rpn_fn] | ||
#[inline] | ||
fn abs_uint(arg: &Option<Int>) -> Result<Option<Int>> { | ||
Ok(*arg) | ||
} | ||
|
||
#[rpn_fn] | ||
#[inline] | ||
fn abs_real(arg: &Option<Real>) -> Result<Option<Real>> { | ||
match arg { | ||
Some(arg) => Ok(Some(num_traits::Signed::abs(arg))), | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
#[rpn_fn] | ||
#[inline] | ||
fn abs_decimal(arg: &Option<Decimal>) -> Result<Option<Decimal>> { | ||
match arg { | ||
Some(arg) => { | ||
let res: codec::Result<Decimal> = arg.to_owned().abs().into(); | ||
Ok(Some(res?)) | ||
} | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use tipb::expression::ScalarFuncSig; | ||
|
||
use crate::coprocessor::dag::rpn_expr::types::test_util::RpnFnScalarEvaluator; | ||
|
||
#[test] | ||
fn test_abs_int() { | ||
let test_cases = vec![ | ||
(ScalarFuncSig::AbsInt, -3, Some(3), false), | ||
( | ||
ScalarFuncSig::AbsInt, | ||
std::i64::MAX, | ||
Some(std::i64::MAX), | ||
false, | ||
), | ||
( | ||
ScalarFuncSig::AbsUInt, | ||
std::u64::MAX as i64, | ||
Some(std::u64::MAX as i64), | ||
false, | ||
), | ||
(ScalarFuncSig::AbsInt, std::i64::MIN, Some(0), true), | ||
]; | ||
|
||
for (sig, arg, expect_output, is_err) in test_cases { | ||
let output = RpnFnScalarEvaluator::new().push_param(arg).evaluate(sig); | ||
|
||
if is_err { | ||
assert!(output.is_err()); | ||
} else { | ||
let output = output.unwrap(); | ||
assert_eq!(output, expect_output, "{:?}", arg); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_abs_real() { | ||
let test_cases: Vec<(Real, Option<Real>)> = vec![ | ||
(Real::new(3.5).unwrap(), Real::new(3.5).ok()), | ||
(Real::new(-3.5).unwrap(), Real::new(3.5).ok()), | ||
]; | ||
|
||
for (arg, expect_output) in test_cases { | ||
let output = RpnFnScalarEvaluator::new() | ||
.push_param(arg) | ||
.evaluate(ScalarFuncSig::AbsReal) | ||
.unwrap(); | ||
assert_eq!(output, expect_output, "{:?}", arg); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_abs_decimal() { | ||
let test_cases = vec![("1.1", "1.1"), ("-1.1", "1.1")]; | ||
|
||
for (arg, expect_output) in test_cases { | ||
let arg = arg.parse::<Decimal>().ok(); | ||
let expect_output = expect_output.parse::<Decimal>().ok(); | ||
let output = RpnFnScalarEvaluator::new() | ||
.push_param(arg.clone()) | ||
.evaluate(ScalarFuncSig::AbsDecimal) | ||
.unwrap(); | ||
assert_eq!(output, expect_output, "{:?}", arg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters