yashtc commented on code in PR #60484:
URL: https://github.com/apache/spark/pull/60484#discussion_r4604397910
##########
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:
With `.internal()`, this conf leaves the generated config docs, and the
`from_xml` notes drop it too. It still changes XML results when the option is
not set (XmlOptions.scala:212-213). So users who set it learn about the change
only from a log warning. Does this need a line in the "Upgrading from Spark SQL
4.3 to 4.4" section of docs/sql-migration-guide.md, like the
`allowZeroIndexInFormatString` line at 189?
##########
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:
Nit: both entries are the same case. `baseOptions` has no `inferSchema`, and
XML defaults it to true (XmlOptions.scala:126). The precedence test at line 225
covers `inferSchema=false`. Can we drop the loop?
##########
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:
Outside tests, `variantPrimitivesAsStringOpt` is only read by the
`variantPrimitivesAsString` val just below it (line 366), and the same holds in
JSON (JSONOptions.scala:230-232). Only XML needs an Opt, for its conf fallback.
In CSV, `getBool(VARIANT_PRIMITIVES_AS_STRING)` gives the same value, because
it returns false for an absent or null value (CSVOptions.scala:109-118). Can we
drop the Opt vals in CSV and JSON, and the two "records whether the user set
it" tests that only check them?
##########
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:
This drops the only LEGACY check of variant timestamp inference. The old
test ran the inferred rows under CORRECTED and LEGACY (base
CSVSuite.scala:4054-4057), and `csv with variant` is excluded from
`CSVLegacyTimeParserSuite` (CSVSuite.scala:4613). Can we keep a LEGACY run of
`inferred`, with `"2000-01-01 01:02:03"` for `_c1`?
--
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]