[GH-3398] Keep extra predicates on broadcast and nested KNN joins - #3399
Merged
jiayuasu merged 5 commits intoSep 26, 2026
Merged
Conversation
Broadcast KNN plans dropped every extra join predicate, and a nested AND hid the non-KNN conjuncts from the one-level extractor. Flatten the conjuncts and apply them on both broadcast sides.
kalayciburak
force-pushed
the
fix/gh-3398-knn-extra-predicates
branch
from
September 24, 2026 16:44
437b34e to
44bb618
Compare
…edicates Use the residual condition already returned by OptimizableJoinCondition for regular and both broadcast KNN plans. Remove duplicate extraction that discarded every KNN marker, and reject additional markers before execution instead of silently ignoring their semantics. Replace all-false-only regression coverage with independently selective conditions, nullable values, both relation orders and broadcast sides, three- and four-argument KNN, and planning-time rejection cases. Validation: 337 join tests pass on Spark 3.5; 62 KNN tests pass on Spark 4.1.1/Scala 2.13. The nine new rejection tests failed on the original PR.
Validate every detected spatial join residual so an ST_KNN marker left after ST_Intersects or ST_DWithin fails during planning with an actionable ordering message. Preserve the existing error for multiple KNN predicates and the behavior of valid KNN-first joins. Add regression coverage for both matcher paths and all join strategies, including positive KNN-first spatial filters with reversed table order. Document residual filtering, the optimizer pushdown caveat, and the single-KNN and spatial predicate ordering requirements.
…aries Quay no longer grants anonymous pull access to minio/minio, causing both S3 reader tests to time out fetching their container image and cancel the Spark build matrix. Build the test image from the same pinned MinIO release on GitHub and verify architecture-specific SHA-256 checksums. Share the image lazily between the reader suites while retaining separate containers per test. Write the Dockerfile build context against Spark's bundled tar API to avoid changing Spark 3.5's older compression dependencies for these tests.
Keep the MinIO S3 test image from upstream apache#3408 and remove the branch-only image builder and Dockerfile. The remaining PR diff contains only the KNN join fix, its tests, and documentation.
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?
KNN joins could drop additional ON conditions when broadcasting either side or when conditions contained nested conjunctions, producing different results depending on the physical strategy.
Reuse the residual condition already extracted by
OptimizableJoinConditionand pass it unchanged to the regular, query-side broadcast, and object-side broadcast KNN executors. Remove the duplicate KNN-specific extractor. The original condition remains available in the regular plan for inspection.Reject
ST_KNNmarkers left in the residual of any detected spatial join during planning, including markers nested inside residual expressions. A physical KNN join implements only its selected predicate, so multiple KNN predicates are unsupported. If another spatial predicate is selected first, the error directs users to placeST_KNNfirst, preserving the existing predicate-selection rule while preventing a later per-pair evaluation failure.Regression coverage checks independently selective predicates, mixed accepted/rejected pairs, null values, predicate ordering and nesting, both SQL table orders, both broadcast directions, three- and four-argument KNN, and filtering after nearest-neighbor selection. It also covers valid KNN-first spatial residuals and planning-time rejection of multiple-KNN conditions or KNN after another spatial predicate.
Document that residual ON predicates filter the selected neighbors without replacing rejected pairs, distinguish this from optimizer filter pushdown, and explain the single-KNN and spatial predicate ordering requirements.
How was this patch tested?
The branch now includes the latest master. Its merged MinIO test-image fix resolves the prior image-pull failure; this PR does not change those reader tests.
Java 17, Maven offline with cached dependencies:
KNN predicate is not supported.KnnJoinSuite,SpatialJoinSuite,BroadcastIndexJoinSuite, andSphereDistanceJoinSuite— 349 tests passed.KnnJoinSuite— 74 tests passed.KnnJoinSuite— 74 tests passed.KnnJoinSuite— 74 tests passed.git diff --checkpassed.Commands, with
JAVA_HOMEset to Java 17 andSPARK_LOCAL_IP=127.0.0.1:Did this PR include necessary documentation updates?