Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ case class SortMergeJoinExec(
RowIterator.fromScala(rightIter),
inMemoryThreshold,
spillThreshold,
cleanupResources
cleanupResources,
false
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please avoid the unnecessary changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, reverted them.

private[this] val joinRow = new JoinedRow

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -251,7 +254,8 @@ case class SortMergeJoinExec(
RowIterator.fromScala(rightIter),
inMemoryThreshold,
spillThreshold,
cleanupResources
cleanupResources,
condition.isEmpty

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @peter-toth !
I think this could be also added to LeftAnti join, which is also only interested in the existence of a match and doesn't need to buffer them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a followup PR is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened #29727

)
private[this] val joinRow = new JoinedRow

Expand Down Expand Up @@ -287,7 +291,8 @@ case class SortMergeJoinExec(
RowIterator.fromScala(rightIter),
inMemoryThreshold,
spillThreshold,
cleanupResources
cleanupResources,
false
)
private[this] val joinRow = new JoinedRow

Expand Down Expand Up @@ -330,7 +335,8 @@ case class SortMergeJoinExec(
RowIterator.fromScala(rightIter),
inMemoryThreshold,
spillThreshold,
cleanupResources
cleanupResources,
condition.isEmpty
)
private[this] val joinRow = new JoinedRow

Expand Down Expand Up @@ -662,7 +668,8 @@ private[joins] class SortMergeJoinScanner(
bufferedIter: RowIterator,
inMemoryThreshold: Int,
spillThreshold: Int,
eagerCleanupResources: () => Unit) {
eagerCleanupResources: () => Unit,
bufferFirstOnly: Boolean) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: bufferFirstOnly -> matchedBufferFirstOnly? And, please add @param, too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bufferFirstOnly: Boolean = false to avoid the unnecessary changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 = _
Expand All @@ -673,8 +680,9 @@ 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 =
new ExternalAppendOnlyUnsafeRowArray(inMemoryThreshold, spillThreshold)
private[this] val bufferedMatches: ExternalAppendOnlyUnsafeRowArray =
new ExternalAppendOnlyUnsafeRowArray(if (bufferFirstOnly) 1 else inMemoryThreshold,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this change avoid spilling?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not this change does it, but the one in bufferMatchingRows(). This change just avoids creating a buffer larger than 1.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spillThreshold)

// Initialization (note: do _not_ want to advance streamed here).
advancedBufferedToRowWithNullFreeJoinKey()
Expand Down Expand Up @@ -834,7 +842,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)
}
Expand Down
8 changes: 8 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,14 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan
)
}

// LEFT SEMI JOIN without bound condition does not spill
assertNotSpilled(sparkContext, "left semi join") {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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),
Expand Down