[ 
https://issues.apache.org/jira/browse/FLINK-40580?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Timo Walther closed FLINK-40580.
--------------------------------
    Fix Version/s: 2.4.0
       Resolution: Fixed

Fixed in master: 44edc3c9694c46a45a62d53d20f98fa595e9559a

For the decision record: We decided for the SQL representation when casting 
VARIANT object/array. The reason for this is that also a CAST(VARIANT AS 
STRING) does not return JSON but SQL. JSON_STRING should serve as proper JSON 
serialization of variant. Casting VARIANT object/array is usually only useful 
debugging purposes and has only been added for completeness.

> Render VARIANT object/array as SQL when casting to character string
> -------------------------------------------------------------------
>
>                 Key: FLINK-40580
>                 URL: https://issues.apache.org/jira/browse/FLINK-40580
>             Project: Flink
>          Issue Type: Improvement
>            Reporter: Ramin Gharib
>            Assignee: Ramin Gharib
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 2.4.0
>
>
> Casting a VARIANT to CHAR/VARCHAR/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 that holds an 
> object or an array has no scalar form, so today the cast fails:
> {code:java}
> Cannot cast a VARIANT OBJECT value to a character string. Use the JSON_STRING 
> function to obtain its JSON representation. {code}
> Redirecting users to JSON_STRING for the container case is surprising: the 
> value already has a well-defined textual form, its JSON representation, which 
> both JSON_STRING and the result-print path already produce.
>  
> Proposal: make CAST(v AS STRING) render an object or array as its JSON 
> representation (via Variant#toJson()) instead of failing. Scalar rendering is 
> unchanged, so a top-level scalar string still casts unquoted while a string 
> nested inside a rendered container stays quoted, matching JSON_STRING and the 
> print path.
> {code:java}
> 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)
> {code}
> A bounded CHAR/VARCHAR target trims the rendered JSON like any other 
> over-length value. TRY_CAST returns the same JSON rather than NULL for a 
> container. A VARIANT storing a JSON null still casts to SQL NULL.
> Scope: only OBJECT and ARRAY change here. Scalar kinds whose string rendering 
> is still unsupported (TIME and nanosecond timestamps, tracked in FLINK-40492) 
> keep failing.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to