raminqaf opened a new pull request, #29125:
URL: https://github.com/apache/flink/pull/29125

   ## What is the purpose of the change
   
   Casting a `VARIANT` to a character string renders a stored scalar the way a 
regular SQL cast of that kind would (a boolean becomes `TRUE`, a timestamp uses 
the SQL format, a binary value is read as UTF-8). A `VARIANT` holding an object 
or an array had no scalar form, so the cast failed and pointed the user at 
`JSON_STRING`.
   
   This makes `CAST(v AS STRING)` render an object or array as its JSON 
representation instead of failing, reusing `Variant#toJson()` — the same text 
`JSON_STRING` and the result-print path already produce. Redirecting users to a 
different function for a value that already has a well-defined textual form was 
surprising.
   
   Scalar rendering is unchanged, so the only asymmetry is the expected one: a 
top-level scalar string still casts unquoted (`foo`), while a string nested 
inside a rendered container stays quoted (`"foo"`), matching `JSON_STRING`.
   
   ```sql
   CAST(PARSE_JSON('["a","b"]') AS STRING)  -- ["a","b"]   (was: runtime error)
   CAST(PARSE_JSON('{"a":1}')   AS STRING)  -- {"a":1}     (was: runtime error)
   CAST(PARSE_JSON('"foo"')     AS STRING)  -- foo         (unchanged)
   ```
   
   Only `OBJECT` and `ARRAY` change here. Scalar kinds whose string rendering 
is still unsupported (`TIME` and nanosecond timestamps, tracked in FLINK-40492) 
keep failing.
   
   ## Brief change log
   
   - `VariantCastUtils.getVariantTypeAsString` renders an `OBJECT`/`ARRAY` 
variant via `toJson() `rather than throwing; other unsupported kinds still fail.
   - A bounded `CHAR(n)`/`VARCHAR(n)` target trims the rendered JSON like any 
other over-length value; `TRY_CAST` returns the JSON rather than `NULL`; a 
stored JSON null still casts to SQL NULL.
   - Updated the cast-support matrix and prose in data-types.md.
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
   - `CastRulesTest`: VARIANT → STRING for an object and an array now assert 
the JSON rendering.
   - `CastFunctionITCase`: `CAST`/`TRY_CAST` of an object and an array to 
STRING return JSON, plus a container cast to a bounded VARCHAR(n) that trims 
the rendered JSON.
   
   ## 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
   
   ##Documentation
   
   - Does this pull request introduce a new feature? yes (a new castable pair 
for VARIANT)
   - If yes, how is the feature documented? docs (cast-support matrix)
   
   ---
   
   #### Was generative AI tooling used to co-author this PR?
   
   - [x] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Claude 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]

Reply via email to