924060929 commented on code in PR #14113:
URL: https://github.com/apache/doris/pull/14113#discussion_r1019041356


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/SimplifyRange.java:
##########
@@ -0,0 +1,346 @@
+// 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.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 (1,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) => a in(1,2,3) and a in (4,5,6)
+ * 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 (same as `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 ? (Expression) 
expr.accept(new RangeInference(), null) : expr;
+    }
+
+    private static class RangeInference extends ExpressionVisitor<Object, 
Void> {
+
+        @Override
+        public Object visit(Expression expr, Void context) {
+            return 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 RangeValue.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 DiscreteValue.EMPTY;
+        }
+
+        @Override
+        public Expression visitAnd(And and, Void context) {
+            List<Expression> result = 
simplify(ExpressionUtils.extractConjunction(and), ValueDesc::intersect);
+            return ExpressionUtils.and(result);
+        }
+
+        @Override
+        public Expression visitOr(Or or, Void context) {
+            List<Expression> result = 
simplify(ExpressionUtils.extractDisjunction(or), ValueDesc::union);
+            return 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) {
+                Object value = predicate.accept(this, null);
+                // can not build `ValueDesc`, so does not handle `predicate`
+                if (value.equals(RangeValue.EMPTY) || 
value.equals(DiscreteValue.EMPTY)) {
+                    result.add(predicate);
+                } else if (value instanceof Expression) {
+                    // With simplified expression, it is possible to build 
`ValueDesc` as well
+                    Object o = ((Expression) value).accept(this, null);
+                    if (o instanceof Expression) {
+                        result.add((Expression) o);
+                    } else {
+                        valueDescList.add((ValueDesc) o);
+                    }
+                } else if (value instanceof ValueDesc) {
+                    valueDescList.add((ValueDesc) value);
+                }

Review Comment:
   The result of `predicate.accept(this, null)` is an `Object`, and it can be 
Expression, ValueDesc.
   This logic like some weakly typed language, and it will become more and more 
complex over time.
   
   I suggest return ValueDesc, if it is null, I think you should create a 
ValueDesc.UNKNOWN with origin Expression, and distinct the ValueDesc.UNKNOWN 
and ValueDesc.EMPTY will more accurate, if EMPTY we should skip the predicate?
   
   
   



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to