raminqaf opened a new pull request, #30092:
URL: https://github.com/apache/flink/pull/30092
## What is the purpose of the change
Adds `IS_VALID_VARIANT(variant)` from FLIP-618. It tells whether every part
of a binary VARIANT can be read, so a query can filter values that another
engine wrote before reading them.
| On a malformed VARIANT `v` | Result
|
|----------------------------------|--------------------------------------------------------------------------------------------------------------------|
| `VARIANT_GET(v, '$', 'INT')` | Fails the job: `Cannot cast a VARIANT
value whose binary encoding is malformed or uses an unknown type.` |
| `TRY_VARIANT_GET(v, '$', 'INT')` | NULL, the same as for a path that
doesn't exist
|
| `IS_VALID_VARIANT(v)` | FALSE. TRUE for any result of
`PARSE_JSON`, NULL for a NULL input
|
The check lives in `VariantValidator` in `flink-core`, an `@Internal` class
that a format or deserializer can reuse.
Decisions:
- **What is checked** follows the FLIP and Spark's `is_valid_variant`: the
metadata is complete, every value has a known type and lies within its parent,
and object keys refer to the metadata. Key order, duplicate keys and UTF-8
aren't checked, since reading works without them. The docs say that unsorted
keys can make a lookup in an object with 32 or more fields miss.
- **Values that reading rejects are FALSE**, not only broken headers: a
DECIMAL beyond precision 38 and a TIME outside one day, since `getDecimal` and
`getTime` throw on them. Type ids 17 to 20 from FLIP-604 are valid, unknown ids
are FALSE.
- **Shared bytes are bounded.** Offsets may point several children at the
same bytes. A 121-byte value built that way, 24 levels deep, renders as 117
million characters of JSON. The validator returns FALSE once it has visited
more values than the value has bytes, which keeps its cost linear in the size.
Values with a little sharing still pass. Spark's check has no such bound, from
reading its code.
- **No recursion.** The walk keeps its own stack, so a value nested 100,000
levels deep returns an answer instead of a `StackOverflowError`.
- **Stricter checks come later.** `VariantValidator` is an instance, so
opt-in checks for key order, UTF-8 and shared bytes can be added without
changing callers. That is FLINK-41643. `IS_VALID_VARIANT` keeps one argument.
Cost, measured with a `System.nanoTime` loop on a laptop, not JMH:
| Value size | `IS_VALID_VARIANT` | `JSON_STRING` of the same value |
|------------|--------------------|---------------------------------|
| 337 B | 87 ns | 680 ns |
| 2.9 KB | 2.7 µs | 13 µs |
| 27 KB | 32 µs | 121 µs |
The third commit changes `AGENTS.md`. FLINK-41569 shipped `ISNAN` instead of
`IS_NAN`, because SQL resolves a built-in function by `name()` while the Table
API prints `sqlName()`. The guide now says not to call `sqlName(...)`, and to
write the ITCase SQL with the documented name.
## Brief change log
- `VariantValidator` and its tests in `flink-core`
- `IS_VALID_VARIANT` in `BuiltInFunctionDefinitions`, with the runtime class
`IsValidVariantFunction`
- `isValidVariant()` in the Table API and `is_valid_variant()` in PyFlink
- A docs page, `docs/data/functions/variant/is_valid_variant.yml`
- `AGENTS.md`: name a new built-in function without `sqlName(...)`
## Verifying this change
- `VariantValidatorTest`: valid values of every type, a list of malformed
values, every truncation of a valid value, a child value inside a larger
buffer, nesting 100,000 levels deep, and a seeded fuzz of 20,000 mutations. The
validator must never throw, and every value it accepts must read in full
without an error.
- `VariantFunctionsITCase`: TRUE, NULL and FALSE, in SQL and the Table API.
- `ExpressionSerializationTest` and PyFlink's `test_expression.py`: the
printed SQL is `IS_VALID_VARIANT(...)`.
- Every docs example ran in batch and streaming mode.
## 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, a new method on `BaseExpressions`, as voted in
FLIP-618
- The serializers: no
- The runtime per-record code paths (performance sensitive): only in
queries that call the new function, see the cost table
- 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 and JavaDocs
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-by: Claude Code 2.1.331 (Claude Opus 5.5)
--
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]