jubins opened a new pull request, #60514: URL: https://github.com/apache/spark/pull/60514
### What changes were proposed in this pull request? Fixes [SPARK-61726](https://issues.apache.org/jira/browse/SPARK-61726). `schema_of_json` ignores the `mode` option. `SchemaOfJsonEvaluator.evaluate` in `JsonExpressionEvalUtils.scala` calls `JsonInferSchema.inferField` with no error handling, so malformed input fails in every mode with a raw Jackson exception that has no error condition: `com.fasterxml.jackson.core.io.JsonEOFException` for `'{"a":'`, `JsonParseException` for `'}'`, and `StreamConstraintsException` for a document nested deeper than `maxNestingDepth`. JSON datasource schema inference (`JsonInferSchema.infer`) and `schema_of_xml` already route the same failures through the parse mode. This PR catches `JsonProcessingException` in `SchemaOfJsonEvaluator.evaluate` and hands it to the existing `JsonInferSchema.handleJsonErrorsByParseMode`, the same helper the datasource path uses: - `PERMISSIVE` (default): returns `STRUCT<_corrupt_record: STRING>`, named by the `columnNameOfCorruptRecord` option or `spark.sql.columnNameOfCorruptRecord`. - `DROPMALFORMED`: returns `STRUCT<>`, the schema the datasource infers when every record is dropped. - `FAILFAST`: fails with `MALFORMED_RECORD_IN_PARSING.WITHOUT_SUGGESTION`, with the Jackson exception as the cause. Well-formed input is not affected. Note on `DROPMALFORMED`: `schema_of_xml` and `from_json` reject this mode with `PARSE_MODE_UNSUPPORTED`. This PR does not do that, because `schema_of_json` accepts `DROPMALFORMED` today and rejecting it would break existing calls on well-formed input. Returning `STRUCT<>` matches what JSON datasource inference does when every record is dropped. Happy to switch to rejecting the mode if reviewers prefer consistency with `schema_of_xml`. ### Brief change log - `JsonExpressionEvalUtils.scala`: `SchemaOfJsonEvaluator` catches `JsonProcessingException` and applies the parse mode through `handleJsonErrorsByParseMode`; `JSONOptions` now takes `spark.sql.columnNameOfCorruptRecord` as the default corrupt record column name (it was `""`, which was never used before). - `JsonInferSchema.scala`: `handleJsonErrorsByParseMode` goes from `private` to `private[catalyst]` so the evaluator can reuse it. - `JsonFunctionsSuite.scala`: five new tests (see below). - `docs/sql-migration-guide.md`: note under "Upgrading from Spark SQL 4.3 to 4.4". ### Why are the changes needed? ```sql SELECT schema_of_json('{"a":'); -- com.fasterxml.jackson.core.io.JsonEOFException: -- Unexpected end-of-input within/between Object entries SELECT schema_of_json('{"a":', map('mode', 'FAILFAST')); -- same raw JsonEOFException, not MALFORMED_RECORD_IN_PARSING SELECT schema_of_json('{"a":', map('mode', 'DROPMALFORMED')); -- same raw JsonEOFException ``` Expected, matching JSON datasource inference: ```sql SELECT schema_of_json('{"a":'); -- STRUCT<_corrupt_record: STRING> SELECT schema_of_json('{"a":', map('mode', 'DROPMALFORMED')); -- STRUCT<> SELECT schema_of_json('{"a":', map('mode', 'FAILFAST')); -- [MALFORMED_RECORD_IN_PARSING.WITHOUT_SUGGESTION] ... ``` The `mode` option is accepted by `schema_of_json` but has no effect, and the error users see is a third-party exception with no error condition or SQLSTATE. `spark.read.json` on a `Dataset[String]` with the same record already returns `_corrupt_record` in `PERMISSIVE` mode and `MALFORMED_RECORD_IN_PARSING` in `FAILFAST` mode, so the function and the datasource disagree on the same input. ### Does this PR introduce _any_ user-facing change? Yes. For malformed input, `schema_of_json` returns `STRUCT<_corrupt_record: STRING>` in the default `PERMISSIVE` mode and `STRUCT<>` in `DROPMALFORMED` mode, and fails with `MALFORMED_RECORD_IN_PARSING.WITHOUT_SUGGESTION` in `FAILFAST` mode. In released versions it failed in every mode with a raw Jackson exception. Well-formed input returns the same schema as before. A migration guide note is added for 4.4. ### How was this patch tested? New tests in `sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala`, each named with SPARK-61726: - `schema_of_json - PERMISSIVE mode returns the corrupt record schema`: `'{"a":'`, `'}'` and `'{"a" 1}'`, with no mode and with explicit `PERMISSIVE`, return `STRUCT<_corrupt_record: STRING>`. - `schema_of_json - PERMISSIVE mode honors columnNameOfCorruptRecord`: the `columnNameOfCorruptRecord` option and `spark.sql.columnNameOfCorruptRecord` both rename the field. - `schema_of_json - FAILFAST mode fails on malformed input`: `checkError` on `MALFORMED_RECORD_IN_PARSING.WITHOUT_SUGGESTION`. - `schema_of_json - DROPMALFORMED mode returns an empty struct`: malformed input returns `STRUCT<>`, and well-formed input still returns `STRUCT<a: BIGINT>`. - `schema_of_json - nesting depth over the limit is a malformed record`: `StreamReadConstraints.DEFAULT_MAX_DEPTH + 1` nested arrays return the corrupt record schema in `PERMISSIVE` mode and fail with `MALFORMED_RECORD_IN_PARSING.WITHOUT_SUGGESTION` in `FAILFAST` mode. All five fail on `master` with the raw `JsonEOFException` / `StreamConstraintsException` and pass with this change. Also ran `JsonFunctionsSuite` (122 tests), `JsonExpressionsSuite` (102 tests) and `SQLQueryTestSuite -z json-functions.sql`, all passing with no golden file changes. ### 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]
