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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/WindowFuncType.java:
##########
@@ -0,0 +1,27 @@
+// 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.types;
+
+/**
+ * WindowFuncType represent the window function which is used in the 
PartitionTopN.
+ */
+public enum WindowFuncType {

Review Comment:
   move this file to `nereids/trees/plans`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,194 @@
+// 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.trees.plans.logical;
+
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.LogicalProperties;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.OrderExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.algebra.PartitionTopN;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.types.WindowFuncType;
+import org.apache.doris.nereids.util.Utils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * Logical partition-top-N plan.
+ */
+public class LogicalPartitionTopN<CHILD_TYPE extends Plan> extends 
LogicalUnary<CHILD_TYPE> implements PartitionTopN {
+    private final WindowFuncType function;
+    private final List<Expression> partitionKeys;
+    private final List<OrderExpression> orderKeys;
+    private final boolean hasGlobalLimit;
+    private final long partitionLimit;
+
+    public LogicalPartitionTopN(WindowExpression windowExpr, boolean 
hasGlobalLimit, long partitionLimit,
+                                CHILD_TYPE child) {
+        this(windowExpr.getFunction(), windowExpr.getPartitionKeys(), 
windowExpr.getOrderKeys(),
+                hasGlobalLimit, partitionLimit, Optional.empty(),
+                Optional.empty(), child);
+    }
+
+    public LogicalPartitionTopN(WindowFuncType windowFuncType, 
List<Expression> partitionKeys,
+                                List<OrderExpression> orderKeys, boolean 
hasGlobalLimit, long partitionLimit,
+                                CHILD_TYPE child) {
+        this(windowFuncType, partitionKeys, orderKeys, hasGlobalLimit, 
partitionLimit,
+                Optional.empty(), Optional.empty(), child);
+    }
+
+    /**
+     * Constructor for LogicalPartitionTopN.
+     */
+    public LogicalPartitionTopN(WindowFuncType windowFuncType, 
List<Expression> partitionKeys,
+                                List<OrderExpression> orderKeys, boolean 
hasGlobalLimit,
+                                long partitionLimit, Optional<GroupExpression> 
groupExpression,
+                                Optional<LogicalProperties> logicalProperties, 
CHILD_TYPE child) {
+        super(PlanType.LOGICAL_PARTITION_TOP_N, groupExpression, 
logicalProperties, child);
+        this.function = windowFuncType;
+        this.partitionKeys = 
ImmutableList.copyOf(Objects.requireNonNull(partitionKeys,
+            "partitionKeys can not be null"));
+        this.orderKeys = ImmutableList.copyOf(Objects.requireNonNull(orderKeys,
+            "orderKeys can not be null"));
+        this.hasGlobalLimit = hasGlobalLimit;
+        this.partitionLimit = partitionLimit;
+    }
+
+    /**
+     * Constructor for LogicalPartitionTopN.
+     */
+    public LogicalPartitionTopN(Expression expr, List<Expression> 
partitionKeys, List<OrderExpression> orderKeys,
+                                boolean hasGlobalLimit, long partitionLimit, 
Optional<GroupExpression> groupExpression,
+                                Optional<LogicalProperties> logicalProperties, 
CHILD_TYPE child) {
+        super(PlanType.LOGICAL_PARTITION_TOP_N, groupExpression, 
logicalProperties, child);
+        String funcName = expr.toString();
+        if (funcName.equals("row_number()")) {

Review Comment:
   we should use `expr instanceOf RowNumber`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownTopNThroughWindow.java:
##########
@@ -0,0 +1,117 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.RewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.Expression;
+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.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalTopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * PushdownTopNThroughWindow push down the TopN through the Window and 
generate the PartitionTopN.
+ */
+public class PushdownTopNThroughWindow implements RewriteRuleFactory {
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.of(
+            // topn -> window
+            logicalTopN(logicalWindow()).then(topn -> {
+                LogicalWindow<Plan> window = topn.child();
+                ExprId windowExprId = getExprID4WindowFunc(window);
+                if (windowExprId == null) {
+                    return topn;
+                }
+
+                if (!checkTopN4PartitionLimitPushDown(topn, windowExprId)) {

Review Comment:
   we do not use number 4 as for and 2 as to, so change the function name to 
`checkTopNForPartitionLimitPushDown `



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