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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/PartitionPruneExpressionExtractor.java:
##########
@@ -0,0 +1,204 @@
+// 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.rules;
+
+import org.apache.doris.nereids.CascadesContext;
+import 
org.apache.doris.nereids.rules.expression.rules.PartitionPruneExpressionExtractor.Context;
+import org.apache.doris.nereids.trees.expressions.And;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Or;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SubqueryExpr;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
+import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * PartitionPruneExpressionExtractor
+ *
+ * This rewriter only used to extract the expression that can be used in 
partition pruning from
+ *     the whole predicate expression.
+ * The theory of extractor is pretty simple:
+ * A:Sort the expression in two kinds:
+ *  1. evaluable-expression (let's mark it as E).
+ *      Expressions that can be evaluated in the partition pruning stage.
+ *      In the other word: not contains non-partition slots or deterministic 
expression.
+ *  2. un-evaluable-expression (let's mark it as UE).
+ *      Expressions that can NOT be evaluated in the partition pruning stage.
+ *      In the other word: contains non-partition slots or deterministic 
expression.
+ *
+ * B: Travel the predicate, only point on AND and OR operator, following the 
rule:
+ *    (E and UE) -> (E and TRUE) -> E
+ *    (UE and UE) -> TRUE
+ *    (E and E) -> (E and E)
+ *    (E or UE) -> TRUE
+ *    (UE or UE) -> TRUE
+ *    (E or E) -> (E or E)
+ *
+ * e.g.
+ *    (part = 1 and non_part = 'a') or (part = 2)
+ * -> (part = 1 and true) or (part = 2)
+ * -> (part = 1) or (part = 2)
+ *
+ * It's better that do some expression optimize(like fold, eliminate etc.) on 
predicate before this step.
+ */
+public class PartitionPruneExpressionExtractor extends 
DefaultExpressionRewriter<Context> {
+    private final ExpressionEvaluableDetector expressionEvaluableDetector;
+
+    private PartitionPruneExpressionExtractor(Set<Slot> interestedSlots, 
CascadesContext cascadesContext) {
+        this.expressionEvaluableDetector = new 
ExpressionEvaluableDetector(interestedSlots);
+    }
+
+    /**
+     * Extract partition prune expression from predicate
+     */
+    public static Expression extract(Expression predicate,
+                                     Set<Slot> partitionSlots,
+                                     CascadesContext cascadesContext) {
+        predicate = predicate.accept(FoldConstantRuleOnFE.VISITOR_INSTANCE, 
null);

Review Comment:
   new an ExpressionRewriteContext as context parameter:
   ```java
   predicate = predicate.accept(FoldConstantRuleOnFE.VISITOR_INSTANCE, new 
ExpressionRewriteContext(cascadesContext));
   ```



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