suibianwanwank commented on code in PR #4244:
URL: https://github.com/apache/calcite/pull/4244#discussion_r2017365349


##########
core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java:
##########
@@ -10048,4 +10048,35 @@ private void 
checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
     sql(sql).withRule(CoreRules.INTERSECT_TO_EXISTS)
         .checkUnchanged();
   }
+
+  /** Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-6891";>
+   * [CALCITE-6891] Implement IntersectReorderRule</a>. */
+  @Test void testIntersectReorderRule() {
+    final String sql = "select deptno from emp\n"
+        + "intersect\n"
+        + "select deptno from dept\n";
+
+    sql(sql).withVolcanoPlanner(true, p -> {

Review Comment:
   Maybe it would be clearer to apply the rules by adding different filters for 
the same scan in the test case.



##########
core/src/main/java/org/apache/calcite/rel/rules/IntersectReorderRule.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.RelNode;
+import org.apache.calcite.rel.core.Intersect;
+import org.apache.calcite.rel.logical.LogicalIntersect;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.Pair;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Planner rule that reorders inputs of an {@link Intersect} to put smaller 
inputs first.
+ * This helps reduce the size of intermediate results.
+ *
+ * <p>Intersect(A, B, ...) where B is smallest will reorder to Intersect(B, A, 
...)
+ */
[email protected]
+public class IntersectReorderRule extends RelRule<IntersectReorderRule.Config>
+      implements SubstitutionRule {
+  /** Creates an IntersectReorderRule. */
+  protected IntersectReorderRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Intersect intersect = call.rel(0);

Review Comment:
   I suggest it could be simpler here, For example:
   ```
       final Intersect intersect = call.rel(0);
       final RelMetadataQuery mq = call.getMetadataQuery();
       final List<RelNode> inputs = intersect.getInputs();
   
       List<RelNode> sortedInputs = inputs.stream()
           .sorted(Comparator.comparingDouble(input -> mq.getRowCount(input)))
           .collect(Collectors.toList());
   
       if (inputs.equals(sortedInputs)) {
           return;
       }
   
       final RelBuilder relBuilder = call.builder();
       relBuilder.pushAll(sortedInputs);
       relBuilder.intersect(intersect.all, sortedInputs.size());
   
       call.transformTo(relBuilder.build());
   ```



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

Reply via email to