godfreyhe commented on a change in pull request #10174: 
[FLINK-14625][table-planner-blink] Add a rule to eliminate cross join as much 
as possible without statistics
URL: https://github.com/apache/flink/pull/10174#discussion_r346276355
 
 

 ##########
 File path: 
flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/rules/logical/EliminateCrossJoinRule.java
 ##########
 @@ -0,0 +1,477 @@
+/*
+ * 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.flink.table.planner.plan.rules.logical;
+
+import org.apache.flink.util.Preconditions;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.rules.LoptMultiJoin;
+import org.apache.calcite.rel.rules.MultiJoin;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.mapping.Mappings;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * This rule tries to eliminate cross joins by reordering joins.
+ * The new order of joins are determined with the following steps:
+ *
+ * <p>1. The inputs related with an equi-join filter (= or IS NOT DISTINCT 
FROM) will be joined first.
+ *       Inputs with smaller indices has higher priority.
+ *
+ * <p>2. The inputs related with other join filters will then be joined.
+ *
+ * <p>3. If not all inner join inputs are joined, they will be joined in input 
order.
+ *
+ * <p>4. Outer joins are added.
+ */
+public class EliminateCrossJoinRule extends RelOptRule {
+
+       public static final EliminateCrossJoinRule INSTANCE = new 
EliminateCrossJoinRule();
+
+       private EliminateCrossJoinRule() {
+               super(operand(MultiJoin.class, any()), 
"EliminateCrossJoinRule");
+       }
+
+       @Override
+       public void onMatch(RelOptRuleCall call) {
+               MultiJoin join = call.rel(0);
+               RelBuilder relBuilder = call.builder();
+
+               if (join.isFullOuterJoin()) {
+                       // full outer join, do not reorder joins
+                       Preconditions.checkArgument(
+                               join.getInputs().size() == 2,
+                               "Full outer join must have exactly 2 inputs. 
This is a bug.");
+
+                       relBuilder
+                               .push(join.getInput(0))
+                               .push(join.getInput(1))
+                               .join(JoinRelType.FULL, join.getJoinFilter());
+                       if (join.getPostJoinFilter() != null) {
+                               relBuilder.filter(join.getPostJoinFilter());
+                       }
+               } else {
+                       int outerJoinCount = 0;
+                       for (int i = 0; i < join.getInputs().size(); i++) {
 
 Review comment:
   move the validation code to another method named "validateOuterJoins"

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