swuferhong commented on code in PR #21530: URL: https://github.com/apache/flink/pull/21530#discussion_r1065381018
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/FlinkBushyJoinReorderRule.java: ########## @@ -0,0 +1,620 @@ +/* + * 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.api.java.tuple.Tuple2; +import org.apache.flink.table.planner.plan.cost.FlinkCost; + +import org.apache.calcite.plan.RelOptCost; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; +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.metadata.RelMetadataQuery; +import org.apache.calcite.rel.rules.LoptMultiJoin; +import org.apache.calcite.rel.rules.MultiJoin; +import org.apache.calcite.rel.rules.TransformationRule; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexShuttle; +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.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static java.util.Objects.requireNonNull; + +/** + * Flink bushy join reorder rule, which will convert {@link MultiJoin} to a bushy join tree. + * + * <p>In this bushy join reorder strategy, we will first try to reorder all the inner join type + * inputs in the multiJoin, and then add all outer join type inputs on the top. + * + * <p>First, reordering all the inner join type inputs in the multiJoin. We adopt the concept of + * level in dynamic programming, and the latter layer will use the results stored in the previous + * layers. First, we put all inputs (each input in {@link MultiJoin}) into level 0, then we build + * all two-inputs join at level 1 based on the {@link FlinkCost} of level 0, then we will build + * three-inputs join based on the previous two levels, then four-inputs joins ... etc, util we + * reorder all the inner join type inputs in the multiJoin. When building m-inputs join, we only + * keep the best plan (have the lowest {@link FlinkCost}) for the same set of m inputs. E.g., for + * three-inputs join, we keep only the best plan for inputs {A, B, C} among plans (A J B) J C, (A J + * C) J B, (B J C) J A. + * + * <p>Second, we will add all outer join type inputs in the MultiJoin on the top. Review Comment: > it's better we can add more comments about left/right/full case Done! -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org