dongjoon-hyun commented on code in PR #60486:
URL: https://github.com/apache/spark/pull/60486#discussion_r4574572985
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteUpdateTable.scala:
##########
@@ -43,22 +45,48 @@ object RewriteUpdateTable extends RewriteRowLevelCommand {
EliminateSubqueryAliases(aliasedTable) match {
case r @ ExtractV2Table(tbl: SupportsRowLevelOperations) =>
checkNoGeneratedColumns(r, UPDATE)
+ val dataAssignments = removeMetadataAssignments(assignments)
val table = buildOperationTable(tbl, UPDATE, r.options)
val updateCond = cond.getOrElse(TrueLiteral)
table.operation match {
case _: SupportsDelta =>
- buildWriteDeltaPlan(r, table, assignments, updateCond)
+ buildWriteDeltaPlan(r, table, dataAssignments, updateCond)
case _ if SubqueryExpression.hasSubquery(updateCond) =>
- buildReplaceDataWithUnionPlan(r, table, assignments, updateCond)
+ buildReplaceDataWithUnionPlan(r, table, dataAssignments,
updateCond)
case _ =>
- buildReplaceDataPlan(r, table, assignments, updateCond)
+ buildReplaceDataPlan(r, table, dataAssignments, updateCond)
}
case _ =>
u
}
}
+ // AddMetadataColumns may add the metadata columns to the target table of an
UPDATE on a table
+ // that accepts any schema, so its aligned assignments may include one for
each metadata column.
+ // However, only data columns are written, and the metadata values passed to
the writer are
+ // defined by MetadataColumn (e.g. PRESERVE_ON_UPDATE). So an assignment of
a metadata column to
+ // itself is removed, and any other assignment to a metadata column is
rejected unless the legacy
+ // behavior is enabled, as ResolveRowLevelCommandAssignments does for other
tables.
+ private def removeMetadataAssignments(assignments: Seq[Assignment]):
Seq[Assignment] = {
Review Comment:
Thank you for the review. You're right that `RewriteMergeIntoTable` has the
same shape. I'd like to keep this PR to `UPDATE`, because Spark reaches the two
rewrites differently:
- Spark resolves an `UPDATE` on a table that accepts any schema, so a plain
SQL statement hits this bug, as the new tests show.
- Spark never resolves a `MERGE` on such a table
([Analyzer.scala#L1805-L1809](https://github.com/apache/spark/blob/d049f2a96c2b9c6ed87b707996ac84495ad7c39b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala#L1805-L1809)),
so `RewriteMergeIntoTable` sees such a `MERGE` only through the rules that the
table injects. A fix for `MERGE` also needs such an injected rule in the tests,
and a test table with nullable metadata columns for the `INSERT` actions, as
you described.
I'll file a new JIRA issue for `MERGE`.
--
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]