ueshin commented on code in PR #36683:
URL: https://github.com/apache/spark/pull/36683#discussion_r883030304


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/arrow/ArrowConverters.scala:
##########
@@ -190,32 +191,30 @@ private[sql] object ArrowConverters {
   }
 
   /**
-   * Create a DataFrame from an RDD of serialized ArrowRecordBatches.
+   * Create a DataFrame from an iterator of serialized ArrowRecordBatches.
    */
-  private[sql] def toDataFrame(
-      arrowBatchRDD: JavaRDD[Array[Byte]],
+  def toDataFrame(
+      arrowBatches: Iterator[Array[Byte]],
       schemaString: String,
       session: SparkSession): DataFrame = {
-    val schema = DataType.fromJson(schemaString).asInstanceOf[StructType]
-    val timeZoneId = session.sessionState.conf.sessionLocalTimeZone
-    val rdd = arrowBatchRDD.rdd.mapPartitions { iter =>
-      val context = TaskContext.get()
-      ArrowConverters.fromBatchIterator(iter, schema, timeZoneId, context)
-    }
-    session.internalCreateDataFrame(rdd.setName("arrow"), schema)
+    val attrs = 
DataType.fromJson(schemaString).asInstanceOf[StructType].toAttributes
+    val data = ArrowConverters.fromBatchIterator(
+      arrowBatches,
+      DataType.fromJson(schemaString).asInstanceOf[StructType],
+      session.sessionState.conf.sessionLocalTimeZone,
+      TaskContext.get())
+    // Project it. Otherwise, the Arrow column vectors will be closed and 
released out.
+    val proj = UnsafeProjection.create(attrs, attrs)
+    Dataset.ofRows(session, LocalRelation(attrs, data.map(r => 
proj(r).copy()).toArray))

Review Comment:
   Btw, if we don't handle it in parallel, we don't need to slice the input: 
https://github.com/apache/spark/blob/8053d1bce3904e15dfe494bfd7bc3f4e92c80037/python/pyspark/sql/pandas/conversion.py#L598-L606?
   
   It could still reduce the memory consumption during the conversion, though.



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to