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/04/15 09:19:44 UTC

[GitHub] [incubator-doris] jackwener opened a new pull request, #9050: [feature](optimizer): spilt implicit `is not null`

jackwener opened a new pull request, #9050:
URL: https://github.com/apache/incubator-doris/pull/9050

   # Proposed changes
   
   Issue Number: close #9042
   
   ## Problem Summary:
   
   
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: (No)
   2. Has unit tests been added: (Yes)
   3. Has document been added or modified: (No Need)
   4. Does it need to update dependencies: (No)
   5. 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] [incubator-doris] jackwener commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r852120491


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java:
##########
@@ -603,14 +604,92 @@ public List<TupleId> collectTupleIds() {
         return result;
     }
 
-    private void whereClauseRewrite() {
+    private void rewriteWhereClause() {
         if (whereClause instanceof IntLiteral) {
             if (((IntLiteral) whereClause).getLongValue() == 0) {
                 whereClause = new BoolLiteral(false);
             } else {
                 whereClause = new BoolLiteral(true);
             }
         }
+
+        whereClause = inferIsNotNull(whereClause);
+    }
+
+    /*-
+     * Infer `IS NOT NULL` from predicate in whereClause.
+     * And spilt it into two parts.
+     * Example:
+     * Where col > 0; -> Where col > 0 and col is not null;
+     */
+    private Expr inferIsNotNull(Expr expr) {

Review Comment:
   This key point is `in planner build.`
   Current rule is recursively optimized.



-- 
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] [incubator-doris] EmmyMiao87 commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
EmmyMiao87 commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r851938464


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java:
##########
@@ -603,14 +604,92 @@ public List<TupleId> collectTupleIds() {
         return result;
     }
 
-    private void whereClauseRewrite() {
+    private void rewriteWhereClause() {
         if (whereClause instanceof IntLiteral) {
             if (((IntLiteral) whereClause).getLongValue() == 0) {
                 whereClause = new BoolLiteral(false);
             } else {
                 whereClause = new BoolLiteral(true);
             }
         }
+
+        whereClause = inferIsNotNull(whereClause);
+    }
+
+    /*-
+     * Infer `IS NOT NULL` from predicate in whereClause.
+     * And spilt it into two parts.
+     * Example:
+     * Where col > 0; -> Where col > 0 and col is not null;
+     */
+    private Expr inferIsNotNull(Expr expr) {

Review Comment:
   It should be a new Rule ~



-- 
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] [incubator-doris] wangshuo128 commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
wangshuo128 commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r852116377


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java:
##########
@@ -603,14 +604,92 @@ public List<TupleId> collectTupleIds() {
         return result;
     }
 
-    private void whereClauseRewrite() {
+    private void rewriteWhereClause() {
         if (whereClause instanceof IntLiteral) {
             if (((IntLiteral) whereClause).getLongValue() == 0) {
                 whereClause = new BoolLiteral(false);
             } else {
                 whereClause = new BoolLiteral(true);
             }
         }
+
+        whereClause = inferIsNotNull(whereClause);
+    }
+
+    /*-
+     * Infer `IS NOT NULL` from predicate in whereClause.
+     * And spilt it into two parts.
+     * Example:
+     * Where col > 0; -> Where col > 0 and col is not null;
+     */
+    private Expr inferIsNotNull(Expr expr) {

Review Comment:
   What's the difference between rule and rewrite?



-- 
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] [incubator-doris] wangshuo128 commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
wangshuo128 commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r852116377


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java:
##########
@@ -603,14 +604,92 @@ public List<TupleId> collectTupleIds() {
         return result;
     }
 
-    private void whereClauseRewrite() {
+    private void rewriteWhereClause() {
         if (whereClause instanceof IntLiteral) {
             if (((IntLiteral) whereClause).getLongValue() == 0) {
                 whereClause = new BoolLiteral(false);
             } else {
                 whereClause = new BoolLiteral(true);
             }
         }
+
+        whereClause = inferIsNotNull(whereClause);
+    }
+
+    /*-
+     * Infer `IS NOT NULL` from predicate in whereClause.
+     * And spilt it into two parts.
+     * Example:
+     * Where col > 0; -> Where col > 0 and col is not null;
+     */
+    private Expr inferIsNotNull(Expr expr) {

Review Comment:
   What' the difference between rule and rewrite?



-- 
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] [incubator-doris] jackwener commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r851174972


##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/RewriteInferNotNullRule.java:
##########
@@ -0,0 +1,115 @@
+// 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.rewrite;
+
+import java.util.function.BinaryOperator;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.BetweenPredicate;
+import org.apache.doris.analysis.BinaryPredicate;
+import org.apache.doris.analysis.CompoundPredicate;
+import org.apache.doris.analysis.InPredicate;
+import org.apache.doris.analysis.LikePredicate;
+import org.apache.doris.analysis.NullLiteral;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.IsNullPredicate;
+import org.apache.doris.analysis.Predicate;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.common.AnalysisException;
+
+/*-
+ * Infer `IS NOT NULL` from predicate in whereClause.
+ * And spilt it into two parts.
+ * Example:
+ * Where col > 0; -> Where col > 0 and col is not null;
+ * Where col1 = col2; -> Where (col2 is not null) and (col1 is not null and col1 = col2);
+ */
+public class RewriteInferNotNullRule implements ExprRewriteRule {
+    public static ExprRewriteRule INSTANCE = new RewriteInferNotNullRule();
+
+    @Override
+    public Expr apply(Expr expr, Analyzer analyzer, ExprRewriter.ClauseType clauseType) throws AnalysisException {
+        if (clauseType != ExprRewriter.ClauseType.WHERE_CLAUSE || !(expr instanceof Predicate)) {
+            return expr;
+        }
+
+        Predicate predicate = (Predicate) expr;
+        Expr leftExpr = predicate.getChild(0);
+        Expr rightExpr = predicate.getChild(1);
+        if (leftExpr == null || rightExpr == null) {
+            return expr;
+        }
+
+        if (predicate instanceof BinaryPredicate) {
+            /**

Review Comment:
   TODO: Trim same col



-- 
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] [incubator-doris] jackwener commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r851174972


##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/RewriteInferNotNullRule.java:
##########
@@ -0,0 +1,115 @@
+// 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.rewrite;
+
+import java.util.function.BinaryOperator;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.BetweenPredicate;
+import org.apache.doris.analysis.BinaryPredicate;
+import org.apache.doris.analysis.CompoundPredicate;
+import org.apache.doris.analysis.InPredicate;
+import org.apache.doris.analysis.LikePredicate;
+import org.apache.doris.analysis.NullLiteral;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.IsNullPredicate;
+import org.apache.doris.analysis.Predicate;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.common.AnalysisException;
+
+/*-
+ * Infer `IS NOT NULL` from predicate in whereClause.
+ * And spilt it into two parts.
+ * Example:
+ * Where col > 0; -> Where col > 0 and col is not null;
+ * Where col1 = col2; -> Where (col2 is not null) and (col1 is not null and col1 = col2);
+ */
+public class RewriteInferNotNullRule implements ExprRewriteRule {
+    public static ExprRewriteRule INSTANCE = new RewriteInferNotNullRule();
+
+    @Override
+    public Expr apply(Expr expr, Analyzer analyzer, ExprRewriter.ClauseType clauseType) throws AnalysisException {
+        if (clauseType != ExprRewriter.ClauseType.WHERE_CLAUSE || !(expr instanceof Predicate)) {
+            return expr;
+        }
+
+        Predicate predicate = (Predicate) expr;
+        Expr leftExpr = predicate.getChild(0);
+        Expr rightExpr = predicate.getChild(1);
+        if (leftExpr == null || rightExpr == null) {
+            return expr;
+        }
+
+        if (predicate instanceof BinaryPredicate) {
+            /**

Review Comment:
   TODO: Trim `same`



-- 
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] [incubator-doris] jackwener commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r851946051


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java:
##########
@@ -603,14 +604,92 @@ public List<TupleId> collectTupleIds() {
         return result;
     }
 
-    private void whereClauseRewrite() {
+    private void rewriteWhereClause() {
         if (whereClause instanceof IntLiteral) {
             if (((IntLiteral) whereClause).getLongValue() == 0) {
                 whereClause = new BoolLiteral(false);
             } else {
                 whereClause = new BoolLiteral(true);
             }
         }
+
+        whereClause = inferIsNotNull(whereClause);
+    }
+
+    /*-
+     * Infer `IS NOT NULL` from predicate in whereClause.
+     * And spilt it into two parts.
+     * Example:
+     * Where col > 0; -> Where col > 0 and col is not null;
+     */
+    private Expr inferIsNotNull(Expr expr) {

Review Comment:
   Note: It isn't a `optimizer rule` 
   
   It is because the following transformation can't terminate in Finite State Machine of rule.
   
   ```sql
   select * from t1 where c1 > 0;  
   -> 
   select * from t1 where c1 > 0 and c1 is not null;
   ->
   select * from t1 where c1 > 0 and c1 is not null and c1 is not null;
   ->
   .....
   ```
   
   So, In fact, this is a `rewrite` in planner build.



-- 
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] [incubator-doris] wangshuo128 commented on a diff in pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
wangshuo128 commented on code in PR #9050:
URL: https://github.com/apache/incubator-doris/pull/9050#discussion_r852571224


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java:
##########
@@ -603,14 +604,92 @@ public List<TupleId> collectTupleIds() {
         return result;
     }
 
-    private void whereClauseRewrite() {
+    private void rewriteWhereClause() {
         if (whereClause instanceof IntLiteral) {
             if (((IntLiteral) whereClause).getLongValue() == 0) {
                 whereClause = new BoolLiteral(false);
             } else {
                 whereClause = new BoolLiteral(true);
             }
         }
+
+        whereClause = inferIsNotNull(whereClause);
+    }
+
+    /*-
+     * Infer `IS NOT NULL` from predicate in whereClause.
+     * And spilt it into two parts.
+     * Example:
+     * Where col > 0; -> Where col > 0 and col is not null;
+     */
+    private Expr inferIsNotNull(Expr expr) {

Review Comment:
   We should make sure that a `SlotRef` has only one is-not-null predicate, no matter the infer-not-null logical is in a recursively applied rule or single-pass plan rewrite.
   
   Please check the following cases:
   1. There is already an is-not-null predicate, e.g., `SELECT a FROM t WHERE a > 1 AND a is not NULL`.
   2. More than one where clauses refer to the same `SlotRef` that could infer an is-not-null predicate, e.g., `SELECT * FROM (SELECT * FROM t WHERE a > 1) t WHERE a < 2`.
   
   One possible solution is that we track the relationship between a `SlotRef` and its is-not-null predicate in `Analyzer`.



-- 
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] [incubator-doris] jackwener closed pull request #9050: [feature](optimizer): spilt implicit `is not null`

Posted by GitBox <gi...@apache.org>.
jackwener closed pull request #9050: [feature](optimizer): spilt implicit `is not null`
URL: https://github.com/apache/incubator-doris/pull/9050


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