dongjoon-hyun opened a new pull request, #60373:
URL: https://github.com/apache/spark/pull/60373

   ### 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 stored value.
   
   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
   


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