-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-32730][SQL] Improve LeftSemi and Existence SortMergeJoin right side buffering #29572
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
Changes from 2 commits
1a49356
acc6646
037b876
3937e4c
5cf3ab3
f699118
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,7 +146,7 @@ case class SortMergeJoinExec( | |
| case _: InnerLike => | ||
| new RowIterator { | ||
| private[this] var currentLeftRow: InternalRow = _ | ||
| private[this] var currentRightMatches: ExternalAppendOnlyUnsafeRowArray = _ | ||
| private[this] var currentRightMatches: AppendOnlyUnsafeRowArray = _ | ||
| private[this] var rightMatchesIterator: Iterator[UnsafeRow] = null | ||
| private[this] val smjScanner = new SortMergeJoinScanner( | ||
| createLeftKeyGenerator(), | ||
|
|
@@ -156,7 +156,8 @@ case class SortMergeJoinExec( | |
| RowIterator.fromScala(rightIter), | ||
| inMemoryThreshold, | ||
| spillThreshold, | ||
| cleanupResources | ||
| cleanupResources, | ||
| false | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please avoid the unnecessary changes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, reverted them. |
||
| private[this] val joinRow = new JoinedRow | ||
|
|
||
|
|
@@ -201,7 +202,8 @@ case class SortMergeJoinExec( | |
| bufferedIter = RowIterator.fromScala(rightIter), | ||
| inMemoryThreshold, | ||
| spillThreshold, | ||
| cleanupResources | ||
| cleanupResources, | ||
| false | ||
| ) | ||
| val rightNullRow = new GenericInternalRow(right.output.length) | ||
| new LeftOuterIterator( | ||
|
|
@@ -216,7 +218,8 @@ case class SortMergeJoinExec( | |
| bufferedIter = RowIterator.fromScala(leftIter), | ||
| inMemoryThreshold, | ||
| spillThreshold, | ||
| cleanupResources | ||
| cleanupResources, | ||
| false | ||
| ) | ||
| val leftNullRow = new GenericInternalRow(left.output.length) | ||
| new RightOuterIterator( | ||
|
|
@@ -251,7 +254,8 @@ case class SortMergeJoinExec( | |
| RowIterator.fromScala(rightIter), | ||
| inMemoryThreshold, | ||
| spillThreshold, | ||
| cleanupResources | ||
| cleanupResources, | ||
| condition.isEmpty | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @peter-toth !
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @juliuszsompolski, I think you are right. Shall I open a follow-up PR or a different ticket?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a followup PR is fine.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opened #29727 |
||
| ) | ||
| private[this] val joinRow = new JoinedRow | ||
|
|
||
|
|
@@ -287,7 +291,8 @@ case class SortMergeJoinExec( | |
| RowIterator.fromScala(rightIter), | ||
| inMemoryThreshold, | ||
| spillThreshold, | ||
| cleanupResources | ||
| cleanupResources, | ||
| false | ||
| ) | ||
| private[this] val joinRow = new JoinedRow | ||
|
|
||
|
|
@@ -330,7 +335,8 @@ case class SortMergeJoinExec( | |
| RowIterator.fromScala(rightIter), | ||
| inMemoryThreshold, | ||
| spillThreshold, | ||
| cleanupResources | ||
| cleanupResources, | ||
| condition.isEmpty | ||
| ) | ||
| private[this] val joinRow = new JoinedRow | ||
|
|
||
|
|
@@ -662,7 +668,8 @@ private[joins] class SortMergeJoinScanner( | |
| bufferedIter: RowIterator, | ||
| inMemoryThreshold: Int, | ||
| spillThreshold: Int, | ||
| eagerCleanupResources: () => Unit) { | ||
| eagerCleanupResources: () => Unit, | ||
| bufferFirstOnly: Boolean) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, fixed. |
||
| private[this] var streamedRow: InternalRow = _ | ||
| private[this] var streamedRowKey: InternalRow = _ | ||
| private[this] var bufferedRow: InternalRow = _ | ||
|
|
@@ -673,8 +680,29 @@ private[joins] class SortMergeJoinScanner( | |
| */ | ||
| private[this] var matchJoinKey: InternalRow = _ | ||
| /** Buffered rows from the buffered side of the join. This is empty if there are no matches. */ | ||
| private[this] val bufferedMatches = | ||
| private[this] val bufferedMatches: AppendOnlyUnsafeRowArray = if (bufferFirstOnly) { | ||
| new AppendOnlyUnsafeRowArray { | ||
| var buffer: UnsafeRow = null | ||
|
|
||
| override def clear(): Unit = { | ||
| buffer = null | ||
| } | ||
|
|
||
| override def add(row: UnsafeRow): Unit = { | ||
| assert(buffer == null) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you saying that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, its threshold parameters do work as expected. Just
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did you test it if |
||
|
|
||
| buffer = row | ||
| } | ||
|
|
||
| override def isEmpty: Boolean = buffer == null | ||
|
|
||
| override def length: Int = if (buffer == null) 0 else 1 | ||
|
|
||
| override def generateIterator(): Iterator[UnsafeRow] = Iterator(buffer) | ||
| } | ||
| } else { | ||
| new ExternalAppendOnlyUnsafeRowArray(inMemoryThreshold, spillThreshold) | ||
| } | ||
|
|
||
| // Initialization (note: do _not_ want to advance streamed here). | ||
| advancedBufferedToRowWithNullFreeJoinKey() | ||
|
|
@@ -683,7 +711,7 @@ private[joins] class SortMergeJoinScanner( | |
|
|
||
| def getStreamedRow: InternalRow = streamedRow | ||
|
|
||
| def getBufferedMatches: ExternalAppendOnlyUnsafeRowArray = bufferedMatches | ||
| def getBufferedMatches: AppendOnlyUnsafeRowArray = bufferedMatches | ||
|
|
||
| /** | ||
| * Advances both input iterators, stopping when we have found rows with matching join keys. If no | ||
|
|
@@ -834,7 +862,9 @@ private[joins] class SortMergeJoinScanner( | |
| matchJoinKey = streamedRowKey.copy() | ||
| bufferedMatches.clear() | ||
| do { | ||
| bufferedMatches.add(bufferedRow.asInstanceOf[UnsafeRow]) | ||
| if (!bufferFirstOnly || bufferedMatches.isEmpty) { | ||
| bufferedMatches.add(bufferedRow.asInstanceOf[UnsafeRow]) | ||
| } | ||
| advancedBufferedToRowWithNullFreeJoinKey() | ||
| } while (bufferedRow != null && keyOrdering.compare(streamedRowKey, bufferedRowKey) == 0) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -749,6 +749,15 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan | |
| ) | ||
| } | ||
|
|
||
| // LEFT SEMI JOIN without bound condition does not use [[ExternalAppendOnlyUnsafeRowArray]] | ||
| // so should not cause any spill | ||
| assertNotSpilled(sparkContext, "left semi join") { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this fix this UT fails. |
||
| checkAnswer( | ||
| sql("SELECT * FROM testData LEFT SEMI JOIN testData2 ON key = a WHERE key = 2"), | ||
| Row(2, "2") :: Nil | ||
| ) | ||
| } | ||
|
|
||
| val expected = new ListBuffer[Row]() | ||
| expected.append( | ||
| Row(1, "1", 1, 1), Row(1, "1", 1, 2), | ||
|
|
||
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.
For core component like this, I remember we rarely change its inheritance. It is easily to have performance regression.
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.
All right, in that case let's drop that new trait and stick to the important part.