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

    https://github.com/apache/spark/pull/1338#discussion_r15555021
  
    --- Diff: core/src/main/scala/org/apache/spark/api/python/SerDeUtil.scala 
---
    @@ -65,20 +66,49 @@ private[python] object SerDeUtil extends Logging {
        * by PySpark. By default, if serialization fails, toString is called 
and the string
        * representation is serialized
        */
    -  def rddToPython(rdd: RDD[(Any, Any)]): RDD[Array[Byte]] = {
    +  def pairRDDToPython(rdd: RDD[(Any, Any)], batchSize: Int): 
RDD[Array[Byte]] = {
         val (keyFailed, valueFailed) = checkPickle(rdd.first())
         rdd.mapPartitions { iter =>
           val pickle = new Pickler
    -      iter.map { case (k, v) =>
    -        if (keyFailed && valueFailed) {
    -          pickle.dumps(Array(k.toString, v.toString))
    -        } else if (keyFailed) {
    -          pickle.dumps(Array(k.toString, v))
    -        } else if (!keyFailed && valueFailed) {
    -          pickle.dumps(Array(k, v.toString))
    -        } else {
    -          pickle.dumps(Array(k, v))
    +      val cleaned = iter.map { case (k, v) =>
    +        val key = if (keyFailed) k.toString else k
    +        val value = if (valueFailed) v.toString else v
    +        Array[Any](key, value)
    +      }
    +      if (batchSize > 1) {
    +        cleaned.grouped(batchSize).map(batched => 
pickle.dumps(seqAsJavaList(batched)))
    +      } else {
    +        cleaned.map(pickle.dumps(_))
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Convert an RDD of serialized Python tuple (K, V) to RDD[(K, V)].
    +   */
    +  def pythonToPairRDD[K, V](pyRDD: RDD[Array[Byte]]): RDD[(K, V)] = {
    +    def isPair(obj: Any): Boolean = {
    +      
Option(obj.getClass.getComponentType).map(!_.isPrimitive).getOrElse(false) &&
    +        obj.asInstanceOf[Array[_]].length == 2
    +    }
    +    pyRDD.mapPartitions { iter =>
    +      val unpickle = new Unpickler
    +      iter.flatMap { row =>
    +        unpickle.loads(row) match {
    +          // batch serialized Python RDDs
    +          case objs: java.util.List[_] => objs
    --- End diff --
    
    You are absolutely right. I fixed it by passing in a flag from the caller, 
since PythonRDD is not always created (e.g., in ```sc.parallelize()```). It 
also helped me finding a bug in ```_toPickleSerialization()```. Let me know if 
you have any further comments. Thx.
    
    This issue may also affect ```PythonRDD.pythonToJavaMap()``` used by 
SchemaRDD. @davies , it might be relevant to your namedtuple work.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to