mihailoale-db commented on code in PR #59267:
URL: https://github.com/apache/spark/pull/59267#discussion_r4261063509


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/resolver/AsOfJoinResolver.scala:
##########
@@ -124,11 +132,43 @@ class AsOfJoinResolver(
         )
         cteRegistry.currentScope.tryPutWithCTE(
           unresolvedOperator = unresolvedAsOfJoin,
-          resolvedOperator = resolvedJoin
+          resolvedOperator = resolvedJoinWithOriginalOutput
         )
     }
   }
 
+  /**
+   * If the join uses a hidden column, wraps it in a [[Project]] that also 
keeps the hidden columns.
+   */
+  private def retainOriginalOutputIfHiddenColumnIsReferenced(
+      unresolvedAsOfJoin: AsOfJoin,
+      resolvedJoin: AsOfJoin,
+      leftNameScope: NameScope,
+      rightNameScope: NameScope): LogicalPlan = {
+    val originalOutput = AsOfJoin.computeOutput(
+      joinType = resolvedJoin.joinType,
+      leftOutput = leftNameScope.output,
+      rightOutput = rightNameScope.output
+    )
+    val originalOutputSet = AttributeSet(originalOutput)
+
+    if (resolvedJoin.references.subsetOf(originalOutputSet)) {
+      resolvedJoin
+    } else {
+      val hiddenColumns =
+        if (unresolvedAsOfJoin.containsTag(ResolverTag.TOP_LEVEL_OPERATOR)) {

Review Comment:
   Could we reuse this code between `buildUsingProject` and here?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/resolver/AsOfJoinResolver.scala:
##########
@@ -124,11 +132,43 @@ class AsOfJoinResolver(
         )
         cteRegistry.currentScope.tryPutWithCTE(
           unresolvedOperator = unresolvedAsOfJoin,
-          resolvedOperator = resolvedJoin
+          resolvedOperator = resolvedJoinWithOriginalOutput
         )
     }
   }
 
+  /**
+   * If the join uses a hidden column, wraps it in a [[Project]] that also 
keeps the hidden columns.
+   */
+  private def retainOriginalOutputIfHiddenColumnIsReferenced(
+      unresolvedAsOfJoin: AsOfJoin,
+      resolvedJoin: AsOfJoin,
+      leftNameScope: NameScope,
+      rightNameScope: NameScope): LogicalPlan = {
+    val originalOutput = AsOfJoin.computeOutput(
+      joinType = resolvedJoin.joinType,
+      leftOutput = leftNameScope.output,
+      rightOutput = rightNameScope.output
+    )
+    val originalOutputSet = AttributeSet(originalOutput)
+
+    if (resolvedJoin.references.subsetOf(originalOutputSet)) {
+      resolvedJoin
+    } else {
+      val hiddenColumns =
+        if (unresolvedAsOfJoin.containsTag(ResolverTag.TOP_LEVEL_OPERATOR)) {
+          Seq.empty
+        } else {
+          resolvedJoin.output.filter { attribute =>

Review Comment:
   Is there a way to avoid calling `.output` since it is recursive? something 
with `NameScope.output` or similar?



##########
sql/core/src/test/resources/sql-tests/inputs/join-asof-semantics.sql:
##########
@@ -228,3 +228,88 @@ FROM VALUES (TIMESTAMP '2026-06-29 10:00:05', 'AAPL', 100) 
AS t(trade_time, symb
      VALUES (TIMESTAMP '2026-06-29 10:00:00', 'AAPL', 100) AS q(quote_time, 
symbol, quantity)
   MATCH_CONDITION (t.trade_time >= q.quote_time)
   USING (symbol, quantity);
+
+-- FVT-ASOF-2-027: a hidden USING column of the right input is a 
MATCH_CONDITION operand
+SELECT l.lts, r.rts
+FROM (VALUES (1, 10), (2, 20)) AS l(k, lts) ASOF JOIN

Review Comment:
   Usually it's better to have temp views (or similar) and reuse data. They are 
also more representative since they are used more often then inline tables 
(`VALUES`)



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