This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new 9459996f888b [SPARK-61504][SQL] Fix wrong results by
DataFrameWriterV2.overwrite when the condition uses a CHAR column
9459996f888b is described below
commit 9459996f888b4debfb1642c4167c8ed5c122d2ba
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Thu Nov 12 08:31:33 2026 -0800
[SPARK-61504][SQL] Fix wrong results by DataFrameWriterV2.overwrite when
the condition uses a CHAR column
### What changes were proposed in this pull request?
This PR aims to fix a correctness issue where
`DataFrameWriterV2.overwrite(condition)` does not delete the rows that match a
condition on a `CHAR` column of a DSv2 table, so the old rows silently remain
next to the new ones.
To fix it, `ResolveOutputRelation` now skips an `OverwriteByExpression`
until its delete condition is resolved.
### Why are the changes needed?
This is a correctness issue. Rows written normally are not replaced.
```scala
sql("CREATE TABLE testcat.t (id bigint, dep char(8)) USING foo PARTITIONED
BY (dep)")
sql("INSERT INTO testcat.t VALUES (1L, 'hr'), (2L, 'software')")
Seq((3L, "hr")).toDF("id", "dep").writeTo("testcat.t").overwrite($"dep" ===
"hr")
// Issue: It should return [2,software] and [3,hr ], but [1,hr ]
also remains.
spark.table("testcat.t").collect()
```
`ResolveReferences` resolves the delete condition against the output of the
target table, which keeps the raw `CHAR(8)` type in its metadata.
`ApplyCharTypePadding` uses it to pad the literal `'hr'` to `'hr '`, the
value that is stored. `ResolveOutputRelation`, however, replaces the target
table with a copy whose output no longer has the raw type
(`CharVarcharUtils.cleanAttrMetadata`).
- SQL `INSERT INTO ... REPLACE WHERE` works because `ResolveInsertInto`
builds `OverwriteByExpression` only after its query is resolved, so
`ResolveReferences`, which runs before `ResolveOutputRelation` in the batch,
always resolves the condition first.
- `DataFrameWriterV2` (and Spark Connect, which builds the same command)
passes the logical plan of the DataFrame, which can be unresolved.
`ResolveReferences` skips the command while its query is unresolved. If a later
rule of the same iteration resolves the query, e.g.
`ResolveSubqueryColumnAliases` for `toDF(...)`, `ResolveOutputRelation` runs
first, and the condition is resolved against the cleaned table in the next
iteration. The literal is not padded, and `dep = 'hr'` matches no [...]
By waiting for the condition, both paths resolve it against the same table
output.
### Does this PR introduce _any_ user-facing change?
Yes. `DataFrameWriterV2.overwrite(condition)` now replaces the rows whose
`CHAR` column matches the condition, like `INSERT INTO ... REPLACE WHERE`.
### How was this patch tested?
Pass the CIs with the newly added test case, which fails without this fix.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60373 from dongjoon-hyun/SPARK-61504.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
.../org/apache/spark/sql/catalyst/analysis/Analyzer.scala | 3 +++
.../scala/org/apache/spark/sql/DataFrameWriterV2Suite.scala | 13 +++++++++++++
2 files changed, 16 insertions(+)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
index 062ef4667639..44f0464a2991 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
@@ -4045,6 +4045,9 @@ class Analyzer(
object ResolveOutputRelation extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan =
plan.resolveOperatorsWithPruning(
_.containsPattern(COMMAND), ruleId) {
+ // The delete condition is resolved against the table output, which this
rule replaces with
+ // one without the CHAR/VARCHAR raw types that the padding of the
condition needs.
+ case o: OverwriteByExpression if !o.deleteExpr.resolved => o
case v2Write: V2WriteCommand
if v2Write.table.resolved && v2Write.query.resolved &&
!v2Write.outputResolved &&
v2Write.pendingSchemaChanges.isEmpty =>
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWriterV2Suite.scala
b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWriterV2Suite.scala
index 09d227fd2617..55c017dfbb27 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWriterV2Suite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWriterV2Suite.scala
@@ -279,6 +279,19 @@ class DataFrameWriterV2Suite extends SharedSparkSession
with BeforeAndAfter {
Seq(Row(1L, "a"), Row(2L, "b"), Row(4L, "d"), Row(5L, "e"), Row(6L,
"f")))
}
+ test("SPARK-61504: Overwrite: overwrite by expression on a CHAR column") {
+ spark.sql(
+ "CREATE TABLE testcat.table_name (id bigint, dep char(8)) USING foo
PARTITIONED BY (dep)")
+ spark.sql("INSERT INTO testcat.table_name VALUES (1L, 'hr'), (2L,
'software')")
+
+ // the query is not resolved when the command is created, unlike the query
of a SQL INSERT
+ Seq((3L, "hr")).toDF("id",
"dep").writeTo("testcat.table_name").overwrite($"dep" === "hr")
+
+ checkAnswer(
+ spark.table("testcat.table_name"),
+ Seq(Row(2L, "software"), Row(3L, "hr ")))
+ }
+
test("Overwrite: write to a temp view of v2 relation") {
spark.sql("CREATE TABLE testcat.table_name (id bigint, data string) USING
foo")
spark.table("source").writeTo("testcat.table_name").append()
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]