Skip to content
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 @@ -32,6 +32,7 @@
import org.apache.doris.nereids.rules.expression.rules.SimplifyArithmeticComparisonRule;
import org.apache.doris.nereids.rules.expression.rules.SimplifyArithmeticRule;
import org.apache.doris.nereids.rules.expression.rules.SimplifyCastRule;
import org.apache.doris.nereids.rules.expression.rules.SimplifyEqualBooleanLiteral;
import org.apache.doris.nereids.rules.expression.rules.SimplifyNotExprRule;
import org.apache.doris.nereids.rules.expression.rules.SupportJavaDateFormatter;
import org.apache.doris.nereids.trees.expressions.Expression;
Expand Down Expand Up @@ -67,7 +68,8 @@ public class ExpressionNormalization extends ExpressionRewrite {
SimplifyArithmeticComparisonRule.INSTANCE,
ConvertAggStateCast.INSTANCE,
MergeDateTrunc.INSTANCE,
CheckCast.INSTANCE
CheckCast.INSTANCE,
SimplifyEqualBooleanLiteral.INSTANCE
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public enum ExpressionRuleType {
SIMPLIFY_COMPARISON_PREDICATE,
SIMPLIFY_CONDITIONAL_FUNCTION,
SIMPLIFY_CONFLICT_COMPOUND,
SIMPLIFY_EQUAL_BOOLEAN_LITERAL,
SIMPLIFY_IN_PREDICATE,
SIMPLIFY_NOT_EXPR,
SIMPLIFY_RANGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@

import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.InPredicate;
import org.apache.doris.nereids.trees.expressions.IsNull;
import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.util.ExpressionUtils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* convert "A <=> null" to "A is null"
Expand Down Expand Up @@ -69,8 +79,112 @@ public Expression visitNullSafeEqual(NullSafeEqual nullSafeEqual, Boolean isInsi
return !newLeft.nullable() ? BooleanLiteral.FALSE : new IsNull(newLeft);
} else if (canConvertToEqual) {
return new EqualTo(newLeft, newRight);
} else if (newRight.equals(BooleanLiteral.TRUE)) {
return simplifySafeEqualTrue(newLeft).orElse(newNullSafeEqual);
} else {
return newNullSafeEqual;
}
}

/**
* try to simplify 'expression <=> TRUE',
* return the rewritten expression if it can be simplified, otherwise return empty.
*/
private Optional<Expression> simplifySafeEqualTrue(Expression expression) {
if (expression.isLiteral()) {
return Optional.of(BooleanLiteral.of(expression.equals(BooleanLiteral.TRUE)));
} else if (!expression.nullable()) {
return Optional.of(expression);
} else if (expression instanceof PropagateNullable) {
Set<Expression> conjuncts = Sets.newLinkedHashSet();
conjuncts.add(expression);
if (tryProcessPropagateNullable(expression, conjuncts)) {
return Optional.of(ExpressionUtils.and(conjuncts));
}
} else if (expression instanceof InPredicate) {
InPredicate in = (InPredicate) expression;
Expression compareExpr = in.getCompareExpr();
if (!compareExpr.isConstant()) {
Set<Expression> conjuncts = Sets.newLinkedHashSet();
if (tryProcessPropagateNullable(compareExpr, conjuncts)) {
boolean allOptionNonNullLiteral = true;
ImmutableList.Builder<Expression> newOptionsBuilder
= ImmutableList.builderWithExpectedSize(in.getOptions().size());
for (Expression option : in.getOptions()) {
if (option.isNullLiteral()) {
continue;
}
if (!option.isLiteral()) {
allOptionNonNullLiteral = false;
break;
}
newOptionsBuilder.add(option);
}
if (allOptionNonNullLiteral) {
List<Expression> newOptions = newOptionsBuilder.build();
if (newOptions.isEmpty()) {
return Optional.of(BooleanLiteral.FALSE);
}
Expression newIn = newOptions.size() == in.getOptions().size()
? in : ExpressionUtils.toInPredicateOrEqualTo(compareExpr, newOptions);
conjuncts.add(newIn);
return Optional.of(ExpressionUtils.and(conjuncts));
}
}
}
} else if (expression instanceof CompoundPredicate) {
// process AND / OR
// (c1 and c2) <=> TRUE rewrite to (c1 <=> TRUE) and (c2 <=> TRUE)
// (c1 or c2) <=> TRUE rewrite to (c1 <=> TRUE) or (c2 <=> TRUE)
List<Expression> oldChildren = expression.children();
ImmutableList.Builder<Expression> newChildrenBuilder
= ImmutableList.builderWithExpectedSize(oldChildren.size());
for (Expression child : expression.children()) {
// rewrite child to child <=> TRUE
Expression newChild = simplifySafeEqualTrue(child)
.orElse(new NullSafeEqual(child, BooleanLiteral.TRUE));
if (newChild.getClass() == expression.getClass()) {
// flatten
newChildrenBuilder.addAll(newChild.children());
} else {
newChildrenBuilder.add(newChild);
}
}
List<Expression> newChildren = newChildrenBuilder.build();
boolean changed = newChildren.size() != oldChildren.size();
if (newChildren.size() == oldChildren.size()) {
for (int i = 0; i < newChildren.size(); i++) {
if (newChildren.get(i) != oldChildren.get(i)) {
changed = true;
break;
}
}
}
return Optional.of(changed ? expression.withChildren(newChildren) : expression);
}
return Optional.empty();
}

private boolean tryProcessPropagateNullable(Expression expression, Set<Expression> conjuncts) {
if (expression.isLiteral()) {
// for propagate nullable function, if any of its child is null literal,
// the fold rule will simplify it to null literal.
// so here no need to handle with the null literal case.
return !expression.isNullLiteral();
} else if (expression instanceof SlotReference) {
if (expression.nullable()) {
conjuncts.add(ExpressionUtils.notIsNull(expression));
}
return true;
} else if (expression instanceof PropagateNullable) {
for (Expression child : expression.children()) {
if (!tryProcessPropagateNullable(child, conjuncts)) {
return false;
}
}
return true;
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.ExpressionPatternMatcher;
import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
import org.apache.doris.nereids.trees.expressions.EqualPredicate;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;

import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* Simplify expression equal to true / false:
* 1.'expr = true' => 'expr';
* 2.'expr = false' => 'not expr'.
*
* NOTE: This rule may downgrade the performance for InferPredicate rule,
* because InferPredicate will collect predicate `f(xxx) = literal`,
* after this rule rewrite `f(xxx) = true/false` to `f(xxx)`/`not f(xxx)`, the predicate will not be collected.
*
* But we think this rule is more useful than harmful.
*
* What's more, for InferPredicate, it will collect f(xxx) = literal, and infer f(yyy) = literal,
* but f(yyy) may be very complex, so it is not always useful, so InferPredicate may also cause downgrade.
* By the way, if InferPredicate not considering the f(yyy) = literal is complex or not,
* the better way for it is to collect all the boolean predicates, not just only the 'xx compare literal' form.
*/
public class SimplifyEqualBooleanLiteral implements ExpressionPatternRuleFactory {
public static final SimplifyEqualBooleanLiteral INSTANCE = new SimplifyEqualBooleanLiteral();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
matchesType(EqualTo.class)
.when(this::needRewrite)
.then(equal -> rewrite(equal, (BooleanLiteral) equal.right()))
.toRule(ExpressionRuleType.SIMPLIFY_EQUAL_BOOLEAN_LITERAL)
);
}

private boolean needRewrite(EqualPredicate equal) {
// we don't rewrite 'slot = true/false' to slot, because:
// 1. for delete command, the where predicate need slot = xxx;
// 2. slot = true/false can generate a uniform for this slot, later it can use in constant propagation.
return !(equal.left() instanceof SlotReference) && equal.right() instanceof BooleanLiteral;
}

private Expression rewrite(EqualTo equal, BooleanLiteral right) {
Expression left = equal.left();
return right.equals(BooleanLiteral.TRUE) ? left : new Not(left);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public EqualTo(Expression left, Expression right) {
}

public EqualTo(Expression left, Expression right, boolean inferred) {
super(ImmutableList.of(left, right), "=", inferred);
this(ImmutableList.of(left, right), inferred);
}

private EqualTo(List<Expression> children, boolean inferred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ public static Expression trueOrNull(Expression expression) {
}
}

public static Expression notIsNull(Expression expression) {
return new Not(new IsNull(expression));
}

public static Expression toInPredicateOrEqualTo(Expression reference, Collection<? extends Expression> values) {
if (values.size() < 2) {
return or(values.stream().map(value -> new EqualTo(reference, value)).collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ public static Expression replaceUnboundSlot(Expression expression, Map<String, S
String name = slot.getNameParts().get(slot.getNameParts().size() - 1);
List<String> qualifier = slot.getQualifier();
DataType dataType = getType(name.charAt(0));
boolean notNullable = name.charAt(0) == 'X' || name.length() >= 2 && name.charAt(1) == 'X';
Column column = new Column(name, dataType.toCatalogDataType());
mem.putIfAbsent(name, new SlotReference(exprId, name, dataType, true, qualifier, null, column, null, null));
mem.putIfAbsent(name, new SlotReference(exprId, name, dataType, !notNullable, qualifier, null, column, null, null));
return mem.get(name);
}
return hasNewChildren ? expression.withChildren(children) : expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void testNullSafeEqualToFalse() {

// "NULL <=> Null" to true
@Test
void testNullSafeEqualToTrue() {
void testNullSafeEqualNull() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(NullSafeEqualToEqual.INSTANCE)
));
Expand Down Expand Up @@ -154,4 +154,27 @@ void testNotInsideCondition() {
assertRewriteAfterTypeCoercion("if(a <=> 3, a <=> 4, a <=> 5)", "if(a = 3, a <=> 4, a <=> 5)");
assertRewriteAfterTypeCoercion("not(if(a <=> 3, a <=> 4, a <=> 5))", "not(if(a = 3, a <=> 4, a <=> 5))");
}

@Test
void testNullSafeEqualToTrue() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(NullSafeEqualToEqual.INSTANCE)
));

assertRewriteAfterTypeCoercion("Ba <=> true", "Ba <=> true");
assertRewriteAfterTypeCoercion("null <=> true", "false");
assertRewriteAfterTypeCoercion("a > 1 <=> true", "a > 1 and a is not null");
assertRewriteAfterTypeCoercion("Xa > 1 <=> true", "Xa > 1 = true");
assertRewriteAfterTypeCoercion("Xa > null <=> true", "Xa > null <=> true");
assertRewriteAfterTypeCoercion("a + b > c - d <=> true", "a + b > c - d and a is not null and b is not null and c is not null and d is not null");
assertRewriteAfterTypeCoercion("(a in (1, 2, c)) <=> true", "(a in (1, 2, c)) <=> true");
assertRewriteAfterTypeCoercion("(a in (1, 2, 3, null)) <=> true", "a is not null and a in (1, 2, 3)");
assertRewriteAfterTypeCoercion("(a in (null, null, null)) <=> true", "false");
assertRewriteAfterTypeCoercion("(a + b in (1, 2, 3, null)) <=> true", "a is not null and b is not null and a + b in (1, 2, 3)");
assertRewriteAfterTypeCoercion("(a > 1 and b > 1 and (c > d or e > 1)) <=> true",
"a > 1 and a is not null and b > 1 and b is not null and (c > d and c is not null and d is not null or e > 1 and e is not null)");
assertRewriteAfterTypeCoercion("(a / b > 1) <=> true", "(a / b > 1) <=> true");
assertRewriteAfterTypeCoercion("(a / b > 1 and c > 1 or (d > 1)) <=> true",
"(a / b > 1) <=> true and (c > 1 and c is not null) or (d > 1 and d is not null)");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;

class SimplifyEqualBooleanLiteralTest extends ExpressionRewriteTestHelper {

@Test
void testEqualToTrue() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(
SimplifyEqualBooleanLiteral.INSTANCE
)
));

assertRewriteAfterTypeCoercion("a > 1 = true", "a > 1");
assertRewriteAfterTypeCoercion("Ba = true", "Ba = true");
}

@Test
void testEqualToFalse() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(
SimplifyEqualBooleanLiteral.INSTANCE
)
));

assertRewriteAfterTypeCoercion("(a > 1) = false", "not(a > 1)");
assertRewriteAfterTypeCoercion("Ba = false", "Ba = false");
}
}
Loading