asolimando commented on code in PR #4557:
URL: https://github.com/apache/calcite/pull/4557#discussion_r2381676656


##########
core/src/main/java/org/apache/calcite/rex/RexUtil.java:
##########
@@ -1718,7 +1731,71 @@ public static boolean isLosslessCast(RelDataType source, 
RelDataType target) {
       final int targetPrecision = target.getPrecision();
       return targetPrecision == PRECISION_NOT_SPECIFIED || targetPrecision >= 
sourceLength;
     }
-    // Return FALSE by default
+
+    // 4) DECIMAL -> DECIMAL
+    if (sourceSqlTypeName == SqlTypeName.DECIMAL
+        && targetSqlTypeName == SqlTypeName.DECIMAL) {
+      int sourcePrecision = source.getPrecision();
+      int sourceScale = Math.max(source.getScale(), 0);
+      int targetPrecision = target.getPrecision();
+      int targetScale = Math.max(target.getScale(), 0);
+      if (sourcePrecision <= 0 || targetPrecision <= 0) {
+        return false;
+      }
+      return targetScale >= sourceScale
+          && (targetPrecision - targetScale) >= (sourcePrecision - 
sourceScale);
+    }
+
+    // 5) integer family (signed or unsigned) -> DECIMAL
+    if (SqlTypeUtil.isIntType(source)
+        && targetSqlTypeName == SqlTypeName.DECIMAL) {
+      int targetPrecision = target.getPrecision();
+      int targetScale = Math.max(target.getScale(), 0);
+      int sourcePrecision = source.getPrecision();
+      if (sourcePrecision <= 0) {

Review Comment:
   Not for the default type system, but I am being paranoid here in case people 
mess things up with a custom type system and return `PRECISION_NOT_SPECIFIED` 
somehow, but I agree it might be confusing as-is, I can either add a comment 
specifying "this is not expected, just for extra safety" or even turn it into 
an assert.
   
   WDYT?
   
   (there are other places where I have done that IIRC, I will adapt all places 
when decided)
   
   EDIT: I grep'ped the codebase to see if I could find such a handling for 
integer precision, and I couldn't find any, so maybe I am overreacting here and 
this check could be dropped entirely



-- 
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]

Reply via email to