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


##########
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:
   Yes,Math.min is more readable



-- 
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