LukaZdravic opened a new pull request, #60608: URL: https://github.com/apache/spark/pull/60608
### What changes were proposed in this pull request? Backport of https://github.com/apache/spark/pull/60570 (fb5b1c9a4f73) to branch-4.4. The cherry-pick applies cleanly. The three touched files (`ResolveAsOfJoin.scala`, `classic/Dataset.scala`, `SortMergeAsOfJoinSuite.scala`) are the same on branch-4.4 and on master before #60570, so the change is the same as on master. The `skip-checks: true` trailer inherited from the master commit is dropped from the commit message. `ResolveAsOfJoin` now assigns each DataFrame `joinAsOf` key to an input by using the input's output plus all of its metadata columns. Before, it used the output plus only the hidden `USING` and `NATURAL` join columns. So a key over a regular metadata column, such as `_metadata.file_size` of a file source or the `index` column of a DSv2 table, matched neither input. Once each key has its input, the join resolves. `AddMetadataColumns` then adds the referenced metadata columns to the inputs, so the planner can bind the sort keys. Regular joins already accept these keys: a DataFrame join or a SQL `JOIN` on `l._metadata.file_size <= r._metadata.file_size` works, and the analyzer adds `_metadata` to both relations. `Dataset.joinAsOf` now also moves the metadata columns of each key onto its own join input, as it already does for output columns. In a self join, or a join with a filtered copy of the same input, the analyzer gives the right input new attributes, also for its metadata columns. Before, a right key such as `df.col("_metadata.file_size")` kept the left input's attribute, so both keys named the left input. Only a metadata column of the key's own Dataset moves, and only to the column at the same position with the same name. So a key taken from the other input still fails. SQL `MATCH_CONDITION` operands do not change: they still match only the output and the hidden join columns. The single-pass resolver does not change either: `ResolverGuard` sends every DataFrame plan that already holds a file or DSv2 relation to the fixed-point analyzer, so in dual-run and tentative mode the single-pass resolver never sees these keys. Forced single-pass (`spark.sql.analyzer.singlePassResolver.enabled=true`) skips the guard and fails on these plans with an internal error, before and after this change. Today `ResolverGuard` lists `_metadata` as unsupported for every query, not only ASOF. Once the single-pass resolver supports metadata columns, the DataFrame key sets in `AsOfJoinResolver` need the same change. ### Why are the changes needed? SPARK-60582 (64439a7b3b20) is in branch-4.4 and checks DataFrame ASOF keys during analysis. That check does not see metadata columns. Without this backport, 4.4.0 ships the error below. With `spark.sql.join.sortMergeAsOfJoin.enabled=true`: ```scala val left = spark.read.parquet(leftPath) val right = spark.read.parquet(rightPath) left.joinAsOf(right, left.col("_metadata.file_size"), right.col("_metadata.file_size"), usingColumns = Seq.empty, joinType = "inner", tolerance = null, allowExactMatches = true, direction = "backward") ``` Before: `[AS_OF_JOIN.KEY_REFERENCE] ... Got: "_metadata.file_size AS file_size" and "_metadata.file_size AS file_size".` The same happens for forward and nearest. Each key references only its own input, so the error is wrong. After: the join plans `SortMergeAsOfJoinExec`. It returns the same rows as the same join keyed on the two fields selected into columns first. A self join without aliases, `df.joinAsOf(df, df.col("index"), df.col("index"), ...)` on a DSv2 table, compared each left row's index with itself. With the flag on it failed with `AS_OF_JOIN.KEY_REFERENCE`. With the flag off (the default) it returned wrong rows: ``` DSv2 table: id x, y, z at index 0, 1, 2. Backward, no exact matches. Aliased join (l.index, r.index): (y, x), (z, y) Unaliased self join, before: no rows Unaliased self join, after: (y, x), (z, y) ``` ### Does this PR introduce _any_ user-facing change? Yes, a wrong result is fixed. With `spark.sql.join.sortMergeAsOfJoin.enabled` off (the default), a DataFrame self join without aliases keyed on a metadata column compared each left row's value with itself. Keyed on a DSv2 metadata column, it returned no rows without exact matches, and an arbitrary right row for every left row with exact matches. It now returns the as-of matches, the same rows as the join written with aliases. This fix has no flag of its own. Keyed on a file `_metadata` field, a flag-off DataFrame ASOF join fails with `UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.AGGREGATE_FUNCTION_MIXED_OUTER_LOCAL_REFERENCES`, before and after this change, with or without aliases. With the internal flag on: DataFrame ASOF joins keyed on metadata columns now run instead of failing, also self joins without aliases. SPARK-60582 is not in a released version (it is not in 4.3.0). SQL `MATCH_CONDITION` plans and errors stay the same. ### How was this patch tested? The cherry-pick brings the new `SortMergeAsOfJoinSuite` tests of #60570. See that PR for what each test checks. On branch-4.4 with this commit, with Maven (`-pl sql/connect/server -am`), 0 tests failed: - catalyst: 382 passed. - sql/core: 1134 passed, 5 canceled, 3 ignored. The 5 canceled tests are Python UDF tests, which cancel themselves when Python UDF testing is off. `SortMergeAsOfJoinSuite` ran 160 tests, the same as on master. - Connect (`SparkConnectProtoSuite`): 75 passed. The suites: `SortMergeAsOfJoinSuite`, `RewriteAsOfJoinSuite`, `AnalysisErrorSuite`, `AsOfJoinMatchConditionTypesSuite`, `UnsupportedOperationsSuite`, `AsOfJoinSQLSuite`, `AsOfJoinSortMergeSQLSuite`, `DataFrameAsOfJoinSuite`, `StreamingAsOfJoinSuite`, `ExplicitlyUnsupportedResolverFeatureSuite`, `ResolverGuardSuite` and `SparkConnectProtoSuite`. Since `Dataset.joinAsOf` changed, also the other suites that call it: `JoinSuite`, `WholeStageCodegenSuite`, `UnionCodegenSuite`, `SQLMetricsSuite`, `ExternalLookupJoinSuite`, `StreamingV2ScanPushDownSuite`, `TimestampNanosJoinAnsiOnSuite` and `TimestampNanosJoinAnsiOffSuite`. Scalastyle: 0 errors on the main sources of catalyst (748 files) and sql/core (872 files), and 0 errors on the three changed files, including the test suite. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code This pull request and its description were written by Isaac. -- 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]
