-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-3861][SQL] Avoid rebuilding hash tables for broadcast joins on each partition #2727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a39be8c
[SPARK-3857] Create a join package for various join operators.
rxin a070d44
Fix line length in HashJoin
rxin cbc664c
Rename join -> joins package.
rxin 0c0082b
Fix line length.
rxin 90b58c0
[SPARK-3861] Avoid rebuilding hash tables on each partition
rxin e0ebdd1
Added a test case.
rxin 4b9d0c9
UniqueKeyHashedRelation.get should return null if the value is null.
rxin 18eb214
Merge branch 'SPARK-3861-broadcast-hash' into SPARK-3861-broadcast-ha…
rxin 7fcffb5
Make UniqueKeyHashedRelation private[joins].
rxin 97626a1
Reuse CompactBuffer in UniqueKeyHashedRelation.
rxin 9c7b1a2
Revert "Reuse CompactBuffer in UniqueKeyHashedRelation."
rxin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * 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.spark.sql.execution.joins | ||
|
|
||
| import java.util.{HashMap => JavaHashMap} | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Projection, Row} | ||
| import org.apache.spark.util.collection.CompactBuffer | ||
|
|
||
|
|
||
| /** | ||
| * Interface for a hashed relation by some key. Use [[HashedRelation.apply]] to create a concrete | ||
| * object. | ||
| */ | ||
| private[joins] sealed trait HashedRelation { | ||
| def get(key: Row): CompactBuffer[Row] | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * A general [[HashedRelation]] backed by a hash map that maps the key into a sequence of values. | ||
| */ | ||
| private[joins] final class GeneralHashedRelation(hashTable: JavaHashMap[Row, CompactBuffer[Row]]) | ||
| extends HashedRelation with Serializable { | ||
|
|
||
| override def get(key: Row) = hashTable.get(key) | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * A specialized [[HashedRelation]] that maps key into a single value. This implementation | ||
| * assumes the key is unique. | ||
| */ | ||
| private[joins] final class UniqueKeyHashedRelation(hashTable: JavaHashMap[Row, Row]) | ||
| extends HashedRelation with Serializable { | ||
|
|
||
| override def get(key: Row) = { | ||
| val v = hashTable.get(key) | ||
| if (v eq null) null else CompactBuffer(v) | ||
| } | ||
|
|
||
| def getValue(key: Row): Row = hashTable.get(key) | ||
| } | ||
|
|
||
|
|
||
| // TODO(rxin): a version of [[HashedRelation]] backed by arrays for consecutive integer keys. | ||
|
|
||
|
|
||
| private[joins] object HashedRelation { | ||
|
|
||
| def apply( | ||
| input: Iterator[Row], | ||
| keyGenerator: Projection, | ||
| sizeEstimate: Int = 64): HashedRelation = { | ||
|
|
||
| // TODO: Use Spark's HashMap implementation. | ||
| val hashTable = new JavaHashMap[Row, CompactBuffer[Row]](sizeEstimate) | ||
| var currentRow: Row = null | ||
|
|
||
| // Whether the join key is unique. If the key is unique, we can convert the underlying | ||
| // hash map into one specialized for this. | ||
| var keyIsUnique = true | ||
|
|
||
| // Create a mapping of buildKeys -> rows | ||
| while (input.hasNext) { | ||
| currentRow = input.next() | ||
| val rowKey = keyGenerator(currentRow) | ||
| if (!rowKey.anyNull) { | ||
| val existingMatchList = hashTable.get(rowKey) | ||
| val matchList = if (existingMatchList == null) { | ||
| val newMatchList = new CompactBuffer[Row]() | ||
| hashTable.put(rowKey, newMatchList) | ||
| newMatchList | ||
| } else { | ||
| keyIsUnique = false | ||
| existingMatchList | ||
| } | ||
| matchList += currentRow.copy() | ||
| } | ||
| } | ||
|
|
||
| if (keyIsUnique) { | ||
| val uniqHashTable = new JavaHashMap[Row, Row](hashTable.size) | ||
| val iter = hashTable.entrySet().iterator() | ||
| while (iter.hasNext) { | ||
| val entry = iter.next() | ||
| uniqHashTable.put(entry.getKey, entry.getValue()(0)) | ||
| } | ||
| new UniqueKeyHashedRelation(uniqHashTable) | ||
| } else { | ||
| new GeneralHashedRelation(hashTable) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.spark.sql.execution.joins | ||
|
|
||
| import org.scalatest.FunSuite | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Projection, Row} | ||
| import org.apache.spark.util.collection.CompactBuffer | ||
|
|
||
|
|
||
| class HashedRelationSuite extends FunSuite { | ||
|
|
||
| // Key is simply the record itself | ||
| private val keyProjection = new Projection { | ||
| override def apply(row: Row): Row = row | ||
| } | ||
|
|
||
| test("GeneralHashedRelation") { | ||
| val data = Array(Row(0), Row(1), Row(2), Row(2)) | ||
| val hashed = HashedRelation(data.iterator, keyProjection) | ||
| assert(hashed.isInstanceOf[GeneralHashedRelation]) | ||
|
|
||
| assert(hashed.get(data(0)) == CompactBuffer[Row](data(0))) | ||
| assert(hashed.get(data(1)) == CompactBuffer[Row](data(1))) | ||
| assert(hashed.get(Row(10)) === null) | ||
|
|
||
| val data2 = CompactBuffer[Row](data(2)) | ||
| data2 += data(2) | ||
| assert(hashed.get(data(2)) == data2) | ||
| } | ||
|
|
||
| test("UniqueKeyHashedRelation") { | ||
| val data = Array(Row(0), Row(1), Row(2)) | ||
| val hashed = HashedRelation(data.iterator, keyProjection) | ||
| assert(hashed.isInstanceOf[UniqueKeyHashedRelation]) | ||
|
|
||
| assert(hashed.get(data(0)) == CompactBuffer[Row](data(0))) | ||
| assert(hashed.get(data(1)) == CompactBuffer[Row](data(1))) | ||
| assert(hashed.get(data(2)) == CompactBuffer[Row](data(2))) | ||
| assert(hashed.get(Row(10)) === null) | ||
|
|
||
| val uniqHashed = hashed.asInstanceOf[UniqueKeyHashedRelation] | ||
| assert(uniqHashed.getValue(data(0)) == data(0)) | ||
| assert(uniqHashed.getValue(data(1)) == data(1)) | ||
| assert(uniqHashed.getValue(data(2)) == data(2)) | ||
| assert(uniqHashed.getValue(Row(10)) == null) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will that cause too many
CompactBufferobject created if there are so many duplicated records in stream side with single match in build side? Or theGeneralHashedRelationperforms great enough?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have a new operator that specializes for unique key joins.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I mean for each row in stream side, will create a
CompactBufferinstance if it finds a matched row in build side, this probably too heavy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea. What I meant was we will add a new operator that specializes for unique key joins, and that operator would just call getValue, bypassing the creation of CompactBuffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even so can't we reuse the same compact buffer? Also should the semantic be to return null or an empty buffer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be null, since that's what a normal hashmap would return, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't really a normal hashmap its for joins, and an empty compact buffer seems like a pretty clear way to indicate no matches found. Then you don't have to special case null on the other side. You just join with whatever rows are returned.
Though I guess that doesn't work great with your getValue idea below....