Repository navigation
Conversation
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of the change
Adds
SCHEMA_OF_VARIANT, which returns the type of aVARIANTvalue as a Flink type string. A user can then pick the target of a cast.[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>nullVARIANTThe rules follow the FLIP-618 text and Spark's
schema_of_variant: integers showBIGINT, objects union their fields by name, numbers merge as SQL widens them and may round, and a pair without a common type givesVARIANT. The aggregateSCHEMA_OF_VARIANT_AGGfollows 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
MAPbecause aROWfield can't have a blank name, counts aBIGINTasDECIMAL(19, 0)instead ofDECIMAL(20, 0), and merges aDATEand aTIMESTAMPtoVARIANT, since a cast of aVARIANTDATEtoTIMESTAMPfails.LogicalTypeMerging.findCommonTypeisn't reused: it merges a number and aDATEtoDATE, merges rows by position, and keepsDECIMAL(38, 0)forDECIMAL(38, 0)andDECIMAL(1, 1).A cast to the result fails today for objects with different fields, because a cast of an object to a
ROWfails on a missing field, where Spark's returns NULL for it. The docs page says so.Brief change log
SCHEMA_OF_VARIANTdefinition with the runtime classSchemaOfVariantFunction.VariantSchemain flink-table-runtime derives the type of a value and merges two types, for reuse by the aggregate.schemaOfVariant(), PyFlinkschema_of_variant(), docs pagevariant/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 withLogicalTypeParser.VariantFunctionsITCase: SQL and Table API result, NULL input, and the error for a value of an unknown type.ExpressionSerializationTestandtest_expression.pyfor the Table API and PyFlink calls.Does this pull request potentially affect one of the following parts:
@Public(Evolving): yes, the new methodBaseExpressions#schemaOfVariantDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code 2.1.334 (Claude Opus 5.5)