kgyrtkirk commented on code in PR #3396:
URL: https://github.com/apache/calcite/pull/3396#discussion_r1310312042


##########
core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+/**
+ * This rule try to merge the double {@link Sort},one is Limit semantics,
+ * another sort is Limit or TOPN semantics.
+ *
+ * <p> It generally used with the {@link SortProjectTransposeRule} rule.
+ *
+ * <p> For example:
+ * <blockquote><pre>{@code
+ * select
+ *   concat('-', N_REGIONKEY) from
+ *   (
+ *     select
+ *       *
+ *     from nation limit 10000) limit 10}
+ *  </pre></blockquote>
+ *
+ * <p> will convert to
+ * <blockquote><pre>{@code
+ * select
+ *   concat('-', N_REGIONKEY)
+ * from
+ *   nation limit 10
+ * }</pre></blockquote>
+ *
+ * <p> The sql :
+ * <blockquote><pre>{@code
+ * select concat('-',N_REGIONKEY) from
+ * (SELECT * FROM nation order BY N_REGIONKEY DESC LIMIT 10000) limit 10
+ * }</pre></blockquote>
+ *
+ * <p> will convert to
+ * <blockquote><pre>{@code
+ * SELECT concat('-',N_REGIONKEY) FROM nation order BY N_REGIONKEY DESC LIMIT 
10
+ * }</pre></blockquote>
+ *
+ * <p> In the future,we could also extend other sort merge logic in this rule.
+ */
+@Value.Enclosing
+public class SortMergeRule
+    extends RelRule<SortMergeRule.Config>
+    implements TransformationRule {
+
+  protected SortMergeRule(final SortMergeRule.Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(final RelOptRuleCall call) {
+    config.matchHandler().accept(this, call);
+  }
+
+  private static void limitMerge(SortMergeRule rule,
+      RelOptRuleCall call) {
+    final Sort sort = call.rel(0);
+    final Sort child = call.rel(1);
+
+    if (child.offset != null || child.fetch == null) {
+      // we could do nothing here
+      return;
+    }
+
+    final RelBuilder builder = call.builder();
+
+    final int limitCnt = sort.fetch instanceof RexLiteral
+        ? RexLiteral.intValue(sort.fetch) : -1;
+
+    final int childLimitCnt = child.fetch instanceof RexLiteral
+        ? RexLiteral.intValue(child.fetch) : -1;
+
+    if (limitCnt == -1 || childLimitCnt == -1) {
+      return;
+    }
+
+    // Get the minimum limit value from parent and child sort RelNode
+    int minLimit = limitCnt < childLimitCnt

Review Comment:
   nit: isn't `Math.min` is more readable?



##########
core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+/**
+ * This rule try to merge the double {@link Sort},one is Limit semantics,
+ * another sort is Limit or TOPN semantics.
+ *
+ * <p> It generally used with the {@link SortProjectTransposeRule} rule.
+ *
+ * <p> For example:
+ * <blockquote><pre>{@code
+ * select
+ *   concat('-', N_REGIONKEY) from
+ *   (
+ *     select
+ *       *
+ *     from nation limit 10000) limit 10}
+ *  </pre></blockquote>
+ *
+ * <p> will convert to
+ * <blockquote><pre>{@code
+ * select
+ *   concat('-', N_REGIONKEY)
+ * from
+ *   nation limit 10
+ * }</pre></blockquote>
+ *
+ * <p> The sql :
+ * <blockquote><pre>{@code
+ * select concat('-',N_REGIONKEY) from
+ * (SELECT * FROM nation order BY N_REGIONKEY DESC LIMIT 10000) limit 10
+ * }</pre></blockquote>
+ *
+ * <p> will convert to
+ * <blockquote><pre>{@code
+ * SELECT concat('-',N_REGIONKEY) FROM nation order BY N_REGIONKEY DESC LIMIT 
10
+ * }</pre></blockquote>
+ *
+ * <p> In the future,we could also extend other sort merge logic in this rule.
+ */
+@Value.Enclosing
+public class SortMergeRule
+    extends RelRule<SortMergeRule.Config>
+    implements TransformationRule {
+
+  protected SortMergeRule(final SortMergeRule.Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(final RelOptRuleCall call) {
+    config.matchHandler().accept(this, call);
+  }
+
+  private static void limitMerge(SortMergeRule rule,
+      RelOptRuleCall call) {
+    final Sort sort = call.rel(0);
+    final Sort child = call.rel(1);

Review Comment:
   nit: I find the naming of these 2 a bit confusing - they are both `Sort` 
nodes - and they are being evaluated as such - so `child` doesn't really match 
that..
   could you rename them something like: `topSort` / `bottomSort`  or similar ? 



##########
core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java:
##########
@@ -1254,6 +1254,30 @@ private void 
checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
     diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5940";>[CALCITE-5940]
+   * Add the Rules to optimize Limit</a>. */
+  @Test void testLimitMerge() {
+    final String sql = "select deptno from "
+        + "(select deptno from emp where sal > 100 limit 10000) limit 10";
+    sql(sql)
+        .withPreRule(CoreRules.SORT_PROJECT_TRANSPOSE)
+        .withRule(CoreRules.LIMIT_MREGE, CoreRules.PROJECT_REMOVE)
+        .check();
+  }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5940";>[CALCITE-5940]
+   * Add the Rules to optimize Limit</a>. */
+  @Test void testLimitMerge1() {

Review Comment:
   I know its not covered in this patch ; but if you could add some test(s) 
with offset so that its "tested" that those cases are not covered right now



##########
core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+/**
+ * This rule try to merge the double {@link Sort},one is Limit semantics,
+ * another sort is Limit or TOPN semantics.
+ *
+ * <p> It generally used with the {@link SortProjectTransposeRule} rule.
+ *
+ * <p> For example:
+ * <blockquote><pre>{@code
+ * select
+ *   concat('-', N_REGIONKEY) from
+ *   (
+ *     select
+ *       *
+ *     from nation limit 10000) limit 10}
+ *  </pre></blockquote>
+ *
+ * <p> will convert to
+ * <blockquote><pre>{@code
+ * select
+ *   concat('-', N_REGIONKEY)
+ * from
+ *   nation limit 10
+ * }</pre></blockquote>
+ *
+ * <p> The sql :
+ * <blockquote><pre>{@code
+ * select concat('-',N_REGIONKEY) from
+ * (SELECT * FROM nation order BY N_REGIONKEY DESC LIMIT 10000) limit 10
+ * }</pre></blockquote>
+ *
+ * <p> will convert to
+ * <blockquote><pre>{@code
+ * SELECT concat('-',N_REGIONKEY) FROM nation order BY N_REGIONKEY DESC LIMIT 
10
+ * }</pre></blockquote>
+ *
+ * <p> In the future,we could also extend other sort merge logic in this rule.
+ */
+@Value.Enclosing
+public class SortMergeRule
+    extends RelRule<SortMergeRule.Config>
+    implements TransformationRule {
+
+  protected SortMergeRule(final SortMergeRule.Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(final RelOptRuleCall call) {
+    config.matchHandler().accept(this, call);
+  }
+
+  private static void limitMerge(SortMergeRule rule,
+      RelOptRuleCall call) {
+    final Sort sort = call.rel(0);
+    final Sort child = call.rel(1);
+
+    if (child.offset != null || child.fetch == null) {
+      // we could do nothing here
+      return;
+    }
+
+    final RelBuilder builder = call.builder();
+
+    final int limitCnt = sort.fetch instanceof RexLiteral

Review Comment:
   I think it might be better to stick with the naming we already have - and 
use the term `fetch` in this case



-- 
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...@calcite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to