rekbun opened a new pull request, #60759: URL: https://github.com/apache/spark/pull/60759
### What changes were proposed in this pull request? `WholeStageCodegenEvaluatorFactory` adds a codegen pipeline's duration to the `pipelineTime` metric (shown as "duration" on the `WholeStageCodegen` node) when the pipeline's iterator reaches its end. It did so on every `hasNext` call that returned false. `BufferedRowIterator.durationMs()` is the time since the pipeline started, so each extra call after the end added the whole duration again. This PR adds the duration once per iterator, the first time `hasNext` returns false. ### Why are the changes needed? Calling `hasNext` again after it returned false is legal, and several operators do it on every partition of the codegen stage below them: - building a cached relation: the iterator of `DefaultCachedBatchSerializer` (`InMemoryRelation.scala`), consumed by `MemoryStore.putIterator`; - Python UDFs (`BatchEvalPythonExec`), through `GroupedIterator`; - pandas and Arrow UDFs, in `BatchedPythonArrowInput.writeNextBatchToArrowStream`. For these, the stage's duration is counted at least twice per partition. SPARK-52323 reports a stage whose `WholeStageCodegen` duration was 21652 h against 139 h of total task time. #42475 replaced `+=` with `set`. That still raises the value to the time of the latest `hasNext` call, and it keeps only the last pipeline when one task runs several pipelines of the same stage, for example when it reads several partitions through a `coalesce`. Adding once per iterator fixes both. ### Does this PR introduce _any_ user-facing change? Yes. The "duration" metric of `WholeStageCodegen` nodes in the SQL UI no longer counts a pipeline more than once when an operator calls `hasNext` again after the end, as caching and Python UDFs do. ### How was this patch tested? Two tests in `WholeStageCodegenSuite` run a codegen stage through its evaluator factory with a metric of their own: - `hasNext` called three more times after the end leaves the metric unchanged. It fails without this PR (about 150 ms instead of 21 ms) and with #42475's `set` (about 44 ms instead of 21 ms). - Two pipelines that share the metric, as one task reading two partitions does, add up. It fails with `set`. `WholeStageCodegenSuite` and `SQLMetricsSuite` pass (223 tests, 3 ignored as on master), and SQL scalastyle passes. ``` build/sbt 'sql/testOnly org.apache.spark.sql.execution.WholeStageCodegenSuite org.apache.spark.sql.execution.metric.SQLMetricsSuite' 'sql/scalastyle' 'sql/Test/scalastyle' ``` ### Was this patch authored or co-authored using generative AI tooling? Co-authored with Claude Code. -- 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]
