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

   ## What is the purpose of the change
   
   Adds `SCHEMA_OF_VARIANT`, which returns the type of a `VARIANT` value as a 
Flink type string. A user can then pick the target of a cast.
   
   ```sql
   SELECT SCHEMA_OF_VARIANT(PARSE_JSON('{"order_id": 17, "items": [{"sku": 
"A1", "qty": 2}]}'));
   -- ROW<`items` ARRAY<ROW<`qty` BIGINT, `sku` STRING>>, `order_id` BIGINT>
   ```
   
   | Input                       | Result                               |
   |-----------------------------|--------------------------------------|
   | `[1, 2.5]`                  | `ARRAY<DECIMAL(20, 1)>`              |
   | `[{"a": 1}, {"b": "x"}]`    | ``ARRAY<ROW<`a` BIGINT, `b` STRING>>`` |
   | `{"data": [{"a": "a"}, 5]}` | ``ROW<`data` ARRAY<VARIANT>>``       |
   | `{"": 1, "a": 2}`           | `MAP<STRING, BIGINT>`                |
   | `null`                      | `VARIANT`                            |
   
   The rules follow the FLIP-618 text and Spark's `schema_of_variant`: integers 
show `BIGINT`, objects union their fields by name, numbers merge as SQL widens 
them and may round, and a pair without a common type gives `VARIANT`. The 
aggregate `SCHEMA_OF_VARIANT_AGG` follows in a second PR and reuses the same 
merge.
   
   Unlike Spark, Flink prints its own type syntax, shows an object with a blank 
key as a `MAP` because a `ROW` field can't have a blank name, counts a `BIGINT` 
as `DECIMAL(19, 0)` instead of `DECIMAL(20, 0)`, and merges a `DATE` and a 
`TIMESTAMP` to `VARIANT`, since a cast of a `VARIANT` `DATE` to `TIMESTAMP` 
fails.
   
   `LogicalTypeMerging.findCommonType` isn't reused: it merges a number and a 
`DATE` to `DATE`, merges rows by position, and keeps `DECIMAL(38, 0)` for 
`DECIMAL(38, 0)` and `DECIMAL(1, 1)`.
   
   A cast to the result fails today for objects with different fields, because 
a cast of an object to a `ROW` fails on a missing field, where Spark's returns 
NULL for it. The docs page says so.
   
   ## Brief change log
   
   - `SCHEMA_OF_VARIANT` definition with the runtime class 
`SchemaOfVariantFunction`.
   - `VariantSchema` in flink-table-runtime derives the type of a value and 
merges two types, for reuse by the aggregate.
   - Table API `schemaOfVariant()`, PyFlink `schema_of_variant()`, docs page 
`variant/schema_of_variant.yml`.
   
   ## Verifying this change
   
   - `VariantSchemaTest`: the type of each stored kind and each merge rule, and 
that every result parses back to the same type with `LogicalTypeParser`.
   - `VariantFunctionsITCase`: SQL and Table API result, NULL input, and the 
error for a value of an unknown type.
   - `ExpressionSerializationTest` and `test_expression.py` for the Table API 
and PyFlink calls.
   
   ## 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)`: yes, the new method `BaseExpressions#schemaOfVariant`
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): no, only the 
new function
     - 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? yes
     - If yes, how is the feature documented? docs, a page for the function
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code 2.1.334 (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