Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ class CSVOptions(
writerSettings.setIgnoreLeadingWhitespaces(ignoreLeadingWhiteSpaceFlagInWrite)
writerSettings.setIgnoreTrailingWhitespaces(ignoreTrailingWhiteSpaceFlagInWrite)
writerSettings.setNullValue(nullValue)
writerSettings.setEmptyValue("\"\"")
if (nullValue == "" || quote == '\u0000') {
writerSettings.setEmptyValue(nullValue)
} else {
writerSettings.setEmptyValue(s"${quote}${quote}")
}
writerSettings.setSkipEmptyLines(true)
writerSettings.setQuoteAllFields(quoteAll)
writerSettings.setQuoteEscapingEnabled(escapeQuotes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1357,11 +1357,54 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils with Te

checkAnswer(computed, expected)
}
// Keeps the old behavior where empty string us coerced to nullValue is not passed.

// Checks for new behavior where an empty string is not coerced to null when `nullValue` is
// set to anything but an empty string literal and quote character is changed.
withTempPath { path =>
df.write
.option("nullValue", "-")
.option("quote", "'")
.csv(path.getAbsolutePath)
val computed = spark.read
.option("nullValue", "-")
.option("quote", "'")
.schema(df.schema)
.csv(path.getAbsolutePath)
val expected = Seq(
(1, "John Doe"),
(2, ""),
(3, litNull),
(4, litNull)
).toDF("id", "name")

checkAnswer(computed, expected)
}

// Keeps the old behavior where empty string is coerced to nullValue if not passed.
withTempPath { path =>
df.write
.csv(path.getAbsolutePath)
val computed = spark.read
.schema(df.schema)
.csv(path.getAbsolutePath)
val expected = Seq(
(1, "John Doe"),
(2, litNull),
(3, "-"),
(4, litNull)
).toDF("id", "name")

checkAnswer(computed, expected)
}

// Keeps the old behavior where empty string is coerced to nullValue if not passed
// with quotes disabled.
withTempPath { path =>
df.write
.option("quote", "")
.csv(path.getAbsolutePath)
val computed = spark.read
.option("quote", "")
.schema(df.schema)
.csv(path.getAbsolutePath)
val expected = Seq(
Expand Down