Skip to content

[GH-2925] Add ST_Expand(box2d, ...) overloads#2930

Open
jiayuasu wants to merge 2 commits intoapache:masterfrom
jiayuasu:feature/box2d-st-expand
Open

[GH-2925] Add ST_Expand(box2d, ...) overloads#2930
jiayuasu wants to merge 2 commits intoapache:masterfrom
jiayuasu:feature/box2d-st-expand

Conversation

@jiayuasu
Copy link
Copy Markdown
Member

@jiayuasu jiayuasu commented May 8, 2026

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

Adds Box2D variants of ST_Expand alongside the existing Geometry-input overloads:

  • ST_Expand(box: Box2D, units: double) -> Box2D — uniform expansion on both axes
  • ST_Expand(box: Box2D, dx: double, dy: double) -> Box2D — per-axis expansion

PostGIS-compatible. NULL on null input. Negative deltas are allowed; if a negative delta produces xmin > xmax or ymin > ymax, the result is returned as-is so callers can detect the degenerate bbox via the accessor functions (matches the Phase 1 design where xmin > xmax is reserved for future antimeridian-wraparound semantics rather than treated as in-band empty).

Where the changes land

Layer Change
common/.../Functions.java Two new expand(Box2D, ...) overloads
spark/common/.../expressions/Functions.scala ST_Expand case class extended with two new inferrableFunction* overloads (Geometry overloads kept)
flink/.../expressions/Functions.java Two new eval(Box2D, ...) methods on the existing ST_Expand class
Python / Scala DataFrame API / Flink Catalog No change — wrappers were already polymorphic over Column, JVM-side overload resolution handles Box2D vs Geometry

How was this patch tested?

  • common/.../FunctionsTest.javaexpandBox2D (uniform, per-axis, negative delta producing degenerate output, NULL input).
  • spark/common/.../functionTestScala.scala — "Passed ST_Expand for Box2D" (uniform, per-axis, shrink, NULL Box2D input).
  • flink/.../FunctionTest.javatestExpandBox2D (uniform + per-axis through Flink Table API).
  • python/tests/sql/test_function.pytest_st_expand_box_2d (uniform + per-axis through PySpark SQL with Box2D Python deserialization).

Did this PR include necessary documentation updates?

Adds Box2D variants alongside the existing Geometry-input ST_Expand:

  ST_Expand(box: Box2D, units: double) -> Box2D
  ST_Expand(box: Box2D, dx: double, dy: double) -> Box2D

Negative deltas are allowed and may produce a degenerate Box2D where
xmin > xmax or ymin > ymax. The result is returned as-is; callers can
detect degenerate output via the accessor functions. NULL on null
input.

JVM, Python, Flink wrappers all updated. The Python and Scala
DataFrame API wrappers were already polymorphic over Column, so no
wrapper-side change was needed; the JVM-side overload resolution
handles Box2D vs Geometry. Flink ScalarFunction needed two new eval
overloads on the existing class.

Closes apache#2925.
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

Adds Box2D overloads for ST_Expand across Sedona’s common layer and engine integrations (Spark + Flink), aligning behavior with PostGIS and enabling bbox expansion/shrink directly on Box2D values.

Changes:

  • Add org.apache.sedona.common.Functions.expand(Box2D, delta) and expand(Box2D, dx, dy) overloads.
  • Extend Spark SQL ST_Expand expression dispatch to support Box2D inputs alongside existing Geometry overloads.
  • Add Flink ST_Expand Box2D eval overloads and expand test coverage across common/Spark/Flink/Python.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
common/src/main/java/org/apache/sedona/common/Functions.java Adds Box2D expand overloads implementing uniform and per-axis expansion with null propagation.
common/src/test/java/org/apache/sedona/common/FunctionsTest.java Adds unit tests for Box2D expansion, including negative deltas and null input.
spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala Updates Spark ST_Expand inferred dispatch to include Box2D overloads (and resolves overload ambiguity via typed lambdas).
spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala Adds Spark SQL test coverage for ST_Expand(Box2D, ...), including shrink and null input.
flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java Adds Flink ST_Expand Box2D overloads with appropriate @DataTypeHint/serializer wiring.
flink/src/test/java/org/apache/sedona/flink/FunctionTest.java Adds Flink Table API test for Box2D expansion results.
python/tests/sql/test_function.py Adds PySpark SQL test verifying Box2D results are deserialized correctly for the new overloads.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +246 to +252
def test_st_expand_box_2d(self):
df = self.spark.sql("""
SELECT
ST_Expand(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')), 1.0) AS uniform,
ST_Expand(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')), 2.0, 0.5) AS per_axis
""")
row = df.first()
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.

Done in 5158213 — added NULL Box2D coverage to both signatures. NULL row deserializes to None as expected.

Comment on lines +538 to +555
@Test
public void testExpandBox2D() {
Table t =
tableEnv.sqlQuery(
"SELECT ST_Expand(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')), 1.0) AS uniform,"
+ " ST_Expand(ST_Box2D(ST_GeomFromText('POLYGON((1 2, 1 5, 4 5, 4 2, 1 2))')), 2.0, 0.5) AS per_axis");
Row row = first(t);
Box2D uniform = (Box2D) row.getField(0);
assertEquals(0.0, uniform.getXMin(), 0.0);
assertEquals(1.0, uniform.getYMin(), 0.0);
assertEquals(5.0, uniform.getXMax(), 0.0);
assertEquals(6.0, uniform.getYMax(), 0.0);
Box2D perAxis = (Box2D) row.getField(1);
assertEquals(-1.0, perAxis.getXMin(), 0.0);
assertEquals(1.5, perAxis.getYMin(), 0.0);
assertEquals(6.0, perAxis.getXMax(), 0.0);
assertEquals(5.5, perAxis.getYMax(), 0.0);
}
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.

Done in 5158213 — added NULL Box2D coverage to both Flink signatures. NULL field comes back as null Java reference through getField.

Both Python and Flink tests now exercise NULL Box2D input through the
uniform and per-axis ST_Expand signatures, confirming NULL propagates
through the deserialization paths.
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.

Implement ST_Expand for Box2D

2 participants