Skip to content

[GH-3632] Place an object once in each partition of a KNN join - #3633

Open
jiayuasu wants to merge 1 commit into
apache:masterfrom
jiayuasu:fix/knn-duplicate-neighbors
Open

jiayuasu wants to merge 1 commit into
apache:masterfrom
jiayuasu:fix/knn-duplicate-neighbors

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?

The partitioned KNN join (KNNJoin) could return one object two or three times for a query, in place of farther neighbors, when the objects are lines or polygons. In the example of #3632, the query in the middle of a grid of squares got the square around it three times and no other object.

ExtendedQuadTree.placeObject puts an object of the object side into every partition whose expanded boundary it meets. The expanded boundary of a partition is a list of cells, and the index of these boundaries has one entry for each cell. An object whose envelope met several cells of one partition was found once per cell and placed in that partition as many times, so the index of the partition held copies of it and the nearest neighbor search returned them as different neighbors. placeObject now places an object once in each partition.

The broadcast joins were not affected. A point meets one cell unless it lies exactly on a cell edge: the grid of #3632 with points as the objects returns no repeated pair.

How was this patch tested?

  • ExtendedQuadTreeTest (new): a square across the middle of the boundary, where the cells meet, is placed once in each partition that it belongs to. Before the change it was placed 25 times in 7 partitions.
  • KnnJoinSuite: squares around the points of a 21 by 21 grid, in four partitions, are joined with the grid points on the partitioned plan with k = 3. With tie breakers the pairs are equal to a ranking of all pairs with RANK(). Without them the join returns three rows per query and no pair twice.

Both tests fail without the change. KnnJoinSuite and KnnJoinQueryTest 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.

Did this PR include necessary documentation updates?

  • No, this PR does not affect any public API so no need to change the documentation.

The expanded boundary of a KNN partition is a list of cells, and the index
of these boundaries has one entry for each cell. An object that met
several cells of one partition was placed in that partition once per cell.
The partitioned join then returned the object as two or more of the k
neighbors of a query, in place of farther neighbors. A point meets one
cell unless it is on a cell edge, so this showed with lines and polygons.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Partitioned KNN join returns the same object more than once for a query when objects are not points

1 participant