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
cop: add abs rpn function #4982
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1ad4af7
tikv/coprocessor: And tests and implementation for AbsInt AbsUInt Abs…
mapleFU f5db988
tikv/coprocessor: Use git 'diff' and 'rebase' to restruct the commits
mapleFU 1e0602e
[FIX] tikv/coprocessor: Fix the compiling problem in impl_math.rs in …
mapleFU 855cda8
[FMT] tikv/coprocessor: Using make format and clippy to check and for…
mapleFU a6c472f
Merge remote-tracking branch 'upstream/master' into dev-rpn-expr-abs
mapleFU 9f3b1d2
[Update] tikv/coprocessor: Remove unnecessary assert message in tests…
mapleFU 2b98672
[Update] coprocessor/tikv: Using num_traits::sign::Signed to simplify…
mapleFU 22b1021
[Update] tikv/coprocessor: int abs_real omit use num_traits::sign::Si…
mapleFU 372825c
[ADD] tikv/coprocessor/dag/rpn_expr: add test for Int Overflow in tes…
mapleFU 10969db
[ADD] tikv/coprocessor/dag/rpn_expr: patch format for last commit
mapleFU 4563e02
[remove] tikv/coprocessor/dag/rpn_expr: remove annotation in abs_real
mapleFU 27bb574
Merge branch 'master' into dev-rpn-expr-abs
mapleFU File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about the case which will cause overflow or truncated?