[ 
https://issues.apache.org/jira/browse/FLINK-27212?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17524179#comment-17524179
 ] 

Marios Trivyzas commented on FLINK-27212:
-----------------------------------------

I propose, when we do {{CAST(<some binary> AS STRING)}} to also use UTF-8 
decoding, so we can have a seamless roundtrip: {{CAST(CAST('<some string>' AS 
BYTES) AS STRING)}} would return `<some string>`.

But when we do a {{SELECT CAST('<some string>' AS BYTES}} and we print the 
result, we will print in a {{x'ABC324Fd32'}} format, which can then be easily 
copy pasted as a binary literal in another SQL query:
{noformat}
tableEnv.executeSql("SELECT CAST('Marios Timo' AS BYTES)").print();{noformat}
will print:
{noformat}
+--------------------------------+
|                         EXPR$0 |
+--------------------------------+
|      x'4d6172696f732054696d6f' |
+--------------------------------+{noformat}
and then:
{noformat}
tableEnv.executeSql("SELECT x'4d6172696f732054696d6f'"){noformat}
which creates the desired binary column.

 

[~jark] [~wenlong.lwl] what do you think?

> Failed to CAST('abcde', VARBINARY)
> ----------------------------------
>
>                 Key: FLINK-27212
>                 URL: https://issues.apache.org/jira/browse/FLINK-27212
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / Runtime
>    Affects Versions: 1.15.0, 1.16.0
>            Reporter: Shengkai Fang
>            Assignee: Marios Trivyzas
>            Priority: Blocker
>              Labels: pull-request-available
>             Fix For: 1.15.0, 1.16.0
>
>
> Please add test in the CalcITCase
> {code:scala}
> @Test
>   def testCalc(): Unit = {
>     val sql =
>       """
>         |SELECT CAST('abcde' AS VARBINARY(6))
>         |""".stripMargin
>     val result = tEnv.executeSql(sql)
>     print(result.getResolvedSchema)
>     result.print()
>   }
> {code}
> The exception is 
> {code:java}
> Caused by: org.apache.flink.table.api.TableException: Odd number of 
> characters.
>       at 
> org.apache.flink.table.utils.EncodingUtils.decodeHex(EncodingUtils.java:203)
>       at StreamExecCalc$33.processElement(Unknown Source)
>       at 
> org.apache.flink.streaming.runtime.tasks.ChainingOutput.pushToOperator(ChainingOutput.java:99)
>       at 
> org.apache.flink.streaming.runtime.tasks.ChainingOutput.collect(ChainingOutput.java:80)
>       at 
> org.apache.flink.streaming.runtime.tasks.ChainingOutput.collect(ChainingOutput.java:39)
>       at 
> org.apache.flink.streaming.api.operators.CountingOutput.collect(CountingOutput.java:56)
>       at 
> org.apache.flink.streaming.api.operators.CountingOutput.collect(CountingOutput.java:29)
>       at 
> org.apache.flink.streaming.api.operators.StreamSourceContexts$ManualWatermarkContext.processAndCollect(StreamSourceContexts.java:418)
>       at 
> org.apache.flink.streaming.api.operators.StreamSourceContexts$WatermarkContext.collect(StreamSourceContexts.java:513)
>       at 
> org.apache.flink.streaming.api.operators.StreamSourceContexts$SwitchingOnClose.collect(StreamSourceContexts.java:103)
>       at 
> org.apache.flink.streaming.api.functions.source.InputFormatSourceFunction.run(InputFormatSourceFunction.java:92)
>       at 
> org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:110)
>       at 
> org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:67)
>       at 
> org.apache.flink.streaming.runtime.tasks.SourceStreamTask$LegacySourceFunctionThread.run(SourceStreamTask.java:332)
> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to