Github user jihoonson commented on a diff in the pull request:
https://github.com/apache/tajo/pull/583#discussion_r31097759
--- Diff:
tajo-core/src/main/java/org/apache/tajo/engine/planner/global/rewriter/rules/BroadcastJoinRule.java
---
@@ -0,0 +1,341 @@
+/**
+ * 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.tajo.engine.planner.global.rewriter.rules;
+
+import org.apache.tajo.ExecutionBlockId;
+import org.apache.tajo.OverridableConf;
+import org.apache.tajo.SessionVars;
+import org.apache.tajo.engine.planner.global.ExecutionBlock;
+import org.apache.tajo.engine.planner.global.GlobalPlanner;
+import org.apache.tajo.engine.planner.global.MasterPlan;
+import
org.apache.tajo.engine.planner.global.rewriter.GlobalPlanRewriteRule;
+import org.apache.tajo.plan.LogicalPlan;
+import org.apache.tajo.plan.PlanningException;
+import org.apache.tajo.plan.logical.*;
+import org.apache.tajo.plan.util.PlannerUtil;
+import org.apache.tajo.util.TUtil;
+import org.apache.tajo.util.graph.DirectedGraphVisitor;
+
+import java.util.*;
+
+/**
+ * {@link BroadcastJoinRule} converts repartition join plan into broadcast
join plan.
+ * Broadcast join rules can be defined as follows.
+ *
+ * <h3>Broadcastable relation</h3>
+ * A relation is broadcastable when its size is smaller than a given
threshold.
+ *
+ * <h3>Assumetion</h3>
+ * If every input of an execution block is broadcastable, the output of
the execution block is also broadcastable.
+ *
+ * <h3>Rules to convert repartition join into broadcast join</h3>
+ * <ul>
+ * <li>Given an EB containing a join and its child EBs, those EBs can be
merged into a single EB if at least one child EB's output is broadcastable.</li>
+ * <li>Given a user-defined threshold, the total size of broadcast
relations of an EB cannot exceed such threshold.</li>
+ * <ul>
+ * <li>After merging EBs according to the first rule, the result EB
may not satisfy the second rule. In this case, enforce repartition join for
large relations to satisfy the second rule.</li>
+ * </ul>
+ * <li>Preserved-row relations cannot be broadcasted to avoid duplicated
results. That is, full outer join cannot be executed with broadcast join.</li>
+ * <ul>
+ * <li>Here is brief backgrounds for this rule. Data of preserved-row
relations will be appeared in the join result regardless of join conditions. If
multiple tasks execute outer join with broadcasted preserved-row relations,
they emit duplicates results.</li>
+ * <li>Even though a single task can execute outer join when every
input is broadcastable, broadcast join is not allowed if one of input relation
consists of multiple files.</li>
+ * </ul>
+ * </ul>
+ *
+ */
+public class BroadcastJoinRule implements GlobalPlanRewriteRule {
+
+ private BroadcastJoinPlanBuilder planBuilder;
+ private BroadcastJoinPlanFinalizer planFinalizer;
+
+ @Override
+ public String getName() {
+ return "BroadcastJoinRule";
+ }
+
+ @Override
+ public boolean isEligible(OverridableConf queryContext, MasterPlan plan)
{
+ if (queryContext.getBool(SessionVars.TEST_BROADCAST_JOIN_ENABLED)) {
+ for (LogicalPlan.QueryBlock block :
plan.getLogicalPlan().getQueryBlocks()) {
+ if (block.hasNode(NodeType.JOIN)) {
+ long broadcastSizeThreshold =
queryContext.getLong(SessionVars.BROADCAST_TABLE_SIZE_LIMIT);
+ if (broadcastSizeThreshold > 0) {
+ GlobalPlanRewriteUtil.ParentFinder parentFinder = new
GlobalPlanRewriteUtil.ParentFinder();
+ RelationSizeComparator relSizeComparator = new
RelationSizeComparator();
+ planBuilder = new BroadcastJoinPlanBuilder(plan,
relSizeComparator, parentFinder, broadcastSizeThreshold);
+ planFinalizer = new BroadcastJoinPlanFinalizer(plan,
relSizeComparator);
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public MasterPlan rewrite(MasterPlan plan) throws PlanningException{
+ plan.accept(plan.getRoot().getId(), planBuilder);
+ plan.accept(plan.getRoot().getId(), planFinalizer);
+ return plan;
+ }
+
+ private static class RelationSizeComparator implements
Comparator<ScanNode> {
+
+ @Override
+ public int compare(ScanNode o1, ScanNode o2) {
+ return (int) (GlobalPlanRewriteUtil.getTableVolume(o1) -
GlobalPlanRewriteUtil.getTableVolume(o2));
--- End diff --
Improved to consider overflow.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---