LukaZdravic commented on code in PR #60227:
URL: https://github.com/apache/spark/pull/60227#discussion_r4513344480
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeAsOfJoinExec.scala:
##########
@@ -133,6 +128,383 @@ case class SortMergeAsOfJoinExec(
}
}
+ /** Whether the join reads the right side once with a cursor, instead of
buffering it. */
+ private def cursorScanEnabled: Boolean =
conf.sortMergeAsOfJoinCursorScanEnabled &&
+ SortMergeAsOfJoinScanner.supportsCursorScan(
+ direction, asOfCondition, condition, leftSortExprs, rightSortExprs)
+
+ // Nearest joins keep the interpreted scan, as its distance compare is
harder to generate.
+ override def supportCodegen: Boolean =
+ direction != Nearest &&
conf.getConf(SQLConf.ENABLE_SORT_MERGE_AS_OF_JOIN_CODEGEN)
+
+ override def inputRDDs(): Seq[RDD[InternalRow]] =
+ left.execute() :: right.execute() :: Nil
+
+ // A left row makes at most one output row, so the output row needs no copy.
+ override def needCopyResult: Boolean = false
+
+ /**
+ * Generates the join loop. For each left row, a generated function finds
the best right row,
+ * or null: the cursor scan, or a scan of the buffered right group. The
output columns go
+ * straight to the parent operator.
+ */
+ override protected def doProduce(ctx: CodegenContext): String = {
+ // Inline mutable state since not many join operations in a task
+ val leftInput = ctx.addMutableState("scala.collection.Iterator",
"leftInput",
+ v => s"$v = inputs[0];", forceInline = true)
+ val rightInput = ctx.addMutableState("scala.collection.Iterator",
"rightInput",
+ v => s"$v = inputs[1];", forceInline = true)
+ // The next right row, or null once the right side is done. Reading the
right row after it
+ // can overwrite it, so anything that keeps it longer must copy it.
+ val rightPeek = ctx.addMutableState("UnsafeRow", "rightPeek", forceInline
= true)
+ val nextRightName = ctx.freshName("nextRight")
+ val nextRight = ctx.addNewFunction(nextRightName,
+ s"""
+ |private UnsafeRow $nextRightName() {
+ | return $rightInput.hasNext() ? (UnsafeRow) $rightInput.next() :
null;
+ |}
+ """.stripMargin, inlineToOuterClass = true)
+
+ val (findMatch, initScan) = if (cursorScanEnabled) {
+ genCursorScan(ctx, rightPeek, nextRight)
+ } else {
+ genBufferedScan(ctx, rightInput, rightPeek, nextRight)
+ }
+
+ val leftRow = ctx.freshName("leftRow")
+ val matched = ctx.freshName("matched")
+ val leftVars = genOneSideJoinVars(ctx, leftRow, left, setDefaultValue =
false)
+ val rightVars =
+ genOneSideJoinVars(ctx, matched, right, setDefaultValue = joinType ==
LeftOuter)
+ // An inner join drops a left row with no match. A left outer join keeps
it with nulls.
+ val skipUnmatched = if (joinType == LeftOuter) "" else s"if ($matched ==
null) continue;"
+ val numOutput = metricTerm(ctx, "numOutputRows")
+ val initJoin = ctx.addMutableState(CodeGenerator.JAVA_BOOLEAN, "initJoin")
+ val thisPlan = ctx.addReferenceObj("plan", this)
+
+ s"""
+ |if (!$initJoin) {
+ | $initJoin = true;
+ | $rightPeek = $nextRight();
+ | $initScan
+ |}
+ |while ($leftInput.hasNext()) {
Review Comment:
Fixed, added `canCheckLimitNotReached` and set it to true and checked that
in the while loop.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeAsOfJoinExec.scala:
##########
@@ -133,6 +128,383 @@ case class SortMergeAsOfJoinExec(
}
}
+ /** Whether the join reads the right side once with a cursor, instead of
buffering it. */
+ private def cursorScanEnabled: Boolean =
conf.sortMergeAsOfJoinCursorScanEnabled &&
+ SortMergeAsOfJoinScanner.supportsCursorScan(
+ direction, asOfCondition, condition, leftSortExprs, rightSortExprs)
+
+ // Nearest joins keep the interpreted scan, as its distance compare is
harder to generate.
+ override def supportCodegen: Boolean =
+ direction != Nearest &&
conf.getConf(SQLConf.ENABLE_SORT_MERGE_AS_OF_JOIN_CODEGEN)
+
+ override def inputRDDs(): Seq[RDD[InternalRow]] =
+ left.execute() :: right.execute() :: Nil
+
+ // A left row makes at most one output row, so the output row needs no copy.
+ override def needCopyResult: Boolean = false
+
+ /**
+ * Generates the join loop. For each left row, a generated function finds
the best right row,
+ * or null: the cursor scan, or a scan of the buffered right group. The
output columns go
+ * straight to the parent operator.
+ */
+ override protected def doProduce(ctx: CodegenContext): String = {
+ // Inline mutable state since not many join operations in a task
+ val leftInput = ctx.addMutableState("scala.collection.Iterator",
"leftInput",
+ v => s"$v = inputs[0];", forceInline = true)
+ val rightInput = ctx.addMutableState("scala.collection.Iterator",
"rightInput",
+ v => s"$v = inputs[1];", forceInline = true)
+ // The next right row, or null once the right side is done. Reading the
right row after it
+ // can overwrite it, so anything that keeps it longer must copy it.
+ val rightPeek = ctx.addMutableState("UnsafeRow", "rightPeek", forceInline
= true)
+ val nextRightName = ctx.freshName("nextRight")
+ val nextRight = ctx.addNewFunction(nextRightName,
+ s"""
+ |private UnsafeRow $nextRightName() {
+ | return $rightInput.hasNext() ? (UnsafeRow) $rightInput.next() :
null;
+ |}
+ """.stripMargin, inlineToOuterClass = true)
+
+ val (findMatch, initScan) = if (cursorScanEnabled) {
+ genCursorScan(ctx, rightPeek, nextRight)
+ } else {
+ genBufferedScan(ctx, rightInput, rightPeek, nextRight)
+ }
+
+ val leftRow = ctx.freshName("leftRow")
+ val matched = ctx.freshName("matched")
+ val leftVars = genOneSideJoinVars(ctx, leftRow, left, setDefaultValue =
false)
+ val rightVars =
+ genOneSideJoinVars(ctx, matched, right, setDefaultValue = joinType ==
LeftOuter)
+ // An inner join drops a left row with no match. A left outer join keeps
it with nulls.
+ val skipUnmatched = if (joinType == LeftOuter) "" else s"if ($matched ==
null) continue;"
+ val numOutput = metricTerm(ctx, "numOutputRows")
+ val initJoin = ctx.addMutableState(CodeGenerator.JAVA_BOOLEAN, "initJoin")
+ val thisPlan = ctx.addReferenceObj("plan", this)
+
+ s"""
+ |if (!$initJoin) {
+ | $initJoin = true;
+ | $rightPeek = $nextRight();
+ | $initScan
+ |}
+ |while ($leftInput.hasNext()) {
Review Comment:
Fixed, added `canCheckLimitNotReached` and set it to true and checked that
in the while loop. Added a test for it as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]