linhongliu-db opened a new pull request, #60398:
URL: https://github.com/apache/spark/pull/60398

   ### What changes were proposed in this pull request?
   
   A CSV record whose only value is a null written as an empty token is an 
empty line, which the
   univocity writer drops (`CSVOptions.asWriterSettings` sets 
`setSkipEmptyLines(true)`), and
   `UnivocityGenerator.convertRow` leaves a null value unset unless the legacy 
quoting conf is on. So:
   
   - A single-column CSV write silently **drops** every row whose only value is 
null.
   - `to_csv` of a one-field struct holding null returns null, and its 
`nullSafeCodeGen` path then
     writes a null reference, throwing a `NullPointerException`.
   
   This PR writes such a lone-null record as a quoted empty string (`""`), 
independent of `emptyValue`,
   so it reads back as null; and makes `to_csv` return `""` for any record the 
writer would otherwise
   drop as an empty line. A new legacy conf 
`spark.sql.legacy.csv.skipSingleNullValueRecordOnWrite`
   (default `false`) restores the previous behavior.
   
   ### Why are the changes needed?
   
   Silent data loss on write (dropped null rows) and a `NullPointerException` 
from `to_csv`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, documented in `docs/sql-migration-guide.md`. A single-column null row 
is now kept as a quoted
   empty string instead of being dropped; `to_csv` of a one-field null struct 
returns `""` instead of
   failing. The CSV reader reads the quoted empty string back as null unless a 
custom read `emptyValue`
   is set. Set `spark.sql.legacy.csv.skipSingleNullValueRecordOnWrite=true` to 
restore the old behavior.
   
   ### How was this patch tested?
   
   New regressions in `CSVSuite` cover round-trips across `emptyValue`, 
`quote`, `quoteAll`,
   `nullValue` and partitioned writes, and a gate-off case that reproduces both 
the dropped row and the
   `to_csv` NPE. Locally `CSVv1Suite` and `CSVv2Suite` (new tests) and 
`CsvFunctionsSuite` (61/61) pass
   and scalastyle is clean on JDK 21.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Isaac)
   
   This pull request and its description were written by Isaac.
   


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