Skip to content

Commit

Permalink
branch-3.0: [fix](Nereids) simplify conditional function generate wro…
Browse files Browse the repository at this point in the history
…ng nullable #44209 (#44334)

Cherry-picked from #44209

Co-authored-by: morrySnow <[email protected]>
  • Loading branch information
github-actions[bot] and morrySnow authored Nov 20, 2024
1 parent 2332c5a commit 55a06fd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
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 )"
}

0 comments on commit 55a06fd

Please sign in to comment.