mihaibudiu commented on code in PR #4353:
URL: https://github.com/apache/calcite/pull/4353#discussion_r2072653286


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java:
##########
@@ -422,6 +439,34 @@ protected boolean booleanEquality(SqlCallBinding binding,
     return false;
   }
 
+  /**
+   * COALESCE type coercion, collect all the branches types to find a common 
type,
+   * then cast the operands to the common type when needed.
+   */
+  private boolean coalesceCoercion(SqlCallBinding callBinding) {
+    List<RelDataType> argTypes = new ArrayList<>();
+    SqlValidatorScope scope = getScope(callBinding);
+    for (SqlNode node : callBinding.operands()) {
+      argTypes.add(validator.deriveType(scope, node));
+    }
+    RelDataType widerType = getWiderTypeFor(argTypes, true);
+    if (null != widerType) {
+      return coerceOperandsType(scope, callBinding.getCall(), widerType);
+    }
+    return false;
+  }
+
+  /**
+   * NULLIF type coercion, cast the second operand type to the first operand 
type when needed.

Review Comment:
   Maybe there is a misunderstanding, I didn't say that the NULLIF coercion has 
to be removed, I said it has to behave like the other two coercions, using the 
wider type.
   
   There is a commutative diagram which must hold at all times:
   
   ```
   NULLIF ----------------> CASE
        | coercion           | coercion
        v                    v
   NULLIF-----------------> CASE
   ```
   
   In other words, if you choose to expand NULLIF into CASE and then coerce, or 
if you coerce first and then expand, you should get the exact same result. So 
once you fix the coercion for CASE (which is fixed in the original code), you 
get a fixed coercion rule for NULLIF. All this is true for COALESCE as well.
   
   BTW: I would recommend putting this diagram in the comments of the coercion 
for NULLIF; if someone implements a new validator, this rule has to be obeyed.



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