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 @@ -24,6 +24,7 @@ import com.univocity.parsers.csv.CsvWriter
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.util.{DateFormatter, DateTimeUtils, IntervalStringStyles, IntervalUtils, TimestampFormatter}
import org.apache.spark.sql.catalyst.util.LegacyDateFormats.FAST_DATE_FORMAT
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._

class UnivocityGenerator(
Expand Down Expand Up @@ -95,6 +96,9 @@ class UnivocityGenerator(
while (i < row.numFields) {
if (!row.isNullAt(i)) {
values(i) = valueConverters(i).apply(row, i)
} else if (
SQLConf.get.getConf(SQLConf.LEGACY_NULL_VALUE_WRITTEN_AS_QUOTED_EMPTY_STRING_CSV)) {
values(i) = options.nullValue
}
i += 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,16 @@ object SQLConf {
.booleanConf
.createWithDefault(false)

val LEGACY_NULL_VALUE_WRITTEN_AS_QUOTED_EMPTY_STRING_CSV =
buildConf("spark.sql.legacy.nullValueWrittenAsQuotedEmptyStringCsv")
.internal()
.doc("When set to false, nulls are written as unquoted empty strings in CSV data source. " +
"If set to false, it restores the legacy behavior that nulls were written as quoted " +
"empty strings, `\"\"`.")
.version("3.3.0")
.booleanConf
.createWithDefault(false)

/**
* Holds information about keys that have been deprecated.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,12 +807,20 @@ abstract class CSVSuite

test("SPARK-37575: null values should be saved as nothing rather than " +
"quoted empty Strings \"\" with default settings") {
withTempPath { path =>
Seq(("Tesla", null: String, ""))
.toDF("make", "comment", "blank")
.write
.csv(path.getCanonicalPath)
checkAnswer(spark.read.text(path.getCanonicalPath), Row("Tesla,,\"\""))
Seq("true", "false").foreach { confVal =>
withSQLConf(SQLConf.LEGACY_NULL_VALUE_WRITTEN_AS_QUOTED_EMPTY_STRING_CSV.key -> confVal) {
withTempPath { path =>
Seq(("Tesla", null: String, ""))
.toDF("make", "comment", "blank")
.write
.csv(path.getCanonicalPath)
if (confVal == "false") {
checkAnswer(spark.read.text(path.getCanonicalPath), Row("Tesla,,\"\""))
} else {
checkAnswer(spark.read.text(path.getCanonicalPath), Row("Tesla,\"\",\"\""))
}
}
}
}
}

Expand Down