|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution.joins |
| 19 | + |
| 20 | +import scala.collection.mutable.ArrayBuffer |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.expressions.Expression |
| 23 | +import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning} |
| 24 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 25 | +import org.apache.spark.sql.execution.SparkPlan |
| 26 | + |
| 27 | +/** |
| 28 | + * When the physical operators are created for JOIN, the ordering of join keys is based on order |
| 29 | + * in which the join keys appear in the user query. That might not match with the output |
| 30 | + * partitioning of the join node's children (thus leading to extra sort / shuffle being |
| 31 | + * introduced). This rule will change the ordering of the join keys to match with the |
| 32 | + * partitioning of the join nodes' children. |
| 33 | + */ |
| 34 | +class ReorderJoinPredicates extends Rule[SparkPlan] { |
| 35 | + private def reorderJoinKeys( |
| 36 | + leftKeys: Seq[Expression], |
| 37 | + rightKeys: Seq[Expression], |
| 38 | + leftPartitioning: Partitioning, |
| 39 | + rightPartitioning: Partitioning): (Seq[Expression], Seq[Expression]) = { |
| 40 | + |
| 41 | + def reorder( |
| 42 | + expectedOrderOfKeys: Seq[Expression], |
| 43 | + currentOrderOfKeys: Seq[Expression]): (Seq[Expression], Seq[Expression]) = { |
| 44 | + val leftKeysBuffer = ArrayBuffer[Expression]() |
| 45 | + val rightKeysBuffer = ArrayBuffer[Expression]() |
| 46 | + |
| 47 | + expectedOrderOfKeys.foreach(expression => { |
| 48 | + val index = currentOrderOfKeys.indexWhere(e => e.semanticEquals(expression)) |
| 49 | + leftKeysBuffer.append(leftKeys(index)) |
| 50 | + rightKeysBuffer.append(rightKeys(index)) |
| 51 | + }) |
| 52 | + (leftKeysBuffer, rightKeysBuffer) |
| 53 | + } |
| 54 | + |
| 55 | + if (leftKeys.forall(_.deterministic) && rightKeys.forall(_.deterministic)) { |
| 56 | + leftPartitioning match { |
| 57 | + case HashPartitioning(leftExpressions, _) |
| 58 | + if leftExpressions.length == leftKeys.length && |
| 59 | + leftKeys.forall(x => leftExpressions.exists(_.semanticEquals(x))) => |
| 60 | + reorder(leftExpressions, leftKeys) |
| 61 | + |
| 62 | + case _ => rightPartitioning match { |
| 63 | + case HashPartitioning(rightExpressions, _) |
| 64 | + if rightExpressions.length == rightKeys.length && |
| 65 | + rightKeys.forall(x => rightExpressions.exists(_.semanticEquals(x))) => |
| 66 | + reorder(rightExpressions, rightKeys) |
| 67 | + |
| 68 | + case _ => (leftKeys, rightKeys) |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + (leftKeys, rightKeys) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + def apply(plan: SparkPlan): SparkPlan = plan.transformUp { |
| 77 | + case BroadcastHashJoinExec(leftKeys, rightKeys, joinType, buildSide, condition, left, right) => |
| 78 | + val (reorderedLeftKeys, reorderedRightKeys) = |
| 79 | + reorderJoinKeys(leftKeys, rightKeys, left.outputPartitioning, right.outputPartitioning) |
| 80 | + BroadcastHashJoinExec(reorderedLeftKeys, reorderedRightKeys, joinType, buildSide, condition, |
| 81 | + left, right) |
| 82 | + |
| 83 | + case ShuffledHashJoinExec(leftKeys, rightKeys, joinType, buildSide, condition, left, right) => |
| 84 | + val (reorderedLeftKeys, reorderedRightKeys) = |
| 85 | + reorderJoinKeys(leftKeys, rightKeys, left.outputPartitioning, right.outputPartitioning) |
| 86 | + ShuffledHashJoinExec(reorderedLeftKeys, reorderedRightKeys, joinType, buildSide, condition, |
| 87 | + left, right) |
| 88 | + |
| 89 | + case SortMergeJoinExec(leftKeys, rightKeys, joinType, condition, left, right) => |
| 90 | + val (reorderedLeftKeys, reorderedRightKeys) = |
| 91 | + reorderJoinKeys(leftKeys, rightKeys, left.outputPartitioning, right.outputPartitioning) |
| 92 | + SortMergeJoinExec(reorderedLeftKeys, reorderedRightKeys, joinType, condition, left, right) |
| 93 | + } |
| 94 | +} |
0 commit comments