viirya commented on code in PR #59582:
URL: https://github.com/apache/spark/pull/59582#discussion_r4507288164


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/ColumnarArrowEvalPythonEvaluatorFactory.scala:
##########
@@ -200,15 +231,21 @@ private[python] class 
ColumnarArrowEvalPythonEvaluatorFactory(
         outputTypes: Seq[DataType],
         columnIndices: Array[Int]): Iterator[ColumnarBatch] = {
 
+      // In pipelined mode the batches are added in the writer thread, and
+      // polled by the consumer of the output, possibly on another thread.
       val passThruQueue =
-        new ArrayDeque[(Array[ColumnVector], Int)]()
-
-      val bufferedIter = inputIter.map { batch =>
-        val passThruCols = childOutput.indices.map(
-          i => batch.column(i)).toArray
-        passThruQueue.add((passThruCols, batch.numRows()))
-        batch
-      }
+        new ConcurrentLinkedQueue[(Array[ColumnVector], Int)]()
+
+      val bufferedIter = new WrittenBatchReleasingIterator(inputIter, { batch 
=>
+        val passThruCols = passThruColumns(batch)
+        if (canHold(passThruCols)) {
+          passThruQueue.add((passThruCols, batch.numRows()))

Review Comment:
   This goes away in 4f85ae623d5 with Path 1: the results are joined with the 
pass-through columns by row, so iterator UDFs no longer need one result batch 
per input batch.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/ColumnarArrowEvalPythonEvaluatorFactory.scala:
##########
@@ -200,15 +231,21 @@ private[python] class 
ColumnarArrowEvalPythonEvaluatorFactory(
         outputTypes: Seq[DataType],
         columnIndices: Array[Int]): Iterator[ColumnarBatch] = {
 
+      // In pipelined mode the batches are added in the writer thread, and
+      // polled by the consumer of the output, possibly on another thread.
       val passThruQueue =
-        new ArrayDeque[(Array[ColumnVector], Int)]()
-
-      val bufferedIter = inputIter.map { batch =>
-        val passThruCols = childOutput.indices.map(
-          i => batch.column(i)).toArray
-        passThruQueue.add((passThruCols, batch.numRows()))
-        batch
-      }
+        new ConcurrentLinkedQueue[(Array[ColumnVector], Int)]()
+
+      val bufferedIter = new WrittenBatchReleasingIterator(inputIter, { batch 
=>
+        val passThruCols = passThruColumns(batch)
+        if (canHold(passThruCols)) {
+          passThruQueue.add((passThruCols, batch.numRows()))
+          false
+        } else {
+          passThruQueue.add((copyColumns(passThruCols, batch.numRows()), 
batch.numRows()))

Review Comment:
   This goes away in 4f85ae623d5: no copies are kept outside the 
`HybridRowQueue` any more.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/ColumnarArrowEvalPythonEvaluatorFactory.scala:
##########
@@ -262,7 +299,7 @@ private[python] class 
ColumnarArrowEvalPythonEvaluatorFactory(
         childOutput, childOutput)
 
       val resultIter = if (inputColumnIndices.isDefined) {
-        // Path 2: simple column refs, non-Arrow vectors.
+        // Path 2: simple column refs, columns that cannot be held.

Review Comment:
   Fixed in 4f85ae623d5: the two overrides are removed, so the limits apply, 
and "The results of Python are sliced with columnar input" checks the limits of 
the runner.



##########
python/pyspark/sql/tests/arrow/test_arrow_python_udf_cached.py:
##########
@@ -134,8 +134,10 @@ def 
test_udf_on_cached_numeric_column_alongside_unselected_string(self):
             df.unpersist()
 
     def test_udf_on_cached_input_with_output_batch_slicing(self):
-        # The columnar path pairs each result batch with one input batch, so 
the 10-row result
-        # of the single cached batch must not be sliced under the configs 
below.
+        # The 10-row result of the single cached batch is sliced under the 
configs below, and

Review Comment:
   With the slicing enabled in 4f85ae623d5, the results are sliced here. I 
shortened the comment to say that they are paired by row.



##########
sql/catalyst/src/main/java/org/apache/spark/sql/vectorized/ColumnVector.java:
##########
@@ -84,6 +84,34 @@ public void closeIfFreeable() {
     close();
   }
 
+  /**
+   * Returns whether a consumer may keep this column vector after it asks the 
producer of its batch
+   * for the next batch, until the consumer releases it with {@link 
#closeIfFreeable()}. By default,
+   * a column vector may be reused or released by its producer once the next 
batch is requested, so
+   * a consumer has to copy what it needs past that point.
+   * <p>
+   * A producer that returns new vectors for every batch can override this to 
return true, which
+   * promises that:
+   * <ul>
+   *   <li>the producer neither overwrites nor frees this vector when the next 
batch is requested,
+   *   or when the producer is closed, e.g. a {@code PartitionReader} once it 
is exhausted;</li>
+   *   <li>{@link #closeIfFreeable()} frees this vector, and may be called 
from another thread
+   *   than the one that requested its batch, concurrently with the producer 
returning the next
+   *   batches or being closed;</li>
+   *   <li>the producer frees the vectors that the consumer has not released 
when the task
+   *   completes, e.g. when the consumer stops early.</li>
+   * </ul>
+   * A consumer may keep such vectors without accounting for their memory or 
spilling them.
+   * <p>
+   * This applies to the top-level columns of a batch: the children of a 
struct, array or map
+   * column share the lifetime of that column.
+   *
+   * @since 4.4.0
+   */
+  public boolean isRetainable() {

Review Comment:
   Removed in 4f85ae623d5, along with the only consumer.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/ColumnarArrowEvalPythonEvaluatorFactory.scala:
##########
@@ -314,6 +351,17 @@ private[python] class 
ColumnarArrowEvalPythonEvaluatorFactory(
       rowsToColumnarBatches(rowIter, context)
     }
 
+    /** Copies the first `numRows` rows of the pass-through columns into 
on-heap vectors. */
+    private def copyColumns(

Review Comment:
   `copyColumns` is removed in 4f85ae623d5.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -5708,15 +5708,16 @@ object SQLConf {
 
   val ARROW_PYSPARK_UDF_COLUMNAR_INPUT_ENABLED =
     buildConf("spark.sql.execution.arrow.pythonUDF.columnarInput.enabled")
-      .doc("When true, Arrow-based Python UDFs (pandas UDFs) can accept " +
-        "columnar input directly from upstream operators that produce " +
-        "Arrow-backed ColumnarBatch (e.g., DataSource V2 connectors), " +
-        "bypassing the ColumnarToRow and ArrowWriter conversion. " +
-        "This optimization reduces data transfer overhead between " +
-        "the JVM and Python worker processes. It keeps the columns that the 
UDFs pass " +
-        "through until the results of their batch come back from Python, so it 
requires " +
-        "an upstream operator whose batches stay valid until then, which a 
columnar " +
-        "reader does not promise.")
+      .doc("When true, Arrow-based Python UDFs (pandas UDFs) accept columnar 
input " +
+        "from upstream operators that produce ColumnarBatch, and write the UDF 
inputs " +
+        "of Arrow-backed batches to Python directly. The columns that the UDFs 
pass " +
+        "through skip the conversion to rows and back only if all the columns 
of the " +

Review Comment:
   Done in 4f85ae623d5: the conf doc now says that the UDF inputs are written 
directly only if they are plain column references. The migration guide no 
longer describes that condition.



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