jubins opened a new pull request, #60513:
URL: https://github.com/apache/spark/pull/60513

   ### What changes were proposed in this pull request?
   
   Fixes [SPARK-61727](https://issues.apache.org/jira/browse/SPARK-61727).
   
   `schema_of_csv` tokenizes its input in `SchemaOfCsvEvaluator.evaluate` 
(`CsvExpressionEvalUtils.scala`) by calling Univocity's `CsvParser.parseLine` 
directly. When the input has more columns than `maxColumns`, or a value longer 
than `maxCharsPerColumn`, Univocity throws `TextParsingException` (caused by 
`ArrayIndexOutOfBoundsException`), and that raw exception reaches the user.
   
   SPARK-57195 added `UnivocityParser.parseLine(tokenizer, line)`, which 
converts these Univocity failures into `MALFORMED_CSV_RECORD`. `from_csv` and 
the CSV file read paths already go through it. `schema_of_csv` does not. This 
PR makes `SchemaOfCsvEvaluator.evaluate` call `UnivocityParser.parseLine` as 
well.
   
   Brief change log:
   
   - `sql/catalyst/.../expressions/csv/CsvExpressionEvalUtils.scala`: 
`SchemaOfCsvEvaluator.evaluate` tokenizes with 
`UnivocityParser.parseLine(csvParser, ...)` instead of 
`csvParser.parseLine(...)`.
   - `sql/core/.../CsvFunctionsSuite.scala`: two new tests, one for 
`maxColumns` and one for `maxCharsPerColumn`.
   
   ### Why are the changes needed?
   
   With the same input and options, `from_csv` reports a named error and 
`schema_of_csv` throws an internal parser exception:
   
   ```sql
   SELECT schema_of_csv('a,b,c', map('maxColumns', '2'));
   -- before: com.univocity.parsers.common.TextParsingException:
   --         java.lang.ArrayIndexOutOfBoundsException - Index 2 out of bounds 
for length 2
   -- after:  [MALFORMED_CSV_RECORD] Malformed CSV record: a,b,c SQLSTATE: KD000
   
   SELECT schema_of_csv('abcdef', map('maxCharsPerColumn', '3'));
   -- before: com.univocity.parsers.common.TextParsingException: Length of 
parsed input (4)
   --         exceeds the maximum number of characters defined in your parser 
settings (3).
   -- after:  [MALFORMED_CSV_RECORD] Malformed CSV record: abcdef SQLSTATE: 
KD000
   ```
   
   `TextParsingException` has no error condition or SQLSTATE, and its message 
is internal Univocity parser state. The fix brings `schema_of_csv` in line with 
`from_csv` and the CSV reader.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. When the `schema_of_csv` input exceeds `maxColumns` or 
`maxCharsPerColumn`, the query now fails with `MALFORMED_CSV_RECORD` (SQLSTATE 
`KD000`) instead of a raw `com.univocity.parsers.common.TextParsingException`. 
Univocity's exception is kept as the cause. Valid input gives the same results 
as before.
   
   ### How was this patch tested?
   
   Added two tests to `CsvFunctionsSuite`:
   
   - `SPARK-61727: schema_of_csv surfaces MALFORMED_CSV_RECORD for input 
exceeding maxColumns`: `schema_of_csv(lit("a,b,c"), Map("maxColumns" -> "2"))` 
must fail with `MALFORMED_CSV_RECORD`, `badRecord = "a,b,c"`, SQLSTATE `KD000`.
   - `SPARK-61727: schema_of_csv surfaces MALFORMED_CSV_RECORD for input 
exceeding maxCharsPerColumn`: `schema_of_csv(lit("abcdef"), 
Map("maxCharsPerColumn" -> "3"))` must fail with `MALFORMED_CSV_RECORD`, 
`badRecord = "abcdef"`, SQLSTATE `KD000`.
   
   Both tests fail on `master` with `Expected exception 
org.apache.spark.SparkRuntimeException to be thrown, but 
com.univocity.parsers.common.TextParsingException was thrown`, and both pass 
with this change. The full `CsvFunctionsSuite` and `CsvExpressionsSuite` also 
pass locally.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
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