-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-27240][PYTHON] Use pandas DataFrame for struct type argument in Scalar Pandas UDF. #24177
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -260,11 +260,10 @@ def __init__(self, timezone, safecheck, assign_cols_by_name): | |
self._safecheck = safecheck | ||
self._assign_cols_by_name = assign_cols_by_name | ||
|
||
def arrow_to_pandas(self, arrow_column): | ||
from pyspark.sql.types import from_arrow_type, \ | ||
_arrow_column_to_pandas, _check_series_localize_timestamps | ||
def arrow_to_pandas(self, arrow_column, data_type): | ||
from pyspark.sql.types import _arrow_column_to_pandas, _check_series_localize_timestamps | ||
|
||
s = _arrow_column_to_pandas(arrow_column, from_arrow_type(arrow_column.type)) | ||
s = _arrow_column_to_pandas(arrow_column, data_type) | ||
s = _check_series_localize_timestamps(s, self._timezone) | ||
return s | ||
|
||
|
@@ -366,8 +365,10 @@ def load_stream(self, stream): | |
""" | ||
batches = super(ArrowStreamPandasSerializer, self).load_stream(stream) | ||
import pyarrow as pa | ||
from pyspark.sql.types import from_arrow_type | ||
for batch in batches: | ||
yield [self.arrow_to_pandas(c) for c in pa.Table.from_batches([batch]).itercolumns()] | ||
yield [self.arrow_to_pandas(c, from_arrow_type(c.type)) | ||
for c in pa.Table.from_batches([batch]).itercolumns()] | ||
|
||
def __repr__(self): | ||
return "ArrowStreamPandasSerializer" | ||
|
@@ -378,6 +379,24 @@ class ArrowStreamPandasUDFSerializer(ArrowStreamPandasSerializer): | |
Serializer used by Python worker to evaluate Pandas UDFs | ||
""" | ||
|
||
def __init__(self, timezone, safecheck, assign_cols_by_name, df_for_struct=False): | ||
super(ArrowStreamPandasUDFSerializer, self) \ | ||
.__init__(timezone, safecheck, assign_cols_by_name) | ||
self._df_for_struct = df_for_struct | ||
|
||
def arrow_to_pandas(self, arrow_column, data_type): | ||
from pyspark.sql.types import StructType, \ | ||
_arrow_column_to_pandas, _check_dataframe_localize_timestamps | ||
|
||
if self._df_for_struct and type(data_type) == StructType: | ||
import pandas as pd | ||
series = [_arrow_column_to_pandas(column, field.dataType).rename(field.name) | ||
for column, field in zip(arrow_column.flatten(), data_type)] | ||
s = _check_dataframe_localize_timestamps(pd.concat(series, axis=1), self._timezone) | ||
else: | ||
s = super(ArrowStreamPandasUDFSerializer, self).arrow_to_pandas(arrow_column, data_type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this create a new serializer each time calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is just calling super class's method. |
||
return s | ||
|
||
def dump_stream(self, iterator, stream): | ||
""" | ||
Override because Pandas UDFs require a START_ARROW_STREAM before the Arrow stream is sent. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,7 +253,11 @@ def read_udfs(pickleSer, infile, eval_type): | |
"spark.sql.legacy.execution.pandas.groupedMap.assignColumnsByName", "true")\ | ||
.lower() == "true" | ||
|
||
ser = ArrowStreamPandasUDFSerializer(timezone, safecheck, assign_cols_by_name) | ||
# Scalar Pandas UDF handles struct type arguments as pandas DataFrames instead of | ||
# pandas Series. See SPARK-27240. | ||
df_for_struct = eval_type == PythonEvalType.SQL_SCALAR_PANDAS_UDF | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems hard to tell why when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, will add a comment. |
||
ser = ArrowStreamPandasUDFSerializer(timezone, safecheck, assign_cols_by_name, | ||
df_for_struct) | ||
else: | ||
ser = BatchedSerializer(PickleSerializer(), 100) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to check for a nested struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. We can't construct pandas DataFrame with a nested DataFrame.
I might miss what you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if
data_type
has a nested struct, then is an error raised before it gets here? That could be addressed as a followup, I'm not sure if there is a test for it, but I'll check.