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/18 10:10:00 UTC
[GitHub] [doris] Reminiscent opened a new pull request, #18784: [feature](Nereids): pushdown filter through window
Reminiscent opened a new pull request, #18784:
URL: https://github.com/apache/doris/pull/18784
# Proposed changes
Issue Number: ref https://github.com/apache/doris/issues/18646
## Problem summary
Support pushdown filter through window in the FE side.
Describe your changes.
The detail design is in the issue.
## Checklist(Required)
* [x] Does it affect the original behavior
* [x] Has unit tests been added
* [ ] Has document been added or modified
* [ ] Does it need to update dependencies
* [x] Is this PR support rollback (If NO, please explain WHY)
## 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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562123725
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1559040506
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] github-actions[bot] commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1545507448
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562797604
run buildall
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194826748
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
Review Comment:
Yes, most of the parts in this file are copied from the `SortNode`. But I think some are used to generate the `SortInfo`which will be passed to BE side. By the way, I can't figure out all the function very precise.
I'll remove some unrelated functions as much as possible. So feel free to point out which function maybe used in the `nereids`.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1543676009
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194719404
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, 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 = function;
+ this.partitionKeys = ImmutableList.copyOf(partitionKeys);
+ this.orderKeys = ImmutableList.copyOf(orderKeys);
+ this.hasGlobalLimit = hasGlobalLimit;
+ this.partitionLimit = partitionLimit;
+ }
+
+ @Override
+ public List<Slot> computeOutput() {
+ return child().getOutput();
+ }
+
+ public Expression getFunction() {
+ return function;
+ }
+
+ @Override
+ public List<Expression> getPartitionKeys() {
+ return partitionKeys;
+ }
+
+ public List<OrderExpression> getOrderKeys() {
+ return orderKeys;
+ }
Review Comment:
I think when we need the function in the interface, we can put it then? For now, we don't need those functions in interface.
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194762538
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughWindow.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.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.EqualTo;
+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.Or;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+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.LogicalEmptyRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPartitionTopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.List;
+import java.util.Set;
+import java.util.function.Predicate;
+
+/**
+ * Push down the 'filter' into the 'window'.
+ * It will convert the filter condition to the 'limit value' and push down below 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)
+ * |
+ * partition_topn(PARTITION BY: a, ORDER BY b, Partition Limit: 100)
+ * |
+ * any_node
+ */
+
+public class PushdownFilterThroughWindow extends OneRewriteRuleFactory {
+
+ @Override
+ public Rule build() {
+ return logicalFilter(logicalWindow()).then(filter -> {
+ LogicalWindow<Plan> window = filter.child();
+
+ // We have already done such optimization rule, so just ignore it.
+ if (window.child(0) instanceof LogicalPartitionTopN) {
+ return filter;
+ }
+
+ // Check the window function. There are some restrictions for window function:
+ // * The number of window function should be 1.
+ // * The window function should be one of the 'row_number()', 'rank()', 'dense_rank()'.
+ // * The window type should be 'ROW'.
+ // * The window frame should be 'UNBOUNDED' to 'CURRENT'.
+ // * The 'PARTITION' key and 'ORDER' key can not be empty at the same time.
+ List<NamedExpression> windowExprs = window.getWindowExpressions();
+ if (windowExprs.size() != 1) {
+ return filter;
+ }
+ NamedExpression windowExpr = windowExprs.get(0);
+ if (windowExpr.children().size() != 1 || !(windowExpr.child(0) instanceof WindowExpression)) {
+ return filter;
+ }
+
+ WindowExpression windowFunc = (WindowExpression) windowExpr.child(0);
+ // Check the window function name.
+ if (!LogicalWindow.checkWindowFuncName4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the partition key and order key.
+ if (!LogicalWindow.checkWindowPartitionAndOrderKey4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the window type and window frame.
+ if (!LogicalWindow.checkWindowFrame4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the filter conditions. Now, we currently only support simple conditions of the form
+ // 'column </ <=/ = constant'. We will extract some related conjuncts and do some check.
+ Set<Expression> conjuncts = filter.getConjuncts();
+ boolean existsORConditionsInConjuncts = conjuncts.stream().anyMatch(this::existOR);
Review Comment:
Remove it. I got some misunderstanding.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
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-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 merged pull request #18784: [feature](Nereids): support the PartitionTopN and realted rewrite rules
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow merged PR #18784:
URL: https://github.com/apache/doris/pull/18784
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1560797298
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562125877
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1559040444
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] github-actions[bot] commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1560409617
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194826748
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
Review Comment:
Yes, most of the parts in this file are copied from the `SortNode`. But I think some are used to generate the `SortInfo`which will be passed to BE side. I'll remove some unrelated functions as much as possible.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1553209416
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1560821566
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1543512628
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1543696331
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194826748
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
Review Comment:
Yes, most of the parts in this file are copied from the `SortNode`. But I think some are used to generate the `SortInfo`which will be passed to BE side.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1550656969
clang-tidy review says "All clean, LGTM! :+1:"
--
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] englefly commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "englefly (via GitHub)" <gi...@apache.org>.
englefly commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1196233623
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
+ // If there is no global limit. So result for the cardinality estimation is:
+ // NDV(partition key) * partitionLimit
+ Map<Expression, ColumnStatistic> childSlotToColumnStats = stats.columnStatistics();
+ List<ColumnStatistic> partitionByKeyStats = partitionKeys.stream()
+ .filter(childSlotToColumnStats::containsKey)
+ .map(childSlotToColumnStats::get)
+ .filter(s -> !s.isUnKnown)
+ .collect(Collectors.toList());
+ if (partitionByKeyStats.isEmpty()) {
+ // all column stats are unknown, use default ratio
+ rowCount = rowCount * DEFAULT_COLUMN_NDV_RATION;
+ } else {
+ rowCount = Math.min(rowCount, partitionByKeyStats.stream().map(s -> s.ndv)
+ .max(Double::compare).get());
+ }
+ } else {
+ rowCount = Math.min(rowCount, partitionTopN.getPartitionLimit());
+ }
+ return stats.withRowCount(rowCount);
Review Comment:
yes, we need to update column stats. if we do not know how to update col stats, just use Statisitcs.updateRowCountOnly()
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1550967883
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194755873
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
Review Comment:
For the limit part, I don't think it will cause some bad cases. Because we only get the minimal value.
```
private Statistics computeLimit(Limit limit) {
Statistics stats = groupExpression.childStatistics(0);
return stats.withRowCount(Math.min(stats.getRowCount(), limit.getLimit()));
}
```
But for the `filter` part, it may cause some bad cases. We should ignore some predicates.
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1197657007
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
Review Comment:
Maybe it's hard to handle because for the `PartitionTopN` node, we do not get the column for the `rank`. So just add the TODO.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1558902461
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1552844403
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562201796
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1563745124
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194755873
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
Review Comment:
For the limit part, I don't think it will cause some bad cases.
```
private Statistics computeLimit(Limit limit) {
Statistics stats = groupExpression.childStatistics(0);
return stats.withRowCount(Math.min(stats.getRowCount(), limit.getLimit()));
}
```
Because we only get the minimal value.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1556478028
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1560815649
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1193462747
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/cost/CostModelV2.java:
##########
@@ -156,6 +157,20 @@ public Cost visitAbstractPhysicalSort(AbstractPhysicalSort<? extends Plan> sort,
return new CostV2(startCost, runCost, statistics.computeSize());
}
+ @Override
+ public Cost visitPhysicalPartitionTopN(PhysicalPartitionTopN<? extends Plan> partitionTopN, PlanContext context) {
Review Comment:
do we need to update cost model v1?
##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticalType.java:
##########
@@ -43,6 +43,7 @@ public enum StatisticalType {
SET_OPERATION_NODE,
SCHEMA_SCAN_NODE,
SORT_NODE,
+ PARTITION_TOPN_MODE,
Review Comment:
Please sort in lexicographic order
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
@@ -0,0 +1,307 @@
+// 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.planner;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.ExprSubstitutionMap;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.SlotId;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.analysis.SortInfo;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.common.NotImplementedException;
+import org.apache.doris.common.UserException;
+import org.apache.doris.statistics.StatisticalType;
+import org.apache.doris.statistics.StatsRecursiveDerive;
+import org.apache.doris.thrift.TExplainLevel;
+import org.apache.doris.thrift.TPartitionSortNode;
+import org.apache.doris.thrift.TPlanNode;
+import org.apache.doris.thrift.TPlanNodeType;
+import org.apache.doris.thrift.TSortInfo;
+import org.apache.doris.thrift.TopNAlgorithm;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * PartitionSortNode.
+ */
+public class PartitionSortNode extends PlanNode {
Review Comment:
add a comment only used in Nereids
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, 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 = function;
+ this.partitionKeys = ImmutableList.copyOf(partitionKeys);
+ this.orderKeys = ImmutableList.copyOf(orderKeys);
Review Comment:
check not null
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
Review Comment:
i think there are a minor issue here. we first do row count prune in PartitionTopN node, and we will do row count prune in later Filter or Limit too. So it is equal that we do row count prune twice. What do u think @englefly ?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughWindow.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.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.EqualTo;
+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.Or;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+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.LogicalEmptyRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPartitionTopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.List;
+import java.util.Set;
+import java.util.function.Predicate;
+
+/**
+ * Push down the 'filter' into the 'window'.
+ * It will convert the filter condition to the 'limit value' and push down below 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)
+ * |
+ * partition_topn(PARTITION BY: a, ORDER BY b, Partition Limit: 100)
+ * |
+ * any_node
+ */
+
+public class PushdownFilterThroughWindow extends OneRewriteRuleFactory {
+
+ @Override
+ public Rule build() {
+ return logicalFilter(logicalWindow()).then(filter -> {
+ LogicalWindow<Plan> window = filter.child();
+
+ // We have already done such optimization rule, so just ignore it.
+ if (window.child(0) instanceof LogicalPartitionTopN) {
+ return filter;
+ }
+
+ // Check the window function. There are some restrictions for window function:
+ // * The number of window function should be 1.
+ // * The window function should be one of the 'row_number()', 'rank()', 'dense_rank()'.
+ // * The window type should be 'ROW'.
+ // * The window frame should be 'UNBOUNDED' to 'CURRENT'.
+ // * The 'PARTITION' key and 'ORDER' key can not be empty at the same time.
+ List<NamedExpression> windowExprs = window.getWindowExpressions();
+ if (windowExprs.size() != 1) {
+ return filter;
+ }
+ NamedExpression windowExpr = windowExprs.get(0);
+ if (windowExpr.children().size() != 1 || !(windowExpr.child(0) instanceof WindowExpression)) {
+ return filter;
+ }
+
+ WindowExpression windowFunc = (WindowExpression) windowExpr.child(0);
+ // Check the window function name.
+ if (!LogicalWindow.checkWindowFuncName4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the partition key and order key.
+ if (!LogicalWindow.checkWindowPartitionAndOrderKey4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the window type and window frame.
+ if (!LogicalWindow.checkWindowFrame4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the filter conditions. Now, we currently only support simple conditions of the form
+ // 'column </ <=/ = constant'. We will extract some related conjuncts and do some check.
+ Set<Expression> conjuncts = filter.getConjuncts();
+ boolean existsORConditionsInConjuncts = conjuncts.stream().anyMatch(this::existOR);
+ if (existsORConditionsInConjuncts) {
+ return filter;
+ }
+
+ Set<Expression> relatedConjuncts = extractRelatedConjuncts(conjuncts, windowExpr.getExprId());
+
+ boolean hasPartitionLimit = false;
+ long partitionLimit = Long.MAX_VALUE;
+
+ for (Expression conjunct : relatedConjuncts) {
+ Preconditions.checkArgument(conjunct instanceof BinaryOperator);
+ BinaryOperator op = (BinaryOperator) conjunct;
+ Expression leftChild = op.children().get(0);
+ Expression rightChild = op.children().get(1);
+
+ Preconditions.checkArgument(leftChild instanceof SlotReference
+ && rightChild instanceof IntegerLikeLiteral);
+
+ long limitVal = ((IntegerLikeLiteral) rightChild).getLongValue();
+ // Adjust the value for 'limitVal' based on the comparison operators.
+ if (conjunct instanceof LessThan) {
+ limitVal--;
+ }
+ if (limitVal < 0) {
+ return new LogicalEmptyRelation(filter.getOutput());
+ }
+ if (hasPartitionLimit) {
+ partitionLimit = Math.min(partitionLimit, limitVal);
+ } else {
+ partitionLimit = limitVal;
+ hasPartitionLimit = true;
+ }
+ }
+
+ if (!hasPartitionLimit) {
+ return filter;
+ }
+ // return PartitionTopN -> Window -> Filter
+ return filter.withChildren(window.withChildren(
+ new LogicalPartitionTopN<>(windowFunc, false, partitionLimit, window.child(0))));
+ }).toRule(RuleType.PUSHDOWN_FILTER_THROUGH_WINDOW);
+ }
+
+ private boolean existOR(Expression conjunct) {
+ if (conjunct instanceof Or) {
+ return true;
+ }
+
+ for (Expression child : conjunct.children()) {
+ if (existOR(child)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Set<Expression> extractRelatedConjuncts(Set<Expression> conjuncts, ExprId slotRefID) {
+ Predicate<Expression> condition = conjunct -> {
+ if (!(conjunct instanceof BinaryOperator)) {
+ return false;
+ }
+ BinaryOperator op = (BinaryOperator) conjunct;
+ Expression leftChild = op.children().get(0);
+ Expression rightChild = op.children().get(1);
+
+ if (!(conjunct instanceof LessThan || conjunct instanceof LessThanEqual || conjunct instanceof EqualTo)) {
+ return false;
+ }
+
+ // Now, we only support the column on the left side.
Review Comment:
add a TODO
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
@@ -0,0 +1,307 @@
+// 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.planner;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.ExprSubstitutionMap;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.SlotId;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.analysis.SortInfo;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.common.NotImplementedException;
+import org.apache.doris.common.UserException;
+import org.apache.doris.statistics.StatisticalType;
+import org.apache.doris.statistics.StatsRecursiveDerive;
+import org.apache.doris.thrift.TExplainLevel;
+import org.apache.doris.thrift.TPartitionSortNode;
+import org.apache.doris.thrift.TPlanNode;
+import org.apache.doris.thrift.TPlanNodeType;
+import org.apache.doris.thrift.TSortInfo;
+import org.apache.doris.thrift.TopNAlgorithm;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * PartitionSortNode.
+ */
+public class PartitionSortNode extends PlanNode {
+ private static final Logger LOG = LogManager.getLogger(PartitionSortNode.class);
+ List<Expr> resolvedTupleExprs;
Review Comment:
why not private?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, 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 = function;
+ this.partitionKeys = ImmutableList.copyOf(partitionKeys);
+ this.orderKeys = ImmutableList.copyOf(orderKeys);
+ this.hasGlobalLimit = hasGlobalLimit;
+ this.partitionLimit = partitionLimit;
+ }
+
+ @Override
+ public List<Slot> computeOutput() {
+ return child().getOutput();
+ }
+
+ public Expression getFunction() {
+ return function;
+ }
+
+ @Override
+ public List<Expression> getPartitionKeys() {
+ return partitionKeys;
+ }
+
+ public List<OrderExpression> getOrderKeys() {
+ return orderKeys;
+ }
Review Comment:
do we need put this method into interface PartitionTopN?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughWindow.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.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.EqualTo;
+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.Or;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+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.LogicalEmptyRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPartitionTopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.List;
+import java.util.Set;
+import java.util.function.Predicate;
+
+/**
+ * Push down the 'filter' into the 'window'.
+ * It will convert the filter condition to the 'limit value' and push down below 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)
+ * |
+ * partition_topn(PARTITION BY: a, ORDER BY b, Partition Limit: 100)
+ * |
+ * any_node
+ */
+
+public class PushdownFilterThroughWindow extends OneRewriteRuleFactory {
+
+ @Override
+ public Rule build() {
+ return logicalFilter(logicalWindow()).then(filter -> {
+ LogicalWindow<Plan> window = filter.child();
+
+ // We have already done such optimization rule, so just ignore it.
+ if (window.child(0) instanceof LogicalPartitionTopN) {
+ return filter;
+ }
+
+ // Check the window function. There are some restrictions for window function:
+ // * The number of window function should be 1.
+ // * The window function should be one of the 'row_number()', 'rank()', 'dense_rank()'.
+ // * The window type should be 'ROW'.
+ // * The window frame should be 'UNBOUNDED' to 'CURRENT'.
+ // * The 'PARTITION' key and 'ORDER' key can not be empty at the same time.
+ List<NamedExpression> windowExprs = window.getWindowExpressions();
+ if (windowExprs.size() != 1) {
+ return filter;
+ }
+ NamedExpression windowExpr = windowExprs.get(0);
+ if (windowExpr.children().size() != 1 || !(windowExpr.child(0) instanceof WindowExpression)) {
+ return filter;
+ }
+
+ WindowExpression windowFunc = (WindowExpression) windowExpr.child(0);
+ // Check the window function name.
+ if (!LogicalWindow.checkWindowFuncName4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the partition key and order key.
+ if (!LogicalWindow.checkWindowPartitionAndOrderKey4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the window type and window frame.
+ if (!LogicalWindow.checkWindowFrame4PartitionTopN(windowFunc)) {
+ return filter;
+ }
+
+ // Check the filter conditions. Now, we currently only support simple conditions of the form
+ // 'column </ <=/ = constant'. We will extract some related conjuncts and do some check.
+ Set<Expression> conjuncts = filter.getConjuncts();
+ boolean existsORConditionsInConjuncts = conjuncts.stream().anyMatch(this::existOR);
Review Comment:
why check this? i think we could push any one of conjunct in this set
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughWindow.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.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.EqualTo;
+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.Or;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+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.LogicalEmptyRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPartitionTopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.List;
+import java.util.Set;
+import java.util.function.Predicate;
+
+/**
+ * Push down the 'filter' into the 'window'.
+ * It will convert the filter condition to the 'limit value' and push down below 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)
+ * |
+ * partition_topn(PARTITION BY: a, ORDER BY b, Partition Limit: 100)
+ * |
+ * any_node
+ */
+
+public class PushdownFilterThroughWindow extends OneRewriteRuleFactory {
+
+ @Override
+ public Rule build() {
+ return logicalFilter(logicalWindow()).then(filter -> {
+ LogicalWindow<Plan> window = filter.child();
+
+ // We have already done such optimization rule, so just ignore it.
+ if (window.child(0) instanceof LogicalPartitionTopN) {
+ return filter;
+ }
+
+ // Check the window function. There are some restrictions for window function:
+ // * The number of window function should be 1.
+ // * The window function should be one of the 'row_number()', 'rank()', 'dense_rank()'.
+ // * The window type should be 'ROW'.
+ // * The window frame should be 'UNBOUNDED' to 'CURRENT'.
+ // * The 'PARTITION' key and 'ORDER' key can not be empty at the same time.
+ List<NamedExpression> windowExprs = window.getWindowExpressions();
Review Comment:
duplicate codes with PushdownLimit, do some abstract
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -115,6 +118,7 @@
*/
public class StatsCalculator extends DefaultPlanVisitor<Statistics, Void> {
public static double DEFAULT_AGGREGATE_RATIO = 0.5;
+ public static double DEFAULT_COLUMN_NDV_RATION = 0.5;
Review Comment:
```suggestion
public static double DEFAULT_COLUMN_NDV_RATIO = 0.5;
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, Optional<GroupExpression> groupExpression,
Review Comment:
```suggestion
boolean hasGlobalLimit, long partitionLimit, Optional<GroupExpression> groupExpression,
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
+ // If there is no global limit. So result for the cardinality estimation is:
+ // NDV(partition key) * partitionLimit
+ Map<Expression, ColumnStatistic> childSlotToColumnStats = stats.columnStatistics();
+ List<ColumnStatistic> partitionByKeyStats = partitionKeys.stream()
+ .filter(childSlotToColumnStats::containsKey)
+ .map(childSlotToColumnStats::get)
+ .filter(s -> !s.isUnKnown)
+ .collect(Collectors.toList());
+ if (partitionByKeyStats.isEmpty()) {
+ // all column stats are unknown, use default ratio
+ rowCount = rowCount * DEFAULT_COLUMN_NDV_RATION;
+ } else {
+ rowCount = Math.min(rowCount, partitionByKeyStats.stream().map(s -> s.ndv)
+ .max(Double::compare).get());
+ }
+ } else {
+ rowCount = Math.min(rowCount, partitionTopN.getPartitionLimit());
+ }
+ return stats.withRowCount(rowCount);
Review Comment:
we only update row count? i think we should update column stats too, @englefly
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, 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 = function;
+ this.partitionKeys = ImmutableList.copyOf(partitionKeys);
+ this.orderKeys = ImmutableList.copyOf(orderKeys);
+ this.hasGlobalLimit = hasGlobalLimit;
+ this.partitionLimit = partitionLimit;
+ }
+
+ @Override
+ public List<Slot> computeOutput() {
+ return child().getOutput();
+ }
+
+ public Expression getFunction() {
+ return function;
+ }
Review Comment:
do we need put this method into interface PartitionTopN
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression function;
Review Comment:
use an enum to instead of function
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
Review Comment:
does this class copy from SortNode? i think we could only retain constructor, toThrift, ...
all function use for Legacy planner could be removed.
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194755873
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
Review Comment:
For the limit part, I don't think it will cause some bad cases. Because we only get the minimal value.
```
private Statistics computeLimit(Limit limit) {
Statistics stats = groupExpression.childStatistics(0);
return stats.withRowCount(Math.min(stats.getRowCount(), limit.getLimit()));
}
```
But for the `filter` part, it may cause some bad cases. We should ignore some related predicates in filter.
--
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] englefly commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "englefly (via GitHub)" <gi...@apache.org>.
englefly commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1196253606
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java:
##########
@@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) {
return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit()));
}
+ private Statistics computePartitionTopN(PartitionTopN partitionTopN) {
+ Statistics stats = groupExpression.childStatistics(0);
+ double rowCount = stats.getRowCount();
+ List<Expression> partitionKeys = partitionTopN.getPartitionKeys();
+ if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) {
Review Comment:
`select * from (select * ,rank() over(partition by b order by a) as num from t ) tt where num < 10;`
rank() column stats should be set manually, its max is 10. And hence, outer filter `num<10` selectivity is 1.
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1552865786
clang-tidy review says "All clean, LGTM! :+1:"
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1200088484
##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticalType.java:
##########
@@ -38,6 +38,7 @@ public enum StatisticalType {
MYSQL_SCAN_NODE,
ODBC_SCAN_NODE,
OLAP_SCAN_NODE,
+ PARTITION_TOPN_MODE,
Review Comment:
PARTITION_TOPN_NODE
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562205812
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1547158836
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1547180186
clang-tidy review says "All clean, LGTM! :+1:"
--
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 pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1547363814
run buildall
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1196114448
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java:
##########
@@ -163,9 +163,8 @@ public int hashCode() {
* Check the window function. There are some restrictions for window function:
* 1. The number of window function should be 1.
* 2. The window function should be one of the 'row_number()', 'rank()', 'dense_rank()'.
- * 3. The window type should be 'ROW'.
Review Comment:
The `rank` function will default use the `range`. And if the window frame is 'UNBOUNDED' to 'CURRENT', it's the same to use `range` or `row`.
--
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 pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1551080584
run buildall
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562196309
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562203774
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1172296923
##########
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:
why check size?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalWindowToPhysicalWindow.java:
##########
@@ -373,12 +373,20 @@ public static class WindowFrameGroup extends WindowFunctionRelatedGroup<NamedExp
private final List<OrderExpression> orderKeys;
private final WindowFrame windowFrame;
+ private final long limitVal;
Review Comment:
```suggestion
private final long partitionLimit;
```
##########
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;
+ }
+
+ Expression conjunct = conjuncts.iterator().next();
+ if (!(conjunct instanceof LessThan || conjunct instanceof LessThanEqual)) {
+ return filter;
+ }
+
+ BinaryOperator op = (BinaryOperator) conjunct;
+ Expression leftChild = op.children().get(0);
+ Expression rightChild = op.children().get(1);
+ if (!(leftChild instanceof SlotReference) || !(rightChild instanceof IntegerLikeLiteral)) {
+ return filter;
+ }
+
+ // Adjust the value for 'limitVal' based on the comparison operators.
+ int limitVal = ((IntegerLikeLiteral) rightChild).getIntValue();
Review Comment:
should call getLongValue()
##########
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;
+ }
+
+ Expression conjunct = conjuncts.iterator().next();
+ if (!(conjunct instanceof LessThan || conjunct instanceof LessThanEqual)) {
+ return filter;
+ }
+
+ BinaryOperator op = (BinaryOperator) conjunct;
+ Expression leftChild = op.children().get(0);
+ Expression rightChild = op.children().get(1);
+ if (!(leftChild instanceof SlotReference) || !(rightChild instanceof IntegerLikeLiteral)) {
+ return filter;
+ }
+
+ // Adjust the value for 'limitVal' based on the comparison operators.
+ int limitVal = ((IntegerLikeLiteral) rightChild).getIntValue();
+ if (conjunct instanceof LessThan) {
+ limitVal--;
+ }
+ if (limitVal < 0) {
+ // TODO: The window can be removed.
+ return filter;
Review Comment:
could return filter(LogicalEmptyRelation())
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1194719404
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, 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 = function;
+ this.partitionKeys = ImmutableList.copyOf(partitionKeys);
+ this.orderKeys = ImmutableList.copyOf(orderKeys);
+ this.hasGlobalLimit = hasGlobalLimit;
+ this.partitionLimit = partitionLimit;
+ }
+
+ @Override
+ public List<Slot> computeOutput() {
+ return child().getOutput();
+ }
+
+ public Expression getFunction() {
+ return function;
+ }
+
+ @Override
+ public List<Expression> getPartitionKeys() {
+ return partitionKeys;
+ }
+
+ public List<OrderExpression> getOrderKeys() {
+ return orderKeys;
+ }
Review Comment:
I think when we need the function in the interface, we can put it later? For now, we don't need those functions in interface.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPartitionTopN.java:
##########
@@ -0,0 +1,166 @@
+// 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.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 Expression 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(Expression function, List<Expression> partitionKeys, List<OrderExpression> orderKeys,
+ Boolean hasGlobalLimit, long partitionLimit, CHILD_TYPE child) {
+ this(function, partitionKeys, orderKeys, hasGlobalLimit, partitionLimit,
+ Optional.empty(), Optional.empty(), child);
+ }
+
+ /**
+ * Constructor for LogicalPartitionTopN.
+ */
+ public LogicalPartitionTopN(Expression function, 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 = function;
+ this.partitionKeys = ImmutableList.copyOf(partitionKeys);
+ this.orderKeys = ImmutableList.copyOf(orderKeys);
+ this.hasGlobalLimit = hasGlobalLimit;
+ this.partitionLimit = partitionLimit;
+ }
+
+ @Override
+ public List<Slot> computeOutput() {
+ return child().getOutput();
+ }
+
+ public Expression getFunction() {
+ return function;
+ }
Review Comment:
Ditto
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562362149
run buildall
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562365276
run p0
--
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 pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1559039578
run buildall
--
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1560750411
run buildall
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on code in PR #18784:
URL: https://github.com/apache/doris/pull/18784#discussion_r1201464942
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalPartitionTopNToPhysicalPartitionTopN.java:
##########
@@ -0,0 +1,56 @@
+// 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.implementation;
+
+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.trees.expressions.OrderExpression;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalPartitionTopN;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Implementation rule that convert logical partition-top-n to physical partition-top-n.
+ */
+public class LogicalPartitionTopNToPhysicalPartitionTopN extends OneImplementationRuleFactory {
+ @Override
+ public Rule build() {
+ return logicalPartitionTopN().then(partitionTopN -> {
+ List<OrderKey> orderKeys = Lists.newArrayList();
+ if (!partitionTopN.getOrderKeys().isEmpty()) {
+ orderKeys.addAll(partitionTopN.getOrderKeys().stream()
+ .map(OrderExpression::getOrderKey)
+ .collect(Collectors.toList())
+ );
+ }
Review Comment:
use immutableList to avoid List copy in PhysicalPartitionTopn's constructor
```java
List<OrderKey> orderKeys = partitionTopN.getOrkerkeys.stream()
.map(...)
.collect(ImmutableList.toImmutableList());
```
##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -620,6 +622,9 @@ public class SessionVariable implements Serializable, Writable {
@VariableMgr.VarAttr(name = ENABLE_BUSHY_TREE, needForward = true)
private boolean enableBushyTree = false;
+ @VariableMgr.VarAttr(name = ENABLE_PARTITION_TOPN)
+ private boolean enablePartitionTopN = true;
Review Comment:
set default value to false until we do merge be code and do sufficient test
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java:
##########
@@ -159,4 +165,62 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(windowExpressions, isChecked);
}
+
+ /**
+ * pushPartitionLimitThroughWindow is used to push the partitionLimit through the window
+ * and generate the partitionTopN. If the window can not meet the requirement,
+ * it will return null. So when we use this function, we need check the null in the outside.
+ */
+ public static Plan pushPartitionLimitThroughWindow(LogicalWindow<Plan> window, long partitionLimit,
+ boolean hasGlobalLimit) {
+ if (!ConnectContext.get().getSessionVariable().isEnablePartitionTopN()) {
+ return null;
Review Comment:
return a Optional is better
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java:
##########
@@ -159,4 +165,62 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(windowExpressions, isChecked);
}
+
+ /**
+ * pushPartitionLimitThroughWindow is used to push the partitionLimit through the window
+ * and generate the partitionTopN. If the window can not meet the requirement,
+ * it will return null. So when we use this function, we need check the null in the outside.
+ */
+ public static Plan pushPartitionLimitThroughWindow(LogicalWindow<Plan> window, long partitionLimit,
Review Comment:
why not implenment as a member function?
##########
fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java:
##########
@@ -0,0 +1,228 @@
+// 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.planner;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.SlotId;
+import org.apache.doris.analysis.SortInfo;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.common.NotImplementedException;
+import org.apache.doris.nereids.types.WindowFuncType;
+import org.apache.doris.statistics.StatisticalType;
+import org.apache.doris.thrift.TExplainLevel;
+import org.apache.doris.thrift.TPartitionSortNode;
+import org.apache.doris.thrift.TPlanNode;
+import org.apache.doris.thrift.TPlanNodeType;
+import org.apache.doris.thrift.TSortInfo;
+import org.apache.doris.thrift.TopNAlgorithm;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * PartitionSortNode.
+ * PartitionSortNode is only used in the Nereids.
+ */
+public class PartitionSortNode extends PlanNode {
+ private static final Logger LOG = LogManager.getLogger(PartitionSortNode.class);
+ private List<Expr> resolvedTupleExprs;
+ private final WindowFuncType function;
+ private final List<Expr> partitionExprs;
+ private final SortInfo info;
+ private final boolean hasGlobalLimit;
+ private final long partitionLimit;
+
+ private boolean isUnusedExprRemoved = false;
+ private ArrayList<Boolean> nullabilityChangedFlags = Lists.newArrayList();
+
+ /**
+ * Constructor.
+ */
+ public PartitionSortNode(PlanNodeId id, PlanNode input, WindowFuncType function, List<Expr> partitionExprs,
+ SortInfo info, boolean hasGlobalLimit, long partitionLimit) {
+ super(id, "PartitionTopN", StatisticalType.PARTITION_TOPN_MODE);
+ this.function = function;
+ this.partitionExprs = partitionExprs;
+ this.info = info;
+ this.hasGlobalLimit = hasGlobalLimit;
+ this.partitionLimit = partitionLimit;
+ this.tupleIds.addAll(Lists.newArrayList(info.getSortTupleDescriptor().getId()));
+ this.tblRefIds.addAll(Lists.newArrayList(info.getSortTupleDescriptor().getId()));
+ this.nullableTupleIds.addAll(input.getNullableTupleIds());
+ this.children.add(input);
+ Preconditions.checkArgument(info.getOrderingExprs().size() == info.getIsAscOrder().size());
+ }
+
+ public SortInfo getSortInfo() {
+ return info;
+ }
+
+ @Override
+ public void getMaterializedIds(Analyzer analyzer, List<SlotId> ids) {
+ super.getMaterializedIds(analyzer, ids);
+ Expr.getIds(info.getOrderingExprs(), null, ids);
+ }
+
+ @Override
+ public String getNodeExplainString(String prefix, TExplainLevel detailLevel) {
+ if (detailLevel == TExplainLevel.BRIEF) {
+ return "";
+ }
+
+ StringBuilder output = new StringBuilder();
+
+ // Add the function name.
+ String funcName;
+ if (function == WindowFuncType.ROW_NUMBER) {
+ funcName = "row_number";
+ } else if (function == WindowFuncType.RANK) {
+ funcName = "rank";
+ } else {
+ funcName = "dense_rank";
+ }
+ output.append(prefix).append("functions: ").append(funcName).append("\n");
+
+ // Add the partition expr.
+ List<String> strings = Lists.newArrayList();
+ if (!partitionExprs.isEmpty()) {
+ output.append(prefix).append("partition by: ");
+
+ for (Expr partitionExpr : partitionExprs) {
+ strings.add(partitionExpr.toSql());
+ }
+
+ output.append(Joiner.on(", ").join(strings));
+ output.append("\n");
+ }
+
+ // Add the order by.
+ output.append(prefix).append("order by: ");
+ Iterator<Expr> expr = info.getOrderingExprs().iterator();
+ Iterator<Boolean> isAsc = info.getIsAscOrder().iterator();
+ boolean start = true;
+ while (expr.hasNext()) {
+ if (start) {
+ start = false;
+ } else {
+ output.append(", ");
+ }
+ output.append(expr.next().toSql()).append(" ");
+ output.append(isAsc.next() ? "ASC" : "DESC");
+ }
+ output.append("\n");
+
+ // Add the limit information;
+ output.append(prefix).append("has global limit: ").append(hasGlobalLimit).append("\n");
+ output.append(prefix).append("partition limit: ").append(partitionLimit).append("\n");
+
+ return output.toString();
+ }
+
+ private void removeUnusedExprs() {
+ if (!isUnusedExprRemoved) {
+ if (resolvedTupleExprs != null) {
+ List<SlotDescriptor> slotDescriptorList = this.info.getSortTupleDescriptor().getSlots();
+ for (int i = slotDescriptorList.size() - 1; i >= 0; i--) {
+ if (!slotDescriptorList.get(i).isMaterialized()) {
+ resolvedTupleExprs.remove(i);
+ nullabilityChangedFlags.remove(i);
+ }
+ }
+ }
+ isUnusedExprRemoved = true;
+ }
+ }
+
+ @Override
+ protected void toThrift(TPlanNode msg) {
+ msg.node_type = TPlanNodeType.PARTITION_SORT_NODE;
+
+ TSortInfo sortInfo = info.toThrift();
+ Preconditions.checkState(tupleIds.size() == 1, "Incorrect size for tupleIds in PartitionSortNode");
+ removeUnusedExprs();
+ if (resolvedTupleExprs != null) {
+ sortInfo.setSortTupleSlotExprs(Expr.treesToThrift(resolvedTupleExprs));
+ // FIXME this is a bottom line solution for wrong nullability of resolvedTupleExprs
+ // remove the following line after nereids online
+ sortInfo.setSlotExprsNullabilityChangedFlags(nullabilityChangedFlags);
+ }
+
+ TopNAlgorithm topNAlgorithm;
+ if (function == WindowFuncType.ROW_NUMBER) {
+ topNAlgorithm = TopNAlgorithm.ROW_NUMBER;
+ } else if (function == WindowFuncType.RANK) {
+ topNAlgorithm = TopNAlgorithm.RANK;
+ } else {
+ topNAlgorithm = TopNAlgorithm.DENSE_RANK;
+ }
+
+ TPartitionSortNode partitionSortNode = new TPartitionSortNode();
+ partitionSortNode.setTopNAlgorithm(topNAlgorithm);
+ partitionSortNode.setPartitionExprs(Expr.treesToThrift(partitionExprs));
+ partitionSortNode.setSortInfo(sortInfo);
+ partitionSortNode.setHasGlobalLimit(hasGlobalLimit);
+ partitionSortNode.setPartitionInnerLimit(partitionLimit);
+ msg.partition_sort_node = partitionSortNode;
+ }
+
+ @Override
+ public Set<SlotId> computeInputSlotIds(Analyzer analyzer) throws NotImplementedException {
+ removeUnusedExprs();
+ List<Expr> materializedTupleExprs = new ArrayList<>(resolvedTupleExprs);
+ List<SlotId> result = Lists.newArrayList();
+ Expr.getIds(materializedTupleExprs, null, result);
+ return new HashSet<>(result);
+ }
+
+ /**
+ * Supplement the information needed by be for the partition sort node.
+ */
+ public void finalizeForNereids(TupleDescriptor tupleDescriptor,
Review Comment:
could we put all things into constructor? `finalizeForNereids` will be removed in the future, new Node should not implement this function
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562196761
clang-tidy review says "All clean, LGTM! :+1:"
--
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 #18784: [feature](Nereids): pushdown filter through window
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562253956
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] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562747223
run buildall
--
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] Reminiscent commented on a diff in pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
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
[GitHub] [doris] Reminiscent commented on pull request #18784: [feature](Nereids): pushdown filter through window
Posted by "Reminiscent (via GitHub)" <gi...@apache.org>.
Reminiscent commented on PR #18784:
URL: https://github.com/apache/doris/pull/18784#issuecomment-1562659509
run buildall
--
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