qzsee commented on code in PR #14113: URL: https://github.com/apache/doris/pull/14113#discussion_r1022789687
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/SimplifyRange.java: ########## @@ -0,0 +1,390 @@ +// 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.rewrite.rules; + +import org.apache.doris.nereids.rules.expression.rewrite.AbstractExpressionRewriteRule; +import org.apache.doris.nereids.rules.expression.rewrite.ExpressionRewriteContext; +import org.apache.doris.nereids.rules.expression.rewrite.ExpressionRuleExecutor; +import org.apache.doris.nereids.trees.expressions.And; +import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; +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.GreaterThan; +import org.apache.doris.nereids.trees.expressions.GreaterThanEqual; +import org.apache.doris.nereids.trees.expressions.InPredicate; +import org.apache.doris.nereids.trees.expressions.LessThan; +import org.apache.doris.nereids.trees.expressions.LessThanEqual; +import org.apache.doris.nereids.trees.expressions.Or; +import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.BoundType; +import com.google.common.collect.Lists; +import com.google.common.collect.Range; +import com.google.common.collect.Sets; + +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.BinaryOperator; +import java.util.stream.Collectors; + +/** + * This class implements the function to simplify expression range. + * for example: + * a > 1 and a > 2 => a > 2 + * a > 1 or a > 2 => a > 1 + * a in (1,2,3) and a > 1 => a in (2,3) + * a in (1,2,3) and a in (3,4,5) => a = 3 + * a in(1,2,3) and a in (4,5,6) => false + * The logic is as follows: + * 1. for `And` expression. + * 1. extract conjunctions then build `ValueDesc` for each conjunction + * 2. grouping according to `reference`, `ValueDesc` in the same group can perform intersect + * for example: + * a > 1 and a > 2 + * 1. a > 1 => RangeValueDesc((1...+∞)), a > 2 => RangeValueDesc((2...+∞)) + * 2. (1...+∞) intersect (2...+∞) => (2...+∞) + * 2. for `Or` expression (similar to `And`). + */ +public class SimplifyRange extends AbstractExpressionRewriteRule { + + public static final SimplifyRange INSTANCE = new SimplifyRange(); + + @Override + public Expression rewrite(Expression expr, ExpressionRewriteContext ctx) { + return expr instanceof CompoundPredicate ? expr.accept(new RangeInference(), null).expr : expr; + } + + private static class RangeInference extends ExpressionVisitor<ValueDesc, Void> { + + @Override + public ValueDesc visit(Expression expr, Void context) { + return ResultValue.of(expr); + } + + private ValueDesc buildRange(ComparisonPredicate predicate) { + Expression rewrite = ExpressionRuleExecutor.normalize(predicate); + Expression right = rewrite.child(1); + // only handle `NumericType` + if (right.isLiteral() && right.getDataType().isNumericType()) { + return ValueDesc.range((ComparisonPredicate) rewrite); + } + return ValueDesc.EMPTY; + } + + @Override + public ValueDesc visitGreaterThan(GreaterThan greaterThan, Void context) { + return buildRange(greaterThan); + } + + @Override + public ValueDesc visitGreaterThanEqual(GreaterThanEqual greaterThanEqual, Void context) { + return buildRange(greaterThanEqual); + } + + @Override + public ValueDesc visitLessThan(LessThan lessThan, Void context) { + return buildRange(lessThan); + } + + @Override + public ValueDesc visitLessThanEqual(LessThanEqual lessThanEqual, Void context) { + return buildRange(lessThanEqual); + } + + @Override + public ValueDesc visitEqualTo(EqualTo equalTo, Void context) { + return buildRange(equalTo); + } + + @Override + public ValueDesc visitInPredicate(InPredicate inPredicate, Void context) { + // only handle `NumericType` + if (ExpressionUtils.isAllLiteral(inPredicate.getOptions()) + && ExpressionUtils.matchNumericType(inPredicate.getOptions())) { + return ValueDesc.discrete(inPredicate); + } + return ValueDesc.EMPTY; + } + + @Override + public ValueDesc visitAnd(And and, Void context) { + List<Expression> result = simplify(ExpressionUtils.extractConjunction(and), ValueDesc::intersect); + return ResultValue.of(ExpressionUtils.and(result)); + } + + @Override + public ValueDesc visitOr(Or or, Void context) { + List<Expression> result = simplify(ExpressionUtils.extractDisjunction(or), ValueDesc::union); + return ResultValue.of(ExpressionUtils.or(result)); + } + + private List<Expression> simplify(List<Expression> predicates, BinaryOperator<ValueDesc> op) { + List<Expression> result = Lists.newArrayList(); + List<ValueDesc> valueDescList = Lists.newArrayList(); + + for (Expression predicate : predicates) { + ValueDesc value = predicate.accept(this, null); + // can not build `ValueDesc`, so does not handle `predicate`, skip it. + if (value.equals(ValueDesc.EMPTY)) { + result.add(predicate); + } else if (value instanceof ResultValue) { + // With simplified expression, it is possible to build `ValueDesc` as well + ValueDesc o = value.expr.accept(this, null); + if (o instanceof ResultValue) { + result.add(o.expr); + } else { + valueDescList.add(o); + } Review Comment: `resultValue.expr` may also be possible to build `ValueDesc` for example: (a > 1 or a > 2) and a in (1,2,3) 1. (a > 1 or a > 2) => a > 1 => a(1...+ ∞) 2. a([1,2,3]) insect a(1...+ ∞) => a([1,2,3]) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
