LakeShen commented on code in PR #3396: URL: https://github.com/apache/calcite/pull/3396#discussion_r1335104439
########## core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java: ########## @@ -0,0 +1,145 @@ +/* + * 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. + */ [email protected] +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 topSort = call.rel(0); + final Sort bottomSort = call.rel(1); + + if (bottomSort.offset != null || bottomSort.fetch == null) { + // we could do nothing here + return; + } + + final RelBuilder builder = call.builder(); + + final int topFetch = topSort.fetch instanceof RexLiteral + ? RexLiteral.intValue(topSort.fetch) : -1; + + final int bottomFetch = bottomSort.fetch instanceof RexLiteral + ? RexLiteral.intValue(bottomSort.fetch) : -1; + + if (topFetch == -1 || bottomFetch == -1) { + return; + } + + // Get the minimum limit value from parent and child sort RelNode + int minFetch = Math.min(topFetch, bottomFetch); + + builder.push(bottomSort.getInput()); + + // Get the collation fields + ImmutableList<RexNode> fields = builder.fields(bottomSort.getCollation()); + builder.sortLimit(-1, minFetch, fields); + call.transformTo(builder.build()); + } + + /** + * Check the {@link Sort} RelNode whether is a limit. + */ + private static boolean isLimit(Sort sort) { Review Comment: > @LakeShen We have `RelOptUtil#isPureLimit`, you can use it. Hi @JiajunBernoulli ,thanks for your reply,I have used the RelOptUtil#isPureLimit method. Because I don't want an offset in this rule, but in the `RelOptUtil#isPureLimit` method, the offset is not checked, so I added a new method `RelOptUtil#isOffset` to help me figure it out. The entry condition for this rule is: `RelOptUtil.isPureLimit(p) &&! RelOptUtil.isOffset(p)` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
