LuciferYang commented on code in PR #59841:
URL: https://github.com/apache/spark/pull/59841#discussion_r4614127723
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -2814,4 +2814,213 @@ class WholeStageCodegenSuite extends SharedSparkSession
}
}
}
+
+ //
---------------------------------------------------------------------------
+ // SPARK-59787: wide join field-based extraction tests
+ //
---------------------------------------------------------------------------
+
+ // Helper: build a DataFrame with `numCols` columns, mixing nullable and
non-nullable types.
+ // Even-indexed columns are non-nullable Long (id + i), odd-indexed are
nullable String
+ // (cast to string, with null when id % 7 == i % 3 to exercise null
propagation).
+ private def wideDF(rows: Int, numCols: Int, prefix: String): DataFrame = {
+ val base = spark.range(rows).toDF("id")
+ (0 until numCols).foldLeft(base) { (df, i) =>
+ if (i % 2 == 0) {
+ // Non-nullable Long
+ df.withColumn(s"${prefix}_c$i", $"id" + i)
+ } else {
+ // Nullable String -- null when id % 7 == i % 3
+ df.withColumn(s"${prefix}_c$i",
+ when($"id" % 7 === lit(i % 3), lit(null: String))
+ .otherwise(concat(lit(s"${prefix}_"), $"id".cast(StringType))))
+ }
+ }
+ }
+
+ // Shared config for SMJ tests: disable broadcast, raise maxFields so
codegen stays on for
+ // wide schemas.
+ private val wideSMJConf = Seq(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.WHOLESTAGE_MAX_NUM_FIELDS.key -> "300"
+ )
+
+ // Columns per side. 120+120 output columns are over the field-based
threshold, and on master
+ // processNext() is ~8.6K bytes, over the 8K JIT limit.
+ private val WIDE_COLS = 120
+
+ // Semi/anti/existence joins only output the streamed side, so only its
columns count towards
+ // the threshold. The buffered side stays narrow, which also checks that its
columns are not
+ // counted.
+ private val WIDE_STREAMED_COLS = 220
+ private val NARROW_COLS = 5
+
+ // Assert that a SortMergeJoinExec is inside a WholeStageCodegenExec in the
plan.
+ // This proves codegen compiled successfully -- a silent fallback would show
+ // SortMergeJoinExec outside WholeStageCodegenExec.
+ private def assertSMJCodegenUsed(df: DataFrame): Unit = {
+ val plan = df.queryExecution.executedPlan
+ val codegenWithSMJ = plan.collect {
+ case ws: WholeStageCodegenExec if
ws.find(_.isInstanceOf[SortMergeJoinExec]).isDefined => ws
+ }
+ assert(codegenWithSMJ.nonEmpty,
+ s"Expected SortMergeJoinExec inside WholeStageCodegenExec, but plan
was:\n$plan")
+ }
+
+ test("SPARK-59787: wide inner SortMergeJoin with mixed nullable columns") {
+ withSQLConf(wideSMJConf: _*) {
+ val left = wideDF(100, WIDE_COLS, "l")
+ val right = wideDF(100, WIDE_COLS, "r")
+ def joined = left.join(right, left("id") === right("id"))
+ assertSMJCodegenUsed(joined)
+ checkAnswer(joined, withoutWholeStage(joined))
+ }
+ }
+
+ test("SPARK-59787: wide left outer SortMergeJoin with null output rows") {
+ withSQLConf(wideSMJConf: _*) {
+ val left = wideDF(100, WIDE_COLS, "l")
+ val right = wideDF(100, WIDE_COLS, "r").where($"id" >= 50)
+ def joined = left.join(right, left("id") === right("id"), "left_outer")
+ assertSMJCodegenUsed(joined)
+ checkAnswer(joined, withoutWholeStage(joined))
+ val nullCount = joined.where(right("id").isNull).count()
+ assert(nullCount == 50, s"Expected 50 null-output rows, got $nullCount")
+ }
+ }
+
+ test("SPARK-59787: wide right outer SortMergeJoin with null output rows") {
+ withSQLConf(wideSMJConf: _*) {
+ val left = wideDF(100, WIDE_COLS, "l").where($"id" >= 50)
+ val right = wideDF(100, WIDE_COLS, "r")
+ def joined = left.join(right, left("id") === right("id"), "right_outer")
+ assertSMJCodegenUsed(joined)
+ checkAnswer(joined, withoutWholeStage(joined))
+ val nullCount = joined.where(left("id").isNull).count()
+ assert(nullCount == 50, s"Expected 50 null-output rows, got $nullCount")
+ }
+ }
+
+ test("SPARK-59787: wide left semi SortMergeJoin") {
+ withSQLConf(wideSMJConf: _*) {
+ val left = wideDF(100, WIDE_STREAMED_COLS, "l")
+ val right = wideDF(100, NARROW_COLS, "r").where($"id" >= 50)
+ def joined = left.join(right, left("id") === right("id"), "left_semi")
+ assertSMJCodegenUsed(joined)
+ checkAnswer(joined, withoutWholeStage(joined))
+ assert(joined.count() == 50)
+ }
+ }
+
+ test("SPARK-59787: wide left anti SortMergeJoin") {
+ withSQLConf(wideSMJConf: _*) {
+ val left = wideDF(100, WIDE_STREAMED_COLS, "l")
+ val right = wideDF(100, NARROW_COLS, "r").where($"id" >= 50)
+ def joined = left.join(right, left("id") === right("id"), "left_anti")
+ assertSMJCodegenUsed(joined)
+ checkAnswer(joined, withoutWholeStage(joined))
+ assert(joined.count() == 50)
+ }
+ }
+
+ test("SPARK-59787: wide existence SortMergeJoin") {
Review Comment:
This test doesn't reach the field path: in its plan the SMJ's streamed child
is only `id`, and the 220 columns are computed above the join, so it runs the
local path and passes with both main files reverted to master. A cross-side
condition on `l_c0` doesn't change that. In my runs, a conjunct that reads only
the streamed side, such as `wide_left.l_c2 > 40`, keeps the wide columns below
the join, and existence then takes the field path.
Could you add an existence test with such a conjunct, a LeftSemi test with
your cross-side condition, and one with
`spark.sql.join.splitStreamedSideJoinCondition` on, which none of the new tests
sets? The guard needs a hoistable conjunct such as `l_c2 > 40` (`%` isn't
hoisted) next to the cross-side one. Each test should check from the generated
code that it takes the field path, and the guard test that the guard is there.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala:
##########
@@ -497,11 +562,33 @@ case class SortMergeJoinExec(
val (findNextJoinRowsFuncName, streamedRow, matches) = genScanner(ctx)
- // Create variables for row from both sides.
- val (streamedVars, streamedVarDecl) = createStreamedVars(ctx, streamedRow)
val bufferedRow = ctx.freshName("bufferedRow")
val setDefaultValue = joinType == LeftOuter || joinType == RightOuter
- val bufferedVars = genOneSideJoinVars(ctx, bufferedRow, bufferedPlan,
setDefaultValue)
+
+ // Use field-based extraction only when the join's output would push
processNext() past the
+ // JVM's 8K JIT limit. Only output columns count: semi/anti/existence
don't emit buffered ones.
+ // The estimate is linear, but local variable slots past 255 need wider
instructions, so keep
+ // 20% headroom.
+ val outputColumns = joinType match {
+ case LeftExistence(_) => streamedPlan.output
+ case _ => streamedPlan.output ++ bufferedPlan.output
+ }
+ val useFieldBasedExtraction =
+ SortMergeJoinExec.estimatedProcessNextBytes(outputColumns) * 6 / 5 >
Review Comment:
The new estimate still switches at the wrong width for some column types.
Measured with `maxFields=1000`: with the tests' column mix, the 20% headroom
makes the gate switch at 154 columns, where master's `processNext()` is 6,373
bytes, and master only passes 8000 at about 190 columns. Non-nullable strings
cost about 27 bytes per column on master rather than 50 (3,364 at 122 columns,
5,008 at 182), so they switch at 132 columns, far below the limit. It also
misses the other way: a left outer join with 150 mixed columns is 8,243 bytes
on master and stays inline, because an outer join costs more per column than
the inner join the constants came from.
So I'd go with the `chooseCode` hook from your last comment, which decides
on the compiled size and needs none of these constants. I answered your three
questions in the review body.
--
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]