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

Ilya Soin commented on FLINK-31778:
-----------------------------------

The problem appears to be in the codegen of casting row to row: 
[https://github.com/apache/flink/blob/master/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/RowToRowCastRule.java#L166]
{code:java}
context.declareClassField(
    className(BinaryRowData.class),
    rowTerm,
    constructorCall(BinaryRowData.class, inputFields.size()));
context.declareClassField(
    className(BinaryRowWriter.class),
    writerTerm, 
    constructorCall(BinaryRowWriter.class, rowTerm)); {code}
 Because we declare these as class fields, in the generated cast the same 
reference to row is used to populate all array elements, so in the end all 
elements reference the same (last) object.

!image-2023-04-12-15-23-46-669.png|width=967,height=590!

!screenshot-2.png|width=967,height=583!

> Casting array of rows produces incorrect result
> -----------------------------------------------
>
>                 Key: FLINK-31778
>                 URL: https://issues.apache.org/jira/browse/FLINK-31778
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / Planner
>    Affects Versions: 1.16.1, 1.15.4
>            Reporter: Ilya Soin
>            Priority: Major
>         Attachments: image-2023-04-12-15-23-46-669.png, screenshot-2.png
>
>
> {code:java}
> select CAST(commissions AS ARRAY<ROW<amount DOUBLE>>) as commissions          
>    from (select ARRAY[ROW(123), ROW(234)] commissions){code}
> Expected output:
> {code:java}
> +----+--------------------------------+
> | op |                    commissions |
> +----+--------------------------------+
> | +I |             [(123.0), (234.0)] |
> +----+--------------------------------+
>  {code}
> Actual output:
> {code:java}
> +----+--------------------------------+
> | op |                    commissions |
> +----+--------------------------------+
> | +I |             [(234.0), (234.0)] |
> +----+--------------------------------+ {code}
> Full working example: 
> https://gist.github.com/soin08/5e0038dbefeba9192706e05a78ef3bc1



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

Reply via email to