Skip to content

Commit

Permalink
fix(type): fix decimal from_str and add e2e test (risingwavelabs#3823)
Browse files Browse the repository at this point in the history
* chore(e2e): add e2e test for decimal special values

* fix(type): fix decimal from_str and add e2e test

* fix(type): add unit-test for decimal from_str
  • Loading branch information
Li0k authored Jul 13, 2022
1 parent eb1d49e commit 3f03b2e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
28 changes: 28 additions & 0 deletions e2e_test/streaming/basic.slt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ create table t3 (v1 int not null, v2 int not null, v3 int not null);
statement ok
create table t4 (v1 real not null, v2 int not null, v3 real not null);

statement ok
create table t5 (d decimal);

statement ok
insert into t1 values (1,4,2), (2,3,3);

Expand All @@ -19,6 +22,15 @@ insert into t3 values (1,3,5), (2,4,6);
statement ok
insert into t4 values (1,1,4), (5,1,4), (1,9,1), (9,8,1), (0,2,3);

statement ok
insert into t5 values ('NaN'::decimal);

statement ok
insert into t5 values ('+inf');

statement ok
insert into t5 values ('-inf');

statement ok
create materialized view mv1 as select * from t1;

Expand All @@ -28,6 +40,9 @@ create materialized view mv2 as select round(avg(v1), 1) as avg_v1, sum(v2) as s
statement ok
create materialized view mv3 as select v3, sum(v1) as sum_v1, min(v1) as min_v1, max(v1) as max_v1 from t4 group by v3;

statement ok
create materialized view mv4 as select count(*) as count, d from t5 group by d;

query III rowsort
select v1, v2, v3 from mv1;
----
Expand Down Expand Up @@ -65,6 +80,13 @@ select sum_v1, min_v1, max_v1, v3 from mv3 order by sum_v1;
6 1 5 4
10 1 9 1

query IR rowsort
select * from mv4
----
1 NaN
1 +Inf
1 -Inf

statement ok
drop materialized view mv1

Expand All @@ -74,6 +96,9 @@ drop materialized view mv2
statement ok
drop materialized view mv3

statement ok
drop materialized view mv4

statement ok
drop table t1

Expand All @@ -85,3 +110,6 @@ drop table t3

statement ok
drop table t4

statement ok
drop table t5
35 changes: 33 additions & 2 deletions src/common/src/types/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ impl FromStr for Decimal {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"nan" | "NaN" | "NAN" => Ok(Decimal::NaN),
"inf" | "INF" | "+inf" | "+INF" => Ok(Decimal::PositiveINF),
"-inf" | "-INF" => Ok(Decimal::NegativeINF),
"inf" | "INF" | "+inf" | "+INF" | "+Inf" => Ok(Decimal::PositiveINF),
"-inf" | "-INF" | "-Inf" => Ok(Decimal::NegativeINF),
s => RustDecimal::from_str(s).map(Decimal::Normalized),
}
}
Expand Down Expand Up @@ -602,8 +602,39 @@ mod tests {
#[test]
fn basic_test() {
assert_eq!(Decimal::from_str("nan").unwrap(), Decimal::NaN,);
assert_eq!(Decimal::from_str("NaN").unwrap(), Decimal::NaN,);
assert_eq!(Decimal::from_str("NAN").unwrap(), Decimal::NaN,);

assert_eq!(Decimal::from_str("inf").unwrap(), Decimal::PositiveINF,);
assert_eq!(Decimal::from_str("INF").unwrap(), Decimal::PositiveINF,);
assert_eq!(Decimal::from_str("+inf").unwrap(), Decimal::PositiveINF,);
assert_eq!(Decimal::from_str("+INF").unwrap(), Decimal::PositiveINF,);
assert_eq!(Decimal::from_str("+Inf").unwrap(), Decimal::PositiveINF,);

assert_eq!(Decimal::from_str("-inf").unwrap(), Decimal::NegativeINF,);
assert_eq!(Decimal::from_str("-INF").unwrap(), Decimal::NegativeINF,);
assert_eq!(Decimal::from_str("-Inf").unwrap(), Decimal::NegativeINF,);

assert!(Decimal::from_str("nAn").is_err());
assert!(Decimal::from_str("nAN").is_err());
assert!(Decimal::from_str("Nan").is_err());
assert!(Decimal::from_str("NAn").is_err());

assert!(Decimal::from_str("iNF").is_err());
assert!(Decimal::from_str("inF").is_err());
assert!(Decimal::from_str("InF").is_err());
assert!(Decimal::from_str("INf").is_err());

assert!(Decimal::from_str("+iNF").is_err());
assert!(Decimal::from_str("+inF").is_err());
assert!(Decimal::from_str("+InF").is_err());
assert!(Decimal::from_str("+INf").is_err());

assert!(Decimal::from_str("-iNF").is_err());
assert!(Decimal::from_str("-inF").is_err());
assert!(Decimal::from_str("-InF").is_err());
assert!(Decimal::from_str("-INf").is_err());

assert_eq!(
Decimal::from_f32(10.0).unwrap() / Decimal::PositiveINF,
Decimal::from_f32(0.0).unwrap(),
Expand Down

0 comments on commit 3f03b2e

Please sign in to comment.