Skip to content

Commit

Permalink
Implement unary factorial operation for (unsigned) INT8 type (glues…
Browse files Browse the repository at this point in the history
  • Loading branch information
MRGRAVITY817 authored and ever0de committed Feb 20, 2022
1 parent b883d62 commit 02f93d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions core/src/data/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,18 @@ impl Value {
use Value::*;

let factorial_function = |a: i64| -> Result<i64> {
if a.is_negative() {
return Err(ValueError::FactorialOnNegativeNumeric.into());
}
(1..(a + 1))
.into_iter()
.try_fold(1i64, |mul, x| mul.checked_mul(x))
.ok_or_else(|| ValueError::FactorialOverflow.into())
};

match self {
I64(a) if *a >= 0 => factorial_function(*a).map(I64),
I64(_) => Err(ValueError::FactorialOnNegativeNumeric.into()),
I8(a) => factorial_function(*a as i64).map(I64),
I64(a) => factorial_function(*a).map(I64),
F64(_) => Err(ValueError::FactorialOnNonInteger.into()),
Null => Ok(Null),
_ => Err(ValueError::FactorialOnNonNumeric.into()),
Expand Down
18 changes: 13 additions & 5 deletions test-suite/src/unary_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ test_case!(unary_operator, async move {

let test_cases = vec![
(
"CREATE TABLE Test (v1 INT, v2 FLOAT, v3 TEXT, v4 INT, v5 INT)",
"CREATE TABLE Test (v1 INT, v2 FLOAT, v3 TEXT, v4 INT, v5 INT, v6 INT(8))",
Ok(Payload::Create),
),
(
r#"INSERT INTO Test VALUES (10, 10.5, "hello", -5, 1000)"#,
r#"INSERT INTO Test VALUES (10, 10.5, "hello", -5, 1000, 20)"#,
Ok(Payload::Insert(1)),
),
(
"SELECT -v1 as v1, -v2 as v2, v3, -v4 as v4 FROM Test",
"SELECT -v1 as v1, -v2 as v2, v3, -v4 as v4, -v6 as v6 FROM Test",
Ok(select_with_null!(
v1 | v2 | v3 | v4;
I64(-10) F64(-10.5) Str("hello".to_owned()) I64(5)
v1 | v2 | v3 | v4 | v6;
I64(-10) F64(-10.5) Str("hello".to_owned()) I64(5) I8(-20)
)),
),
(
Expand Down Expand Up @@ -86,6 +86,14 @@ test_case!(unary_operator, async move {
"SELECT v5! as v5 FROM Test",
Err(ValueError::FactorialOverflow.into()),
),
(
"SELECT (-v6)! as v6 FROM Test",
Err(ValueError::FactorialOnNegativeNumeric.into()),
),
(
"SELECT (v6 * 100)! as v6 FROM Test",
Err(ValueError::FactorialOverflow.into()),
),
(
"SELECT (-5)! as v4 FROM Test",
Err(ValueError::FactorialOnNegativeNumeric.into()),
Expand Down

0 comments on commit 02f93d8

Please sign in to comment.