Skip to content
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-48938][PYTHON] Improve error messages when registering Python UDTFs #47408

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/pyspark/errors/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
"Metadata can only be provided for a single column."
]
},
"CANNOT_REGISTER_UDTF": {
"message": [
"Cannot register the UDTF '<name>': expected a 'UserDefinedTableFunction'. Please make sure the UDTF is correctly defined as a class, and then either wrap it in the `udtf()` function or annotate it with `@udtf(...)`."
]
},
"CANNOT_SET_TOGETHER": {
"message": [
"<arg_list> should not be set together."
Expand Down
8 changes: 8 additions & 0 deletions python/pyspark/sql/connect/udtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ def register(
name: str,
f: "UserDefinedTableFunction",
) -> "UserDefinedTableFunction":
if not isinstance(f, UserDefinedTableFunction):
raise PySparkTypeError(
error_class="CANNOT_REGISTER_UDTF",
message_parameters={
"name": name,
},
)

if f.evalType not in [PythonEvalType.SQL_TABLE_UDF, PythonEvalType.SQL_ARROW_TABLE_UDF]:
raise PySparkTypeError(
error_class="INVALID_UDTF_EVAL_TYPE",
Expand Down
17 changes: 15 additions & 2 deletions python/pyspark/sql/tests/test_udtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,23 @@ def upper(s: str):

self.check_error(
exception=e.exception,
error_class="INVALID_UDTF_EVAL_TYPE",
error_class="CANNOT_REGISTER_UDTF",
message_parameters={
"name": "test_udf",
"eval_type": "SQL_TABLE_UDF, SQL_ARROW_TABLE_UDF",
},
)

class TestUDTF:
...

with self.assertRaises(PySparkTypeError) as e:
self.spark.udtf.register("test_udtf", TestUDTF)

self.check_error(
exception=e.exception,
error_class="CANNOT_REGISTER_UDTF",
message_parameters={
"name": "test_udtf",
},
)

Expand Down
8 changes: 8 additions & 0 deletions python/pyspark/sql/udtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ def register(
>>> spark.sql("SELECT * FROM VALUES (0, 1), (1, 2) t(x, y), LATERAL plus_one(x)").collect()
[Row(x=0, y=1, c1=0, c2=1), Row(x=1, y=2, c1=1, c2=2)]
"""
if not isinstance(f, UserDefinedTableFunction):
raise PySparkTypeError(
error_class="CANNOT_REGISTER_UDTF",
message_parameters={
"name": name,
},
)

if f.evalType not in [PythonEvalType.SQL_TABLE_UDF, PythonEvalType.SQL_ARROW_TABLE_UDF]:
raise PySparkTypeError(
error_class="INVALID_UDTF_EVAL_TYPE",
Expand Down