pritishatx opened a new pull request, #30099:
URL: https://github.com/apache/flink/pull/30099

   ## What is the purpose of the change
   
   Reading a `VARIANT` column with the `json` format printed the value's 
`JsonNode` tree back to text and parsed that text again with 
`BinaryVariantInternalBuilder.parseJson(String)`. Both readers did this:
   
   - `JsonToRowDataConverters`: `parseJson(jsonNode.toString(), false)`
   - `JsonParserToRowDataConverters`: 
`parseJson(jp.readValueAsTree().toString(), false)`
   
   This change builds the variant straight from the tree. The stored bytes do 
not change.
   
   On this event, read as a `VARIANT`:
   
   ```json
   
{"id":123,"price":19.99,"qty":3,"lat":52.520008,"lon":13.404954,"score":0.875,"name":"widget","tags":["a","b"],
    
"items":[{"sku":"x1","price":4.5,"w":0.25},{"sku":"x2","price":15.49,"w":1.75}],"ts":1730000000000}
   ```
   
   | Reader                          | Before  | After   | Allocated before | 
Allocated after |
   
|---------------------------------|---------|---------|------------------|-----------------|
   | `JsonToRowDataConverters`       | 1879 ns | 1073 ns | 5520 B           | 
3656 B          |
   | `JsonParserToRowDataConverters` | 2949 ns | 2110 ns | 18256 B          | 
7936 B          |
   
   JMH 1.37 on JDK 21, 3 forks, 3×1 s warmup, 5×1 s measurement, `-prof gc`. 
Errors are within ±31 ns. The mapper reads floats as `BigDecimal`, as the 
format does when the row has a `DECIMAL` column.
   
   **Why the streaming reader still builds a tree.** 
`JsonParserToRowDataConverters` already holds a `JsonParser`, so it could hand 
that parser to the variant builder and skip the tree entirely. That would be 
faster, but it changes results:
   
   | Input           | Today (through the tree)                                 
                  | Parsing the stream directly         |
   
|-----------------|----------------------------------------------------------------------------|-------------------------------------|
   | `{"a":1,"a":2}` | `{"a":2}`, pinned by 
`testVariantDuplicateKeysUseLastValue`                | duplicate-key error     
            |
   | `-11E+0`        | the tree prints `-11`, stored as an integer              
                  | stored as a double                  |
   
   This PR keeps the tree, so it is a pure performance change. Reading the 
stream directly can be proposed separately as a behavior change.
   
   ## Brief change log
   
   - `BinaryVariantInternalBuilder.parseJson(JsonNode, boolean)` walks the tree 
with `JsonNode.traverse()` and gives the same variant as 
`parseJson(node.toString(), boolean)`.
   - A parser over a tree reports a `DecimalNode` as a float token even when 
its text is an integer, such as `-11` for `-11E+0`. `PARSE_JSON` reads that 
text as an integer, so the builder now tries a `long` first for integer text on 
a float token. A parser over JSON text never gives a float token with integer 
text, so `PARSE_JSON` is unchanged.
   - Both JSON readers call the new method.
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
   - 
`BinaryVariantInternalBuilderTest#testParseJsonTreeMatchesParseJsonOfTheTreeText`
 compares `parseJson(tree)` with `parseJson(tree.toString())` for scalars, 
integers up to and beyond a long, decimals at the `DECIMAL(38)` limit, exponent 
notation and nested containers. Trees are read both with and without 
`USE_BIG_DECIMAL_FOR_FLOATS`.
   - 
`BinaryVariantInternalBuilderTest#testParseJsonTreeRejectsNumbersOutOfDoubleRange`:
 `1e400` is still rejected.
   - 
`JsonRowDataSerDeSchemaTest#testVariantWholeNumberInExponentNotationIsStoredAsInteger`,
 for both readers. It fails without the integer-text fallback.
   - Existing VARIANT tests in `JsonRowDataSerDeSchemaTest` still pass, 
including the duplicate-key test.
   
   ## 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 (`BinaryVariantInternalBuilder` is `@Internal`)
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): yes, the JSON 
format's `VARIANT` read path
     - 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
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code 2.1.317 (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]

Reply via email to