raminqaf opened a new pull request, #29342:
URL: https://github.com/apache/flink/pull/29342
## What is the purpose of the change
`PARSE_JSON` and `TRY_PARSE_JSON` decoded every input `STRING` from UTF-8
into a Java `String`, and Jackson then parsed its chars. A Flink `STRING` is
already UTF-8 bytes. This PR parses those bytes directly with Jackson's UTF-8
byte parser.
JMH, JDK 21. The input sits at a non-zero offset in a `MemorySegment`, like
a field read from `BinaryRowData`.
| Document | Size | Before | After |
Change |
|-------------------------------------------|--------|---------|---------|--------|
| Records: nested objects, numbers, strings | 1 KB | 3.51 µs | 3.01 µs |
-14% |
| Records: nested objects, numbers, strings | 1 MB | 4.08 ms | 3.51 ms |
-14% |
| One long string value | 100 KB | 160 µs | 63 µs |
-61% |
| One long string value | 1 MB | 1.71 ms | 0.63 ms |
-63% |
Allocation per call drops by 22 to 29% for documents of 100 KB and more.
Valid JSON parses to the same `VARIANT` as before. Only a `STRING` that
holds invalid UTF-8 behaves differently:
| Input bytes | Function | Before | After
|
|-------------|------------------|------------|--------------------------------------------------------------------------|
| `22 FF 22` | `PARSE_JSON` | `"\uFFFD"` | `Failed to parse json
string`, caused by `Invalid UTF-8 start byte 0xff` |
| `22 FF 22` | `TRY_PARSE_JSON` | `"\uFFFD"` | `NULL`
|
This matches FLINK-39623, which made `CAST(BYTES AS STRING)` fail on invalid
UTF-8 by default.
The byte parser runs with `JsonFactory.Feature.CHARSET_DETECTION` disabled.
With detection on, Jackson guesses UTF-16 or UTF-32 from NUL bytes or a BOM in
the first four bytes. `TRY_PARSE_JSON(CONCAT('1', CHR(0)))` would then return
`1` instead of `NULL`.
One open question for reviewers: Jackson decodes overlong UTF-8 such as `C0
AF` to `/` instead of rejecting it. Rejecting it too would need a separate
UTF-8 validation pass. I left that out.
## Brief change log
- `BinaryVariantInternalBuilder#parseJson(byte[], boolean)` parses UTF-8
bytes. The shared `JsonFactory` disables `CHARSET_DETECTION`.
- `ParseJsonFunction` and `TryParseJsonFunction` pass
`StringData.toBytes()` instead of `StringData.toString()`.
## Verifying this change
This change added tests and can be verified as follows:
- `BinaryVariantInternalBuilderTest`: the byte path yields the same
`VARIANT` as the `String` path for non-ASCII input. Nine byte inputs must be
rejected: a leading or trailing NUL, a UTF-8 BOM, three UTF-16 encodings, and
three kinds of invalid UTF-8.
- `JsonFunctionsITCase`: `PARSE_JSON` fails and `TRY_PARSE_JSON` returns
`NULL` for `1` followed by NUL, NUL followed by `1`, and a BOM followed by `1`.
These fail without the `CHARSET_DETECTION` change.
- Existing `JsonFunctionsITCase`, `CastFunctionITCase`,
`MapFunctionITCase`, `VariantSemanticTest` and `CalcRestoreTest` pass.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with
`@Public(Evolving)`: no
- The serializers: no
- The runtime per-record code paths (performance sensitive): yes.
`PARSE_JSON` and `TRY_PARSE_JSON` run per record. The benchmark above covers
them.
- Anything that affects deployment or recovery: JobManager (and its
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? no
- If yes, how is the feature documented? not applicable. The invalid UTF-8
behavior change is in the release note on FLINK-40854.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-by: 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]