This is an automated email from the ASF dual-hosted git repository.

morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 4636d6c476 [enhancement](Nereids)merge consecutive LogicalLimit plan 
(#12096)
4636d6c476 is described below

commit 4636d6c4761baae7651999f9ff8e5a301c5a051d
Author: minghong <minghong.z...@163.com>
AuthorDate: Fri Aug 26 15:53:44 2022 +0800

    [enhancement](Nereids)merge consecutive LogicalLimit plan (#12096)
    
    This rule aims to merge consecutive limits.
    
    LogicalLimit(limit=10, offset=4)
    +---LogicalLimit(limit=3, offset=5)
    
    transformed to
    
    LogicalLimit(limit=3, offset=5)
    
    where
    
    newLimit.limit    = min(topLimit.limit, bottomLimit.limit)
    newLimit.offset = bottomLimit.offset
    topLimit.offset  is ignored
---
 .../doris/nereids/jobs/batch/RewriteJob.java       |  4 ++
 .../org/apache/doris/nereids/rules/RuleType.java   |  1 +
 .../rewrite/logical/MergeConsecutiveLimits.java    | 54 ++++++++++++++++++++++
 .../logical/MergeConsecutiveLimitsTest.java        | 50 ++++++++++++++++++++
 4 files changed, 109 insertions(+)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/RewriteJob.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/RewriteJob.java
index b5c6b114a1..15f394d8de 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/RewriteJob.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/RewriteJob.java
@@ -22,6 +22,8 @@ import org.apache.doris.nereids.jobs.Job;
 import 
org.apache.doris.nereids.rules.expression.rewrite.ExpressionNormalization;
 import org.apache.doris.nereids.rules.rewrite.AggregateDisassemble;
 import org.apache.doris.nereids.rules.rewrite.logical.FindHashConditionForJoin;
+import org.apache.doris.nereids.rules.rewrite.logical.MergeConsecutiveFilters;
+import org.apache.doris.nereids.rules.rewrite.logical.MergeConsecutiveLimits;
 import org.apache.doris.nereids.rules.rewrite.logical.MergeConsecutiveProjects;
 import org.apache.doris.nereids.rules.rewrite.logical.PushPredicateThroughJoin;
 import org.apache.doris.nereids.rules.rewrite.logical.ReorderJoin;
@@ -42,6 +44,8 @@ public class RewriteJob extends BatchRulesJob {
         super(cascadesContext);
         ImmutableList<Job> jobs = new ImmutableList.Builder<Job>()
                 .add(bottomUpBatch(ImmutableList.of(new 
MergeConsecutiveProjects())))
+                .add(bottomUpBatch(ImmutableList.of(new 
MergeConsecutiveFilters())))
+                .add(bottomUpBatch(ImmutableList.of(new 
MergeConsecutiveLimits())))
                 .add(topDownBatch(ImmutableList.of(new 
ExpressionNormalization())))
                 .add(topDownBatch(ImmutableList.of(new ReorderJoin())))
                 .add(topDownBatch(ImmutableList.of(new 
FindHashConditionForJoin())))
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
index 665836c987..4d991102e3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
@@ -72,6 +72,7 @@ public enum RuleType {
     REORDER_JOIN(RuleTypeClass.REWRITE),
     MERGE_CONSECUTIVE_FILTERS(RuleTypeClass.REWRITE),
     MERGE_CONSECUTIVE_PROJECTS(RuleTypeClass.REWRITE),
+    MERGE_CONSECUTIVE_LIMITS(RuleTypeClass.REWRITE),
     FIND_HASH_CONDITION_FOR_JOIN(RuleTypeClass.REWRITE),
     REWRITE_SENTINEL(RuleTypeClass.REWRITE),
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveLimits.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveLimits.java
new file mode 100644
index 0000000000..32d36318c8
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveLimits.java
@@ -0,0 +1,54 @@
+// 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.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
+
+import java.util.List;
+
+/**
+ * this rule aims to merge consecutive limits.
+ *   LIMIT1(limit=10, offset=4)
+ *     |
+ *   LIMIT2(limit=3, offset=5)
+ *
+ *   transformed to
+ *    LIMITl(limit=3, offset=5)
+ *   where
+ *   LIMIT.limit = min(LIMIT1.limit, LIMIT2.limit)
+ *   LIMIT.offset = LIMIT2.offset
+ *   LIMIT1.offset is ignored
+ */
+public class MergeConsecutiveLimits extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalLimit(logicalLimit()).then(upperLimit -> {
+            LogicalLimit bottomLimit = upperLimit.child();
+            List<Plan> children = bottomLimit.children();
+            return new LogicalLimit(
+                    Math.min(upperLimit.getLimit(), bottomLimit.getLimit()),
+                    bottomLimit.getOffset(),
+                    children.get(0)
+            );
+        }).toRule(RuleType.MERGE_CONSECUTIVE_LIMITS);
+    }
+}
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveLimitsTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveLimitsTest.java
new file mode 100644
index 0000000000..e64f71c852
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveLimitsTest.java
@@ -0,0 +1,50 @@
+// 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.CascadesContext;
+import org.apache.doris.nereids.analyzer.UnboundRelation;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
+import org.apache.doris.nereids.util.MemoTestUtils;
+
+import com.google.common.collect.Lists;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+public class MergeConsecutiveLimitsTest {
+    @Test
+    public void testMergeConsecutiveLimits() {
+        LogicalLimit limit3 = new LogicalLimit(3, 5, new 
UnboundRelation(Lists.newArrayList("db", "t")));
+        LogicalLimit limit2 = new LogicalLimit(2, 0, limit3);
+        LogicalLimit limit1 = new LogicalLimit(10, 2, limit2);
+
+        CascadesContext context = MemoTestUtils.createCascadesContext(limit1);
+        List<Rule> rules = Lists.newArrayList(new 
MergeConsecutiveLimits().build());
+        context.topDownRewrite(rules);
+        LogicalLimit limit = (LogicalLimit) context.getMemo().copyOut();
+
+        Assertions.assertEquals(2, limit.getLimit());
+        Assertions.assertEquals(5, limit.getOffset());
+        Assertions.assertEquals(1, limit.children().size());
+        Assertions.assertTrue(limit.child(0) instanceof UnboundRelation);
+
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to