spark-test commented on code in PR #60476:
URL: https://github.com/apache/spark/pull/60476#discussion_r4571380223


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoinExec.scala:
##########
@@ -98,33 +98,27 @@ case class ShuffledHashJoinExec private (
   }
 
   /**
-   * This is called by generated Java class, should be public.
+   * Creates the builder of the build-side [[HashedRelation]]. It holds only 
what building the
+   * relation needs, so the generated code can reference it instead of this 
plan node, which would
+   * serialize the child subtrees into every task.
    */
-  def buildHashedRelation(iter: Iterator[InternalRow]): HashedRelation = {
-    val buildDataSize = longMetric("buildDataSize")
-    val buildTime = longMetric("buildTime")
-    val start = System.nanoTime()
-    val context = TaskContext.get()
-    val relation = HashedRelation(
-      iter,
+  private def createRelationBuilder(): ShuffledHashJoinRelationBuilder = {
+    new ShuffledHashJoinRelationBuilder(
       buildBoundKeys,
-      taskMemoryManager = context.taskMemoryManager(),
       // build-side or full outer join needs support for NULL key in 
HashedRelation.
       allowsNullKey = joinType == FullOuter ||
         (joinType == LeftOuter && buildSide == BuildLeft) ||
         (joinType == RightOuter && buildSide == BuildRight),
       ignoresDuplicatedKey = ignoreDuplicatedKey)
-    buildTime += NANOSECONDS.toMillis(System.nanoTime() - start)
-    buildDataSize += relation.estimatedSize
-    // This relation is usually used until the end of task.
-    context.addTaskCompletionListener[Unit](_ => relation.close())
-    relation
   }
 
   protected override def doExecute(): RDD[InternalRow] = {
     val numOutputRows = longMetric("numOutputRows")
+    val buildTime = longMetric("buildTime")
+    val buildDataSize = longMetric("buildDataSize")
+    val relationBuilder = createRelationBuilder()
     streamedPlan.execute().zipPartitions(buildPlan.execute()) { (streamIter, 
buildIter) =>
-      val hashed = buildHashedRelation(buildIter)
+      val hashed = relationBuilder.build(buildIter, buildTime, buildDataSize)

Review Comment:
   **Nit (P3):** This call now passes the two build metrics by position. Both 
parameters of `build` are `SQLMetric`, so swapping them still compiles. Before 
this PR, both execution paths went through `buildHashedRelation`, which looked 
the metrics up by name, and the codegen-mode `buildDataSize` assertion in 
`SQLMetricsSuite` ("SPARK-32629: ShuffledHashJoin(full outer) metrics") 
exercised that shared lookup. Nothing covers this interpreted call site now:
   
   - that test runs with whole-stage codegen on;
   - the codegen-off `ShuffledHashJoin` metric tests check only "number of 
output rows";
   - no test reads `buildTime` at all, so a wrong `buildTime` argument in the 
generated call at line 377 also goes unnoticed.
   
   From reading the code, `relationBuilder.build(buildIter, buildDataSize, 
buildTime)` here would pass every suite. Joins that run outside whole-stage 
codegen would then report wrong "time to build hash map" and "data size of 
build side" values in the SQL UI. Those are joins with whole-stage codegen 
disabled, with the full-outer or build-side-outer shuffled hash join codegen 
flag off, with too many fields for codegen, or that hit a codegen fallback. The 
current wiring is correct and query results are unaffected; this is only a 
coverage gap.
   
   Checking only that the build-side size is positive would not catch a swap. 
After a swap, the size metric receives the build durations in milliseconds, 
which can be nonzero, and on this path full outer and build-side outer joins 
also add their matched-row tracker's size to it. Instead, run an inner or 
stream-side outer shuffled hash join, such as the existing right outer case 
that builds the left side, with whole-stage codegen on and off, and read the 
join node's metrics after each run. Assert that `buildDataSize` is positive and 
equal in both runs, since both paths build the same relation from the same 
rows. Assert that `buildTime` was updated in both runs with `!metric.isZero`: a 
timing metric starts at -1, and a fast build can record 0 ms.
   
   **Verification:**
   - **Behavior:** For the chosen inner or stream-side outer shuffled hash 
join, the runs with whole-stage codegen enabled and disabled report the same 
positive build-side data size, and the time-to-build-hash-map metric left its 
initial value in both runs, so both metrics are checked on both execution paths.
   - **Regression:** Temporarily swapping the build-time and build-size 
arguments on the interpreted path, or passing the build-size metric as the 
build time in the generated relation-building call, makes the extended metric 
test fail, showing that it catches the wiring mistakes that currently pass 
every suite.



-- 
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]

Reply via email to