-
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-27163][PYTHON] Cleanup and consolidate Pandas UDF functionality #24095
Changes from all commits
dd743d5
9a32b44
93bb831
bc08d1b
5832d63
1809dfe
f6b0e30
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 |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
from pyspark.rdd import PythonEvalType | ||
from pyspark.serializers import write_with_length, write_int, read_long, read_bool, \ | ||
write_long, read_int, SpecialLengths, UTF8Deserializer, PickleSerializer, \ | ||
BatchedSerializer, ArrowStreamPandasSerializer | ||
BatchedSerializer, ArrowStreamPandasUDFSerializer | ||
from pyspark.sql.types import to_arrow_type, StructType | ||
from pyspark.util import _get_argspec, fail_on_stopiteration | ||
from pyspark import shuffle | ||
|
@@ -101,10 +101,7 @@ def verify_result_length(*a): | |
return lambda *a: (verify_result_length(*a), arrow_return_type) | ||
|
||
|
||
def wrap_grouped_map_pandas_udf(f, return_type, argspec, runner_conf): | ||
assign_cols_by_name = runner_conf.get( | ||
"spark.sql.legacy.execution.pandas.groupedMap.assignColumnsByName", "true") | ||
assign_cols_by_name = assign_cols_by_name.lower() == "true" | ||
def wrap_grouped_map_pandas_udf(f, return_type, argspec): | ||
|
||
def wrapped(key_series, value_series): | ||
import pandas as pd | ||
|
@@ -123,15 +120,9 @@ def wrapped(key_series, value_series): | |
"Number of columns of the returned pandas.DataFrame " | ||
"doesn't match specified schema. " | ||
"Expected: {} Actual: {}".format(len(return_type), len(result.columns))) | ||
return result | ||
|
||
# Assign result columns by schema name if user labeled with strings, else use position | ||
if assign_cols_by_name and any(isinstance(name, basestring) for name in result.columns): | ||
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. Eh, @BryanCutler, sorry if I runshed to read but where did this logic go? 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. Oops, okie. The logic was actually duplicated with 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. yup, that's correct |
||
return [(result[field.name], to_arrow_type(field.dataType)) for field in return_type] | ||
else: | ||
return [(result[result.columns[i]], to_arrow_type(field.dataType)) | ||
for i, field in enumerate(return_type)] | ||
|
||
return wrapped | ||
return lambda k, v: [(wrapped(k, v), to_arrow_type(return_type))] | ||
|
||
|
||
def wrap_grouped_agg_pandas_udf(f, return_type): | ||
|
@@ -225,7 +216,7 @@ def read_single_udf(pickleSer, infile, eval_type, runner_conf, udf_index): | |
return arg_offsets, wrap_scalar_pandas_udf(func, return_type) | ||
elif eval_type == PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF: | ||
argspec = _get_argspec(row_func) # signature was lost when wrapping it | ||
return arg_offsets, wrap_grouped_map_pandas_udf(func, return_type, argspec, runner_conf) | ||
return arg_offsets, wrap_grouped_map_pandas_udf(func, return_type, argspec) | ||
elif eval_type == PythonEvalType.SQL_GROUPED_AGG_PANDAS_UDF: | ||
return arg_offsets, wrap_grouped_agg_pandas_udf(func, return_type) | ||
elif eval_type == PythonEvalType.SQL_WINDOW_AGG_PANDAS_UDF: | ||
|
@@ -255,12 +246,12 @@ def read_udfs(pickleSer, infile, eval_type): | |
timezone = runner_conf.get("spark.sql.session.timeZone", None) | ||
safecheck = runner_conf.get("spark.sql.execution.pandas.arrowSafeTypeConversion", | ||
"false").lower() == 'true' | ||
# NOTE: this is duplicated from wrap_grouped_map_pandas_udf | ||
# Used by SQL_GROUPED_MAP_PANDAS_UDF and SQL_SCALAR_PANDAS_UDF when returning StructType | ||
assign_cols_by_name = runner_conf.get( | ||
"spark.sql.legacy.execution.pandas.groupedMap.assignColumnsByName", "true")\ | ||
.lower() == "true" | ||
|
||
ser = ArrowStreamPandasSerializer(timezone, safecheck, assign_cols_by_name) | ||
ser = ArrowStreamPandasUDFSerializer(timezone, safecheck, assign_cols_by_name) | ||
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.
I'm not too thrilled with creating a record batch just to get the Arrow schema, but this was the most reliable way I could figure to do it pre v0.12.0. I will propose bumping the pyarrow version soon, and then this could be removed.
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.
Okie