[GH-3021] Python Box3DType UDT bindings#3022
Merged
Merged
Conversation
Adds the Python-side UDT so Box3D columns deserialize into a real Python object instead of requiring a `CAST(... AS STRING)` workaround. Pairs the Python `Box3DType` with the Scala `Box3DUDT.pyUDT` override that was deferred in apache#2978 pending the Python class. - `python/sedona/spark/core/geom/box3d.py`: new `Box3D` class with six float fields in PostGIS storage order, `__eq__` / `__hash__` / `__repr__`, mirroring the Box2D class shape. - `python/sedona/spark/sql/types.py`: new `Box3DType(UserDefinedType)`. `sqlType` mirrors `Box3DUDT.sqlType` (six non-nullable doubles in the same order); `scalaUDT` points at `org.apache.spark.sql.sedona_sql.UDT.Box3DUDT`. - Re-exported `Box3DType` from `python/sedona/spark/sql/__init__.py` and `python/sedona/spark/__init__.py` alongside `Box2DType`. - Restored `Box3DUDT.pyUDT` override on the Scala side now that the Python class actually exists (removed in apache#2978 to satisfy the P2 review note about a dangling pyUDT pointer). - Replaced the `CAST(geom AS STRING)` workaround in the existing `ST_3DMakeBox` / `ST_Box3D` / `ST_3DExtent` entries in `python/tests/sql/test_dataframe_api.py` with direct `Box3D` field-wise comparisons, matching how Box2D is tested.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds first-class Python bindings for Sedona’s Box3D UDT so Spark SQL Box3D columns deserialize into a real Python Box3D object (instead of requiring string casts), aligning Python behavior with the existing JVM Box3D UDT.
Changes:
- Restores/introduces the Scala
Box3DUDT.pyUDToverride pointing to the Python UDT class. - Adds Python
Box3Dvalue class andBox3DType(UserDefinedType)implementation matching the Scala struct layout/order. - Updates Python SQL/DataFrame tests to assert
Box3Dobjects directly (removing the priorCAST(... AS STRING)workaround) and re-exportsBox3DTypefrom public Python modules.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/UDT/Box3DUDT.scala | Adds the pyUDT override so Spark can deserialize Box3D values into Python via the new UDT. |
| python/sedona/spark/core/geom/box3d.py | Introduces the Python Box3D value object (6 doubles in PostGIS order) with equality/hash/representation. |
| python/sedona/spark/sql/types.py | Implements Box3DType UDT (StructType of 6 non-nullable doubles) with serialize/deserialize to Box3D. |
| python/sedona/spark/sql/init.py | Re-exports Box3DType from the sedona.spark.sql package surface. |
| python/sedona/spark/init.py | Re-exports Box3DType from the top-level sedona.spark namespace. |
| python/tests/sql/test_dataframe_api.py | Updates existing Box3D-related test expectations to compare Box3D objects directly (no string-cast workaround). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Did you read the Contributor Guide?
Is this PR related to a ticket?
What changes were proposed in this PR?
Adds the Python-side Box3D UDT so columns deserialize into a real Python object instead of requiring a
CAST(... AS STRING)workaround. Pairs the PythonBox3DTypewith the ScalaBox3DUDT.pyUDToverride that was deferred in #2978 pending the Python class.python/sedona/spark/core/geom/box3d.py: newBox3Dclass with six float fields in PostGIS storage order (xmin, ymin, zmin, xmax, ymax, zmax),__eq__/__hash__/__repr__, mirroring the Box2D class shape.python/sedona/spark/sql/types.py: newBox3DType(UserDefinedType).sqlTypemirrorsBox3DUDT.sqlType(six non-nullable doubles in the same order);scalaUDTpoints atorg.apache.spark.sql.sedona_sql.UDT.Box3DUDT.Box3DTypefrompython/sedona/spark/sql/__init__.pyandpython/sedona/spark/__init__.pyalongsideBox2DType.Box3DUDT.pyUDToverride on the Scala side now that the Python class actually exists. This was removed in [GH-2973] Box3D foundation: value class + UDT + Catalyst plumbing #2978 to satisfy the P2 review note about a dangling pyUDT pointer.CAST(geom AS STRING)workaround in the existingST_3DMakeBox/ST_Box3D/ST_3DExtententries inpython/tests/sql/test_dataframe_api.pywith directBox3Dfield-wise comparisons, matching how Box2D is tested.How was this patch tested?
test_dataframe_api.pyentries (ST_3DMakeBox,ST_Box3D,ST_3DExtent,ST_3DBoxIntersects,ST_3DBoxContains) now collectBox3DPython objects and pass with the real UDT. All green locally.spark.sql("SELECT ST_Box3D(...)")→df.first()[0]and verified the result is aBox3Dinstance with the expected field values.Box3DUDTSuite,Box3DConstructorSuite,Box3DAccessorSuite,Box3DPredicateSuite,Box3DExtentSuite— all 23 tests pass locally after thepyUDTrestore.Did this PR include necessary documentation updates?