Skip to content

Commit

Permalink
tests: add unit test of cast string to decimal.
Browse files Browse the repository at this point in the history
  • Loading branch information
youngsofun committed Mar 6, 2023
1 parent d841960 commit ac2d1df
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/query/functions/tests/it/scalars/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn test_cast() {
test_cast_number_to_date(file, is_try);
test_cast_between_number_and_string(file, is_try);
test_cast_between_boolean_and_string(file, is_try);
test_cast_between_string_and_decimal(file, is_try);
test_cast_between_number_and_boolean(file, is_try);
test_cast_between_date_and_timestamp(file, is_try);
test_cast_between_string_and_timestamp(file, is_try);
Expand Down Expand Up @@ -622,3 +623,36 @@ fn test_cast_to_nested_type(file: &mut impl Write, is_try: bool) {
&[],
);
}

fn test_cast_between_string_and_decimal(file: &mut impl Write, is_try: bool) {
let prefix = if is_try { "TRY_" } else { "" };

run_ast(file, format!("{prefix}CAST('010.010' AS DECIMAL(5,3))"), &[
]);
run_ast(file, format!("{prefix}CAST('010.010' AS DECIMAL(5,4))"), &[
]);
run_ast(file, format!("{prefix}CAST('010.010' AS DECIMAL(5,2))"), &[
]);
run_ast(file, format!("{prefix}CAST('010.010' AS DECIMAL(4,3))"), &[
]);
run_ast(file, format!("{prefix}CAST('010.010' AS DECIMAL(4,2))"), &[
]);
run_ast(
file,
format!("{prefix}CAST('-1010.010' AS DECIMAL(7,3))"),
&[],
);
run_ast(file, format!("{prefix}CAST('00' AS DECIMAL(2,1))"), &[]);
run_ast(file, format!("{prefix}CAST('0.0' AS DECIMAL(2,0))"), &[]);
run_ast(file, format!("{prefix}CAST('.0' AS DECIMAL(1,0))"), &[]);
run_ast(
file,
format!("{prefix}CAST('+1.0e-10' AS DECIMAL(11, 10))"),
&[],
);
run_ast(
file,
format!("{prefix}CAST('-1.0e+10' AS DECIMAL(11, 0))"),
&[],
);
}
185 changes: 185 additions & 0 deletions src/query/functions/tests/it/scalars/testdata/cast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,103 @@ evaluation (internal):
+--------+-----------------------------------------------------------------+


ast : CAST('010.010' AS DECIMAL(5,3))
raw expr : CAST("010.010" AS Decimal(5, 3))
checked expr : to_decimal<String>("010.010")
optimized expr : 10.010_d128(5,3)
output type : Decimal(5, 3)
output domain : {10.010..=10.010}
output : 10.010


error:
--> SQL:1:1
|
1 | CAST('010.010' AS DECIMAL(5,4))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fail to decode string as decimal: Code: 1049, displayText = decimal overflow. while evaluating function `to_decimal("010.010")`



ast : CAST('010.010' AS DECIMAL(5,2))
raw expr : CAST("010.010" AS Decimal(5, 2))
checked expr : to_decimal<String>("010.010")
optimized expr : 10.01_d128(5,2)
output type : Decimal(5, 2)
output domain : {10.01..=10.01}
output : 10.01


error:
--> SQL:1:1
|
1 | CAST('010.010' AS DECIMAL(4,3))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fail to decode string as decimal: Code: 1049, displayText = decimal overflow. while evaluating function `to_decimal("010.010")`



ast : CAST('010.010' AS DECIMAL(4,2))
raw expr : CAST("010.010" AS Decimal(4, 2))
checked expr : to_decimal<String>("010.010")
optimized expr : 10.01_d128(4,2)
output type : Decimal(4, 2)
output domain : {10.01..=10.01}
output : 10.01


ast : CAST('-1010.010' AS DECIMAL(7,3))
raw expr : CAST("-1010.010" AS Decimal(7, 3))
checked expr : to_decimal<String>("-1010.010")
optimized expr : -1010.010_d128(7,3)
output type : Decimal(7, 3)
output domain : {-1010.010..=-1010.010}
output : -1010.010


ast : CAST('00' AS DECIMAL(2,1))
raw expr : CAST("00" AS Decimal(2, 1))
checked expr : to_decimal<String>("00")
optimized expr : 0.0_d128(2,1)
output type : Decimal(2, 1)
output domain : {0.0..=0.0}
output : 0.0


ast : CAST('0.0' AS DECIMAL(2,0))
raw expr : CAST("0.0" AS Decimal(2, 0))
checked expr : to_decimal<String>("0.0")
optimized expr : 0_d128(2,0)
output type : Decimal(2, 0)
output domain : {0..=0}
output : 0


ast : CAST('.0' AS DECIMAL(1,0))
raw expr : CAST(".0" AS Decimal(1, 0))
checked expr : to_decimal<String>(".0")
optimized expr : 0_d128(1,0)
output type : Decimal(1, 0)
output domain : {0..=0}
output : 0


ast : CAST('+1.0e-10' AS DECIMAL(11, 10))
raw expr : CAST("+1.0e-10" AS Decimal(11, 10))
checked expr : to_decimal<String>("+1.0e-10")
optimized expr : 0.0000000001_d128(11,10)
output type : Decimal(11, 10)
output domain : {0.0000000001..=0.0000000001}
output : 0.0000000001


ast : CAST('-1.0e+10' AS DECIMAL(11, 0))
raw expr : CAST("-1.0e+10" AS Decimal(11, 0))
checked expr : to_decimal<String>("-1.0e+10")
optimized expr : -10000000000_d128(11,0)
output type : Decimal(11, 0)
output domain : {-10000000000..=-10000000000}
output : -10000000000


ast : CAST(0 AS BOOLEAN)
raw expr : CAST(0_u8 AS Boolean)
checked expr : to_boolean<UInt8>(0_u8)
Expand Down Expand Up @@ -2806,6 +2903,94 @@ evaluation (internal):
+--------+--------------------------------------------------------------------------------------------------------------------+


error:
--> SQL:1:1
|
1 | TRY_CAST('010.010' AS DECIMAL(5,3))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(5, 3) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('010.010' AS DECIMAL(5,4))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(5, 4) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('010.010' AS DECIMAL(5,2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(5, 2) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('010.010' AS DECIMAL(4,3))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(4, 3) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('010.010' AS DECIMAL(4,2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(4, 2) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('-1010.010' AS DECIMAL(7,3))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(7, 3) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('00' AS DECIMAL(2,1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(2, 1) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('0.0' AS DECIMAL(2,0))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(2, 0) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('.0' AS DECIMAL(1,0))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(1, 0) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('+1.0e-10' AS DECIMAL(11, 10))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(11, 10) NULL`



error:
--> SQL:1:1
|
1 | TRY_CAST('-1.0e+10' AS DECIMAL(11, 0))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to cast type `String` to type `Decimal(11, 0) NULL`



ast : TRY_CAST(0 AS BOOLEAN)
raw expr : TRY_CAST(0_u8 AS Boolean)
checked expr : try_to_boolean<UInt8>(0_u8)
Expand Down

0 comments on commit ac2d1df

Please sign in to comment.