LukaZdravic opened a new pull request, #60570:
URL: https://github.com/apache/spark/pull/60570

   ### What changes were proposed in this pull request?
   `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.
   
   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 the single-pass resolver never 
sees these keys. 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 checks DataFrame ASOF keys during analysis. That check does not 
see metadata columns. 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.
   
   ### Does this PR introduce _any_ user-facing change?
   Only with the internal `spark.sql.join.sortMergeAsOfJoin.enabled` flag on 
(default off): DataFrame ASOF joins keyed on metadata columns now run instead 
of failing. SPARK-60582 is not in a released version. With the flag off, and 
for SQL `MATCH_CONDITION`, plans and errors stay the same.
   
   ### How was this patch tested?
   New tests in `SortMergeAsOfJoinSuite`. Each test that runs a join checks the 
exact rows and the `SortMergeAsOfJoinExec` plan, with the join codegen off and 
on.
   - Parquet inputs keyed on `_metadata.file_size`, backward, forward and 
nearest. The file sizes order the rows. The same join on the fields selected 
into columns returns the same rows.
   - A parquet self join through aliases (`l._metadata.file_size`, 
`r._metadata.file_size`) gives each key its own input.
   - Two DSv2 tables keyed on their `index` metadata column, a top-level 
metadata column: all three directions, a left outer join whose last left row 
has no match (NULL right columns), and an `ON` key next to the metadata key.
   - `AS_OF_JOIN.KEY_REFERENCE` still fails a key over both inputs, two keys 
over the same input, and a self join without aliases.
   - A known limit stays: SQL `MATCH_CONDITION` still rejects `_metadata` 
operands with `ASOF_JOIN_MATCH_CONDITION_TABLE_REFERENCE`. A follow-up JIRA 
will cover it.
   
   The tests that run a join failed before this change with 
`AS_OF_JOIN.KEY_REFERENCE`. Connect `joinAsOf` calls the same 
`Dataset.joinAsOf`, so it needs no test of its own.
   
   Also ran `RewriteAsOfJoinSuite`, `AnalysisErrorSuite`, 
`AsOfJoinMatchConditionTypesSuite`, `UnsupportedOperationsSuite`, 
`AsOfJoinSQLSuite`, `AsOfJoinSortMergeSQLSuite`, `DataFrameAsOfJoinSuite`, 
`StreamingAsOfJoinSuite`, `ExplicitlyUnsupportedResolverFeatureSuite`, 
`ResolverGuardSuite` and `SparkConnectProtoSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Generated-by: Claude Code
   


-- 
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]

Reply via email to