Skip to content

[GH-3635] Remove use_spheroid from ST_KNN on geometries - #3636

Open
jiayuasu wants to merge 1 commit into
apache:masterfrom
jiayuasu:feature/knn-remove-use-spheroid
Open

jiayuasu wants to merge 1 commit into
apache:masterfrom
jiayuasu:feature/knn-remove-use-spheroid

Conversation

@jiayuasu

@jiayuasu jiayuasu commented Dec 6, 2026

Copy link
Copy Markdown
Member

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

ST_KNN on geometries loses its fourth argument, use_spheroid. It ranks geometries by their distance in the plane, in the units of the coordinates:

SELECT *
FROM queries JOIN objects
ON ST_KNN(queries.geom, objects.geom, 3)

With use_spheroid set to true the join ranked the neighbors by a distance in meters, while it indexed and partitioned the objects by their coordinates in degrees. It could return a farther neighbor when a nearer one was on the other side of the antimeridian or was written in the other longitude convention, and the search of the index measured every object for each query (#3634). Geography columns rank on the sphere with an index made for it, so that is where a distance on the sphere goes:

  • ST_KNN(ST_GeomToGeography(a), ST_GeomToGeography(b), k) reads two geometry columns as geographies. A geography ST_KNN join is a broadcast join.
  • Or transform both sides to a projected coordinate reference system and call ST_KNN(a, b, k).

What is removed:

  • SQL: the four-argument form. ST_KNN(a, b, k, false) and ST_KNN(a, b, k, true) fail at analysis with "no overloaded function accepts 4 arguments".
  • Scala DataFrame API: ST_KNN(a, b, k, useSphere) with columns and with names.
  • The three KNN join nodes (KNNJoinExec, BroadcastQuerySideKNNJoinExec, BroadcastObjectSideKNNJoinExec) no longer take isGeography.
  • RDD API: JoinQuery.KNNJoinQuery and JoinQuery.JoinParams no longer take a DistanceMetric, and the classes DistanceMetric, HaversineItemDistance and SpheroidDistance are deleted. KnnJoinIndexJudgement and InMemoryKNNJoinIterator measure with EuclideanItemDistance only. KNNJoinQuery is not in the documentation and has no Python or R wrapper.

What follows from it:

  • The local outlier factor passed its useSphere on to ST_KNN. ST_LocalOutlierFactor(geometry, k, useSphere), localOutlierFactor and local_outlier_factor keep the parameter and raise an IllegalArgumentException when it is true. The message says to transform the geometries to a projected coordinate reference system first.
  • The GeoPandas sjoin_nearest passed false as the fourth argument and now calls the three-argument form.

#3630 then adds include_tie_breakers as the fourth argument for geometries, as for geographies.

How was this patch tested?

  • KnnJoinSuite: a new test that ST_KNN(geom, geom, k, true) and ST_KNN(geom, geom, k, false) fail at analysis. The other tests call the three-argument form. The test of the sphere ranking is removed, and the test of empty geometries no longer runs once per metric.
  • KnnJoinGeographySuite: a new test of the replacement. With the points of ST_KNN with use_spheroid = true can return a farther neighbor across the antimeridian, and its index search measures every object #3634 as geometry columns, ST_KNN(ST_GeomToGeography(qgeom), ST_GeomToGeography(ogeom), 2) returns the object that is 22 m away on the other side of the antimeridian and the one that is 111 m away.
  • LocalOutlierFactorTest: a new test that useSphere = true raises an error that names the parameter.
  • KnnJoinQueryTest, KnnShapeBindingTest and BarrierFunctionTest follow the new signatures.

The new rejection test and the outlier factor test fail without the change. The test of the replacement passes without it too. These suites and GeoStatsSuite pass on Spark 3.5 with Scala 2.12 and on Spark 4.0 with Scala 2.13. The whole spark/common module on Spark 3.5 passes locally except for 12 tests that need the libpostal data directory or Docker. The Python tests were not run locally. The documentation builds, and the new links between the pages resolve in English and in Chinese.

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation: the format, the distance and the examples on docs/api/sql/NearestNeighbourSearching.md and its .zh.md version, a section in docs/setup/migration-2.0.md, useSphere on docs/api/sql/Clustering-Functions/ST_LocalOutlierFactor.md and on docs/api/stats/sql.md with its .zh.md version, and the ST_KNN calls of the notebook docs/usecases/03-fire-risk-fusion.ipynb.

ST_KNN(a, b, k, use_spheroid) ranked geometries by a distance on the
sphere while the join indexed and partitioned them by their coordinates
in degrees. It could return a farther neighbor across the antimeridian
and measured every object for each query.

ST_KNN now has the three-argument form only and ranks geometries by their
distance in the plane. Geography columns rank on the sphere.

The distance metric goes from the join as well: the three KNN join nodes,
JoinQuery.KNNJoinQuery and JoinParams, KnnJoinIndexJudgement and
InMemoryKNNJoinIterator, with the classes DistanceMetric,
HaversineItemDistance and SpheroidDistance. The local outlier factor
raises an error when useSphere is true.
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant