agavra commented on code in PR #9832:
URL: https://github.com/apache/pinot/pull/9832#discussion_r1027002234


##########
pinot-query-planner/src/main/java/org/apache/calcite/rel/rules/PinotSortExchangeCopyRule.java:
##########
@@ -0,0 +1,117 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.SortExchange;
+import org.apache.calcite.rel.logical.LogicalSort;
+import org.apache.calcite.rel.logical.LogicalSortExchange;
+import org.apache.calcite.rel.metadata.RelMdUtil;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.query.type.TypeSystem;
+
+
+public class PinotSortExchangeCopyRule extends RelRule<RelRule.Config> {
+
+  public static final PinotSortExchangeCopyRule SORT_EXCHANGE_COPY =
+      PinotSortExchangeCopyRule.Config.DEFAULT.toRule();
+  public static final TypeFactory TYPE_FACTORY = new TypeFactory(new 
TypeSystem());
+
+  /**
+   * Creates a PinotSortExchangeCopyRule.
+   */
+  protected PinotSortExchangeCopyRule(Config config) {
+    super(config);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final Sort sort = call.rel(0);
+    final SortExchange exchange = call.rel(1);
+    final RelMetadataQuery metadataQuery = call.getMetadataQuery();
+
+    if (RelMdUtil.checkInputForCollationAndLimit(

Review Comment:
   Good catch! I'll add a test for this in a follow-up PR (I need to introduce 
a new testing framework to do this well) but in the meantime here's proof that 
the new code works:
   ```
   Generated Plan for 'SELECT * FROM {basic} LIMIT 2':
   
   [0]@localhost:52943 MAIL_RECEIVE(RANDOM_DISTRIBUTED)
   ├── [1]@localhost:52941 MAIL_SEND(RANDOM_DISTRIBUTED)->{[0]@localhost:52943} 
(Subtree Omitted)
   └── [1]@localhost:52942 MAIL_SEND(RANDOM_DISTRIBUTED)->{[0]@localhost:52943}
      └── [1]@localhost:52942 SORT LIMIT 2
         └── [1]@localhost:52942 MAIL_RECEIVE(HASH_DISTRIBUTED)
            └── [2]@localhost:52941 
MAIL_SEND(HASH_DISTRIBUTED)->{[1]@localhost:52941,[1]@localhost:52942}
               └── [2]@localhost:52941 SORT LIMIT 2
                  └── [2]@localhost:52941 TABLE SCAN (basic_order_by_basic) 
{OFFLINE=[basic_order_by_basic_OFFLINE_3d103a31-4bea-46a0-b83a-cbfa305ac403]}
   
   
   ```



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

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


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

Reply via email to