hsyuan commented on a change in pull request #1160: [CALCITE-2712] Add rule to 
eliminate unnecessary outer join
URL: https://github.com/apache/calcite/pull/1160#discussion_r276743099
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinJoinRemoveRule.java
 ##########
 @@ -0,0 +1,129 @@
+/*
+ * 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.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that matches an {@link org.apache.calcite.rel.core.Aggregate}
+ * on a {@link org.apache.calcite.rel.core.Join} and removes the left input
+ * of the join provided that the left input is also a left join if possible.
+ *
+ * <p>For instance,</p>
+ *
+ * <blockquote>
+ * <pre>select distinct s.product_id, pc.product_id from
+ * sales as s
+ * left join product as p
+ * on s.product_id = p.product_id
+ * left join product_class pc
+ * on s.product_id = pc.product_id</pre></blockquote>
+ *
+ * <p>becomes
+ *
+ * <blockquote>
+ * <pre>select distinct s.product_id, pc.product_id from
+ * sales as s
+ * left join product_class pc
+ * on s.product_id = pc.product_id</pre></blockquote>
+ *
+ */
+public class AggregateJoinJoinRemoveRule extends RelOptRule {
+  public static final AggregateJoinJoinRemoveRule INSTANCE
+      = new AggregateJoinJoinRemoveRule(LogicalAggregate.class,
+      LogicalJoin.class, RelFactories.LOGICAL_BUILDER);
+
+  /** Creates an AggregateJoinJoinRemoveRule. */
+  public AggregateJoinJoinRemoveRule(
+      Class<? extends Aggregate> aggregateClass,
+      Class<? extends Join> joinClass, RelBuilderFactory relBuilderFactory) {
+    super(
+        operand(aggregateClass,
+            operandJ(joinClass, null,
+                join -> join.getJoinType() == JoinRelType.LEFT,
+                operandJ(joinClass, null,
+                    join -> join.getJoinType() == JoinRelType.LEFT, any()))),
+        relBuilderFactory, null);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Aggregate aggregate = call.rel(0);
+    final Join topJoin = call.rel(1);
+    final Join bottomJoin = call.rel(2);
+    int leftBottomChildSize = bottomJoin.getLeft().getRowType()
+        .getFieldCount();
+
+    // Check whether the aggregate uses unexpected columns.
+    if (!RelOptUtil.removeJoin(aggregate, leftBottomChildSize,
+        bottomJoin.getRowType().getFieldCount())) {
+      return;
+    }
+
+    // Check whether the top join uses unexpected columns.
+    final List<Integer> leftKeys = new ArrayList<>();
+    RelOptUtil.splitJoinCondition(topJoin.getLeft(), topJoin.getRight(),
+        topJoin.getCondition(), leftKeys, new ArrayList<>(),
+        new ArrayList<>());
+    if (leftKeys.stream().filter(s -> s >= leftBottomChildSize).count() > 0) {
 
 Review comment:
   I hope the if condition part can be splitted into 2 statements.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to