mengxr commented on a change in pull request #24643:
[SPARK-26412][PySpark][SQL] Allow Pandas UDF to take an iterator of pd.Series
or an iterator of tuple of pd.Series
URL: https://github.com/apache/spark/pull/24643#discussion_r293602217
##########
File path: python/pyspark/sql/tests/test_pandas_udf_scalar.py
##########
@@ -225,67 +270,77 @@ def test_vectorized_udf_datatype_string(self):
col('id').cast('decimal').alias('decimal'),
col('id').cast('boolean').alias('bool'))
f = lambda x: x
- str_f = pandas_udf(f, 'string')
- int_f = pandas_udf(f, 'integer')
- long_f = pandas_udf(f, 'long')
- float_f = pandas_udf(f, 'float')
- double_f = pandas_udf(f, 'double')
- decimal_f = pandas_udf(f, 'decimal(38, 18)')
- bool_f = pandas_udf(f, 'boolean')
- res = df.select(str_f(col('str')), int_f(col('int')),
- long_f(col('long')), float_f(col('float')),
- double_f(col('double')), decimal_f('decimal'),
- bool_f(col('bool')))
- self.assertEquals(df.collect(), res.collect())
+ for udf_type in [PandasUDFType.SCALAR, PandasUDFType.SCALAR_ITER]:
+ str_f = pandas_udf(f, 'string', udf_type)
+ int_f = pandas_udf(f, 'integer', udf_type)
+ long_f = pandas_udf(f, 'long', udf_type)
+ float_f = pandas_udf(f, 'float', udf_type)
+ double_f = pandas_udf(f, 'double', udf_type)
+ decimal_f = pandas_udf(f, 'decimal(38, 18)', udf_type)
+ bool_f = pandas_udf(f, 'boolean', udf_type)
+ res = df.select(str_f(col('str')), int_f(col('int')),
+ long_f(col('long')), float_f(col('float')),
+ double_f(col('double')), decimal_f('decimal'),
+ bool_f(col('bool')))
+ self.assertEquals(df.collect(), res.collect())
def test_vectorized_udf_null_binary(self):
data = [(bytearray(b"a"),), (None,), (bytearray(b"bb"),),
(bytearray(b"ccc"),)]
schema = StructType().add("binary", BinaryType())
df = self.spark.createDataFrame(data, schema)
- str_f = pandas_udf(lambda x: x, BinaryType())
- res = df.select(str_f(col('binary')))
- self.assertEquals(df.collect(), res.collect())
+ for udf_type in [PandasUDFType.SCALAR, PandasUDFType.SCALAR_ITER]:
+ str_f = pandas_udf(lambda x: x, BinaryType(), udf_type)
+ res = df.select(str_f(col('binary')))
+ self.assertEquals(df.collect(), res.collect())
def test_vectorized_udf_array_type(self):
data = [([1, 2],), ([3, 4],)]
array_schema = StructType([StructField("array",
ArrayType(IntegerType()))])
df = self.spark.createDataFrame(data, schema=array_schema)
- array_f = pandas_udf(lambda x: x, ArrayType(IntegerType()))
- result = df.select(array_f(col('array')))
- self.assertEquals(df.collect(), result.collect())
+ for udf_type in [PandasUDFType.SCALAR, PandasUDFType.SCALAR_ITER]:
+ array_f = pandas_udf(lambda x: x, ArrayType(IntegerType()),
udf_type)
+ result = df.select(array_f(col('array')))
+ self.assertEquals(df.collect(), result.collect())
def test_vectorized_udf_null_array(self):
data = [([1, 2],), (None,), (None,), ([3, 4],), (None,)]
array_schema = StructType([StructField("array",
ArrayType(IntegerType()))])
df = self.spark.createDataFrame(data, schema=array_schema)
- array_f = pandas_udf(lambda x: x, ArrayType(IntegerType()))
- result = df.select(array_f(col('array')))
- self.assertEquals(df.collect(), result.collect())
+ for udf_type in [PandasUDFType.SCALAR, PandasUDFType.SCALAR_ITER]:
+ array_f = pandas_udf(lambda x: x, ArrayType(IntegerType()),
udf_type)
+ result = df.select(array_f(col('array')))
+ self.assertEquals(df.collect(), result.collect())
def test_vectorized_udf_struct_type(self):
df = self.spark.range(10)
return_type = StructType([
StructField('id', LongType()),
StructField('str', StringType())])
- def func(id):
+ def scalar_func(id):
return pd.DataFrame({'id': id, 'str': id.apply(unicode)})
- f = pandas_udf(func, returnType=return_type)
+ def iter_func(it):
+ for id in it:
+ yield pd.DataFrame({'id': id, 'str': id.apply(unicode)})
Review comment:
minor: `yield scalar_func(id)`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]