dongjoon-hyun opened a new pull request, #60783: URL: https://github.com/apache/spark/pull/60783
### What changes were proposed in this pull request? This PR fixes a correctness issue and an `INTERNAL_ERROR` in DataFrame as-of joins (`Dataset.joinAsOf`, PySpark `DataFrame._joinAsOf` and `pyspark.pandas.merge_asof`) that are rewritten into a correlated scalar subquery, which is the default (`spark.sql.join.sortMergeAsOfJoin.enabled` is false), when the internal `spark.sql.optimizer.decorrelateInnerQuery.enabled` is false. `RewriteAsOfJoin` rewrites the join into the following subquery for each left row. ```sql SELECT MIN_BY(STRUCT(right.*), left.t - right.t) AS __nearest_right__ FROM right WHERE condition AND left.t >= right.t AND right.t >= left.t - tolerance ``` Its outer references are in the `MIN_BY` order expression and in a non-equality predicate under an `Aggregate`, and only `DecorrelateInnerQuery` can decorrelate them. When the configuration is false, `PullupCorrelatedPredicates` uses the legacy `pullOutCorrelatedPredicates` instead, which only pulls correlated predicates out of a `Filter`: - It pulls the non-equality predicates above the `Aggregate` and adds their right-side columns to the grouping. The subquery then returns one row per group instead of the nearest right row, and the join returns every matching right row. This shows when the left as-of key is a constant, because then no other outer reference is left. - It leaves the outer reference in the `MIN_BY` order expression, so the query fails at execution with `INTERNAL_ERROR`. This happens for every as-of join whose left as-of key references the left side, with or without a join condition. The analyzer rejects both shapes in user-written subqueries when the configuration is false, but `RewriteAsOfJoin` is an optimizer rule and builds them after analysis. With this PR, when the configuration is false, `RewriteAsOfJoin` decorrelates its subquery as the optimizer does when the configuration is true. It runs the two rules that do it then, `PullOutNestedDataOuterRefExpressions` and `PullupCorrelatedPredicates`, with the configuration enabled, on the `Project` that holds the subquery alone. A leaf stands in for the left side during these runs, so that other subqueries in it still follow the configuration. The later run of `PullupCorrelatedPredicates` keeps the decorrelated subquery as it is, since it has no outer references left, and `RewriteCorrelatedScalarSubquery` builds the join as usual. The optimized plan is the same as with the configuration enabled. Nothing changes when the configuration is true, which is the default. ### Why are the changes needed? `spark.sql.optimizer.decorrelateInnerQuery.enabled` is an internal configuration (since 3.2.0) that turns off the `DecorrelateInnerQuery` framework. Nothing documents turning it off as a supported fallback, but when it is turned off, every non-sort-merge as-of join fails or returns wrong results. This affects all releases since [v3.3.0](https://github.com/apache/spark/releases/tag/v3.3.0) (2022-06-09), which added `RewriteAsOfJoin`. It is confirmed on `master`, [v3.5.9](https://github.com/apache/spark/releases/tag/v3.5.9) (2026-07-12), [v4.0.5](https://github.com/apache/spark/releases/tag/v4.0.5) (2026-10-21), [v4.1.4](https://github.com/apache/spark/releases/tag/v4.1.4) (2026-10-21), [v4.2.1](https://github.com/apache/spark/releases/tag/v4.2.1) (2026-10-21) and [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27). ```python from pyspark.sql.functions import lit spark.conf.set("spark.sql.optimizer.decorrelateInnerQuery.enabled", "false") l = spark.sql("SELECT * FROM VALUES (1, 'x', 'a'), (5, 'y', 'b'), (10, 'z', 'c') AS l(a, b, lv)") r = spark.sql("SELECT * FROM VALUES (1, 'v', 1), (2, 'w', 2), (3, 'x', 3), (6, 'y', 6), " "(7, 'z', 7) AS r(a, b, rv)") # Issue 1: It should return 3 rows, one nearest right row per left row, but it returns 9 rows, # every right row with `rv <= l.a`. l._joinAsOf(r, lit(10), r.a, on=l.a >= r.rv, how="left").collect() # Issue 2: It should return [(1, x, a, 1, v, 1), (5, y, b, 3, x, 3), (10, z, c, 7, z, 7)], # but it fails with `[INTERNAL_ERROR] Cannot evaluate expression: outer(a#0)`. l._joinAsOf(r, l.a, r.a, how="left").collect() ``` ### Does this PR introduce _any_ user-facing change? Yes, only when `spark.sql.optimizer.decorrelateInnerQuery.enabled` is false. DataFrame as-of joins return the same results as with the configuration enabled, instead of wrong results or `INTERNAL_ERROR`. ### How was this patch tested? Pass the CIs with the newly added test case in `DataFrameAsOfJoinSuite`. It checks that, with the configuration false, the optimized plans and the results are the same as with it enabled, including a join condition on a map member of the left side. Without this fix, it fails because the optimized plans differ. I also checked manually that the results and the optimized plans are the same with the configuration false and true for inner and left outer joins in all three directions, with a tolerance, with `usingColumns`, for a self join and for nested as-of joins. A left side with a correlated `EXISTS` subquery also gives the same results. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
