Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](Nereids) simplify conditional function generate wrong nullable #44209

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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 @@ -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;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ abc
ab
abc

-- !test_nullable_nullif --
1 1

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