[GH-3034] Spatial join planner: index ST_3DDWithin distance joins#3035
Merged
Conversation
Wires ST_3DDWithin into the existing distance-join pipeline using the same XY-projection trick as apache#3032 (Box3D ST_Intersects / ST_Contains). Correctness rests on the inequality |A_XY - B_XY|_2 <= |A - B|_3D, so expanding each XY rectangle by `distance` and probing the 2D R-tree gives a valid superset filter; the per-pair condition then enforces the 3D distance via the original predicate. - JoinQueryDetector: new case for `ST_3DDWithin(Seq(left, right, d))` producing a JoinQueryDetection with SpatialPredicate.INTERSECTS (R-tree's distance-expanded envelope pass), `condition` (the full original join condition) as the per-pair filter, and `distance = Some(d)` so the executor builds an expanded envelope. Routes both overloads through the same plan — Geometry inputs land on the JTS envelope, Box3D inputs on the XY footprint materialised by apache#3032. Explain output adds an "ST_3DDWithin" label. - OptimizableJoinCondition: add `ST_3DDWithin` to the whitelist of distance-join predicates. Tests: - New Box3DDWithinJoinSuite covers broadcast and non-broadcast distance joins, the closed-interval threshold edge, and a discriminating row XY-overlapping with the left side but Z=99 away — the R-tree pairs it via XY-expansion, the 3D refine rejects it. Geometry-input case (POINT Z) exercises the same plan via the Geometry overload.
f91f508 to
f3d8f69
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends Sedona SQL’s spatial join planner so ST_3DDWithin joins (both Geometry and Box3D overloads) can use the existing indexed distance-join pipeline instead of falling back to row-by-row evaluation, improving join planning from O(n×m) toward indexed execution with a 2D superset filter + 3D refine.
Changes:
- Add
ST_3DDWithin(left, right, distance)detection inJoinQueryDetectorto plan an indexed distance join via 2D envelope expansion + per-pair refinement using the original predicate. - Allow
ST_3DDWithininOptimizableJoinConditionso the optimizer accepts it as an eligible distance-join predicate. - Add
Box3DDWithinJoinSuitecoverage for broadcast and non-broadcast indexed execution, threshold edge semantics, and correctness of the 3D refine vs XY-only candidates.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| spark/common/src/test/scala/org/apache/sedona/sql/Box3DDWithinJoinSuite.scala | Adds focused planner/executor tests ensuring ST_3DDWithin uses indexed join paths and that 3D refinement filters XY-only candidates correctly. |
| spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/strategy/join/OptimizableJoinCondition.scala | Whitelists ST_3DDWithin as an optimizable distance-join predicate. |
| spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/strategy/join/JoinQueryDetector.scala | Plans ST_3DDWithin using the distance-join pipeline (expanded 2D envelopes for indexing + original predicate for 3D refinement) and adds an EXPLAIN relationship label. |
💡 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?
ST_3DDWithinwas unplanned, so 3D distance joins ran as O(n × m) row-by-row evaluation. This PR wires both inferrable branches into the existing indexed distance-join pipeline.Approach — same XY-projection trick as #3032 (Box3D
ST_Intersects/ST_Contains), applied to the distance-join path. Correctness rests on the inequality|A_XY − B_XY|_2 ≤ |A − B|_3D, so expanding each XY rectangle bydistanceand probing the 2D R-tree is a valid superset filter; the actual 3D refine runs at the per-pair condition step.JoinQueryDetector: new arm forST_3DDWithin(Seq(left, right, d))that produces aJoinQueryDetectionwithSpatialPredicate.INTERSECTSfor the distance-expanded envelope R-tree pass,condition(the full original join condition) as the per-pair filter, anddistance = Some(d)so the executor builds the expanded envelope. Routes both overloads through the same plan — Geometry inputs land on the JTS envelope; Box3D inputs land on the XY footprint already materialised inTraitJoinQueryBase.shapeToGeometryby [GH-3031] Box3D spatial join: index ST_Intersects / ST_Contains on Box3D #3032. EXPLAIN output adds anST_3DDWithinlabel.OptimizableJoinCondition: addST_3DDWithinto the whitelist of distance-join predicates soisDistanceJoinOptimizableaccepts it (parallel to the existingST_DWithinentry).TraitJoinQueryBaserequired — Box3D's XY-footprint materialisation already lives there from [GH-3031] Box3D spatial join: index ST_Intersects / ST_Contains on Box3D #3032, andtoExpandedEnvelopeRDDalready calls into it.ST_3DDWithin has no Geography overload, so no Geography branching is needed in the new arm (the analyzer would reject Geography input upstream).
How was this patch tested?
Box3DDWithinJoinSuitecovers broadcast index distance join, non-broadcastDistanceJoinExec, the closed-interval threshold edge, and a discriminating row that is XY-overlapping with the left side but Z=99 away — the 2D R-tree pairs it via XY-expansion, the 3D refine rejects it. Also a Geometry-input case (POINT Z) that exercises the same indexed plan via the Geometry overload.Box3DDWithinJoinSuite+Box3DJoinSuite+Box3DDWithinSuite+Box2DJoinSuite: 30 tests pass locally.Did this PR include necessary documentation updates?