Repository navigation
Conversation
ST_KNN takes an optional constant argument that says whether the join also returns the objects as near as the k-th nearest one. It comes after use_spheroid for geometries, ST_KNN(a, b, k, use_spheroid, include_tie_breakers), and after k for geographies, ST_KNN(a, b, k, include_tie_breakers), which take no use_spheroid. Without the argument the spark.sedona.join.knn.includeTieBreakers setting decides, as before.
…ith equal geometries With tie breakers, the join took an object as a tie when it was as far from the query as the k-th neighbor in the units of the coordinates, also when use_spheroid ranks the neighbors by a distance in meters. An object at one degree of latitude was a tie of an object at one degree of longitude. The search for ties now measures with the item distance that ranked the neighbors, and looks for candidates in the range of longitudes and latitudes that covers this distance. The join also left out a tied row when its geometry was equal to the geometry of a row that was already a neighbor. The neighbors are now told apart by the indexed object, so each row of a repeated point is returned.
…f the k-th neighbor The search for ties looked for candidates in an envelope around the query. With use_spheroid this envelope was folded into the longitudes from -180 to 180, while the index keeps the coordinates as they are. A tie at 269 or 271 degrees, which ST_ShiftLongitude produces, was not found. The join now takes the distance of the farthest of the k neighbors that the index returns, reads every object within this distance from the index and ranks them again. The range of longitudes for the distance comes from Haversine.expandEnvelope with the query moved to the meridian of 0 degrees. It is searched in each full turn of longitude that the index reaches, so a tie on the other side of the antimeridian or in the other longitude convention is found as well. The search of the index prunes a branch by its distance in degrees, which can leave out an object that is as near in meters, or nearer. With tie breakers the result no longer depends on what that search left out.
This was referenced Dec 6, 2026
This branch has not been deployed
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?
[GH-XXX] my subject. Closes KNN Join including tie config needs to be a function call param like SedonaDB #3556What changes were proposed in this PR?
ST_KNNtakes an optionalinclude_tie_breakersargument. It says whether a KNN join also returns the objects that are as near as the k-th nearest one (true), or at most k objects (false):Until now only the
spark.sedona.join.knn.includeTieBreakerssetting decided this. The join reads the setting when it runs, so the rows of a query or a view depended on the session that executed it, and two KNN joins in one query could not differ. With the argument, the choice is part of the query. SedonaDB has the same argument (apache/sedona-db#1577).ST_KNN(a, b, k, use_spheroid, include_tie_breakers), the same form as in SedonaDB.ST_KNN(a, b, k, include_tie_breakers). The geography forms take nouse_spheroid, as withST_Bufferon geography, so the argument comes right afterk. The five-argument form does not accept geographies and fails at analysis time.trueorfalse. A column reference or a NULL fails at planning time with a message that names the argument.The value is kept as an
Option[Boolean]onJoinQueryDetectionand on the four KNN join nodes (KNNJoinExec,BroadcastQuerySideKNNJoinExec,BroadcastObjectSideKNNJoinExec,BroadcastGeographyKNNJoinExec).Nonemeans that the setting decides. The Scala DataFrame API has two newST_KNNoverloads for the five-argument form.A fourth argument on geographies was rejected before this PR, and geography
ST_KNNis not in a release yet.LocalOutlierFactorand the GeoPandassjoin_neareststill use the setting and are not changed here.Which objects are tie breakers
The geometry join had two defects in the rows that it returns with tie breakers. They are fixed here, because the argument makes these rows part of a query's result:
use_spheroidistrueand the neighbors are ranked by a distance in meters. For a query atPOINT(0 80), the object atPOINT(0 81)was returned as a tie of the object atPOINT(1 80), which is about six times nearer.InMemoryKNNJoinIteratornow finds the ties in two steps, as before, but with the metric of the join in both:With
use_spheroidthe distance is in meters and the index is in degrees. The range of latitudes and longitudes for the distance comes fromHaversine.expandEnvelope, which distance joins already use. The query is moved to the meridian of 0 degrees for it, so that the range is not folded at 180 degrees, and the range is searched in each full turn of longitude that the index reaches. A tie is found at longitudes above 180, such as the output ofST_ShiftLongitude, on the other side of the antimeridian, and in a table with the other longitude convention.The search of the index (
STRtree.nearestNeighbour) prunes a branch by its distance in degrees and compares that with distances in meters. Withuse_spheroidit can leave out an object that is as near as the ones it returns, or nearer, when the object is a full turn of longitude away in degrees. With tie breakers the result no longer depends on this, because the objects within the distance are ranked again. Without tie breakers the join returns what that search returns, as before this PR. That is #3634.The cost is the cost before this PR: one search for k neighbors and one range query. In a benchmark of the iterator with
k = 1and tie breakers on 10,000 objects, 5,000 places with two records a few meters apart, a query takes 296 µs on the sphere and 3.7 µs in the plane, against 284 µs and 4.2 µs before. With a query polygon that has 1,000 points inside, in the plane, a query takes 120 µs, against 113 µs before.On the partitioned plan a line or a polygon can still be returned more than once for a query, with and without tie breakers. That is #3632 and is not changed here.
How was this patch tested?
KnnJoinSuite:use_spheroid. The test uses objects at 80 degrees north whose nearest neighbor is different in degrees and on the spheroid.include_tie_breakersare rejected.use_spheroidfalse and true, also when they tie with an object on the other side of the query, and when k is above the number of objects.use_spheroidfalse and not withuse_spheroidtrue. On a grid of one degree cells joined with itself, the ties are the neighbors in four directions in degrees and the neighbors to the east and west on the sphere.ST_ShiftLongitude, at 270 degrees, have the same two tied neighbors, foruse_spheroidfalse and true.k = 1, and the pair that ties for the third place fork = 3.InMemoryKNNJoinIteratorTest(new). The first three cases use an index of three levels:Integer.MAX_VALUE.KnnJoinGeographySuite:ST_KNN(geog, geog, k, true)with the setting off andST_KNN(geog, geog, k, false)with the setting on, and the four-argument form through the DataFrame API.The tests of the argument, of the two defects and of the ties a full turn of longitude away fail without the change. The test with
ST_ShiftLongitudepasses without it too: it keeps the ties that the join already found there. The KNN suites pass on Spark 3.5 with Scala 2.12 and on Spark 4.0 with Scala 2.13. The wholespark/commonmodule on Spark 3.5 passes locally except for 12 tests that need the libpostal data directory or Docker.Did this PR include necessary documentation updates?
docs/api/sql/NearestNeighbourSearching.md, the row ofspark.sedona.join.knn.includeTieBreakersondocs/api/sql/Parameter.md, and the.zh.mdversion of both pages. The argument is marked as available sincev2.0.0. The GeoPandas pages and thesjoin_nearestdocstring no longer say that rows with identical geometries can be left out of the ties.