Skip to content
Permalink
Browse files

coprocessor: RPN function UnaryNot (tikv#4808)

Signed-off-by: Breezewish <breezewish@pingcap.com>
  • Loading branch information...
breeswish committed Jun 3, 2019
1 parent 6cad183 commit 9f90a536eb7bf8b8a6c4f5416959260bfd92c819
Showing with 34 additions and 0 deletions.
  1. +33 −0 src/coprocessor/dag/rpn_expr/impl_op.rs
  2. +1 −0 src/coprocessor/dag/rpn_expr/mod.rs
@@ -54,6 +54,21 @@ impl RpnFnLogicalOr {
}
}

#[derive(Debug, Clone, Copy, RpnFunction)]
#[rpn_function(args = 1)]
pub struct RpnFnUnaryNot;

impl RpnFnUnaryNot {
#[inline]
fn call(
_ctx: &mut EvalContext,
_payload: RpnFnCallPayload<'_>,
arg: &Option<i64>,
) -> Result<Option<i64>> {
Ok(arg.map(|v| (v == 0) as i64))
}
}

#[derive(Clone, Debug, RpnFunction)]
#[rpn_function(args = 1)]
pub struct RpnFnIsNull<T: Evaluable> {
@@ -218,6 +233,24 @@ mod tests {
}
}

#[test]
fn test_unary_not() {
let test_cases = vec![
(Some(0), Some(1)),
(Some(1), Some(0)),
(Some(2), Some(0)),
(Some(-1), Some(0)),
(None, None),
];
for (arg, expect_output) in test_cases {
let output = RpnFnScalarEvaluator::new()
.push_param(arg)
.evaluate(ScalarFuncSig::UnaryNot)
.unwrap();
assert_eq!(output, expect_output);
}
}

#[test]
fn test_is_null() {
let test_cases = vec![
@@ -151,6 +151,7 @@ fn map_pb_sig_to_rpn_func(value: ScalarFuncSig, children: &[Expr]) -> Result<Box
ScalarFuncSig::DecimalIsFalse => Box::new(RpnFnDecimalIsFalse),
ScalarFuncSig::LogicalAnd => Box::new(RpnFnLogicalAnd),
ScalarFuncSig::LogicalOr => Box::new(RpnFnLogicalOr),
ScalarFuncSig::UnaryNot => Box::new(RpnFnUnaryNot),
ScalarFuncSig::PlusInt => map_int_sig(value, children, plus_mapper)?,
ScalarFuncSig::PlusReal => Box::new(RpnFnArithmetic::<RealPlus>::new()),
ScalarFuncSig::PlusDecimal => Box::new(RpnFnArithmetic::<DecimalPlus>::new()),

0 comments on commit 9f90a53

Please sign in to comment.
You can’t perform that action at this time.