viirya opened a new pull request, #60327: URL: https://github.com/apache/spark/pull/60327
### What changes were proposed in this pull request? This is a follow-up of SPARK-60325 (#59582), as discussed there. `BatchEvalPython` / `ArrowEvalPython` (and their physical nodes) always output `child.output ++ resultAttrs`. Column pruning only narrows their child to the columns used above plus the UDF arguments, so every remaining child column, including each argument, is copied into a `HybridRowQueue` and joined with the result of its row, even when the parent drops it, e.g. `df.select(udf(a))` or `df.select(udf(c1, ..., c8))`. With columnar input (`spark.sql.execution.arrow.pythonUDF.columnarInput.enabled`), the joined rows are also converted back to columnar batches. This PR makes the Python UDF evaluation pass through only the child columns its parent uses, like the Python UDTF nodes do: - `BaseEvalPython` gets `requiredChildOutput`, and its output becomes `requiredChildOutput ++ resultAttrs`. `ExtractPythonUDFs` sets it to `child.output`, and a new case of `ColumnPruning` (which runs again right after `ExtractPythonUDFs`) narrows it to the columns that the `Project` above uses and prunes the child to those and the UDF arguments. - `EvalPythonExec` gets the same field. In the row evaluator shared by `BatchEvalPythonExec` and `ArrowEvalPythonExec`, the queue holds only the pass-through columns, and without any it is not created: the results from Python are the output rows. - In the columnar evaluator of `ArrowEvalPythonExec`, the queue holds only the pass-through columns, and without any, the result batches from Python are returned as they are, instead of being joined by row and converted back. Their vectors are reused for the next batch (`ReusedArrowColumnVector`), which consumers that respect the `ColumnVector` contract handle, e.g. `ColumnarToRowExec`, a stacked `ArrowEvalPythonExec` and the Arrow cache, which copy what they keep. These batches are sliced only by `spark.sql.execution.arrow.maxRecordsPerOutputBatch` / `maxBytesPerOutputBatch` when set, which the reader of Python results applies, rather than by `maxRecordsPerBatch` / `maxBytesPerBatch`. - `InProcessArrowEvalPythonExec` keeps passing through its whole child, and the planner puts a `ProjectExec` above it when the logical node is pruned. Whether the columnar path is used is still fixed when the plan is built (SPARK-60306). ### Why are the changes needed? To avoid copying, buffering and joining columns that are discarded right after the evaluation, and to give the zero-copy output back to the columnar path when nothing is passed through, which SPARK-60325 had to drop for correctness. A local benchmark (`local[1]`, a pandas UDF over 8 string columns, selecting only its result, noop sink, median of 5 runs; the columnar input reads an Arrow-cached relation): | Input | Path | Before | After | |---|---|---|---| | 1M rows, 8 x ~64 B | row | 0.632 s | 0.583 s | | 1M rows, 8 x ~64 B | columnar | 0.509 s | 0.379 s | | 500K rows, 8 x ~256 B | row | 0.573 s | 0.577 s | | 500K rows, 8 x ~256 B | columnar | 0.605 s | 0.458 s | The columnar path gains about 25%. On the row path, the queue is a small part of the cost. ### Does this PR introduce _any_ user-facing change? No, other than the plans: `BatchEvalPython` / `ArrowEvalPython` show the columns they pass through in `EXPLAIN`, like `BatchEvalPythonUDTF` / `ArrowEvalPythonUDTF`, and the columns used only as UDF arguments are no longer in their output. ### How was this patch tested? New tests: - `ExtractPythonUDFsSuite`: the columns that batched, scalar pandas and scalar Arrow iterator UDF evaluations pass through, when only the results are used, with other or the argument columns, through filters, under an aggregate, and for stacked evaluations; and the logical node and its pruned child. - `ArrowColumnarPythonUDFSuite`: without pass-through columns, the output batches are the Python result batches (over reused vectors, not sliced to `maxRecordsPerBatch`), the results are correct with codegen on and off, over fresh and reused input vectors, with column references or expressions as the UDF inputs, with a filter, a projection and a limit above, and every input batch is released; with some pass-through columns; and stacked UDFs without pass-through columns. The test Arrow source gets an opt-in `pruneColumns` option so that the scan stays columnar under the evaluation. - `test_arrow_python_udf_cached.py`: pandas and Arrow iterator UDFs (one returning other batch sizes than its input), a scalar pandas UDF and an Arrow-optimized UDF without pass-through columns, with a filter above; stacked UDFs; caching the results. Also ran `FilterPushdownSuite`, `ColumnPruningSuite`, `ArrowCachedPythonUDFSuite`, `PythonUDFSuite`, `BatchEvalPythonExecSuite`, `ExtractPythonUDFFromLambdaSuite`, `InProcessPythonUDFSuite`, `PythonUDTFSuite`, and `pyspark.sql.tests.arrow.test_arrow_python_udf`, `test_arrow_udf_scalar`, `pyspark.sql.tests.pandas.test_pandas_udf_scalar`, `test_pipelined_udf`, `pyspark.sql.tests.test_udf`, `test_inprocess_udf`. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5) This pull request and its description were written by Isaac. -- 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]
