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/07 08:19:44 UTC

[GitHub] [doris] adonis0147 opened a new pull request, #10672: [refactor](nereids) Refine some code snippets

adonis0147 opened a new pull request, #10672:
URL: https://github.com/apache/doris/pull/10672

   # Proposed changes
   
   ~~Issue Number: close #xxx~~
   
   ## Problem Summary:
   
   Refine some code snippets:
   1. Rename: ExpressionUtils::add -> ExpressionUtils::and
   2. Reduce temporary objects when combing expressions.
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: No
   3. Has unit tests been added: No Need
   4. Has document been added or modified: No Need
   5. Does it need to update dependencies: No
   6. Are there any changes that cannot be rolled back: No
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   


-- 
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


[GitHub] [doris] github-actions[bot] commented on pull request #10672: [refactor](nereids) Refine some code snippets

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #10672:
URL: https://github.com/apache/doris/pull/10672#issuecomment-1180110261

   PR approved by at least one committer and no changes requested.


-- 
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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
adonis0147 commented on code in PR #10672:
URL: https://github.com/apache/doris/pull/10672#discussion_r917632764


##########
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:
   Simplified it by `reduce` api.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
morrySnow commented on code in PR #10672:
URL: https://github.com/apache/doris/pull/10672#discussion_r916542491


##########
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:
   maybe we could use stream reduce api to do this without recursion



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java:
##########
@@ -28,10 +28,7 @@ public class Utils {
      * @return quoted string
      */
     public static String quoteIfNeeded(String part) {
-        if (part.matches("[a-zA-Z0-9_]+") && !part.matches("\\d+")) {
-            return part;
-        } else {
-            return part.replace("`", "``");
-        }
+        return part.matches("\\w*[\\w&&[^\\d]]+\\w*")

Review Comment:
   this pattern is not intuitional.  it is better add a comment to explain the pattern means all legal string except pure digit string.



##########
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);

Review Comment:
   If u do that, u need add a check at the top of this function to check `NodeType` is either `AND` or `OR`



-- 
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


[GitHub] [doris] github-actions[bot] commented on pull request #10672: [refactor](nereids) Refine some code snippets

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #10672:
URL: https://github.com/apache/doris/pull/10672#issuecomment-1180098695

   PR approved by anyone and no changes requested.


-- 
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


[GitHub] [doris] EmmyMiao87 merged pull request #10672: [refactor](nereids) Refine some code snippets

Posted by GitBox <gi...@apache.org>.
EmmyMiao87 merged PR #10672:
URL: https://github.com/apache/doris/pull/10672


-- 
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