Skip to content

Commit

Permalink
Fixing bug: IS DISTINCT FROM NULL should work for NULL column values
Browse files Browse the repository at this point in the history
  • Loading branch information
ssaumitra authored and Rongrong Zhong committed Sep 17, 2020
1 parent ee1627e commit d79804d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,10 @@ protected Object visitComparisonExpression(ComparisonExpression node, Object con
if (left == null && right == null) {
return false;
}
else if (left == null || right == null) {
else if (left == null && !hasUnresolvedValue(right)) {
return true;
}
else if (right == null && !hasUnresolvedValue(left)) {
return true;
}
}
Expand Down Expand Up @@ -1272,7 +1275,13 @@ private List<Type> types(Expression... types)

private boolean hasUnresolvedValue(Object... values)
{
return hasUnresolvedValue(ImmutableList.copyOf(values));
ArrayList<Object> valuesList = new ArrayList<>(values.length);
for (Object value : values) {
if (value != null) {
valuesList.add(value);
}
}
return !valuesList.isEmpty() && hasUnresolvedValue(valuesList);
}

private boolean hasUnresolvedValue(List<Object> values)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5344,4 +5344,39 @@ public void testMultipleSqlFunctionsWithLambda()
"SELECT array_sum(zip_with(a, b, (x, y) -> x * y)), array_sum(zip_with(a, b, (x, y) -> x * y)) + array_sum(zip_with(a, a, (x, y) -> x * y)) FROM (VALUES (ARRAY[1, 2, 3], ARRAY[1, 0, 0])) t(a, b)",
"SELECT 1, 15");
}

@Test
public void testDistinctFrom()
{
assertQuery(
"SELECT x IS DISTINCT FROM NULL FROM (SELECT CAST(NULL AS VARCHAR)) T(x)",
"SELECT FALSE");
assertQuery(
"SELECT NULL IS DISTINCT FROM x FROM (SELECT CAST(NULL AS VARCHAR)) T(x)",
"SELECT FALSE");
assertQuery(
"SELECT x IS DISTINCT FROM NULL FROM (SELECT CAST('something' AS VARCHAR)) T(x)",
"SELECT TRUE");
assertQuery(
"SELECT NULL IS DISTINCT FROM x FROM (SELECT CAST('something' AS VARCHAR)) T(x)",
"SELECT TRUE");
assertQuery(
"SELECT R.name IS DISTINCT FROM NULL FROM nation N LEFT OUTER JOIN region R ON N.regionkey = R.regionkey AND R.regionkey = 2 WHERE N.name='GERMANY'",
"SELECT FALSE");
assertQuery(
"SELECT NULL IS DISTINCT FROM R.name FROM nation N LEFT OUTER JOIN region R ON N.regionkey = R.regionkey AND R.regionkey = 2 WHERE N.name='KENYA'",
"SELECT FALSE");
assertQuery(
"SELECT NULL IS DISTINCT FROM R.name FROM nation N LEFT OUTER JOIN region R ON N.regionkey = R.regionkey AND R.regionkey = 2 WHERE N.name='KENYA'",
"SELECT FALSE");
assertQuery(
"SELECT NULL IS DISTINCT FROM R.name FROM nation N LEFT OUTER JOIN region R ON N.regionkey = R.regionkey AND R.regionkey = 2 WHERE N.name='KENYA'",
"SELECT FALSE");
assertQuery(
"SELECT NULL IS DISTINCT FROM R.name FROM nation N LEFT OUTER JOIN region R ON N.regionkey = R.regionkey AND R.regionkey = 2 WHERE N.name='INDIA'",
"SELECT TRUE");
assertQuery(
"SELECT NULL IS DISTINCT FROM R.name FROM nation N LEFT OUTER JOIN region R ON N.regionkey = R.regionkey AND R.regionkey = 2 WHERE N.name='JAPAN'",
"SELECT TRUE");
}
}

0 comments on commit d79804d

Please sign in to comment.