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

Jungtaek Lim commented on CALCITE-1387:
---------------------------------------

[~julianhyde] Yes it can be closed. I found similar NPE issue even without 
ITEM() (so CAST itself), but it can be filed to separate issue. I'll look into 
more and file an issue.

> CAST(ITEM() as INTEGER) throws RuntimeException in Runtime when return value 
> of ITEM() is null
> ----------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-1387
>                 URL: https://issues.apache.org/jira/browse/CALCITE-1387
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.9.0
>            Reporter: Jungtaek Lim
>            Assignee: Julian Hyde
>
> Suppose we're defining the following table:
> {code}
> .field("ID", SqlTypeName.INTEGER)
> .field("MAPFIELD", SqlTypeName.ANY)
> .field("NESTEDMAPFIELD", SqlTypeName.ANY)
> .field("ARRAYFIELD", SqlTypeName.ANY)
> {code}
> and query like this:
> {code}
> SELECT * FROM <TBL> WHERE CAST(MAPFIELD['a'] AS INTEGER) = 1
> {code}
> When ITEM returns null (because MAPFIELD\['a'] is really null, or there's no 
> key 'a'), CAST throws RuntimeException, saying "cannot convert null to int".
> Generated code block is here:
> {code}
> outputValues[0] = inp1_ == null ? (Boolean) null : 
> Boolean.valueOf(org.apache.calcite.runtime.SqlFunctions.toInt(org.apache.calcite.runtime.SqlFunctions.item(inp1_,
>  "a")) == 2);
> {code}
> Btw, CAST(COALESCE(MAPFIELD\['a'], -1) AS INTEGER) throws
> java.lang.RuntimeException: org.codehaus.commons.compiler.CompileException: 
> Incompatible expression types "java.lang.Object" and "int".
> Code for where expression is below:
> {code}
>   final Object inp1_ = (context.values)[1];
>   outputValues[0] = inp1_ != null && inp1_ == null ? (Boolean) null : 
> Boolean.valueOf(org.apache.calcite.runtime.SqlFunctions.toInt(inp1_ != null ? 
> org.apache.calcite.runtime.SqlFunctions.item(inp1_, "a") : -1) == 2);
> {code}
> CAST(COALESCE(MAPFIELD\['a'], CAST(-1 AS ANY)) AS INTEGER) is generating same 
> code block and also throws same Exception above.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to