Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[FLINK-40582][table] Add SCHEMA_OF_VARIANT function - #30229

Open
raminqaf wants to merge 1 commit into
apache:masterfrom
raminqaf:FLINK-40582
Open

raminqaf wants to merge 1 commit into
apache:masterfrom
raminqaf:FLINK-40582

Conversation

@raminqaf

@raminqaf raminqaf commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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.

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?
  • Yes (please specify the tool below)

Generated-by: Claude Code 2.1.334 (Claude Opus 5.5)

SCHEMA_OF_VARIANT returns the type of a VARIANT value as a Flink type string, such as ROW<`a` BIGINT> for {"a": 1}, so that a user can see which type to cast the value to.

- An object shows a ROW with its fields ordered by name, an array an ARRAY of its merged elements, an integer BIGINT, a decimal DECIMAL(p, s) without trailing zeros, a TIME TIME(6), and a timestamp precision 6 or 9.
- An object with a blank key shows MAP<STRING, ...>, since a ROW field can't have a blank name.
- Types merge as in Spark's schema_of_variant: numbers widen and may round, objects union their fields by name, a variant null merges into any type, and any other pair gives VARIANT. A BIGINT counts as DECIMAL(19, 0), its precision in Flink.
- VariantSchema in flink-table-runtime derives and merges the types, so that SCHEMA_OF_VARIANT_AGG can reuse it.

Generated-by: Claude Code 2.1.334 (Claude Opus 5.5)
@flinkbot

flinkbot commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants