This is an automated email from the ASF dual-hosted git repository.
dbtsai pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.2 by this push:
new 89906d766ea1 [SPARK-57397][PYTHON] Raise an exception when parsed UDT
is the wrong type
89906d766ea1 is described below
commit 89906d766ea1b0a08b003db2ff9347a354f9d7fe
Author: Tian Gao <[email protected]>
AuthorDate: Fri Jun 12 10:09:17 2026 -0700
[SPARK-57397][PYTHON] Raise an exception when parsed UDT is the wrong type
### What changes were proposed in this pull request?
Check whether the imported class is a real UserDefinedType before calling
it.
### Why are the changes needed?
If it's not a UDT, it could run wrong code.
### Does this PR introduce _any_ user-facing change?
Not for normal use.
### How was this patch tested?
Regression test added.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #56460 from gaogaotiantian/add-import-type-check.
Authored-by: Tian Gao <[email protected]>
Signed-off-by: DB Tsai <[email protected]>
(cherry picked from commit 1f5367459d790e868640ed2f9fe1bf0c6b776b63)
Signed-off-by: DB Tsai <[email protected]>
---
python/pyspark/sql/tests/test_types.py | 11 +++++++++++
python/pyspark/sql/types.py | 5 +++++
2 files changed, 16 insertions(+)
diff --git a/python/pyspark/sql/tests/test_types.py
b/python/pyspark/sql/tests/test_types.py
index 26a6e169f74f..826c66d5a306 100644
--- a/python/pyspark/sql/tests/test_types.py
+++ b/python/pyspark/sql/tests/test_types.py
@@ -67,6 +67,7 @@ from pyspark.sql.types import (
GeographyType,
GeometryType,
NullType,
+ UserDefinedType,
VariantType,
VariantVal,
_create_row,
@@ -1060,6 +1061,16 @@ class TypesTestsMixin:
_make_type_verifier(PythonOnlyUDT())(PythonOnlyPoint(1.0, 2.0))
self.assertRaises(ValueError, lambda:
_make_type_verifier(PythonOnlyUDT())([1.0, 2.0]))
+ def test_udt_from_json_import_type_mismatch(self):
+ json = {
+ "type": "udt",
+ "pyClass": "random.random",
+ "serializedClass": "",
+ "sqlType": StringType().jsonValue(),
+ }
+ with self.assertRaises(PySparkTypeError):
+ UserDefinedType.fromJson(json)
+
def test_simple_udt_in_df(self):
schema = StructType().add("key", LongType()).add("val",
PythonOnlyUDT())
df = self.spark.createDataFrame(
diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py
index 02a1af77906d..6b9020b3b104 100644
--- a/python/pyspark/sql/types.py
+++ b/python/pyspark/sql/types.py
@@ -1996,6 +1996,11 @@ class UserDefinedType(DataType):
)
else:
UDT = getattr(m, pyClass)
+ if not (isinstance(UDT, type) and issubclass(UDT,
UserDefinedType)):
+ raise PySparkTypeError(
+ errorClass="FIELD_TYPE_MISMATCH",
+ messageParameters={"obj": str(UDT), "data_type":
"UserDefinedType"},
+ )
return UDT()
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]