Skip to content

[GH-2887] Box2D DataFrame API + Python bindings for Phase 1#2904

Merged
jiayuasu merged 3 commits intoapache:masterfrom
jiayuasu:feature/box2d-python-bindings
May 6, 2026
Merged

[GH-2887] Box2D DataFrame API + Python bindings for Phase 1#2904
jiayuasu merged 3 commits intoapache:masterfrom
jiayuasu:feature/box2d-python-bindings

Conversation

@jiayuasu
Copy link
Copy Markdown
Member

@jiayuasu jiayuasu commented May 5, 2026

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

Adds the Scala DataFrame API wrappers and matching PySpark Column wrappers for the Phase 1 Box2D surface.

The two pieces ship together because they're a hard dependency: the Python wrappers dispatch through the Scala st_functions / st_constructors / st_aggregates objects (see dataframe_api.py:78-79), so the Python bindings would fail at runtime without the Scala wrappers. The original plan to defer DataFrame API work (#2891) has been folded into this PR.

Scala DataFrame API (closes #2891)

org.apache.spark.sql.sedona_sql.expressions.*:

Wrapper Object
ST_Box2D(geom: Column | String) st_functions
ST_MakeBox2D(ll, ur) st_constructors
ST_GeomFromBox2D(box) st_constructors
ST_Extent(geom) st_aggregates

The existing ST_XMin/XMax/YMin/YMax and ST_AsText wrappers already accept any Column, so no Scala-side changes are needed for them — JVM overload resolution handles Box2D arguments.

Python bindings (closes #2887)

sedona.spark.sql.*:

Wrapper Module
ST_Box2D(geometry) st_functions
ST_MakeBox2D(p1, p2) st_constructors
ST_GeomFromBox2D(box) st_constructors
ST_Extent(geom) st_aggregates

The Python Box2DType UDT and Box2D value class were added in #2878. Collected results materialize as Box2D objects with xmin/ymin/xmax/ymax attributes.

How was this patch tested?

  • python/tests/sql/test_function.pytest_st_box_2d, test_st_as_text_box_2d.
  • python/tests/sql/test_constructor_test.pytest_st_make_box_2d, test_st_geom_from_box_2d (POLYGON/POINT/LINESTRING dispatch).
  • python/tests/sql/test_aggregate_functions.pytest_st_extent, test_st_extent_returns_null_for_empty_input.
  • python/tests/sql/test_dataframe_api.py — parametric coverage for ST_Box2D, ST_MakeBox2D, ST_GeomFromBox2D, ST_Extent exercising the Column-API wrapper dispatch end-to-end.

Did this PR include necessary documentation updates?

Mirrors the Phase 1 SQL surface added in apache#2890, apache#2895, apache#2897, apache#2898,
apache#2899 in PySpark wrappers:

- ST_Box2D in st_functions
- ST_MakeBox2D and ST_GeomFromBox2D in st_constructors
- ST_Extent in st_aggregates

Accessor overloads (ST_XMin/XMax/YMin/YMax) and ST_AsText already
worked with Box2D inputs through their existing wrappers; SQL
overload resolution happens on the JVM side.

The Python Box2DType UDT and Box2D value class were merged in apache#2878,
so collected results materialize as Box2D Python objects with
xmin/ymin/xmax/ymax attributes.

Closes apache#2887.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Comment on lines +662 to +663
"""
return _call_st_function("ST_Box2D", geometry)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct catch — confirmed by dataframe_api.py:78-79 which dispatches via getattr(jvm, object_name). Added the Scala wrapper st_functions.ST_Box2D (Column + String overloads) in c134508. Closing #2891 here too.

Comment on lines +568 to +570
:rtype: Column
"""
return _call_constructor_function("ST_MakeBox2D", (lower_left, upper_right))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added st_constructors.ST_MakeBox2D (Column + String overloads) in c134508.

Comment on lines +584 to +586
:rtype: Column
"""
return _call_constructor_function("ST_GeomFromBox2D", box)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added st_constructors.ST_GeomFromBox2D (Column + String overloads) in c134508.

Comment on lines +53 to +56
:return: Box2D representing the union of bounding boxes of the geometry column.
:rtype: Column
"""
return _call_aggregate_function("ST_Extent", geometry)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added st_aggregates.ST_Extent (Column + String overloads) in c134508, following the udaf-wrapper pattern of the other aggregates in that file.

Comment on lines +188 to +192
def test_st_box_2d(self):
df = self.spark.sql("""
SELECT
ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')) AS bbox,
ST_Box2D(ST_GeomFromText('POINT EMPTY')) AS bbox_empty,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added DataFrame API parametric coverage in test_dataframe_api.py for ST_Box2D, ST_MakeBox2D, ST_GeomFromBox2D, and ST_Extent in c134508. The existing test_dataframe_function harness handles Box2D return values via __eq__ on the Python Box2D value class.

The Python wrappers in apache#2887 dispatch through the Scala DataFrame API
objects (org.apache.spark.sql.sedona_sql.expressions.st_functions /
st_constructors / st_aggregates) — without matching Scala wrappers,
the Python bindings would fail at runtime. Folding the deferred
DataFrame API work (apache#2891) into this PR since it is a hard dependency.

- st_functions.ST_Box2D
- st_constructors.ST_MakeBox2D, ST_GeomFromBox2D
- st_aggregates.ST_Extent

Each gets the standard Column / String overload pair. Existing
ST_XMin/XMax/YMin/YMax and ST_AsText wrappers already accept any
Column (including Box2D), so they need no Scala-side changes.

Adds DataFrame API coverage in test_dataframe_api.py for the new
wrappers (ST_Box2D, ST_MakeBox2D, ST_GeomFromBox2D, ST_Extent).
@jiayuasu jiayuasu changed the title [GH-2887] Python bindings for Box2D Phase 1 surface [GH-2887] Box2D DataFrame API + Python bindings for Phase 1 May 6, 2026
The two_points fixture has a=(0,0,0) and b=(3,0,4) — both have y=0.
ST_MakeBox2D drops Z, so the bbox is Box2D(0, 0, 3, 0), not
Box2D(0, 0, 3, 4) as I had wrongly entered (confused Z with Y).
@jiayuasu jiayuasu added this to the sedona-1.9.1 milestone May 6, 2026
@jiayuasu jiayuasu merged commit 593305f into apache:master May 6, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scala/Python DataFrame API wrappers for Box2D Phase 1 functions Python bindings for Box2D scalar functions and ST_Extent

2 participants