This is an automated email from the ASF dual-hosted git repository.

libenchao pushed a commit to branch release-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.11 by this push:
     new 39b4778  [FLINK-16181][table-planner-blink] Fix IfCallGen throw NPE 
when operand's resultTerm is null
39b4778 is described below

commit 39b4778a3eaae81902203d558335ed4fbbd8e379
Author: libenchao <libenc...@gmail.com>
AuthorDate: Thu Feb 20 22:19:17 2020 +0800

    [FLINK-16181][table-planner-blink] Fix IfCallGen throw NPE when operand's 
resultTerm is null
    
    This closes #11161
---
 .../apache/flink/table/planner/codegen/calls/IfCallGen.scala | 12 +++++++++---
 .../table/planner/expressions/ScalarOperatorsTest.scala      |  1 +
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git 
a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/IfCallGen.scala
 
b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/IfCallGen.scala
index 532f8fe..fcc62c3 100644
--- 
a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/IfCallGen.scala
+++ 
b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/calls/IfCallGen.scala
@@ -18,7 +18,7 @@
 
 package org.apache.flink.table.planner.codegen.calls
 
-import 
org.apache.flink.table.planner.codegen.CodeGenUtils.primitiveTypeTermForType
+import 
org.apache.flink.table.planner.codegen.CodeGenUtils.{primitiveDefaultValue, 
primitiveTypeTermForType}
 import org.apache.flink.table.planner.codegen.{CodeGenUtils, 
CodeGeneratorContext, GeneratedExpression}
 import org.apache.flink.table.types.logical.LogicalType
 
@@ -44,6 +44,7 @@ class IfCallGen() extends CallGenerator {
     }
 
     val resultTypeTerm = primitiveTypeTermForType(returnType)
+    val resultDefault = primitiveDefaultValue(returnType)
     val Seq(resultTerm, nullTerm) = ctx.addReusableLocalVariables(
       (resultTypeTerm, "result"),
       ("boolean", "isNull"))
@@ -51,13 +52,18 @@ class IfCallGen() extends CallGenerator {
     val resultCode =
       s"""
          |${operands.head.code}
+         |$resultTerm = $resultDefault;
          |if (${operands.head.resultTerm}) {
          |  ${operands(1).code}
-         |  $resultTerm = $castedResultTerm1;
+         |  if (!${operands(1).nullTerm}) {
+         |    $resultTerm = $castedResultTerm1;
+         |  }
          |  $nullTerm = ${operands(1).nullTerm};
          |} else {
          |  ${operands(2).code}
-         |  $resultTerm = $castedResultTerm2;
+         |  if (!${operands(2).nullTerm}) {
+         |    $resultTerm = $castedResultTerm2;
+         |  }
          |  $nullTerm = ${operands(2).nullTerm};
          |}
        """.stripMargin
diff --git 
a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarOperatorsTest.scala
 
b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarOperatorsTest.scala
index 04b2002..4f6cbe4 100644
--- 
a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarOperatorsTest.scala
+++ 
b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarOperatorsTest.scala
@@ -124,5 +124,6 @@ class ScalarOperatorsTest extends ScalarOperatorsTestBase {
     testSqlApi("CASE 1 WHEN 1 THEN true WHEN 2 THEN false ELSE NULL END", 
"true")
 
     testSqlApi("CASE WHEN f2 = 1 THEN CAST('' as INT) ELSE 0 END", "null")
+    testSqlApi("IF(true, CAST('non-numeric' AS BIGINT), 0)", "null")
   }
 }

Reply via email to