-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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-23141][SQL][PYSPARK] Support data type string as a returnType for registerJavaFunction. #20307
Closed
Closed
[SPARK-23141][SQL][PYSPARK] Support data type string as a returnType for registerJavaFunction. #20307
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,7 +206,8 @@ def register(self, name, f, returnType=None): | |
:param f: a Python function, or a user-defined function. The user-defined function can | ||
be either row-at-a-time or vectorized. See :meth:`pyspark.sql.functions.udf` and | ||
:meth:`pyspark.sql.functions.pandas_udf`. | ||
:param returnType: the return type of the registered user-defined function. | ||
:param returnType: the return type of the registered user-defined function. The value can | ||
be either a :class:`pyspark.sql.types.DataType` object or a DDL-formatted type string. | ||
:return: a user-defined function. | ||
|
||
`returnType` can be optionally specified when `f` is a Python function but not | ||
|
@@ -303,21 +304,30 @@ def registerJavaFunction(self, name, javaClassName, returnType=None): | |
|
||
:param name: name of the user-defined function | ||
:param javaClassName: fully qualified name of java class | ||
:param returnType: a :class:`pyspark.sql.types.DataType` object | ||
:param returnType: the return type of the registered Java function. The value can be either | ||
a :class:`pyspark.sql.types.DataType` object or a DDL-formatted type string. | ||
|
||
>>> from pyspark.sql.types import IntegerType | ||
>>> spark.udf.registerJavaFunction( | ||
... "javaStringLength", "test.org.apache.spark.sql.JavaStringLength", IntegerType()) | ||
>>> spark.sql("SELECT javaStringLength('test')").collect() | ||
[Row(UDF:javaStringLength(test)=4)] | ||
|
||
>>> spark.udf.registerJavaFunction( | ||
... "javaStringLength2", "test.org.apache.spark.sql.JavaStringLength") | ||
>>> spark.sql("SELECT javaStringLength2('test')").collect() | ||
[Row(UDF:javaStringLength2(test)=4)] | ||
|
||
>>> spark.udf.registerJavaFunction( | ||
... "javaStringLength3", "test.org.apache.spark.sql.JavaStringLength", "integer") | ||
>>> spark.sql("SELECT javaStringLength3('test')").collect() | ||
[Row(UDF:javaStringLength3(test)=4)] | ||
""" | ||
|
||
jdt = None | ||
if returnType is not None: | ||
if not isinstance(returnType, DataType): | ||
returnType = _parse_datatype_string(returnType) | ||
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. The param doc needs to be modified too. 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 #20307 (comment) :). |
||
jdt = self.sparkSession._jsparkSession.parseDataType(returnType.json()) | ||
self.sparkSession._jsparkSession.udf().registerJava(name, javaClassName, jdt) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ah, seems we need to fix
:param returnType:
across all other related APIs saying it takes DDL-formatted type string.@ueshin, mind opening a minor PR for this -
udf
,pandas_udf
,registerJavaFunction
andregister
separately? If you are busy, will do it tonight. Doing this here is fine to me too, up to you.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.
Sure, I'll update them here soon.