gengliangwang commented on code in PR #59596:
URL: https://github.com/apache/spark/pull/59596#discussion_r4618524910


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala:
##########
@@ -139,30 +139,48 @@ object ResolveChangelogTable extends Rule[LogicalPlan] {
         updatedRel = injectNetChangeComputation(
           updatedRel, rowIdExprs, table.changelogContext.computeUpdates())
       }
-      updatedRel
+      changelogOutput(updatedRel, rel, table.name())
 
     case rel @ StreamingRelationV2(_, _, table: ChangelogTable, _, _, _, _, _, 
_)
         if !table.resolved =>
       val changelog = table.changelog
       val resolvedRel = rel.copy(table = table.copy(resolved = true))
       var updatedRel: LogicalPlan = resolvedRel
-      if (table.requiresCarryoverRemoval || table.requiresUpdateDetection) {
+      val requiresRowLevelPostProcessing =
+        table.requiresCarryoverRemoval || table.requiresUpdateDetection
+      if (requiresRowLevelPostProcessing) {
         updatedRel = addStreamingRowLevelPostProcessing(
           resolvedRel, changelog, table.requiresCarryoverRemoval, 
table.requiresUpdateDetection)
       }
       if (table.requiresNetChangeCollapse) {
-        // Resolve the rowId references against `updatedRel` (the 
post-row-level plan)
-        // rather than the bare `resolvedRel`. The streaming row-level rewrite 
uses
-        // Aggregate + Generate(Inline), neither of which preserves the 
original
-        // attribute ExprIds for the inlined columns; resolving against 
`resolvedRel`
-        // yields stale ExprIds that fail post-analysis attribute resolution. 
The
-        // row-level rewrite preserves the connector's schema (column names) 
on its
-        // output, so name-based resolution against `updatedRel` recovers the 
right
-        // attributes regardless of any preceding wrapping.
+        // The row-level aggregate preserves row identity as scalar helper 
columns. Hidden
+        // metadata is not part of its buffered row payload and cannot be 
resolved afterwards.
+        val rowIdExprs = if (requiresRowLevelPostProcessing) {
+          changelog.rowId().indices.map { idx =>
+            getAttribute(updatedRel, NetChangesHelperColumns.rowIdColumn(idx))
+          }
+        } else {
+          
V2ExpressionUtils.resolveRefs[NamedExpression](changelog.rowId().toSeq, 
updatedRel)
+        }
         updatedRel = addStreamingNetChangeComputation(
-          updatedRel, changelog, table.changelogContext.computeUpdates())
+          updatedRel, rowIdExprs, table.changelogContext.computeUpdates())
       }
-      updatedRel
+      changelogOutput(updatedRel, rel, table.name())

Review Comment:
   **[P1] Preserve streaming source naming through the output boundary**
   
   This unconditional wrapper breaks named streaming reads even when no 
post-processing or metadata is required. RelationResolution already adds a 
SubqueryAlias; changelogOutput now inserts another alias and a Project 
underneath it. NameStreamingSources.propagateSourceNames only handles a bare 
source or one alias directly wrapping a source, and its fallback accepts only 
Unassigned names. Consequently, a UserProvided NamedStreamingRelation remains 
unresolved and CheckAnalysis throws `Found the unresolved operator`. This 
affects both `.name()` and SQL `IDENTIFIED BY`, including their existing 
ChangelogEndToEndSuite cases. Source-name propagation must traverse the new 
boundary or occur before it is inserted.
   
   **Failure scenario:** Enable 
spark.sql.streaming.queryEvolution.enableSourceEvolution, then call 
spark.readStream.name("cdc").option("startingVersion", 
"1").changes("catalog.events") against a valid changelog with all 
post-processing flags false. Analysis fails instead of producing the named 
stream.



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