johanl-db commented on code in PR #59596:
URL: https://github.com/apache/spark/pull/59596#discussion_r4611342390
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveChangelogTable.scala:
##########
@@ -139,30 +139,47 @@ 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())
+ }
+
+ /** Keeps internal metadata and helper columns behind the changelog's
visible schema. */
+ private def changelogOutput(
+ plan: LogicalPlan,
+ source: LogicalPlan,
+ name: String): LogicalPlan = {
+ val columnNames = source.output.map(_.name).toSet
+ val project = Project(plan.output.filter(a =>
columnNames.contains(a.name)), plan)
+ // Metadata availability must not depend on which post-processing passes
were required.
Review Comment:
nit:
```
// Set hiddenOutputTag = empty to hide metadata columns, and wrap in a
SubqueryAlias to prevent analyzer rules reaching inside the Project to find the
metadata columns.
```
##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/Changelog.java:
##########
@@ -218,4 +218,15 @@ default NamedReference[] rowId() {
default NamedReference rowVersion() {
throw new UnsupportedOperationException("rowVersion is not supported.");
}
+
+ /**
+ * Returns metadata columns exposed when scanning this Changelog.
+ * <p>
+ * The default implementation returns an empty array.
+ *
+ * @since 4.4.0
Review Comment:
nit: I think this is now 4.5.0?
--
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]