morrySnow commented on code in PR #11209: URL: https://github.com/apache/doris/pull/11209#discussion_r932006408
########## fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LimitClauseTest.java: ########## @@ -0,0 +1,93 @@ +// 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.parser; + +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalSort; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class LimitClauseTest { + @Test + public void testLimit() { + NereidsParser nereidsParser = new NereidsParser(); + String sql = "SELECT b FROM test order by a limit 3 offset 100"; + LogicalPlan logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + LogicalLimit limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(100, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + + sql = "SELECT b FROM test order by a limit 100, 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(100, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + + sql = "SELECT b FROM test limit 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(0, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalProject); + + sql = "SELECT b FROM test order by a limit 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(0, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + } + + @Test + public void testLimitExceptionCase() { + NereidsParser nereidsParser = new NereidsParser(); + String sql = "SELECT b FROM test limit 3 offset 100"; + try { + LogicalPlan logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + } catch (IllegalStateException e) { Review Comment: if we want to check exception, please use assertThrow: https://howtodoinjava.com/junit5/expected-exception-example/ ########## fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LimitClauseTest.java: ########## @@ -0,0 +1,93 @@ +// 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.parser; + +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalSort; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class LimitClauseTest { + @Test + public void testLimit() { + NereidsParser nereidsParser = new NereidsParser(); + String sql = "SELECT b FROM test order by a limit 3 offset 100"; + LogicalPlan logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + LogicalLimit limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(100, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + + sql = "SELECT b FROM test order by a limit 100, 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); Review Comment: use junit5 assert: Assertions.xxxx ########## fe/fe-core/src/test/java/org/apache/doris/nereids/rules/implementation/LogicalLimitToPhysicalLimitTest.java: ########## @@ -0,0 +1,44 @@ +// 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.PlannerContext; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; + +import mockit.Mocked; +import org.junit.Assert; +import org.junit.Test; Review Comment: use junit5 Test ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java: ########## @@ -532,13 +534,23 @@ public List<Expression> visitNamedExpressionSeq(NamedExpressionSeqContext namedC * @return List of OrderKey */ @Override - public List<OrderKey> visitQueryOrganization(QueryOrganizationContext ctx) { + public QueryOrganization visitQueryOrganization(QueryOrganizationContext ctx) { return ParserUtils.withOrigin(ctx, () -> { + List<OrderKey> orderKeys; if (ctx.sortClause().ORDER() != null) { - return visit(ctx.sortClause().sortItem(), OrderKey.class); + orderKeys = visit(ctx.sortClause().sortItem(), OrderKey.class); } else { - return ImmutableList.of(); + orderKeys = ImmutableList.of(); } + long limit = Long.MAX_VALUE; + long offset = 0; + if (ctx.limitClause().LIMIT() != null) { + limit = Long.parseLong(ctx.limitClause().limit.getText()); + if (ctx.limitClause().OFFSET() != null || ctx.limitClause().COMMA() != null) { + offset = Long.parseLong(ctx.limitClause().offset.getText()); + } + } Review Comment: ```suggestion if (ctx.limitClause().limit != null) { limit = Long.parseLong(ctx.limitClause().limit.getText()); if (ctx.limitClause().offset != null) { offset = Long.parseLong(ctx.limitClause().offset.getText()); } } ``` ########## fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LimitClauseTest.java: ########## @@ -0,0 +1,93 @@ +// 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.parser; + +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalSort; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class LimitClauseTest { + @Test + public void testLimit() { + NereidsParser nereidsParser = new NereidsParser(); + String sql = "SELECT b FROM test order by a limit 3 offset 100"; + LogicalPlan logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + LogicalLimit limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(100, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + + sql = "SELECT b FROM test order by a limit 100, 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(100, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + + sql = "SELECT b FROM test limit 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(0, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalProject); + + sql = "SELECT b FROM test order by a limit 3"; + logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); + Assert.assertTrue(logicalPlan instanceof LogicalLimit); + limit = (LogicalLimit) logicalPlan; + Assert.assertEquals(3, limit.getLimit()); + Assert.assertEquals(0, limit.getOffset()); + Assert.assertEquals(1, limit.children().size()); + Assert.assertTrue(limit.child(0) instanceof LogicalSort); + } + + @Test + public void testLimitExceptionCase() { + NereidsParser nereidsParser = new NereidsParser(); + String sql = "SELECT b FROM test limit 3 offset 100"; + try { + LogicalPlan logicalPlan = nereidsParser.parseSingle(sql); + System.out.println(logicalPlan.treeString()); Review Comment: remove useless print ########## fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LimitClauseTest.java: ########## @@ -0,0 +1,93 @@ +// 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.parser; + +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalSort; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class LimitClauseTest { Review Comment: add a test without limit and offset -- 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