Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@ abstract class CodeGenerator(
val right = operands(1)
generateEquals(nullCheck, left, right)

case IS_NOT_DISTINCT_FROM =>
val left = operands.head
val right = operands(1)
generateIsNotDistinctFrom(left, right);

case NOT_EQUALS =>
val left = operands.head
val right = operands(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,37 @@ object ScalarOperators {
)
)
}
}
}

def generateIsNotDistinctFrom(
left: GeneratedExpression,
right: GeneratedExpression)
: GeneratedExpression = {
val resultTerm = newName("result")
val nullTerm = newName("isNull")
val resultTypeTerm = primitiveTypeTermForTypeInfo(BOOLEAN_TYPE_INFO)
val equalExpression = generateEquals(
false,
left.copy(code = GeneratedExpression.NO_CODE),
right.copy(code = GeneratedExpression.NO_CODE))

val resultCode =
s"""
|${left.code}
|${right.code}
|$resultTypeTerm $resultTerm;
|if (${left.nullTerm}) {
| $resultTerm = ${right.nullTerm};
|} else if (${right.nullTerm}) {
| $resultTerm = ${left.nullTerm};
|} else {
| ${equalExpression.code}
| $resultTerm = ${equalExpression.resultTerm};
|}
|""".stripMargin

GeneratedExpression(resultTerm, GeneratedExpression.NEVER_NULL, resultCode, BOOLEAN_TYPE_INFO)
}

def generateEquals(
nullCheck: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ class ScalarOperatorsTest extends ScalarOperatorsTestBase {
"((((true) === true) || false).cast(STRING) + 'X ').trim",
"trueX")
testTableApi(12.isNull, "12.isNull", "false")

testSqlApi("f12 IS NOT DISTINCT FROM NULL", "true")
testSqlApi("f9 IS NOT DISTINCT FROM NULL", "false")
testSqlApi("f9 IS NOT DISTINCT FROM 10", "true")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,41 @@ class AggregateITCase(

TestBaseUtils.compareResultAsText(result.asJava, expected)
}

@Test
def testMultipleDistinctWithDiffParams(): Unit = {
val env = ExecutionEnvironment.getExecutionEnvironment
val tEnv = TableEnvironment.getTableEnvironment(env, config)

val sqlWithNull = "SELECT a, " +
" CASE WHEN b = 2 THEN null ELSE b END AS b, " +
" c FROM MyTable"

val sqlQuery =
"SELECT b, " +
" COUNT(DISTINCT b), " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a IS NOT DISTINCT FROM expression test to org.apache.flink.table.expressions.ScalarOperatorsTest?

" SUM(DISTINCT (a / 3)), " +
" COUNT(DISTINCT SUBSTRING(c FROM 1 FOR 2))," +
" COUNT(DISTINCT c) " +
"FROM (" +
sqlWithNull +
") GROUP BY b " +
"ORDER BY b"

val t = CollectionDataSets.get3TupleDataSet(env).toTable(tEnv).as('a, 'b, 'c)
tEnv.registerTable("MyTable", t)

val result = tEnv.sqlQuery(sqlQuery).toDataSet[Row].collect()

val expected = Seq(
"1,1,0,1,1",
"3,1,3,3,3",
"4,1,5,1,4",
"5,1,12,1,5",
"6,1,18,1,6",
"null,0,1,1,2"
).mkString("\n")

TestBaseUtils.compareResultAsText(result.asJava, expected)
}
}