morrySnow commented on code in PR #11209: URL: https://github.com/apache/doris/pull/11209#discussion_r936202722
########## fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java: ########## @@ -405,6 +408,34 @@ public PlanFragment visitPhysicalFilter(PhysicalFilter<Plan> filter, PlanTransla return inputFragment; } + @Override + public PlanFragment visitPhysicalLimit(PhysicalLimit<Plan> physicalLimit, PlanTranslatorContext context) { + PlanFragment inputFragment = physicalLimit.child(0).accept(this, context); + PlanNode child = inputFragment.getPlanRoot(); + if (child instanceof OlapScanNode) { + child.setLimit(physicalLimit.getLimit() + physicalLimit.getOffset()); + return inputFragment; + } + + if (child instanceof SortNode) { + ((SortNode) child).setOffset(physicalLimit.getOffset()); + child.setLimit(physicalLimit.getLimit()); + inputFragment.getChildren().forEach(fragment -> { + PlanNode root = fragment.getPlanRoot(); + if (root instanceof SortNode) { + root.setLimit(physicalLimit.getLimit() + physicalLimit.getOffset()); + } + }); + return inputFragment; + } + + if (child instanceof AggregationNode) { Review Comment: except sort node, all other node should use same logical to do limit merge, right? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLimit.java: ########## @@ -0,0 +1,122 @@ +// 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.IntegerLiteral; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +/** + * Logical limit plan + * eg: select * from table limit 10 + * limit: 10 + * + * eg: select * from table order by a limit 100, 10 + * limit: 10 + * offset 100 + */ +public class LogicalLimit<CHILD_TYPE extends Plan> extends LogicalUnary<CHILD_TYPE> { + private long limit = Long.MAX_VALUE; + private long offset = 0L; Review Comment: forgot to update them to final? ########## fe/fe-core/src/main/java/org/apache/doris/statistics/PlanStats.java: ########## @@ -32,7 +32,9 @@ public interface PlanStats { void setStatsDeriveResult(StatsDeriveResult result); - long getLimit(); + default long getLimit() { Review Comment: why need to change this interface? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/properties/QueryOrganization.java: ########## @@ -0,0 +1,57 @@ +// 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.properties; + +import java.util.List; +/** + * Represents order keys and limit and offset + */ + +public class QueryOrganization { Review Comment: remove this class ########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java: ########## @@ -70,14 +72,23 @@ public StatsCalculator(GroupExpression groupExpression) { * Do estimate. */ public void estimate() { + StatsDeriveResult stats = groupExpression.getPlan().accept(this, null); groupExpression.getOwnerGroup().setStatistics(stats); - Plan plan = groupExpression.getPlan(); - long limit = plan.getLimit(); - if (limit != -1) { - stats.setRowCount(Math.min(limit, stats.getRowCount())); - } groupExpression.setStatDerived(true); + + } + + @Override + public StatsDeriveResult visitLogicalLimit(LogicalLimit<Plan> limit, Void context) { + StatsDeriveResult stats = groupExpression.getCopyOfChildStats(0); + return stats.updateRowCountByLimit(limit.getLimit() + limit.getOffset()); + } + + @Override + public StatsDeriveResult visitPhysicalLimit(PhysicalLimit<Plan> limit, Void context) { + StatsDeriveResult stats = groupExpression.getCopyOfChildStats(0); + return stats.updateRowCountByLimit(limit.getLimit() + limit.getOffset()); Review Comment: i think we should set row count to limit.getLimit() directly, @Kikyou1997 PTAL ########## fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java: ########## @@ -68,17 +69,16 @@ public class SortNode extends PlanNode { /** * Constructor. */ - public SortNode(PlanNodeId id, PlanNode input, SortInfo info, boolean useTopN, - boolean isDefaultLimit, long offset) { + public SortNode(PlanNodeId id, PlanNode input, SortInfo info, boolean useTopN) { Review Comment: it is better that do not change this Constructor and add another one for nereids ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java: ########## @@ -565,13 +569,19 @@ public List<Expression> visitNamedExpressionSeq(NamedExpressionSeqContext namedC * @return List of OrderKey */ @Override - public List<OrderKey> visitQueryOrganization(QueryOrganizationContext ctx) { + public QueryOrganization visitQueryOrganization(QueryOrganizationContext ctx) { Review Comment: i think this method will never be called, and we should remove it ########## fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java: ########## @@ -307,28 +307,31 @@ public PlanFragment visitPhysicalHeapSort(PhysicalHeapSort<Plan> sort, SortInfo sortInfo = new SortInfo(newOrderingExprList, ascOrderList, nullsFirstParamList, tupleDesc); PlanNode childNode = childFragment.getPlanRoot(); // TODO: notice topN - SortNode sortNode = new SortNode(context.nextPlanNodeId(), childNode, sortInfo, true, - sort.hasLimit(), sort.getOffset()); + // TODO: (zmh) set limit info when translate PhysicalLimit Review Comment: TODO has be done? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org