Skip to content
Open
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
130 changes: 0 additions & 130 deletions be/src/exprs/function/function_ifnull.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3493,6 +3493,15 @@ public Expression visitFunctionCallExpression(DorisParser.FunctionCallExpression
private Expression processUnboundFunction(ParserRuleContext ctx, String dbName, String functionName,
boolean isDistinct, List<Expression> params,
WindowSpecContext windowContext, IdentifierContext hintContext) {
if (dbName == null && "nullif".equalsIgnoreCase(functionName) && !isDistinct
&& windowContext == null && hintContext == null && params.size() == 2) {
Expression first = params.get(0);
Expression second = params.get(1);
return new UnboundFunction("if", ImmutableList.of(
new EqualTo(first, second),
NullLiteral.INSTANCE,
first));
}
List<UnboundStar> unboundStars = ExpressionUtils.collectAll(params, UnboundStar.class::isInstance);
if (!unboundStars.isEmpty()) {
if (dbName != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ private static Expression rewriteNvl(ExpressionMatchingContext<Nvl> ctx) {

/*
* nullif(null, R) => Null
* nullif(L, null) => Null
* nullif(L, null) => nullable(L)
* nullif(null, null) => Null
* nullif(L, R) => if(L = R, null, L)
*/
private static Expression rewriteNullIf(ExpressionMatchingContext<NullIf> ctx) {
NullIf nullIf = ctx.expr;
Expand All @@ -120,7 +121,7 @@ private static Expression rewriteNullIf(ExpressionMatchingContext<NullIf> ctx) {
nullIf, new Nullable(nullIf.child(0)), ctx.rewriteContext
);
} else {
return nullIf;
return NullIfToIf.rewrite(nullIf, ctx.rewriteContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.rules.expression.rules;

import org.apache.doris.nereids.rules.expression.ExpressionRewriteTestHelper;
import org.apache.doris.nereids.rules.expression.ExpressionRuleExecutor;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.scalar.If;
import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.types.StringType;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;

class NullIfToIfTest extends ExpressionRewriteTestHelper {

@Test
void testRewriteConstructedNullIfToIf() {
executor = new ExpressionRuleExecutor(ImmutableList.of(NullIfToIf.INSTANCE));

SlotReference first = new SlotReference("a", StringType.INSTANCE, true);
SlotReference second = new SlotReference("b", StringType.INSTANCE, true);
assertRewrite(
new NullIf(first, second),
new If(new EqualTo(first, second), new NullLiteral(StringType.INSTANCE), first));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ void testPushIntoNullIf() {
));
assertRewriteAfterTypeCoercion("cast(nullif(TA, TB) as bigint)", "if(TA = TB, NULL, cast(TA as bigint))");
assertRewriteAfterTypeCoercion("cast(nullif(TA, 1) as bigint)", "if(TA = 1, null, cast(TA as bigint))");
assertRewriteAfterTypeCoercion("a > nullif(b, c)", "a > nullif(b, c)");
assertRewriteAfterTypeCoercion("a > nullif(b, c)", "if(b = c, null, a > b)");
assertRewriteAfterTypeCoercion("2 > nullif(b, c)", "if(b = c, null, 2 > b)");
assertRewriteAfterTypeCoercion("2 > nullif(b + random(1, 10), c)", "2 > nullif(b + random(1, 10), c)");
assertRewriteAfterTypeCoercion("2 > nullif(b + random(1, 10), c)",
"if(b + random(1, 10) = c, null, 2 > b + random(1, 10))");
assertRewriteAfterTypeCoercion("2 > nullif(b, c + random(1, 10))", "if(b = c + random(1, 10), null, 2 > b)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public void testNullIf() {
),
new NullLiteral(DateTimeV2Type.of(6))
);

// nullif(L, R) -> if(L = R, null, L)
assertRewriteAfterTypeCoercion("nullif(a, b)", "if(a = b, null, a)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

package org.apache.doris.nereids.trees.expressions;

import org.apache.doris.nereids.analyzer.UnboundFunction;
import org.apache.doris.nereids.analyzer.UnboundSlot;
import org.apache.doris.nereids.exceptions.SyntaxParseException;
import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.parser.ParserTestBase;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.SqlModeHelper;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;

public class ExpressionParserTest extends ParserTestBase {
Expand Down Expand Up @@ -92,6 +95,15 @@ public void testExprBetweenPredicate() {
);
}

@Test
public void testNullIfRewriteInPlanBuilder() {
parseExpression("nullif(a, b)")
.assertEquals(new UnboundFunction("if", ImmutableList.of(
new EqualTo(new UnboundSlot("a"), new UnboundSlot("b")),
NullLiteral.INSTANCE,
new UnboundSlot("a"))));
}

@Test
public void testInPredicate() {
String in = "select * from test1 where d1 in (1, 2, 3)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ PhysicalResultSink
-- !nullif_one_side_1 --
PhysicalResultSink
--PhysicalProject[t1.a, t2.x]
----NestedLoopJoin[INNER_JOIN](nullif(a, x) = (a + b))
----NestedLoopJoin[INNER_JOIN](if((a = x), NULL, a) = (a + b))
------PhysicalProject[(a + b) AS `(a + b)`, t1.a]
--------filter((t1.a = (t1.a + t1.b)))
----------PhysicalOlapScan[tbl_join_extract_or_from_case_when_1(t1)]
Expand All @@ -285,7 +285,7 @@ PhysicalResultSink
-- !nullif_two_side_1 --
PhysicalResultSink
--PhysicalProject[t1.a, t2.x]
----hashJoin[INNER_JOIN] hashCondition=((t1.a = expr_(x + y))) otherCondition=((nullif(a, x) = (t2.x + t2.y)))
----hashJoin[INNER_JOIN] hashCondition=((t1.a = expr_(x + y))) otherCondition=((if((a = x), NULL, a) = (t2.x + t2.y)))
------PhysicalProject[t1.a]
--------PhysicalOlapScan[tbl_join_extract_or_from_case_when_1(t1)]
------PhysicalProject[(x + y) AS `expr_(x + y)`, tbl_join_extract_or_from_case_when_2.x, tbl_join_extract_or_from_case_when_2.y]
Expand Down
Loading