sandip-db commented on code in PR #60484:
URL: https://github.com/apache/spark/pull/60484#discussion_r4653466220
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/csv/CSVOptions.scala:
##########
@@ -356,8 +356,14 @@ class CSVOptions(
// E.g. spark.read.format("csv").option("singleVariantColumn", "colName")
val singleVariantColumn: Option[String] =
parameters.get(SINGLE_VARIANT_COLUMN)
- // When true, reading CSV values into a VARIANT honors `inferSchema`
(default: false).
- val variantRespectInferSchema = getBool(VARIANT_RESPECT_INFER_SCHEMA,
default = false)
+ // The value of the `variantPrimitivesAsString` option, or None if the user
did not set it.
+ val variantPrimitivesAsStringOpt: Option[Boolean] =
Review Comment:
Done in fc8e7cf49cf: removed from CSV and JSON along with the two tests. XML
keeps it for the conf fallback.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -9413,13 +9379,15 @@ object SQLConf {
val XML_VARIANT_RESPECT_INFER_SCHEMA =
buildConf("spark.sql.xml.variant.respectInferSchema")
+ .internal()
Review Comment:
Done in fc8e7cf49cf: added a line under 'Upgrading from Spark SQL 4.3 to
4.4' covering the deprecation, the option's precedence, and the conf's behavior
when the option is not set.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/csv/CSVSuite.scala:
##########
@@ -4011,122 +4011,103 @@ abstract class CSVSuite
}
}
- test("csv variant retains scalars as strings when inferSchema is disabled") {
+ test("csv variant keeps primitives as strings with
variantPrimitivesAsString") {
withTempPath { path =>
val data =
- """field 1,field2
- |100,1.1
- |2000-01-01,2000-01-01 01:02:03
- |,true
- |1e9,hello,extra
- |missing
+ """0001,+5,1e9,True
+ |2000-01-01,2000-01-01 01:02:03,n/a,
|""".stripMargin
Files.write(path.toPath, data.getBytes(StandardCharsets.UTF_8))
def checkSingleVariant(options: Map[String, String], expected: String*):
Unit = {
- val allOptions = options ++ Map("singleVariantColumn" -> "v")
+ val allOptions = options ++ Map("singleVariantColumn" -> "v",
"nullValue" -> "n/a")
checkAnswer(
spark.read.options(allOptions).csv(path.getCanonicalPath).selectExpr("cast(v as
string)"),
- expected.map(Row(_))
- )
+ expected.map(Row(_)))
}
+ val asStrings = Seq(
+ """{"_c0":"0001","_c1":"+5","_c2":"1e9","_c3":"True"}""",
+ """{"_c0":"2000-01-01","_c1":"2000-01-01
01:02:03","_c2":null,"_c3":null}""")
+ val inferred = Seq(
+ """{"_c0":1,"_c1":5,"_c2":1000000000,"_c3":true}""",
+ """{"_c0":"2000-01-01","_c1":"2000-01-01
01:02:03+00:00","_c2":null,"_c3":null}""")
+
// A small partition size ensures each value is parsed independently and
would otherwise get
- // its type inferred.
+ // its type inferred. The time parser policy is pinned because the
inferred timestamp is
+ // rendered differently under LEGACY (see CSVLegacyTimeParserSuite).
withSQLConf(SQLConf.FILES_MAX_PARTITION_BYTES.key -> "10",
- SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") {
- // With variantRespectInferSchema on and inferSchema off, scalar
values parse as string,
- // while the null value (empty token) parses as a variant null.
inferSchema defaults to
- // false, so passing it explicitly and omitting it behave the same.
- for (opts <- Seq(Map("variantRespectInferSchema" -> "true",
"inferSchema" -> "false"),
- Map("variantRespectInferSchema" -> "true"))) {
- checkSingleVariant(opts,
- """{"_c0":"field 1","_c1":"field2"}""",
- """{"_c0":"100","_c1":"1.1"}""",
- """{"_c0":"2000-01-01","_c1":"2000-01-01 01:02:03"}""",
- """{"_c0":null,"_c1":"true"}""",
- """{"_c0":"1e9","_c1":"hello","_c2":"extra"}""",
- """{"_c0":"missing"}""")
+ SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC",
+ SQLConf.LEGACY_TIME_PARSER_POLICY.key -> "CORRECTED") {
Review Comment:
Done in fc8e7cf49cf: the inferred case now runs under both CORRECTED and
LEGACY, with `"2000-01-01 01:02:03"` for `_c1` under LEGACY.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/xml/XmlVariantSuite.scala:
##########
@@ -176,6 +178,68 @@ class XmlVariantSuite extends SharedSparkSession with
TestXmlData {
}
}
+ test("Parser: variantPrimitivesAsString keeps primitive values as strings") {
+ val asString = Map("variantPrimitivesAsString" -> "true")
+ // inferSchema defaults to true for XML; the option does not depend on it.
+ for (extra <- Seq(Map.empty[String, String], Map("inferSchema" ->
"true"))) {
Review Comment:
Done in fc8e7cf49cf: removed the loop; the `inferSchema=false` case stays in
the precedence test.
--
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]