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


##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/Changelog.java:
##########
@@ -218,4 +218,20 @@ default NamedReference[] rowId() {
   default NamedReference rowVersion() {
     throw new UnsupportedOperationException("rowVersion is not supported.");
   }
+
+  /**
+   * Returns metadata columns required by Spark's changelog post-processing, 
separate from the
+   * user-visible data columns in {@link #columns()}.
+   * <p>
+   * Row identity and row version may be backed by connector metadata rather 
than user-visible
+   * columns. Spark makes these columns available to internal plan rules 
without including them in
+   * the changelog schema or {@code SELECT *}. Users cannot query these 
columns directly.
+   * <p>
+   * The default implementation returns an empty array.

Review Comment:
   Ok got it, reworded it. Thanks!
   
   
https://github.com/apache/spark/blob/adb4141e92846aaa368496dac7d107aa43f544b2/sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/Changelog.java#L223-L230



##########
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:
   Tbh at first this was for me a decision of simply not showing them due to 
them being metadata and I deemed them only used internally. I see now that 
there are points where it make sense for users to query metadata.
   
   So this is overly restrictive as we block all metadata accesses. But at this 
moment it ensures uniform behaviour and also safeguards against inconsistencies 
like you mentioned above. Hence I'd vouch for keeping it restricted for now, 
but I'd also value your opinion here.  
   
   The alternative would be that we bubble up the metadata but make them only 
explicitly queryable, right? Do you prefer this behaviour?



##########
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 = {
+    val metadataColumnsByExprId = plan.collect {
+      case relation: DataSourceV2Relation
+          if relation.table.isInstanceOf[ChangelogTable] =>
+        relation.metadataOutput.map(attribute => attribute.exprId -> 
attribute.name)
+      case relation: StreamingRelationV2
+          if relation.table.isInstanceOf[ChangelogTable] =>
+        relation.metadataOutput.map(attribute => attribute.exprId -> 
attribute.name)
+    }.flatten.toMap
+
+    def check(node: LogicalPlan): Unit = {
+      if (!node.containsTag(internalChangelogSubtree)) {

Review Comment:
   Valid, I've used hiddenOutputTag, tested it and it works.
   
   
https://github.com/apache/spark/blob/adb4141e92846aaa368496dac7d107aa43f544b2/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala#L176-L183



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala:
##########
@@ -295,7 +341,8 @@ object ResolveChangelogTable extends Rule[LogicalPlan] {
       plan: LogicalPlan,
       cl: Changelog,
       requiresCarryOverRemoval: Boolean,
-      requiresUpdateDetection: Boolean): LogicalPlan = {
+      requiresUpdateDetection: Boolean,
+      retainRowIds: Boolean): LogicalPlan = {

Review Comment:
   Yes this was for streaming changelogs, I reworked it to follow your 
proposal, thank you!
   
   
https://github.com/apache/spark/blob/adb4141e92846aaa368496dac7d107aa43f544b2/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala#L338-L342
   
   
https://github.com/apache/spark/blob/adb4141e92846aaa368496dac7d107aa43f544b2/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala#L384-L402
   
   
https://github.com/apache/spark/blob/adb4141e92846aaa368496dac7d107aa43f544b2/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala#L176-L183



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