amaliujia commented on code in PR #38468:
URL: https://github.com/apache/spark/pull/38468#discussion_r1015819772


##########
connector/connect/src/main/scala/org/apache/spark/sql/connect/service/SparkConnectStreamHandler.scala:
##########
@@ -117,10 +129,91 @@ class SparkConnectStreamHandler(responseObserver: 
StreamObserver[Response]) exte
       responseObserver.onNext(response.build())
     }
 
-    responseObserver.onNext(sendMetricsToResponse(clientId, rows))
+    responseObserver.onNext(sendMetricsToResponse(clientId, dataframe))
     responseObserver.onCompleted()
   }
 
+  def processRowsAsArrowBatches(clientId: String, dataframe: DataFrame): Unit 
= {
+    val spark = dataframe.sparkSession
+    val schema = dataframe.schema
+    // TODO: control the batch size instead of max records
+    val maxRecordsPerBatch = spark.sessionState.conf.arrowMaxRecordsPerBatch
+    val timeZoneId = spark.sessionState.conf.sessionLocalTimeZone
+
+    SQLExecution.withNewExecutionId(dataframe.queryExecution, 
Some("collectArrow")) {
+      val pool = 
ThreadUtils.newDaemonSingleThreadExecutor("connect-collect-arrow")
+      val tasks = collection.mutable.ArrayBuffer.empty[Future[_]]
+      val rows = dataframe.queryExecution.executedPlan.execute()
+
+      if (rows.getNumPartitions > 0) {
+        val batches = rows.mapPartitionsInternal { iter =>
+          ArrowConverters
+            .toArrowBatchIterator(iter, schema, maxRecordsPerBatch, timeZoneId)
+        }
+
+        val processPartition = (iter: Iterator[(Array[Byte], Long, Long)]) => 
iter.toArray
+
+        val resultHandler = (partitionId: Int, taskResult: Array[(Array[Byte], 
Long, Long)]) => {

Review Comment:
   @cloud-fan and I had a discussion on this. I remember our initial thought 
was to maintain the partition ordering on the server side. For example, there 
is `def foreachPartition` API already. The question was whether we can foreach 
partitions in the partitioning order.



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