HyukjinKwon commented on code in PR #59933:
URL: https://github.com/apache/spark/pull/59933#discussion_r4469613808


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/WriteToDataSourceV2Exec.scala:
##########
@@ -873,6 +942,34 @@ object DataWritingSparkTask extends 
WritingSparkTask[DataWriter[InternalRow]] {
       writer: DataWriter[InternalRow], iter: java.util.Iterator[InternalRow]): 
Unit = {
     writer.writeAll(iter)
   }
+
+  override def runColumnar(
+      writerFactory: DataWriterFactory,
+      context: TaskContext,
+      iter: Iterator[ColumnarBatch],
+      useCommitCoordinator: Boolean,
+      customMetrics: Map[String, SQLMetric]): DataWritingSparkTaskResult = {
+    val dataWriter = writerFactory.createWriter(context.partitionId(), 
context.taskAttemptId())
+    writeAndCommit(dataWriter, context, useCommitCoordinator, customMetrics) {
+      val columnarWriter = dataWriter match {
+        case writer: ColumnarInputDataWriter => writer
+        case writer => throw SparkException.internalError(
+          s"Expected a data writer of columnar batches, but got 
${writer.getClass.getName}.")
+      }
+      var numRows = 0L
+      iter.foreach { batch =>
+        // Releases the batch once it is written, as ColumnarToRowExec does 
once it returned its
+        // rows: a producer of new column vectors for each batch leaves them 
to the consumer.
+        Utils.tryWithSafeFinally {
+          columnarWriter.writeBatch(batch)
+          numRows += batch.numRows()
+        }(batch.closeIfFreeable())

Review Comment:
   Added in 7ee5eb7: "count the rows and release each batch of a columnar 
write" writes a scan whose Arrow batches have new vectors for each batch, which 
the consumer releases, as with a connector that returns plain 
`ArrowColumnVector`s. It checks that each batch is released exactly once and 
that nothing of the scan stays allocated, also when the data writer fails on 
the batch. Without the release in `runColumnar`, it fails (`Set(0) did not 
equal Set(1)`).



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/WriteToDataSourceV2Exec.scala:
##########
@@ -873,6 +942,34 @@ object DataWritingSparkTask extends 
WritingSparkTask[DataWriter[InternalRow]] {
       writer: DataWriter[InternalRow], iter: java.util.Iterator[InternalRow]): 
Unit = {
     writer.writeAll(iter)
   }
+
+  override def runColumnar(
+      writerFactory: DataWriterFactory,
+      context: TaskContext,
+      iter: Iterator[ColumnarBatch],
+      useCommitCoordinator: Boolean,
+      customMetrics: Map[String, SQLMetric]): DataWritingSparkTaskResult = {
+    val dataWriter = writerFactory.createWriter(context.partitionId(), 
context.taskAttemptId())
+    writeAndCommit(dataWriter, context, useCommitCoordinator, customMetrics) {
+      val columnarWriter = dataWriter match {
+        case writer: ColumnarInputDataWriter => writer
+        case writer => throw SparkException.internalError(
+          s"Expected a data writer of columnar batches, but got 
${writer.getClass.getName}.")
+      }
+      var numRows = 0L
+      iter.foreach { batch =>
+        // Releases the batch once it is written, as ColumnarToRowExec does 
once it returned its
+        // rows: a producer of new column vectors for each batch leaves them 
to the consumer.
+        Utils.tryWithSafeFinally {
+          columnarWriter.writeBatch(batch)
+          numRows += batch.numRows()

Review Comment:
   Added in 7ee5eb7: the same test checks that the write node of a columnar 
batch write reports 10 output rows, and "streaming read and write" checks that 
the progress of the micro-batch reports the 5 output rows of the sink.



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