[SPARK-57088] [SQL] Allow non-deterministic ranking expression for EXACT NEAREST BY#56128
Closed
zhidongqu-db wants to merge 3 commits into
Closed
[SPARK-57088] [SQL] Allow non-deterministic ranking expression for EXACT NEAREST BY#56128zhidongqu-db wants to merge 3 commits into
zhidongqu-db wants to merge 3 commits into
Conversation
dtenedor
approved these changes
May 26, 2026
Contributor
dtenedor
left a comment
There was a problem hiding this comment.
LGTM, thanks for working on this.
sigmod
approved these changes
May 27, 2026
Contributor
|
thanks, merging to master/4.x/4.2! |
cloud-fan
added a commit
that referenced
this pull request
May 27, 2026
…CT NEAREST BY ### What changes were proposed in this pull request? Removes the `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION` rejection in `CheckAnalysis` so the `EXACT` mode of `NEAREST BY JOIN` (added in SPARK-56395) accepts non-deterministic ranking expressions, the same way `APPROX` already does. Concretely: - Drop the `NearestByJoin` arm in `CheckAnalysis` that failed analysis when `approx = false` and the ranking expression was non-deterministic. - Change `NearestByJoin.allowNonDeterministicExpression` to return `true` unconditionally (was previously returning `approx`). - Delete the `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION` error condition. - Update scaladoc/comments in `NearestByJoin` and `RewriteNearestByJoin` to reflect that both modes permit a non-deterministic ranking expression. - Update the user-facing docs in `sql-ref-syntax-qry-select-join.md`. - Convert the existing rejection tests (Scala, Python, SQL golden) to positive tests asserting that EXACT + a non-deterministic ranking expression now succeeds. ### Why are the changes needed? `APPROX` vs. `EXACT` and determinism are orthogonal concerns: - `APPROX` vs. `EXACT` is about the search algorithm contract: `APPROX` permits the optimizer to use faster approximate strategies (e.g. indexed ANN); `EXACT` forces brute-force evaluation. - Determinism is a property of the ranking expression itself. Ordinary joins, for example, accept non-deterministic join conditions without forcing the user into an "approximate" join. `EXACT` describes algebraic semantics ("compute the exact top-K according to the user's ranking expression"); it does not promise reproducibility across runs when the ranking expression is itself non-deterministic. Coupling the two was an over-restriction that this PR removes. ### Does this PR introduce _any_ user-facing change? Yes. Queries of the form ```sql SELECT ... FROM left JOIN right EXACT NEAREST k BY {DISTANCE | SIMILARITY} <non-deterministic expression> ``` previously failed at analysis with `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION`; they are now accepted and evaluated through the same brute-force rewrite as the `APPROX` variant. The error condition `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION` is removed. ### How was this patch tested? - Updated `DataFrameNearestByJoinSuite`: the rejection test is converted to a positive test asserting the result count (21/21 passing locally). - Updated the PySpark equivalent in `test_nearest_by_join.py`. - Updated the SQL golden file `join-nearest-by.sql` (replaced the failing-EXACT query with a `COUNT(*)` query mirroring the existing APPROX case); regenerated `results/` and `analyzer-results/`. `SQLQueryTestSuite -z join-nearest-by` passes (2/2). - `RewriteNearestByJoinSuite` (12/12) still passes — the materializing-Project path in the optimizer rewrite already handled non-deterministic ranking expressions; only the analyzer gate changes. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 4.7) Closes #56128 from zhidongqu-db/allow-exact-no-deter-expr. Lead-authored-by: Zero Qu <zhidong.qu@databricks.com> Co-authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com> (cherry picked from commit 2603f6a) Signed-off-by: Wenchen Fan <wenchen@databricks.com>
cloud-fan
added a commit
that referenced
this pull request
May 27, 2026
…CT NEAREST BY ### What changes were proposed in this pull request? Removes the `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION` rejection in `CheckAnalysis` so the `EXACT` mode of `NEAREST BY JOIN` (added in SPARK-56395) accepts non-deterministic ranking expressions, the same way `APPROX` already does. Concretely: - Drop the `NearestByJoin` arm in `CheckAnalysis` that failed analysis when `approx = false` and the ranking expression was non-deterministic. - Change `NearestByJoin.allowNonDeterministicExpression` to return `true` unconditionally (was previously returning `approx`). - Delete the `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION` error condition. - Update scaladoc/comments in `NearestByJoin` and `RewriteNearestByJoin` to reflect that both modes permit a non-deterministic ranking expression. - Update the user-facing docs in `sql-ref-syntax-qry-select-join.md`. - Convert the existing rejection tests (Scala, Python, SQL golden) to positive tests asserting that EXACT + a non-deterministic ranking expression now succeeds. ### Why are the changes needed? `APPROX` vs. `EXACT` and determinism are orthogonal concerns: - `APPROX` vs. `EXACT` is about the search algorithm contract: `APPROX` permits the optimizer to use faster approximate strategies (e.g. indexed ANN); `EXACT` forces brute-force evaluation. - Determinism is a property of the ranking expression itself. Ordinary joins, for example, accept non-deterministic join conditions without forcing the user into an "approximate" join. `EXACT` describes algebraic semantics ("compute the exact top-K according to the user's ranking expression"); it does not promise reproducibility across runs when the ranking expression is itself non-deterministic. Coupling the two was an over-restriction that this PR removes. ### Does this PR introduce _any_ user-facing change? Yes. Queries of the form ```sql SELECT ... FROM left JOIN right EXACT NEAREST k BY {DISTANCE | SIMILARITY} <non-deterministic expression> ``` previously failed at analysis with `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION`; they are now accepted and evaluated through the same brute-force rewrite as the `APPROX` variant. The error condition `NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION` is removed. ### How was this patch tested? - Updated `DataFrameNearestByJoinSuite`: the rejection test is converted to a positive test asserting the result count (21/21 passing locally). - Updated the PySpark equivalent in `test_nearest_by_join.py`. - Updated the SQL golden file `join-nearest-by.sql` (replaced the failing-EXACT query with a `COUNT(*)` query mirroring the existing APPROX case); regenerated `results/` and `analyzer-results/`. `SQLQueryTestSuite -z join-nearest-by` passes (2/2). - `RewriteNearestByJoinSuite` (12/12) still passes — the materializing-Project path in the optimizer rewrite already handled non-deterministic ranking expressions; only the analyzer gate changes. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 4.7) Closes #56128 from zhidongqu-db/allow-exact-no-deter-expr. Lead-authored-by: Zero Qu <zhidong.qu@databricks.com> Co-authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com> (cherry picked from commit 2603f6a) Signed-off-by: Wenchen Fan <wenchen@databricks.com>
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.
What changes were proposed in this pull request?
Removes the
NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSIONrejection inCheckAnalysisso theEXACTmode ofNEAREST BY JOIN(added in SPARK-56395) accepts non-deterministic ranking expressions, the same wayAPPROXalready does.Concretely:
NearestByJoinarm inCheckAnalysisthat failed analysis whenapprox = falseand the ranking expression was non-deterministic.NearestByJoin.allowNonDeterministicExpressionto returntrueunconditionally (was previously returningapprox).NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSIONerror condition.NearestByJoinandRewriteNearestByJointo reflect that both modes permit a non-deterministic ranking expression.sql-ref-syntax-qry-select-join.md.Why are the changes needed?
APPROXvs.EXACTand determinism are orthogonal concerns:APPROXvs.EXACTis about the search algorithm contract:APPROXpermits the optimizer to use faster approximate strategies (e.g. indexed ANN);EXACTforces brute-force evaluation.EXACTdescribes algebraic semantics ("compute the exact top-K according to the user's ranking expression"); it does not promise reproducibility across runs when the ranking expression is itself non-deterministic. Coupling the two was an over-restriction that this PR removes.Does this PR introduce any user-facing change?
Yes. Queries of the form
previously failed at analysis with
NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSION; they are now accepted and evaluated through the same brute-force rewrite as theAPPROXvariant.The error condition
NEAREST_BY_JOIN.EXACT_WITH_NONDETERMINISTIC_EXPRESSIONis removed.How was this patch tested?
DataFrameNearestByJoinSuite: the rejection test is converted to a positive test asserting the result count (21/21 passing locally).test_nearest_by_join.py.join-nearest-by.sql(replaced the failing-EXACT query with aCOUNT(*)query mirroring the existing APPROX case); regeneratedresults/andanalyzer-results/.SQLQueryTestSuite -z join-nearest-bypasses (2/2).RewriteNearestByJoinSuite(12/12) still passes — the materializing-Project path in the optimizer rewrite already handled non-deterministic ranking expressions; only the analyzer gate changes.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.7)