|
| 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 org.apache.spark.annotation.DeveloperApi |
| 21 | +import org.apache.spark.sql.Row |
| 22 | +import org.apache.spark.sql.catalyst.expressions._ |
| 23 | +import org.apache.spark.sql.catalyst.plans._ |
| 24 | +import org.apache.spark.sql.catalyst.plans.physical.{ClusteredOrderedDistribution, Partitioning} |
| 25 | +import org.apache.spark.sql.execution.{BinaryNode, SparkPlan} |
| 26 | +import org.apache.spark.util.collection.CompactBuffer |
| 27 | + |
| 28 | +/** |
| 29 | + * :: DeveloperApi :: |
| 30 | + * Performs an sort merge join of two child relations. |
| 31 | + */ |
| 32 | +@DeveloperApi |
| 33 | +case class SortMergeJoin( |
| 34 | + leftKeys: Seq[Expression], |
| 35 | + rightKeys: Seq[Expression], |
| 36 | + joinType: JoinType, |
| 37 | + left: SparkPlan, |
| 38 | + right: SparkPlan) extends BinaryNode { |
| 39 | + |
| 40 | + override def output: Seq[Attribute] = left.output ++ right.output |
| 41 | + |
| 42 | + override def outputPartitioning: Partitioning = left.outputPartitioning |
| 43 | + |
| 44 | + override def requiredChildDistribution: Seq[ClusteredOrderedDistribution] = |
| 45 | + ClusteredOrderedDistribution(leftKeys) :: ClusteredOrderedDistribution(rightKeys) :: Nil |
| 46 | + |
| 47 | + private val orders: Seq[SortOrder] = leftKeys.map(s => SortOrder(s, Ascending)) |
| 48 | + private val ordering: RowOrdering = new RowOrdering(orders, left.output) |
| 49 | + |
| 50 | + @transient protected lazy val leftKeyGenerator = newProjection(leftKeys, left.output) |
| 51 | + @transient protected lazy val rightKeyGenerator = newProjection(rightKeys, right.output) |
| 52 | + |
| 53 | + override def execute() = { |
| 54 | + val leftResults = left.execute().map(_.copy()) |
| 55 | + val rightResults = right.execute().map(_.copy()) |
| 56 | + |
| 57 | + leftResults.zipPartitions(rightResults) { (leftIter, rightIter) => |
| 58 | + new Iterator[Row] { |
| 59 | + // Mutable per row objects. |
| 60 | + private[this] val joinRow = new JoinedRow5 |
| 61 | + private[this] var leftElement: Row = _ |
| 62 | + private[this] var rightElement: Row = _ |
| 63 | + private[this] var leftKey: Row = _ |
| 64 | + private[this] var rightKey: Row = _ |
| 65 | + private[this] var read: Boolean = false |
| 66 | + private[this] var currentlMatches: CompactBuffer[Row] = _ |
| 67 | + private[this] var currentrMatches: CompactBuffer[Row] = _ |
| 68 | + private[this] var currentlPosition: Int = -1 |
| 69 | + private[this] var currentrPosition: Int = -1 |
| 70 | + |
| 71 | + override final def hasNext: Boolean = |
| 72 | + (currentlPosition != -1 && currentlPosition < currentlMatches.size) || |
| 73 | + (leftIter.hasNext && rightIter.hasNext && nextMatchingPair) |
| 74 | + |
| 75 | + override final def next(): Row = { |
| 76 | + val joinedRow = |
| 77 | + joinRow(currentlMatches(currentlPosition), currentrMatches(currentrPosition)) |
| 78 | + currentrPosition += 1 |
| 79 | + if (currentrPosition >= currentrMatches.size) { |
| 80 | + currentlPosition += 1 |
| 81 | + currentrPosition = 0 |
| 82 | + } |
| 83 | + joinedRow |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Searches the left/right iterator for the next rows that matches. |
| 88 | + * |
| 89 | + * @return true if the search is successful, and false if the left/right iterator runs out |
| 90 | + * of tuples. |
| 91 | + */ |
| 92 | + private def nextMatchingPair(): Boolean = { |
| 93 | + currentlPosition = -1 |
| 94 | + currentlMatches = null |
| 95 | + if (rightElement == null) { |
| 96 | + rightElement = rightIter.next() |
| 97 | + rightKey = rightKeyGenerator(rightElement) |
| 98 | + } |
| 99 | + while (currentlMatches == null && leftIter.hasNext) { |
| 100 | + if (!read) { |
| 101 | + leftElement = leftIter.next() |
| 102 | + leftKey = leftKeyGenerator(leftElement) |
| 103 | + } |
| 104 | + while (ordering.compare(leftKey, rightKey) > 0 && rightIter.hasNext) { |
| 105 | + rightElement = rightIter.next() |
| 106 | + rightKey = rightKeyGenerator(rightElement) |
| 107 | + } |
| 108 | + currentrMatches = new CompactBuffer[Row]() |
| 109 | + while (ordering.compare(leftKey, rightKey) == 0 && rightIter.hasNext) { |
| 110 | + currentrMatches += rightElement |
| 111 | + rightElement = rightIter.next() |
| 112 | + rightKey = rightKeyGenerator(rightElement) |
| 113 | + } |
| 114 | + if (ordering.compare(leftKey, rightKey) == 0) { |
| 115 | + currentrMatches += rightElement |
| 116 | + } |
| 117 | + if (currentrMatches.size > 0) { |
| 118 | + // there exists rows match in right table, should search left table |
| 119 | + currentlMatches = new CompactBuffer[Row]() |
| 120 | + val leftMatch = leftKey.copy() |
| 121 | + while (ordering.compare(leftKey, leftMatch) == 0 && leftIter.hasNext) { |
| 122 | + currentlMatches += leftElement |
| 123 | + leftElement = leftIter.next() |
| 124 | + leftKey = leftKeyGenerator(leftElement) |
| 125 | + } |
| 126 | + if (ordering.compare(leftKey, leftMatch) == 0) { |
| 127 | + currentlMatches += leftElement |
| 128 | + } else { |
| 129 | + read = true |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (currentlMatches == null) { |
| 135 | + false |
| 136 | + } else { |
| 137 | + currentlPosition = 0 |
| 138 | + currentrPosition = 0 |
| 139 | + true |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments