Skip to content

Commit

Permalink
[FLINK-16181][table-planner-blink] Fix IfCallGen throw NPE when opera…
Browse files Browse the repository at this point in the history
…nd's resultTerm is null

This closes apache#11161
  • Loading branch information
libenchao committed Jul 14, 2020
1 parent 07e43c9 commit d677f02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -44,20 +44,26 @@ class IfCallGen() extends CallGenerator {
}

val resultTypeTerm = primitiveTypeTermForType(returnType)
val resultDefault = primitiveDefaultValue(returnType)
val Seq(resultTerm, nullTerm) = ctx.addReusableLocalVariables(
(resultTypeTerm, "result"),
("boolean", "isNull"))

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d677f02

Please sign in to comment.