johanl-db commented on code in PR #59596:
URL: https://github.com/apache/spark/pull/59596#discussion_r4588454563
##########
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:
You're assuming the intent of the metadata columns exposed, that's an
artificial limitation. Metadata columns can be anything, the fact that this is
a changelog relation doesn't change that.
A simple "Returns metadata columns exposed when scanning this Changelog"
would suffice
##########
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:
What's the reason to restrict access to metadata columns?
An issue I see is that when post-processing is required, metadata columns
won't be accessible through the Window added, which creates a weird user
experience, where metadata columns become arbitrarily innaccessible for ranges
that contain copied rows / updates.
Is that the reason?
##########
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:
I'm not sure I follow this `retainRowIds`: can't you just keep rowIds in the
plan. If needed, add a Project on top to hide them instead of manually removing
them from another node
##########
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:
I find this tagging / blocking during checkAnalysis rather hacky.
There may be a way to introduce a boundary during `ResolveChangelogTable`
that prevents accessing `_metadata`. I found
[Project.hiddenOutputTag](https://github.com/apache/spark/blob/143b0b1edc935a14143114313f1618c9eef7a344/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala#L120),
not sure how reliable that is though:
```
val project = Project(child.output, child)
project.setTagValue(Project.hiddenOutputTag, Seq.empty[Attribute])
val boundary = SubqueryAlias("boundary", project)
```
--
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]