spark-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "jiangyu (JIRA)" <>
Subject [jira] [Created] (SPARK-28482) Data incomplete when using pandas udf in pyspark
Date Tue, 23 Jul 2019 08:42:00 GMT
jiangyu created SPARK-28482:

             Summary: Data incomplete when using pandas udf in pyspark
                 Key: SPARK-28482
             Project: Spark
          Issue Type: Bug
          Components: PySpark
    Affects Versions: 2.4.3
         Environment: centos 7.4   

pyarrow 0.10.0 0.14.0

python 2.7 3.5 3.6
            Reporter: jiangyu

Since Spark 2.3.x, pandas udf has been introduced as default ser/des method when using udf.
However, an issue raises with python >= 3.5.x version.
We use pandas udf to process batches of data, but we find the data is incomplete in python
3.x. At first , i think the process logical maybe wrong, so i change the code to very simple
one and it has the same problem.After investigate for a week, i find it is related to pyarrow.
*Reproduce procedure:*

1. prepare data
The data have seven column, a、b、c、d、e、f and g, data type is Integer
 produce 100,000 rows and name the file test.csv ,upload to hdfs, then load it , and repartition
it to 1 partition.
{code:java}'csv').option("header","true").load('/test.csv')*(col(c).cast("int").alias(c) for c in df.columns))
spark_context = SparkContext.getOrCreate() {code}
2.register pandas udf
def add_func(a,b,c,d,e,f,g):
    print('iterator one time')
    return a
add = pandas_udf(add_func, returnType=IntegerType())"a"),col("b"),col("c"),col("d"),col("e"),col("f"),col("g"))){code}
3.apply pandas udf
def trigger_func(iterator):
      yield iterator
4.execute it in pyspark (local or yarn)
run it with conf spark.sql.execution.arrow.maxRecordsPerBatch=100000. As mentioned before
the total row number is 1000000, it should print "iterator one time " 10 times.
(1)Python 2.7 envs:
PYSPARK_PYTHON=/usr/lib/conda/envs/py2.7/bin/python pyspark --conf spark.sql.execution.arrow.maxRecordsPerBatch=100000
--conf spark.executor.pyspark.memory=2g --conf spark.sql.execution.arrow.enabled=true --executor-cores
The result is right, 10 times of print.



(2)Python 3.5 or 3.6 envs:
PYSPARK_PYTHON=/usr/lib/conda/envs/python3.6/bin/python pyspark --conf spark.sql.execution.arrow.maxRecordsPerBatch=100000
--conf spark.executor.pyspark.memory=2g --conf spark.sql.execution.arrow.enabled=true --executor-cores{code}


The data is incomplete. Exception is print by jvm spark which have been added by us , I will
explain it later.
h3. *Investigation*

The “process done” is added in the
In order to get the exception,  change the spark code, the code is under core/src/main/scala/org/apache/spark/util/Utils.scala
, and add this code to print the exception.

@@ -1362,6 +1362,8 @@ private[spark] object Utils extends Logging {
 case t: Throwable =>
 // Purposefully not using NonFatal, because even fatal exceptions
 // we don't want to have our finallyBlock suppress
+ logInfo(t.getLocalizedMessage)
+ t.printStackTrace()
 originalThrowable = t
 throw originalThrowable
 } finally {{code}

It seems the pyspark get the data from jvm , but pyarrow get the data incomplete. Pyarrow
side think the data is finished, then shutdown the socket. At the same time, the jvm side
still writes to the same socket , but get socket close exception.
The pyarrow part is in ipc.pxi:
cdef class _RecordBatchReader:
 shared_ptr[CRecordBatchReader] reader
 shared_ptr[InputStream] in_stream
cdef readonly:
 Schema schema
def _cinit_(self):
def _open(self, source):
 get_input_stream(source, &self.in_stream)
 with nogil:
 self.in_stream.get(), &self.reader))
self.schema = pyarrow_wrap_schema(self.reader.get().schema())
def _iter_(self):
 while True:
 yield self.read_next_batch()
def get_next_batch(self):
 import warnings
 warnings.warn('Please use read_next_batch instead of '
 'get_next_batch', FutureWarning)
 return self.read_next_batch()
def read_next_batch(self):
 Read next RecordBatch from the stream. Raises StopIteration at end of
 cdef shared_ptr[CRecordBatch] batch
with nogil:
if batch.get() == NULL:
 raise StopIteration
 return pyarrow_wrap_batch(batch){code}

read_next_batch function get NULL, think the iterator is over.
h3. *RESULT*

Our environment is spark 2.4.3, we have tried pyarrow version 0.10.0 and 0.14.0 , python version
is python 2.7, python 3.5, python 3.6.
When using python 2.7, everything is fine. But when change to python 3.5,3,6, the data is
The column number is critical to trigger this bug, if column number is less than 5 , this
bug probably will not happen. But If the column number is big , for example 7 or above, it
will happened every time.
So we wonder if there is some conflict between python 3.x and pyarrow version? 
I have put the code and data as attachment.

This message was sent by Atlassian JIRA

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message