You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/12/02 01:19:47 UTC

[GitHub] [calcite] danny0405 commented on a change in pull request #1611: [CALCITE-3355] Deduce whether CASE and COALESCE may produce NULL values

danny0405 commented on a change in pull request #1611: [CALCITE-3355] Deduce whether CASE and COALESCE may produce NULL values
URL: https://github.com/apache/calcite/pull/1611#discussion_r352390724
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCaseOperator.java
 ##########
 @@ -230,14 +232,29 @@ private RelDataType inferTypeFromValidator(
     SqlNodeList thenList = caseCall.getThenOperands();
     ArrayList<SqlNode> nullList = new ArrayList<>();
     List<RelDataType> argTypes = new ArrayList<>();
-    for (SqlNode node : thenList) {
-      argTypes.add(
-          callBinding.getValidator().deriveType(
-              callBinding.getScope(), node));
+
+    final SqlNodeList whenOperands = caseCall.getWhenOperands();
+    final RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
+
+    final int size = thenList.getList().size();
+    for (int i = 0; i < size; i++) {
+      SqlNode node = thenList.get(i);
+      RelDataType type = callBinding.getValidator().deriveType(
+          callBinding.getScope(), node);
+      SqlNode operand = whenOperands.get(i);
+      if (operand.getKind() == SqlKind.IS_NOT_NULL && type.isNullable()) {
+        SqlBasicCall call = (SqlBasicCall) operand;
+        if (call.getOperandList().get(0).equalsDeep(node, Litmus.IGNORE)) {
+          // We're sure that the type is not nullable if the kind is IS NOT NULL.
+          type = typeFactory.createTypeWithNullability(type, false);
+        }
+      }
 
 Review comment:
   The default type created from the type factory is always nullable false, so it make more sense to fix the `IS_NULL` expression.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services