raminqaf opened a new pull request, #29073:
URL: https://github.com/apache/flink/pull/29073
## What is the purpose of the change
Let `CAST` and `TRY_CAST` convert a `VARIANT` into an `ARRAY<T>`. This is
the first of three stacked PRs for FLINK-37926 (VARIANT to constructed types);
the follow-ups add `ROW`/`STRUCTURED` and `MAP<STRING, V>` and build on this
branch. It extends FLINK-37925 (VARIANT to scalar types, already merged): a
constructed cast is that same scalar cast applied to every element, plus a
shape check at each level, so no new leaf semantics are introduced.
A variant array casts to `ARRAY<T>` by casting each element to `T` with the
full VARIANT-to-`T` rule, recursively. The recursion bottoms out at the scalar
cast, which keeps one mental model for the whole type and one deliberate
strictness: a stored value keeps its kind, so a stored string is never parsed
into a number. `CAST(PARSE_JSON('["1","2"]') AS ARRAY<INT>)` therefore fails;
read it as `ARRAY<STRING>` and convert with a regular cast instead.
## Brief change log
- Add `VariantToArrayCastRule`: shape-check the variant is an array, then
cast each element to the target element type, recursively reusing the existing
VARIANT cast rules (including the `VARIANT`-to-`VARIANT` identity, so
`ARRAY<VARIANT>` shreds exactly one level).
- A JSON `null` element maps to SQL `NULL` for a nullable element type and
fails the cast for a `NOT NULL` one; an empty array casts to an empty
`ARRAY<T>`; a top-level JSON `null` casts to SQL `NULL` for a nullable target
before the shape check. `CAST` fails on any bad element and `TRY_CAST` returns
`NULL` for the whole array (all-or-nothing), matching nested `ARRAY` casts
today.
- Add a shared `AbstractVariantToConstructedCastRule` base (JSON-null
passthrough and `canFail`), reused by the follow-up ROW and MAP PRs.
- Castability is explicit only and recursive: `VARIANT` to `ARRAY<T>` is
castable iff `VARIANT` to `T` is, so an element type with no variant
counterpart (e.g. `INTERVAL`) is rejected at plan time.
- Extend the cast-rules table and the data types docs (EN and zh).
## Verifying this change
This change added tests and can be verified as follows:
- `LogicalTypeCastsTest`: `VARIANT` to `ARRAY` castability, including
nesting, `ARRAY<VARIANT>`, and an unsupported leaf.
- `CastRuleProviderTest#testResolveVariantToArray`: rule resolution and
recursive resolvability.
- `CastRulesTest`: codegen behavior for `ARRAY<INT>` (nullable and `NOT
NULL`), `ARRAY<STRING>`, `ARRAY<DOUBLE>`, nested `ARRAY<ARRAY<INT>>`,
`ARRAY<VARIANT>`, empty and null-element arrays, a heterogeneous array, and
shape mismatches.
- `CastFunctionITCase`: end-to-end `CAST`/`TRY_CAST` over `PARSE_JSON`,
covering the same shapes, the strict-leaf failures, and validation errors.
## 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 (a new
opt-in cast rule; existing paths are unchanged)
- 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 (`data-types.md`, EN and zh)
and JavaDocs
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR,
please
change the checkbox below to `[X]` and replace the placeholder in the
"Generated-by"
line with the tool name and version. Otherwise remove the "Generated-by"
line.
See the ASF Generative Tooling Guidance for details:
https://www.apache.org/legal/generative-tooling.html
You are responsible for the quality and correctness of every change in this
PR
regardless of the tooling used. Low-effort AI-generated PRs will be closed.
See
AGENTS.md for the full guidance.
-->
- [X] Yes (please specify the tool below)
Generated-by: Opus 4.8
--
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]