Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -30,6 +30,14 @@ import org.apache.spark.sql.execution.ExternalAppendOnlyUnsafeRowArray.DefaultIn
import org.apache.spark.storage.BlockManager
import org.apache.spark.util.collection.unsafe.sort.{UnsafeExternalSorter, UnsafeSorterIterator}

trait AppendOnlyUnsafeRowArray {
def clear()
def add(row: UnsafeRow)
def isEmpty: Boolean
def length: Int
def generateIterator(): Iterator[UnsafeRow]
}

/**
* An append-only array for [[UnsafeRow]]s that strictly keeps content in an in-memory array
* until [[numRowsInMemoryBufferThreshold]] is reached post which it will switch to a mode which
Expand All @@ -50,7 +58,7 @@ private[sql] class ExternalAppendOnlyUnsafeRowArray(
initialSize: Int,
pageSizeBytes: Long,
numRowsInMemoryBufferThreshold: Int,
numRowsSpillThreshold: Int) extends Logging {
numRowsSpillThreshold: Int) extends AppendOnlyUnsafeRowArray with Logging {

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.

For core component like this, I remember we rarely change its inheritance. It is easily to have performance regression.

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.

All right, in that case let's drop that new trait and stick to the important part.


def this(numRowsInMemoryBufferThreshold: Int, numRowsSpillThreshold: Int) {
this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,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)

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.

Are you saying that ExternalAppendOnlyUnsafeRowArray will do spill even if we only add one row?

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.

No, its threshold parameters do work as expected. Just ExternalAppendOnlyUnsafeRowArray looked a bit heavy weight for this case when we want to store only one row. But we can also use new ExternalAppendOnlyUnsafeRowArray(1, 1) for this case.

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 did you test it if ExternalAppendOnlyUnsafeRowArray doesn't spill either?


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()
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 9 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,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") {

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