|
1 | 1 | -- SPARK-23179: SQL ANSI 2011 states that in case of overflow during arithmetic operations, |
2 | 2 | -- an exception should be thrown instead of returning NULL. |
3 | 3 | -- This is what most of the SQL DBs do (eg. SQLServer, DB2). |
4 | | ---import decimalArithmeticOperations.sql |
| 4 | + |
| 5 | +-- tests for decimals handling in operations |
| 6 | +create table decimals_test(id int, a decimal(38,18), b decimal(38,18)) using parquet; |
| 7 | + |
| 8 | +insert into decimals_test values(1, 100.0, 999.0), (2, 12345.123, 12345.123), |
| 9 | + (3, 0.1234567891011, 1234.1), (4, 123456789123456789.0, 1.123456789123456789); |
| 10 | + |
| 11 | +-- test operations between decimals and constants |
| 12 | +select id, a*10, b/10 from decimals_test order by id; |
| 13 | + |
| 14 | +-- test operations on constants |
| 15 | +select 10.3 * 3.0; |
| 16 | +select 10.3000 * 3.0; |
| 17 | +select 10.30000 * 30.0; |
| 18 | +select 10.300000000000000000 * 3.000000000000000000; |
| 19 | +select 10.300000000000000000 * 3.0000000000000000000; |
| 20 | + |
| 21 | +-- arithmetic operations causing an overflow throw exception |
| 22 | +select (5e36 + 0.1) + 5e36; |
| 23 | +select (-4e36 - 0.1) - 7e36; |
| 24 | +select 12345678901234567890.0 * 12345678901234567890.0; |
| 25 | +select 1e35 / 0.1; |
| 26 | + |
| 27 | +-- arithmetic operations causing a precision loss throw exception |
| 28 | +select 123456789123456789.1234567890 * 1.123456789123456789; |
| 29 | +select 123456789123456789.1234567890 * 1.123456789123456789; |
| 30 | +select 12345678912345.123456789123 / 0.000000012345678; |
| 31 | + |
| 32 | +drop table decimals_test; |
0 commit comments