viirya opened a new pull request, #60545:
URL: https://github.com/apache/spark/pull/60545

   ### What changes were proposed in this pull request?
   
   `InProcessArrowEvalPythonEvaluatorFactory.evaluateBatches` (SPARK-59718) 
interleaves three things in one method: pulling input rows under the iterator's 
lock, building and exporting Arrow batches to Python, and releasing both at 
task completion.
   
   This PR moves the second into a class of its own, 
`InProcessArrowBatchExporter`, which owns the Arrow writer of the current 
batch, the imported result vectors, and the registrations of the UDFs:
   
   ```scala
   private[python] class InProcessArrowBatchExporter(...) extends AutoCloseable 
{
     def startBatch(): Unit          // releases the previous batch and starts 
an empty one
     def write(row: InternalRow): Unit
     def rowCount: Int
     def sizeInBytes: Long
     def evaluate(python: PythonCall): Iterator[InternalRow]  // register 
(first batch), export, invoke, import
     def close(): Unit               // releases the batch, then the 
registrations
   }
   ```
   
   - `evaluate` takes the function that runs each call into Python (`PythonCall 
= (SQLMetric, () => Long) => Unit`). The iterator implements it by releasing 
its lock, so the exporter does not know about the lock or about task completion.
   - The pre-sizing helpers `dataBytes` and `roundedDensity` move to the 
companion object of the exporter.
   - `evaluateBatches` keeps the orchestration: it pulls rows into the queue 
and the exporter until the batch is full, checks that task completion did not 
happen meanwhile, evaluates the batch, and joins the results. 
`IteratorResources.releaseOthers` now calls `exporter.close()`.
   
   The code of the exporter is the code that was inline, so the following do 
not change:
   
   - the release order: the vectors of the batch, then 
`session.release(handles)` only if a registration was attempted (`registered` 
is still set before the first `register`);
   - the order and timing of the metric updates;
   - the cancellation checks: once per input row while filling (in the 
iterator) and before each UDF is invoked (in the exporter);
   - the C Data structs are closed in a `finally` right after each call;
   - the threads that create and release the resources, and the exceptions.
   
   ### Why are the changes needed?
   
   `evaluateBatches` is about 300 lines, and the Arrow batch handling could 
only be exercised through the whole evaluator with a Python interpreter. As a 
class of its own, the batch export has an explicit state (none, filling, 
evaluated) and can be tested without Python.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No.
   
   ### How was this patch tested?
   
   New `InProcessArrowBatchExporterSuite` (8 tests), which uses a session 
without an interpreter: reading the input back, results-only rows, releasing a 
batch in each state, releasing a failed registration, a refused call into 
Python, the cancellation check before a UDF is invoked, and pre-sizing from the 
previous batch. The existing `roundedDensity` test moves there from 
`InProcessPythonRuntimeSuite`.
   
   Existing suites: `InProcessPythonRuntimeSuite`, `InProcessPythonUDFSuite`, 
`InProcessArrowBridgeSuite`, `RowQueueSuite`, `BatchEvalPythonExecSuite`, 
`ArrowCachedPythonUDFSuite`, `ArrowColumnarPythonUDFSuite`, `PythonUDFSuite`, 
and `pyspark.sql.tests.test_inprocess_udf` in CI.
   
   `write`, `rowCount` and `sizeInBytes` are on the per-row path, so I compared 
filling batches through the exporter with the previous inline code in a local 
micro-benchmark without Python (`(long, string)` rows, 10000 rows per batch, 
300 batches, median of 15 runs after warm-up): 46.0 ns/row before, 44.8 ns/row 
after, i.e. no difference beyond noise.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac
   
   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]

Reply via email to