WeichenXu123 commented on code in PR #44294:
URL: https://github.com/apache/spark/pull/44294#discussion_r1437960948


##########
sql/core/src/main/scala/org/apache/spark/sql/api/python/ChunkReadUtils.scala:
##########
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.api.python
+
+import java.io.ByteArrayOutputStream
+
+import scala.collection.mutable.ArrayBuffer
+import scala.jdk.CollectionConverters._
+
+import org.apache.spark.{PartitionEvaluator, PartitionEvaluatorFactory, 
SparkEnv, TaskContext}
+import org.apache.spark.sql.{DataFrame, SparkSession}
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.execution.arrow.{ArrowBatchStreamWriter, 
ArrowConverters}
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.storage.{ArrowBatchBlockId, BlockId, StorageLevel}
+
+
+case class ChunkMeta(
+  id: String,
+  rowCount: Long,
+  byteCount: Long
+)
+
+class PersistDataFrameAsArrowBatchChunksPartitionEvaluator(
+    schema: StructType,
+    timeZoneId: String,
+    errorOnDuplicatedFieldNames: Boolean,
+    maxRecordsPerBatch: Long
+) extends PartitionEvaluator[InternalRow, ChunkMeta] {
+
+  def eval(partitionIndex: Int, inputs: Iterator[InternalRow]*): 
Iterator[ChunkMeta] = {
+    val blockManager = SparkEnv.get.blockManager
+    val chunkMetaList = new ArrayBuffer[ChunkMeta]()
+
+    val context = TaskContext.get()
+    val arrowBatchIter = ArrowConverters.toBatchIterator(
+      inputs(0), schema, maxRecordsPerBatch, timeZoneId,
+      errorOnDuplicatedFieldNames, context
+    )
+
+    try {
+      while (arrowBatchIter.hasNext) {
+        val arrowBatch = arrowBatchIter.next()
+        val rowCount = arrowBatchIter.lastBatchRowCount
+
+        val uuid = java.util.UUID.randomUUID()
+        val blockId = ArrowBatchBlockId(uuid)
+
+        val out = new ByteArrayOutputStream(32 * 1024 * 1024)
+
+        val batchWriter =
+          new ArrowBatchStreamWriter(schema, out, timeZoneId, 
errorOnDuplicatedFieldNames)
+
+        batchWriter.writeBatches(Iterator.single(arrowBatch))
+        batchWriter.end()
+
+        val blockData = out.toByteArray
+
+        blockManager.putSingle[Array[Byte]](
+          blockId, blockData, StorageLevel.MEMORY_AND_DISK, tellMaster = true
+        )
+        chunkMetaList.append(
+          ChunkMeta(blockId.toString, rowCount, blockData.length)
+        )
+      }
+    } catch {
+      case e: Exception =>
+        // Clean cached chunks
+        for (chunkMeta <- chunkMetaList) {
+          try {
+            blockManager.master.removeBlock(BlockId(chunkMeta.id))
+          } catch {
+            case _: Exception => ()

Review Comment:
   If we do nothing in catch block, then the exception is raised , and we lost 
tracking on these persisted blocks, i.e., we have no way remove these blocks if 
we don't remove them in the catch block



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