0lai0 commented on code in PR #5051:
URL: https://github.com/apache/datafusion-comet/pull/5051#discussion_r3690964474
##########
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:
Minor Nit: `columnarBatchToArrowBatch` allocates root before
`NativeUtil.rootAsBatch` wraps it.
If `writeColumns` or the wrap throws, root leaks, since the caller only
closes the returned batch.
Consider guarding the body with `try { ... } catch { case NonFatal(e) =>
root.close(); throw e }.
But normal path will not lead to
--
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]