cloud-fan commented on code in PR #60570:
URL: https://github.com/apache/spark/pull/60570#discussion_r4594972799


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveAsOfJoin.scala:
##########
@@ -74,10 +75,12 @@ object ResolveAsOfJoin extends Rule[LogicalPlan] with 
SQLConfHelper {
             case None => joinBase
           }
         case (Some(leftExpr), None, Some(rightExpr)) =>
+          // A DataFrame key can use a metadata column, such as a `_metadata` 
field, that its
+          // input does not output yet. AddMetadataColumns adds it once the 
join is resolved.
           AsOfJoinMatchConditionResolution.materializeDataFrame(
             join = joinBase,
-            leftSet = leftSet,
-            rightSet = rightSet,
+            leftSet = AttributeSet(left.output ++ left.metadataOutput),
+            rightSet = AttributeSet(right.output ++ right.metadataOutput),

Review Comment:
   **Non-blocking (P2):** With `spark.sql.join.sortMergeAsOfJoin.enabled` on, 
this set now admits the right input's metadata columns, but in a self join the 
right key never names them. `createAsOfJoinPlan` in `Dataset.joinAsOf` 
(sql/core/src/main/scala/org/apache/spark/sql/classic/Dataset.scala:865-875) 
moves a key onto its own join input only for attributes in `logicalPlan.output` 
and `other.logicalPlan.output`. A key such as `df.col("_metadata.file_size")` 
resolves through `metadataOutput` instead, so when `other` shares a relation 
with `this`, as in `df.joinAsOf(df, df.col("_metadata.file_size"), 
df.col("_metadata.file_size"), ...)` or a join with `df.filter(...)`, 
DeduplicateRelations gives the right input a fresh `_metadata` attribute while 
the right key keeps the left one. Both keys then fall in the left set, and 
`normalizeMatchOperands` throws `AS_OF_JOIN.KEY_REFERENCE`, which the new third 
negative case in SortMergeAsOfJoinSuite.scala:1981-1983 asserts as expected.
   
   The same call keyed on a regular column runs (the existing `self join` test, 
SortMergeAsOfJoinSuite.scala:1326-1343), so after this PR one self join 
succeeds or fails depending on whether its key is a regular column or a 
metadata column. The error is loud and an alias works around it. Spark Connect 
is not affected the same way: its keys are resolved by plan id, and an 
unaliased Connect self join already fails with `AMBIGUOUS_COLUMN_REFERENCE` for 
regular keys too.
   
   **Recommended change:** In `createAsOfJoinPlan`, move metadata-column 
attributes of each key onto the matching metadata attribute of its own join 
input, the same way output-column attributes are moved today: an attribute of 
`leftAsOf` found in this Dataset's metadata columns maps to the attribute at 
the same position in the left join input's metadata columns, and an attribute 
of `rightAsOf` found in `other`'s metadata columns maps to the one at the same 
position in the right join input's metadata columns. Then change the third case 
of the "join keys over file metadata columns must each reference one input" 
test in SortMergeAsOfJoinSuite.scala (lines 1981-1983) from an expected 
AS_OF_JOIN.KEY_REFERENCE error into a positive DataFrame ASOF join test that 
checks rows for an unaliased self join and for a join with a filtered copy of 
the same input, and add a flag-off DataFrame ASOF join test for the same self 
join.
   
   **Why this works:** DeduplicateRelations gives the right input fresh 
metadata attributes, so a metadata key taken from `other` keeps naming the left 
input until something moves it. Moving it onto the right input's attribute 
makes the right key reference only the right input, so the key-side check 
orients the two keys as it does for regular columns, AddMetadataColumns adds 
the right input's `_metadata`, and the flag-off rewrite compares the left value 
with the right one.
   
   **Compatibility:** Output-column keys keep their current mapping, aliased 
metadata keys keep resolving by qualifier, and every AS_OF_JOIN.KEY_REFERENCE 
case other than the unaliased self join keeps failing with the same parameters.
   
   **Risks:** The mapping also applies with 
spark.sql.join.sortMergeAsOfJoin.enabled off, which is the default. There, 
RewriteAsOfJoin treats every key attribute of the left input as a reference to 
the left row (RewriteAsOfJoin.scala:61-75), so today the same unaliased self 
join compares each left row's metadata value with itself and returns an 
arbitrary right row, or no match when exact matches are not allowed. The repair 
changes those rows to the intended as-of matches. This corrects a wrong result 
and has no flag of its own; reverting the mapping restores the old behavior. 
When the deduplicated right input lists its metadata columns differently from 
`other`, the name check leaves the key unchanged, so that join keeps failing 
with AS_OF_JOIN.KEY_REFERENCE as it does today.
   
   **Constraints:** Move a metadata attribute only when it belongs to that 
Dataset's own metadata columns and the attribute at the same position on the 
join input has the same name; otherwise leave the key unchanged, so that a key 
that really references both inputs, or two keys taken from the same input of a 
non-self join, still fails with AS_OF_JOIN.KEY_REFERENCE. Leave the key-side 
sets in ResolveAsOfJoin.scala, the SQL MATCH_CONDITION branch, and the 
single-pass resolver unchanged.
   
   **Exclusions:**
   - SQL MATCH_CONDITION operands over metadata columns, which the author 
defers to a follow-up JIRA.
   - The single-pass resolver's DataFrame key sets, which the author defers 
until that resolver supports metadata columns.
   - Spark Connect `joinAsOf`, which needs no change: its keys are resolved by 
plan id against the join's inputs, and an unaliased Connect self join already 
fails with AMBIGUOUS_COLUMN_REFERENCE for regular keys too.
   
   **Verification:**
   - **Regression:** With the sort-merge flag on, an unaliased self join of one 
parquet input keyed on _metadata.file_size, and a join of that input with a 
filtered copy of itself, run for backward, forward and nearest with ASOF join 
codegen off and on, plan the sort-merge operator, and each return the same rows 
as the same join written with aliases.
   - **Behavior:** With the sort-merge flag on, an unaliased self join of a 
DSv2 table keyed on its index metadata column returns the same rows as the 
aliased self join on that column.
   - **Compatibility:** With the sort-merge flag off, the unaliased parquet 
self join keyed on _metadata.file_size returns the same rows as the same join 
with the flag on.
   - **Compatibility:** A key that references both inputs and two keys taken 
from the same input of a non-self join still fail analysis with 
AS_OF_JOIN.KEY_REFERENCE and the same refs1 and refs2 values, and a self join 
keyed on a regular column still returns its current rows.



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