johanl-db commented on code in PR #59596:
URL: https://github.com/apache/spark/pull/59596#discussion_r4600232819


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala:
##########
@@ -139,30 +144,70 @@ object ResolveChangelogTable extends Rule[LogicalPlan] {
         updatedRel = injectNetChangeComputation(
           updatedRel, rowIdExprs, table.changelogContext.computeUpdates())
       }
-      updatedRel
+      markInternalChangelogSubtree(updatedRel, rel)
 
     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)
+          resolvedRel, changelog, table.requiresCarryoverRemoval, 
table.requiresUpdateDetection,
+          retainRowIds = table.requiresNetChangeCollapse)
       }
       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())
+      }
+      markInternalChangelogSubtree(updatedRel, rel)
+  }
+
+  /** Rejects user access to metadata columns exposed only for changelog 
post-processing. */
+  private[analysis] def checkNoUserMetadataColumnAccess(plan: LogicalPlan): 
Unit = {

Review Comment:
   I would say for now hide `_metadata` and delay to a future follow up 
surfacing it to the user, unless it's trivial to propagate `_metadata` through 
the Window that's injected for post-processing (I imagine it could be simple)



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