You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "Reminiscent (via GitHub)" <gi...@apache.org> on 2023/04/21 08:09:27 UTC

[GitHub] [doris] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window

Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1173465854


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughWindow.java:
##########
@@ -0,0 +1,174 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.BinaryOperator;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.LessThan;
+import org.apache.doris.nereids.trees.expressions.LessThanEqual;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+import org.apache.doris.nereids.trees.expressions.WindowFrame;
+import org.apache.doris.nereids.trees.expressions.functions.window.DenseRank;
+import org.apache.doris.nereids.trees.expressions.functions.window.Rank;
+import org.apache.doris.nereids.trees.expressions.functions.window.RowNumber;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Push down the 'filter' into the 'window'.
+ * It will convert the filter condition to the 'limit value' and embed it to the 'window'.
+ * But there are some restrictions, the details are explained below.
+ * For example:
+ * 'SELECT * FROM (
+ *     SELECT *, ROW_NUMBER() OVER (ORDER BY b) AS row_number
+ *     FROM t
+ * ) AS tt WHERE row_number <= 100;'
+ * The filter 'row_number <= 100' can be pushed down into the window operator.
+ * The following will demonstrate how the plan changes:
+ * Logical plan tree:
+ *                 any_node
+ *                   |
+ *                filter (row_number <= 100)
+ *                   |
+ *                window (PARTITION BY a ORDER BY b)
+ *                   |
+ *                 any_node
+ * transformed to:
+ *                 any_node
+ *                   |
+ *                filter (row_number <= 100)
+ *                   |
+ *                window (PARTITION BY a ORDER BY b) [Limit: 100]
+ *                   |
+ *                 any_node
+ */
+
+public class PushdownFilterThroughWindow extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule build() {
+        return logicalFilter(logicalWindow()).then(filter -> {
+            LogicalWindow<Plan> window = filter.child();
+
+            // Check the filter conditions. Now, we currently only support
+            // simple conditions of the form 'column </<= constant'.
+            // TODO: Support more complex situations in filter conditions.
+            Set<Expression> conjuncts = filter.getConjuncts();
+            if (conjuncts.size() != 1) {
+                return filter;
+            }

Review Comment:
   You can see the `TODO`. It maybe needs to normalize the filter conditions, for example if there is a condition contains `and` and `or`. We need to expand it. So I think it can be handled separate.



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