Skip to content

Conversation

@JinkunLiu
Copy link
Contributor

What is the purpose of the change

Introduce Built-in Function to Access field or element in the Variant

SQL Function Table Function Description
variant ‘[’ INT ‘]’ VARIANT.at(INT) If the VARIANT is an ARRAY value, returns a VARIANT whose value is the element at the specified index. Otherwise, this operation returns NULL
variant ‘[’ STRING ‘]’ VARIANT.at(STRING) If the VARIANT is a MAP value that has an element with this key, a VARIANT holding the associated value is returned. Otherwise, NULL is returned.

Brief change log

  • Support accessing VARIANT elements using [INT] or VARIANT.at(INT) when the VARIANT contains an ARRAY value in SQL or table expressions.
  • Support accessing VARIANT elements using ['STRING'] or VARIANT.at('STRING') when the VARIANT contains a MAP value in SQL or table expressions.

Verifying this change

This change added tests and can be verified as follows:

  • Added six new test at org.apache.flink.table.planner.plan.nodes.exec.stream.VariantSemanticTest

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): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
  • The S3 file system connector: (no)

@JinkunLiu JinkunLiu force-pushed the feature/FLINK-37924 branch from efb13bf to 53f4993 Compare December 9, 2025 16:27
@JinkunLiu JinkunLiu changed the title [FLINK-37924][sql]Introduce Built-in Function to Access field or element in the Variant [FLINK-37924][table]Introduce Built-in Function to Access field or element in the Variant Dec 9, 2025
@JinkunLiu JinkunLiu marked this pull request as ready for review December 9, 2025 16:28
@flinkbot
Copy link
Collaborator

flinkbot commented Dec 9, 2025

CI report:

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

FunctionDefinition functionDefinition, int argumentPos) {
return Signature.Argument.of("[<INTEGER NUMERIC> | <MAP_KEY_TYPE>]");
return Signature.Argument.of(
"[<INTEGER NUMERIC> | <MAP_KEY_TYPE> | <CHARACTER STRING> or <INTEGER NUMERIC>]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need <INTEGER NUMERIC> twice here?

Copy link
Contributor Author

@JinkunLiu JinkunLiu Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original intent was to clearly illustrate the one-to-one correspondence between logical root types and their allowed index/key argument types, for example:

  • LogicalTypeRoot.ARRAY<INTEGER NUMERIC>
  • LogicalTypeRoot.MAP<MAP_KEY_TYPE>
  • LogicalTypeRoot.VARIANT<CHARACTER STRING> or <INTEGER NUMERIC>

The expected function signatures in old code are:

at([<ARRAY> | <MAP> | <VARIANT>], [<INTEGER NUMERIC> | <MAP_KEY_TYPE> | <CHARACTER STRING> or <INTEGER NUMERIC>])

However, I have realized that this mapping is already effectively conveyed through the error messages generated by inferArgumentType(). I have updated the implementation to reflect this.

Thanks for your reminder!

} else {
return callContext.fail(
throwOnFailure,
"Variant values can only be accessed with a CHARACTER STRING key or indexed with an INTEGER NUMERIC index.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we include what type was given.

nit: wording suggestion - not need to take unless you want to.
Incorrect type {} supplied for the variant value. Variant values can only be accessed with a CHARACTER STRING map key or an INTEGER NUMERIC array index.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this makes the error clearer for users. Done, thanks!

@github-actions github-actions bot added the community-reviewed PR has been reviewed by the community. label Dec 10, 2025
@JinkunLiu JinkunLiu marked this pull request as draft December 11, 2025 16:22
@JinkunLiu JinkunLiu marked this pull request as ready for review December 19, 2025 17:43
@JinkunLiu JinkunLiu requested a review from davidradl December 20, 2025 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-reviewed PR has been reviewed by the community.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants