Background and Motivation
Python is one of the most popular programming languages among Spark users. Spark currently exposes a row-at-a-time interface for defining and executing user-defined functions (UDFs). This introduces high overhead in serialization and deserialization, and also makes it difficult to leverage Python libraries (e.g. numpy, Pandas) that are written in native code.
This proposal advocates introducing new APIs to support vectorized UDFs in Python, in which a block of data is transferred over to Python in some columnar format for execution.
Data scientists, data engineers, library developers.
- Support vectorized UDFs that apply on chunks of the data frame
- Low system overhead: Substantially reduce serialization and deserialization overhead when compared with row-at-a-time interface
- UDF performance: Enable users to leverage native libraries in Python (e.g. numpy, Pandas) for data manipulation in these UDFs
The following are explicitly out of scope for the current SPIP, and should be done in future SPIPs. Nonetheless, it would be good to consider these future use cases during API design, so we can achieve some consistency when rolling out new APIs.
- Define block oriented UDFs in other languages (that are not Python).
- Define aggregate UDFs
- Tight integration with machine learning frameworks
Proposed API Changes
The following sketches some possibilities. I haven’t spent a lot of time thinking about the API (wrote it down in 5 mins) and I am not attached to this design at all. The main purpose of the SPIP is to get feedback on use cases and see how they can impact API design.
A few things to consider are:
1. Python is dynamically typed, whereas DataFrames/SQL requires static, analysis time typing. This means users would need to specify the return type of their UDFs.
2. Ratio of input rows to output rows. We propose initially we require number of output rows to be the same as the number of input rows. In the future, we can consider relaxing this constraint with support for vectorized aggregate UDFs.
3. How do we handle null values, since Pandas doesn't have the concept of nulls?
Proposed API sketch (using examples):
Use case 1. A function that defines all the columns of a DataFrame (similar to a “map” function):
Use case 2. A function that defines only one column (similar to existing UDFs):
Optional Design Sketch
I’m more concerned about getting proper feedback for API design. The implementation should be pretty straightforward and is not a huge concern at this point. We can leverage the same implementation for faster toPandas (using Arrow).
Optional Rejected Designs