You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/05/27 05:53:10 UTC

[GitHub] [spark] viirya commented on a diff in pull request #36683: [SPARK-39301][SQL][PYTHON] Leverage LocalRelation in createDataFrame with Arrow optimization

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


##########
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:
   SGTM for a configuration flag.



-- 
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-unsubscribe@spark.apache.org

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


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