andygrove commented on code in PR #5051:
URL: https://github.com/apache/datafusion-comet/pull/5051#discussion_r3693196569


##########
spark/src/main/scala/org/apache/spark/sql/comet/execution/arrow/CometArrowConverters.scala:
##########
@@ -74,4 +76,52 @@ object CometArrowConverters extends Logging {
       }
     }
   }
+
+  /**
+   * Copy `numRows` rows starting at `startRow` from a Spark `ColumnarBatch` 
into `root`.
+   *
+   * Spark's `ColumnVector` implementations do not expose Arrow buffers, so 
values are necessarily
+   * copied element-wise. Shared by [[SparkColumnarArrowReader]], which slices 
into a stable root,
+   * and [[columnarBatchToArrowBatch]], which fills a fresh one.
+   */
+  private[arrow] def writeColumns(
+      root: VectorSchemaRoot,
+      batch: ColumnarBatch,
+      startRow: Int,
+      numRows: Int): Unit = {
+    val writer = ArrowWriter.create(root)
+    var col = 0
+    while (col < batch.numCols()) {
+      val column = batch.column(col)
+      val columnArray = new ColumnarArray(column, startRow, numRows)
+      if (column.hasNull) {
+        writer.writeCol(columnArray, col)
+      } else {
+        writer.writeColNoNull(columnArray, col)
+      }
+      col += 1
+    }
+    writer.finish()
+    // ArrowWriter derives the root row count from its per-column writes, so a 
zero-column input
+    // batch (Spark's count-from-metadata scan: numRows > 0, numCols == 0) 
would otherwise produce
+    // a root with rowCount == 0 and silently drop the rows.
+    root.setRowCount(numRows)
+  }
+
+  /**
+   * Copy a Spark `ColumnarBatch` whose columns are not Arrow-backed (e.g.
+   * `On/OffHeapColumnVector` from Spark's vectorized Parquet reader, or a 
third-party connector's
+   * vectors) into a freshly allocated Arrow `ColumnarBatch` of `CometVector`s.
+   *
+   * The input batch is not consumed or closed; the caller owns the returned 
batch and must close
+   * it.
+   */
+  def columnarBatchToArrowBatch(
+      batch: ColumnarBatch,
+      arrowSchema: Schema,
+      allocator: BufferAllocator): ColumnarBatch = {
+    val root = VectorSchemaRoot.create(arrowSchema, allocator)

Review Comment:
   Good catch, fixed in 1367665bd. Guarded with `try`/`catch NonFatal` 
releasing the root before rethrowing, as you suggested.
   
   You are right that the normal path cannot hit it, but `writeColumns` walks 
arbitrary `ColumnVector` implementations, so a badly-behaved external vector is 
exactly the case that would throw here — and that is the path this PR added.
   
   `rowToArrowBatchIter` just above has the same shape (root allocated, then 
`writer.write(rowIter.next())` before `rootAsBatch` takes ownership), so I gave 
it the same guard rather than leaving one of the two fixed.



##########
spark/src/main/scala/org/apache/spark/sql/comet/util/Utils.scala:
##########
@@ -397,6 +397,15 @@ object Utils extends CometTypeShim with Logging {
     }
   }
 
+  /**
+   * Whether every column in `batch` satisfies [[getBatchFieldVectors]]'s 
precondition, i.e. is an
+   * Arrow-backed `CometVector`. Callers that may receive batches from a plan 
they did not build
+   * (e.g. Comet's cache serializer, which Spark hands the cached plan's 
columnar output) use this
+   * to convert foreign vectors to Arrow instead of tripping the exception 
below.
+   */
+  def isArrowBacked(batch: ColumnarBatch): Boolean =

Review Comment:
   Yes — and this is the case the PR exists to handle.
   
   `isArrowBacked` is false whenever Comet is handed a columnar batch from a 
plan it did not build. The concrete reproduction is the one from the report on 
this PR: with `spark.comet.scan.enabled=false`, Spark's own vectorized Parquet 
reader produces `OnHeapColumnVector`s, `supportsColumnarInput` returns true so 
`InMemoryRelation.apply` strips the `ColumnarToRow` above that scan, and the 
cache serializer receives non-Comet vectors. Before the converter path those 
batches hit the exception in `getBatchFieldVectors`.
   
   It is also false for any external columnar source — an Iceberg or other 
connector's `ColumnVector` implementation — which is why the conversion is 
written against the `ColumnVector` contract rather than against specific 
classes.
   
   The two tests at the end of `CometInMemoryCacheSuite` ("cache a Spark 
columnar plan whose vectors are not Arrow-backed" and the complex-types 
variant) cover exactly this, so the false branch is exercised.



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