You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/07/08 08:08:59 UTC

[GitHub] [doris] adonis0147 commented on a diff in pull request #10672: [refactor](nereids) Refine some code snippets

adonis0147 commented on code in PR #10672:
URL: https://github.com/apache/doris/pull/10672#discussion_r916569297


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java:
##########
@@ -89,51 +87,36 @@ public static Expression or(List<Expression> expressions) {
      * Use AND/OR to combine expressions together.
      */
     public static Expression combine(NodeType op, List<Expression> expressions) {
-
         Objects.requireNonNull(expressions, "expressions is null");
 
         if (expressions.size() == 0) {
-            if (op == NodeType.AND) {
-                return new Literal(true);
-            }
-            if (op == NodeType.OR) {
-                return new Literal(false);
-            }
-        }
-
-        if (expressions.size() == 1) {
+            return new Literal(op == NodeType.AND);
+        } else if (expressions.size() == 1) {
             return expressions.get(0);
         }
 
-        List<Expression> distinctExpressions = Lists.newArrayList(new LinkedHashSet<>(expressions));
-        if (op == NodeType.AND) {
-            if (distinctExpressions.contains(Literal.FALSE_LITERAL)) {
-                return Literal.FALSE_LITERAL;
+        Expression shortCircuit = (op == NodeType.AND ? Literal.FALSE_LITERAL : Literal.TRUE_LITERAL);
+        Expression skip = (op == NodeType.AND ? Literal.TRUE_LITERAL : Literal.FALSE_LITERAL);
+        LinkedHashSet<Expression> distinctExpressions = Sets.newLinkedHashSetWithExpectedSize(expressions.size());
+        for (Expression expression : expressions) {
+            if (expression.equals(shortCircuit)) {
+                return shortCircuit;
+            } else if (!expression.equals(skip)) {
+                distinctExpressions.add(expression);
             }
-            distinctExpressions = distinctExpressions.stream().filter(p -> !p.equals(Literal.TRUE_LITERAL))
-                    .collect(Collectors.toList());
         }
 
-        if (op == NodeType.OR) {
-            if (distinctExpressions.contains(Literal.TRUE_LITERAL)) {
-                return Literal.TRUE_LITERAL;
+        List<Expression> result = Lists.newArrayListWithCapacity(distinctExpressions.size() / 2 + 1);

Review Comment:
   The output would not be the same as the original one if we used `stream reduce` api.
   
   recursion: `(A AND B) AND (C AND D)`
   reduce: `((A AND B) AND C) AND D)`
   
   If the inconsistency is acceptable, I will use the `reduce` way to simplify it.



-- 
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-unsubscribe@doris.apache.org

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


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