mihaibudiu commented on code in PR #3945:
URL: https://github.com/apache/calcite/pull/3945#discussion_r1769014518
##########
core/src/test/resources/sql/cast.iq:
##########
@@ -1234,4 +1234,385 @@ EnumerableCalc(expr#0..7=[{inputs}],
expr#8=[CAST($t6):INTEGER], expr#9=[IS NOT
EnumerableTableScan(table=[[scott, EMP]])
!plan
+# [CALCITE-6560] Add supportsNegativeScale in RelDataTypeSystem
+
+!use scott-negative-scale
+
+# supports negative scale and the rounding mode is DOWN
+
+# throw an exception when CAST target decimal type has illegal precision and
scale
+
+# precision is 0
+values cast(15.3 as decimal(0, 4));
+DECIMAL precision 0 must be between 1 and 19
+!error
+
+# Same as previous; the number is 0
+values cast(0 as decimal(0,0));
+DECIMAL precision 0 must be between 1 and 19
+!error
+
+# scale greater than precision
+values cast(0 as decimal(3, 4));
++--------+
+| EXPR$0 |
++--------+
+| 0.0000 |
++--------+
+(1 row)
+
+!ok
+
+# Same as previous; the number is 1
+select cast(1 as decimal(3, 4));
+Value 1 cannot be represented as a DECIMAL(3, 4)
+!error
+
+# test the maximum value that can be stored
+select cast(99999 as decimal(2, -3));
++--------+
+| EXPR$0 |
++--------+
+| 99000 |
++--------+
+(1 row)
+
+!ok
+
+# Same as previous; the value is minimum value -99999
+select cast(-99999 as decimal(2, -3));
++--------+
+| EXPR$0 |
++--------+
+| -99000 |
++--------+
+(1 row)
+
+!ok
+
+# Same as previous; the value is 100000
+select cast(100000 as decimal(2, -3));
+Value 100000 cannot be represented as a DECIMAL(2, -3)
+!error
+
+# Same as previous; the value is -100000
+select cast(-100000 as decimal(2, -3));
+Value -100000 cannot be represented as a DECIMAL(2, -3)
+!error
+
+# Test cast integer to decimal with negative scale
+
+# The non-rounded digits greater than the precision represents the maximum
value
+select cast(12340 as decimal(3, -1));
+Value 12340 cannot be represented as a DECIMAL(3, -1)
+!error
+
+# The precision represents the maximum value greater than the number
+select cast(12340 as decimal(6, -1));
++--------+
+| EXPR$0 |
++--------+
+| 12340 |
++--------+
+(1 row)
+
+!ok
+
+# Same as previous; the precision is 5
+select cast(12340 as decimal(5, -1));
++--------+
+| EXPR$0 |
++--------+
+| 12340 |
++--------+
+(1 row)
+
+!ok
+
+# Same as previous; the precision is 4
+select cast(12340 as decimal(4, -1));
++--------+
+| EXPR$0 |
++--------+
+| 12340 |
++--------+
+(1 row)
+
+!ok
+
+# Test the negative scale that can be represents
+select cast(12340 as decimal(3, -2));
++--------+
+| EXPR$0 |
++--------+
+| 12300 |
++--------+
+(1 row)
+
+!ok
+
Review Comment:
I can't give you a unit test, but at least in
Primitive.checkOverflow(BigDecimal, ...) there is an implicit assumption that
negative scales are not legal. I think we can leave this for another PR, here
you introduce support, but not all operations that need negative scales need to
work yet.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]