Skip to content

Commit 30ccf8d

Browse files
uros-dbzhengruifeng
authored andcommitted
[SPARK-53921][GEO][PYTHON][FOLLOWUP] Fix invalid algorithm error for Geography JSON parsing
### What changes were proposed in this pull request? This PR follows up on #52627, and fixes an issue with `GeographyType` JSON parsing in PySpark. Also, this PR adds appropriate tests for JSON parsing, both for `GeographyType` and `GeometryType`. ### Why are the changes needed? Fixing a wrong error class thrown in the JSON parsing method for `GeographyType`. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Added new test cases to: - `pyspark.sql.tests.test_geographytype` - `pyspark.sql.tests.test_geometrytype` ### Was this patch authored or co-authored using generative AI tooling? No. Closes #52772 from uros-db/geo-python-types-tests. Authored-by: Uros Bojanic <[email protected]> Signed-off-by: Ruifeng Zheng <[email protected]>
1 parent 8860509 commit 30ccf8d

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

python/pyspark/sql/tests/test_geographytype.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,47 @@ def test_geographytype_different_srid_values(self):
7878
geography_type_2 = GeographyType("ANY")
7979
self.assertNotEqual(geography_type_1.srid, geography_type_2.srid)
8080

81+
def test_geographytype_from_invalid_crs(self):
82+
"""Test that GeographyType construction fails when an invalid CRS is specified."""
83+
84+
for invalid_crs in ["srid", "any", "ogccrs84", "ogc:crs84", "ogc:CRS84", "asdf", ""]:
85+
with self.assertRaises(IllegalArgumentException) as error_context:
86+
GeographyType._from_crs(invalid_crs, "SPHERICAL")
87+
crs_header = "[ST_INVALID_CRS_VALUE] Invalid or unsupported CRS"
88+
self.assertEqual(
89+
str(error_context.exception),
90+
f"{crs_header} (coordinate reference system) value: '{invalid_crs}'.",
91+
)
92+
93+
# The tests below verify GEOGRAPHY type JSON parsing based on CRS and algorithm.
94+
95+
def test_geographytype_from_invalid_algorithm(self):
96+
"""Test that GeographyType construction fails when an invalid CRS is specified."""
97+
98+
for invalid_alg in ["alg", "algorithm", "KARNEY", "spherical", "SPHEROID", "asdf", ""]:
99+
with self.assertRaises(IllegalArgumentException) as error_context:
100+
GeographyType._from_crs("OGC:CRS84", invalid_alg)
101+
alg_header = "[ST_INVALID_ALGORITHM_VALUE] Invalid or unsupported"
102+
self.assertEqual(
103+
str(error_context.exception),
104+
f"{alg_header} edge interpolation algorithm value: '{invalid_alg}'.",
105+
)
106+
107+
def test_geographytype_from_valid_crs_and_algorithm(self):
108+
"""Test that GeographyType construction passes when valid CRS & ALG are specified."""
109+
110+
supported_crs = {
111+
"OGC:CRS84": 4326,
112+
}
113+
for valid_crs, srid in supported_crs.items():
114+
for valid_alg in ["SPHERICAL"]:
115+
geography_type = GeographyType._from_crs(valid_crs, valid_alg)
116+
self.assertEqual(geography_type.srid, srid)
117+
self.assertEqual(geography_type.typeName(), "geography")
118+
self.assertEqual(geography_type.simpleString(), f"geography({srid})")
119+
self.assertEqual(geography_type.jsonValue(), f"geography({valid_crs}, {valid_alg})")
120+
self.assertEqual(repr(geography_type), f"GeographyType({srid})")
121+
81122

82123
class GeographyTypeTest(GeographyTypeTestMixin, ReusedSQLTestCase):
83124
pass

python/pyspark/sql/tests/test_geometrytype.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@ def test_geometrytype_different_srid_values(self):
7878
geometry_type_2 = GeometryType("ANY")
7979
self.assertNotEqual(geometry_type_1.srid, geometry_type_2.srid)
8080

81+
# The tests below verify GEOMETRY type JSON parsing based on the CRS value.
82+
83+
def test_geometrytype_from_invalid_crs(self):
84+
"""Test that GeometryType construction fails when an invalid CRS is specified."""
85+
86+
for invalid_crs in ["srid", "any", "ogccrs84", "ogc:crs84", "ogc:CRS84", "asdf", ""]:
87+
with self.assertRaises(IllegalArgumentException) as error_context:
88+
GeometryType._from_crs(invalid_crs)
89+
crs_header = "[ST_INVALID_CRS_VALUE] Invalid or unsupported CRS"
90+
self.assertEqual(
91+
str(error_context.exception),
92+
f"{crs_header} (coordinate reference system) value: '{invalid_crs}'.",
93+
)
94+
95+
def test_geometrytype_from_valid_crs(self):
96+
"""Test that GeometryType construction passes when a valid CRS is specified."""
97+
98+
supported_crs = {
99+
"OGC:CRS84": 4326,
100+
}
101+
for valid_crs, srid in supported_crs.items():
102+
geometry_type = GeometryType._from_crs(valid_crs)
103+
self.assertEqual(geometry_type.srid, srid)
104+
self.assertEqual(geometry_type.typeName(), "geometry")
105+
self.assertEqual(geometry_type.simpleString(), f"geometry({srid})")
106+
self.assertEqual(geometry_type.jsonValue(), f"geometry({valid_crs})")
107+
self.assertEqual(repr(geometry_type), f"GeometryType({srid})")
108+
81109

82110
class GeometryTypeTest(GeometryTypeTestMixin, ReusedSQLTestCase):
83111
pass

python/pyspark/sql/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def _from_crs(cls, crs: str, alg: str) -> "GeographyType":
567567
# Algorithm value must be validated, although only SPHERICAL is supported currently.
568568
if alg != cls.DEFAULT_ALG:
569569
raise IllegalArgumentException(
570-
errorClass="INVALID_ALGORITHM_VALUE",
570+
errorClass="ST_INVALID_ALGORITHM_VALUE",
571571
messageParameters={
572572
"alg": str(alg),
573573
},

0 commit comments

Comments
 (0)