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

yisha zhou commented on FLINK-34656:
------------------------------------

[~libenchao]  Hi ,sorry for the late reply.  To reproduce the problem, you can 
add code below in
`org.apache.flink.table.planner.runtime.stream.sql.CalcITCase`:
{code:java}
@Test
def testElementGet(): Unit = {
  val sqlQuery =
    s"""
       | SELECT a[1] FROM MyTableRow
       |""".stripMargin

  val rowData1: GenericRowData = new GenericRowData(2)
  rowData1.setField(0, null)

  val data = List(rowData1)

  implicit val dataType: TypeInformation[GenericRowData] =
    InternalTypeInfo
      .ofFields(ARRAY(BIGINT.notNull()).nullable().getLogicalType)
      .asInstanceOf[TypeInformation[GenericRowData]]

  val ds = env.fromCollection(data)

  val t = ds.toTable(tEnv, 'a)
  tEnv.createTemporaryView("MyTableRow", t)

  val outputType = InternalTypeInfo.ofFields(new BigIntType())

  val result = tEnv.sqlQuery(sqlQuery)
  val sink = new TestingAppendRowDataSink(outputType)
  tEnv.toDataStream(result, outputType.getDataType).addSink(sink)
  env.execute()

  val expected = List(
    "+I(-1)"
  )
  assertThat(sink.getAppendResults.sorted).isEqualTo(expected.sorted)
} {code}
The root reason is that element is of not-null type, therefore the nullTerm 
won't be checked.

> Generated code for `ITEM` operator should return null when getting element of 
> a null map/array/row
> --------------------------------------------------------------------------------------------------
>
>                 Key: FLINK-34656
>                 URL: https://issues.apache.org/jira/browse/FLINK-34656
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / Planner
>    Affects Versions: 1.20.0
>            Reporter: yisha zhou
>            Priority: Major
>
> In FieldAccessFromTableITCase we can find that the expected result of f0[1] 
> is null when f0 is a null array. 
> However, behavior in generated code for ITEM is not consistent with case 
> above. The main code is:
>  
> {code:java}
> val arrayAccessCode =
>   s"""
>      |${array.code}
>      |${index.code}
>      |boolean $nullTerm = ${array.nullTerm} || ${index.nullTerm} ||
>      |   $idxStr < 0 || $idxStr >= ${array.resultTerm}.size() || $arrayIsNull;
>      |$resultTypeTerm $resultTerm = $nullTerm ? $defaultTerm : $arrayGet;
>      |""".stripMargin {code}
> If `array.nullTerm` is true, a default value of element type will be 
> returned, for example -1 for null bigint array.
> The reason why FieldAccessFromTableITCase can get expected result is that the 
> ReduceExpressionsRule generated an expression code for that case like:
> {code:java}
> boolean isNull$0 = true || false ||
>    ((int) 1) - 1 < 0 || ((int) 1) - 1 >= 
> ((org.apache.flink.table.data.ArrayData) null).size() || 
> ((org.apache.flink.table.data.ArrayData) null).isNullAt(((int) 1) - 1);
> long result$0 = isNull$0 ? -1L : ((org.apache.flink.table.data.ArrayData) 
> null).getLong(((int) 1) - 1);
> if (isNull$0) {
>   out.setField(0, null);
> } else {
>   out.setField(0, result$0);
> } {code}
> The reduced expr will be a null literal.
>  
> I think the behaviors for getting element of a null value should be unified.



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

Reply via email to