raminqaf opened a new pull request, #28758: URL: https://github.com/apache/flink/pull/28758
## What is the purpose of the change
Follow-up to the initial `VARIANT`-to-primitive cast support (FLINK-37925).
It tightens numeric cast semantics and completes the set of supported cast
targets:
- Numeric casts from a `VARIANT` now reject values that do not fit the
target instead of silently wrapping. An out-of-range integer or an overflowing
`DECIMAL`
fails `CAST` and returns `NULL` for `TRY_CAST`. `FLOAT` and `DOUBLE` keep
lenient IEEE conversion, where overflow becomes infinity. This follows the
behavior of Spark's variant casts.
- `CAST(VARIANT AS CHAR/VARCHAR)` is now allowed and returns the JSON string
representation, so the earlier `JSON_STRING`-only restriction and its cast hint
are removed.
This makes a `VARIANT`-to-integer cast stricter than a regular numeric
narrowing cast, which wraps. That is intentional: a `VARIANT` carries untrusted
semi-structured data, so silently corrupting an out-of-range value is worse
than failing. `TRY_CAST` is the escape hatch, and the docs show a double-cast
pattern for wrap-around narrowing.
## Brief change log
- Add `VariantCastUtils` (flink-table-runtime) that performs checked
narrowing: truncate any fraction toward zero, range-check, and throw on
overflow. It uses `BigDecimal`/`BigInteger` so it is correct for every stored
numeric kind.
- Route integer and `DECIMAL` targets in `VariantToPrimitiveCastRule`
through the checked helpers; keep `FLOAT`/`DOUBLE` lenient.
- Express `VARIANT` cast validation in the per-target rules of
`LogicalTypeCasts`; allow `CAST` to `CHAR`/`VARCHAR` and drop the `JSON_STRING`
cast hint.
- Document `VARIANT` value encoding, the `PARSE_JSON` type mapping, and
timestamp microsecond precision; fix the `getInstant` javadoc to reference
`Type.TIMESTAMP_LTZ`.
## Verifying this change
This change added tests and can be verified as follows:
- Extended `CastFunctionITCase#variantCasts` with overflow cases (`CAST`
fails, `TRY_CAST` returns `NULL`), the `1e20 AS INT` case, and the lenient
`FLOAT`/`DOUBLE` cases; the full `CastFunctionITCase` (752 tests) passes.
- Updated `LogicalTypeCastsTest` for the `VARIANT` cast-support matrix.
## 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, javadoc-only change to the `@PublicEvolving`
`Variant` interface (no
signature change)
- The serializers: no
- The runtime per-record code paths (performance sensitive): yes, only on
the `VARIANT`-to-numeric cast path
- Anything that affects deployment or recovery: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? no (it refines existing
FLINK-37925 behavior)
- If yes, how is the feature documented? docs 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]` followed by the name of the tool, and
uncomment 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]
