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

xiaogang zhou updated FLINK-32132:
----------------------------------
    Description: 
I am trying to generate code cast string to bigint, and got generated code like:

 

 
{code:java}
// code placeholder

if (!isNull$14) {
result$15 = 
org.apache.flink.table.data.binary.BinaryStringDataUtil.toLong(field$13.trim());
} else {
result$15 = -1L;
}

   castRuleResult$16 = result$15;
   castRuleResultIsNull$17 = isNull$14;
 } catch (java.lang.Throwable e) {
   castRuleResult$16 = -1L;
   castRuleResultIsNull$17 = true;
 }
 // --- End cast section

out.setLong(0, castRuleResult$16); {code}
such kind of handle does not provide a perfect solution as the default value of 
long is set to -1L, which can be meaningful in some case. And can cause some 
calculation error.
 
And I understand the cast returns a bigint not null, But since there is a 
exception, we should ignore the type restriction, so I suggest to modify the 
CodeGenUtils.rowSetField like below:
 
{code:java}
// code placeholder

if (fieldType.isNullable || 
fieldExpr.nullTerm.startsWith("castRuleResultIsNull")) {
  s"""
     |${fieldExpr.code}
     |if (${fieldExpr.nullTerm}) {
     |  $setNullField;
     |} else {
     |  $writeField;
     |}
    """.stripMargin
} else {
  s"""
     |${fieldExpr.code}
     |$writeField;
   """.stripMargin
} {code}

> Cast function CODEGEN does not work as expected when nullOnFailure enabled
> --------------------------------------------------------------------------
>
>                 Key: FLINK-32132
>                 URL: https://issues.apache.org/jira/browse/FLINK-32132
>             Project: Flink
>          Issue Type: Improvement
>          Components: Table SQL / Planner
>    Affects Versions: 1.16.1
>            Reporter: xiaogang zhou
>            Priority: Major
>
> I am trying to generate code cast string to bigint, and got generated code 
> like:
>  
>  
> {code:java}
> // code placeholder
> if (!isNull$14) {
> result$15 = 
> org.apache.flink.table.data.binary.BinaryStringDataUtil.toLong(field$13.trim());
> } else {
> result$15 = -1L;
> }
>    castRuleResult$16 = result$15;
>    castRuleResultIsNull$17 = isNull$14;
>  } catch (java.lang.Throwable e) {
>    castRuleResult$16 = -1L;
>    castRuleResultIsNull$17 = true;
>  }
>  // --- End cast section
> out.setLong(0, castRuleResult$16); {code}
> such kind of handle does not provide a perfect solution as the default value 
> of long is set to -1L, which can be meaningful in some case. And can cause 
> some calculation error.
>  
> And I understand the cast returns a bigint not null, But since there is a 
> exception, we should ignore the type restriction, so I suggest to modify the 
> CodeGenUtils.rowSetField like below:
>  
> {code:java}
> // code placeholder
> if (fieldType.isNullable || 
> fieldExpr.nullTerm.startsWith("castRuleResultIsNull")) {
>   s"""
>      |${fieldExpr.code}
>      |if (${fieldExpr.nullTerm}) {
>      |  $setNullField;
>      |} else {
>      |  $writeField;
>      |}
>     """.stripMargin
> } else {
>   s"""
>      |${fieldExpr.code}
>      |$writeField;
>    """.stripMargin
> } {code}



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

Reply via email to