diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunction.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunction.java index 359cb887fe54b7..c1c6283e32d21c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunction.java @@ -22,6 +22,7 @@ import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.functions.scalar.Coalesce; import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Nullable; import org.apache.doris.nereids.trees.expressions.functions.scalar.Nvl; import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; @@ -98,7 +99,7 @@ private static Expression rewriteNvl(Nvl nvl) { */ private static Expression rewriteNullIf(NullIf nullIf) { if (nullIf.child(0) instanceof NullLiteral || nullIf.child(1) instanceof NullLiteral) { - return nullIf.child(0); + return new Nullable(nullIf.child(0)); } else { return nullIf; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunctionTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunctionTest.java index 33c62f142c22aa..7ba9cf09ff26fd 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunctionTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConditionalFunctionTest.java @@ -22,6 +22,7 @@ import org.apache.doris.nereids.trees.expressions.SlotReference; import org.apache.doris.nereids.trees.expressions.functions.scalar.Coalesce; import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Nullable; import org.apache.doris.nereids.trees.expressions.functions.scalar.Nvl; import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; import org.apache.doris.nereids.types.BooleanType; @@ -99,13 +100,14 @@ public void testNullIf() { SlotReference slot = new SlotReference("a", StringType.INSTANCE, true); SlotReference nonNullableSlot = new SlotReference("b", StringType.INSTANCE, false); // nullif(null, slot) -> null - assertRewrite(new NullIf(NullLiteral.INSTANCE, slot), new NullLiteral(VarcharType.SYSTEM_DEFAULT)); + assertRewrite(new NullIf(NullLiteral.INSTANCE, slot), + new Nullable(new NullLiteral(VarcharType.SYSTEM_DEFAULT))); // nullif(nullable_slot, null) -> slot - assertRewrite(new NullIf(slot, NullLiteral.INSTANCE), slot); + assertRewrite(new NullIf(slot, NullLiteral.INSTANCE), new Nullable(slot)); // nullif(non-nullable_slot, null) -> non-nullable_slot - assertRewrite(new NullIf(nonNullableSlot, NullLiteral.INSTANCE), nonNullableSlot); + assertRewrite(new NullIf(nonNullableSlot, NullLiteral.INSTANCE), new Nullable(nonNullableSlot)); } } diff --git a/regression-test/data/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.out b/regression-test/data/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.out index 91f9110804b8b6..d9f4f3b335872c 100644 --- a/regression-test/data/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.out +++ b/regression-test/data/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.out @@ -131,3 +131,6 @@ abc ab abc +-- !test_nullable_nullif -- +1 1 + diff --git a/regression-test/suites/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.groovy b/regression-test/suites/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.groovy index 18443bb152abda..2b99ad0a23a8a5 100644 --- a/regression-test/suites/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.groovy +++ b/regression-test/suites/nereids_rules_p0/simplify_conditional_function/simplify_conditional_function.groovy @@ -49,4 +49,6 @@ suite("simplify_conditional_function") { qt_test_outer_ref_coalesce "select c1 from (select coalesce(null,a,c) c1,a,b from test_simplify_conditional_function order by c1,a,b limit 2) t group by c1 order by c1" qt_test_outer_ref_nvl "select c1 from (select ifnull(null, c) c1 from test_simplify_conditional_function order by 1 limit 2) t group by c1 order by c1" qt_test_outer_ref_nullif "select c1 from (select nullif(a, null) c1,c from test_simplify_conditional_function order by c1,c limit 2 ) t group by c1 order by c1" + + qt_test_nullable_nullif "SELECT COUNT( DISTINCT NULLIF ( 1, NULL ) ), COUNT( DISTINCT 72 )" } \ No newline at end of file