Github user tedyu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11664#discussion_r56214366
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkPlan.scala ---
    @@ -220,7 +221,47 @@ abstract class SparkPlan extends QueryPlan[SparkPlan] 
with Logging with Serializ
        * Runs this query returning the result as an array.
        */
       def executeCollect(): Array[InternalRow] = {
    -    execute().map(_.copy()).collect()
    +    // Packing the UnsafeRows into byte array for faster serialization.
    +    // The byte arrays are in the following format:
    +    // [size] [bytes of UnsafeRow] [size] [bytes of UnsafeRow] ... [-1]
    +    //
    +    // UnsafeRow is highly compressible (at least 8 bytes for any column), 
the byte array is also
    +    // compressed.
    +    val byteArrayRdd = execute().mapPartitionsInternal { iter =>
    +      val buffer = new Array[Byte](4 << 10)  // 4K
    +      val codec = CompressionCodec.createCodec(SparkEnv.get.conf)
    +      val bos = new ByteArrayOutputStream()
    +      val out = new DataOutputStream(codec.compressedOutputStream(bos))
    +      while (iter.hasNext) {
    +        val row = iter.next().asInstanceOf[UnsafeRow]
    +        out.writeInt(row.getSizeInBytes)
    +        row.writeToStream(out, buffer)
    +      }
    +      out.writeInt(-1)
    +      out.flush()
    +      out.close()
    +      Iterator(bos.toByteArray)
    +    }
    +
    +    // Collect the byte arrays back to driver, then decode them as 
UnsafeRows.
    +    val nFields = schema.length
    +    val results = ArrayBuffer[InternalRow]()
    +
    +    byteArrayRdd.collect().foreach { bytes =>
    +      val codec = CompressionCodec.createCodec(SparkEnv.get.conf)
    +      val bis = new ByteArrayInputStream(bytes)
    +      val ins = new DataInputStream(codec.compressedInputStream(bis))
    +      var sizeOfNextRow = ins.readInt()
    +      while (sizeOfNextRow >= 0) {
    --- End diff --
    
    Why is 0 a legitimate size ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to