morrySnow commented on code in PR #11209: URL: https://github.com/apache/doris/pull/11209#discussion_r930696935
########## fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4: ########## @@ -119,6 +119,12 @@ sortItem : expression ordering = (ASC | DESC)? ; +limitClause + : (LIMIT INTEGER_VALUE)? + | (LIMIT INTEGER_VALUE OFFSET INTEGER_VALUE)? + | (LIMIT INTEGER_VALUE COMMA INTEGER_VALUE)? Review Comment: (LIMIT limit=INTEGER_VALUE ((OFFSET | COMMA) offset=INTEGER_VALUE)?)? ########## 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; + + public LogicalLimit(long limit, long offset, CHILD_TYPE child) { + this(limit, offset, Optional.empty(), Optional.empty(), child); + } + + public LogicalLimit(long limit, long offset, Optional<GroupExpression> groupExpression, + Optional<LogicalProperties> logicalProperties, CHILD_TYPE child) { + super(PlanType.LOGICAL_LIMIT, groupExpression, logicalProperties, child); + this.limit = limit; + this.offset = offset; + } + + public long getLimit() { + return limit; + } + + public long getOffset() { + return offset; + } + + @Override + public List<Slot> computeOutput(Plan input) { + return input.getOutput(); + } + + @Override + public String toString() { + return "LogicalLimit (" + offset + "," + limit + ")"; Review Comment: ```suggestion return "LogicalLimit (offset=" + offset + ", limit=" + limit + ")"; ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java: ########## @@ -532,16 +537,52 @@ public List<Expression> visitNamedExpressionSeq(NamedExpressionSeqContext namedC * @return List of OrderKey */ @Override - public List<OrderKey> visitQueryOrganization(QueryOrganizationContext ctx) { Review Comment: nit: we could just add sort node and limit node to plan in withQueryOrganization directly and remove this function and related data structure like LimitAndOffset.. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLimit.java: ########## @@ -0,0 +1,116 @@ +// 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.physical; + +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.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; + +/** + * Physical limit plan + */ +public class PhysicalLimit<CHILD_TYPE extends Plan> extends PhysicalUnary<CHILD_TYPE> { + private final long limit; + + private final long offset; + + /** + * constructor + * select * from t order by a limit [offset], [limit]; + * @param limit the number of tuples retrieved. + * @param offset the number of tuples skipped. + */ + public PhysicalLimit(long limit, long offset, + Optional<GroupExpression> groupExpression, LogicalProperties logicalProperties, + CHILD_TYPE child) { + super(PlanType.PHYSICAL_LIMIT, groupExpression, logicalProperties, child); + this.limit = limit; + this.offset = offset; + } + + public PhysicalLimit(long limit, long offset, + LogicalProperties logicalProperties, + CHILD_TYPE child) { + this(limit, offset, Optional.empty(), logicalProperties, child); + } + + public long getLimit() { + return limit; + } + + public long getOffset() { + return offset; + } + + @Override + public Plan withChildren(List<Plan> children) { + Preconditions.checkArgument(children.size() == 1); + return new PhysicalLimit<>(limit, offset, logicalProperties, children.get(0)); + } + + @Override + public List<Expression> getExpressions() { + return Lists.newArrayList( + new IntegerLiteral((int) limit), + new IntegerLiteral((int) offset) + ); + } + + @Override + public Plan withGroupExpression(Optional<GroupExpression> groupExpression) { + return new PhysicalLimit<>(limit, offset, groupExpression, logicalProperties, child()); + } + + @Override + public Plan withLogicalProperties(Optional<LogicalProperties> logicalProperties) { + return new PhysicalLimit<>(limit, offset, logicalProperties.get(), child()); + } + Review Comment: add toString() ########## 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: ```suggestion private final long limit; private final long offset; ``` and initialize them in constructor -- 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