diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConvertJoinMapJoin.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConvertJoinMapJoin.java index 98fec77010..21d0053611 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConvertJoinMapJoin.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConvertJoinMapJoin.java @@ -888,19 +888,31 @@ private void removeCycleCreatingSemiJoinOps(MapJoinOperator mapjoinOp, } // Found a semijoin branch. - for (Operator parent : mapjoinOp.getParentOperators()) { - if (!(parent instanceof ReduceSinkOperator)) { - continue; - } + // There can be more than one semijoin branch coming from the parent + // GBY Operator of the RS Operator. + Operator parentGB = op.getParentOperators().get(0); + for (Operator childRS : parentGB.getChildOperators()) { + // Get the RS and TS for this branch + rs = (ReduceSinkOperator) childRS; + ts = parseContext.getRsToSemiJoinBranchInfo().get(rs).getTsOp(); + assert ts != null; + for (Operator parent : mapjoinOp.getParentOperators()) { + if (!(parent instanceof ReduceSinkOperator)) { + continue; + } - Set tsOps = OperatorUtils.findOperatorsUpstream(parent, - TableScanOperator.class); - for (TableScanOperator parentTS : tsOps) { - // If the parent is same as the ts, then we have a cycle. - if (ts == parentTS) { - semiJoinMap.put(rs, ts); - break; + Set tsOps = OperatorUtils.findOperatorsUpstream(parent, + TableScanOperator.class); + boolean found = false; + for (TableScanOperator parentTS : tsOps) { + // If the parent is same as the ts, then we have a cycle. + if (ts == parentTS) { + semiJoinMap.put(rs, ts); + found = true; + break; + } } + if (found) break; } } }