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

   ### What changes were proposed in this pull request?
   
   This PR adds a read option, `variantPrimitivesAsString` (default `false`), 
to the CSV, JSON and XML data sources. When it is `true`, every non-null 
primitive value read into a VARIANT is stored as a variant string holding its 
text from the input, instead of a type inferred from that text:
   
   - **CSV:** the field token. No long / decimal / date / timestamp / boolean 
inference is attempted.
   - **XML:** element text, attribute values and value tags. 
`ignoreSurroundingSpaces` trimming still applies.
   - **JSON:** number and boolean tokens, kept exactly as written (for example 
`1e2`, `1.50`, or a 50-digit integer).
   
   Nulls, including values matching `nullValue`, remain variant nulls. Objects, 
arrays and XML nesting keep their structure. The option applies wherever these 
formats build a VARIANT: `singleVariantColumn` reads, VARIANT columns in a 
user-specified schema, and `from_csv` / `from_json` / `from_xml` with a variant 
schema. `parse_json` is not affected.
   
   Implementation:
   - The option key is defined once in `DataSourceOptions` and read by 
`CSVOptions`, `JSONOptions` and `XmlOptions`.
   - CSV (`UnivocityParser.VariantValueConverter`) and XML 
(`StaxXmlParser.appendXMLCharacterToVariant`) already had a single check that 
skips inference. That check now reads the resolved option.
   - JSON: `VariantBuilder` gets a `primitivesAsString` flag (a new constructor 
and `parseJson` overload). `buildJson` uses it to append number and boolean 
tokens with `parser.getText()`. The existing constructors and overloads 
delegate with `false`, so other callers are unchanged. 
`JacksonParser.parseVariant` passes the option through.
   
   Deprecation of the existing knobs:
   - The CSV option `variantRespectInferSchema` (SPARK-58917, not yet released) 
and the SQL conf `spark.sql.xml.variant.respectInferSchema` (SPARK-56554) each 
keep values as strings only together with `inferSchema=false`. Both are now 
deprecated in favor of the new option.
   - When `variantPrimitivesAsString` is set, it takes precedence over them in 
both directions. When it is not set, they behave exactly as before, so existing 
workloads are unaffected.
   - Both are removed from the user-facing documentation. The XML conf is 
marked `.internal()` and added to `SQLConf.deprecatedSQLConfigs`, so setting it 
logs a deprecation warning. It is also removed from the "Affected by these 
public SQL configurations" notes of `from_xml` in the Scala and Python APIs.
   
   ### Why are the changes needed?
   
   Reading text formats into a VARIANT infers a type for each primitive value, 
which can change or lose data:
   - CSV / XML: `0001` becomes the integer `1`, `+5` becomes `5`, `True` 
becomes a boolean, and `37,77` becomes `3777`.
   - JSON: numbers that do not fit a variant decimal (more than 38 digits) or 
use exponent notation are stored as lossy doubles, and decimals lose trailing 
zeros (`1.50` becomes `1.5`).
   
   The existing ways to turn this off are inconsistent: CSV uses a read option, 
XML uses a session SQL conf, and JSON has none. Both existing knobs are also 
tied to `inferSchema`, whose default differs per format. This PR gives all 
three formats one option with the same meaning.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A new read option `variantPrimitivesAsString` is available for CSV, 
JSON and XML, documented in `docs/sql-data-sources-{csv,json,xml}.md`. For 
example:
   
   ```scala
   spark.read.option("singleVariantColumn", 
"v").option("variantPrimitivesAsString", "true")
     .json(path)
   // {"amount": 1.50, "ratio": 1e2, "ok": true}
   // is read as {"amount":"1.50","ok":"true","ratio":"1e2"}
   // instead of {"amount":1.5,"ok":true,"ratio":100.0}
   ```
   
   The default (`false`) keeps the current behavior. 
`variantRespectInferSchema` and `spark.sql.xml.variant.respectInferSchema` keep 
working but are deprecated and no longer documented. Setting the XML conf now 
logs a deprecation warning.
   
   ### How was this patch tested?
   
   New tests:
   - `CSVSuite`: file reads with `singleVariantColumn` and with VARIANT schema 
columns, independent of `inferSchema`, with header, null and `nullValue` 
handling, precedence over `variantRespectInferSchema` in both directions, and 
rejection of a non-boolean value.
   - `CsvFunctionsSuite`: `from_csv` with the option, including precedence over 
the deprecated option.
   - `JsonSuite`: file reads and `from_json`, covering integers, a 50-digit 
integer, exponent numbers, decimals, booleans, null, nested arrays and objects, 
`NaN`, and malformed records. Also checks that the existing 
`primitivesAsString` option does not affect variant values.
   - `XmlVariantSuite`: element text, attributes, value tags, repeated 
elements, trimming, null handling, precedence over the SQL conf in both 
directions, and end-to-end file reads and `from_xml`. These also run under 
`XmlVariantSuiteWithLegacyParser`.
   - Option registration and count assertions in `CSVSuite`, `JsonSuite` and 
`XmlSuite`.
   
   Ran locally with JDK 21:
   - `build/sbt "sql/testOnly *CSVv1Suite *CsvFunctionsSuite *JsonV1Suite 
*JsonFunctionsSuite *XmlVariantSuite *XmlVariantSuiteWithLegacyParser 
*XmlFunctionsSuite"`: 704 tests passed.
   - `XmlSuite -z "validate XML Options"`, `variant/test`, `catalyst/testOnly 
*VariantExpressionSuite` and `sql/testOnly *VariantSuite 
*VariantEndToEndSuite`: all passed. These show `parse_json` and the default 
JSON path are unchanged.
   - `scalastyle` (catalyst, sql-api, sql, sql tests) and Java `checkstyle` 
(`common/variant`): clean.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Opus 5.5)
   
   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