javasqlapache-calcite

Calcite order by nullpointer exception


I try to perform query over over some table data in Java using Calcite. I try to execute query:

select
    distinct tbl.col1 ,
    tbl.col2  
from
    infoset.SUBEKT tbl   
order by
    col1 asc nulls last ,
    col2 asc nulls last

And the following exception occurs

java.lang.NullPointerException
    at org.apache.calcite.runtime.Utilities.compare(Utilities.java:202)
    at Baz$2.compare(ANONYMOUS.java:15)
    at Baz$2.compare(ANONYMOUS.java:27)
    at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:376)
    at java.util.TreeMap.getEntry(TreeMap.java:345)
    at java.util.TreeMap.get(TreeMap.java:278)
    at org.apache.calcite.linq4j.EnumerableDefaults.toLookup_(EnumerableDefaults.java:3608)
    at org.apache.calcite.linq4j.EnumerableDefaults$14.enumerator(EnumerableDefaults.java:2660)
    at org.apache.calcite.linq4j.AbstractEnumerable.iterator(AbstractEnumerable.java:33)
    at org.apache.calcite.avatica.MetaImpl.createCursor(MetaImpl.java:90)
    at org.apache.calcite.avatica.AvaticaResultSet.execute(AvaticaResultSet.java:186)
    at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:64)
    at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:43)
    at org.apache.calcite.avatica.AvaticaConnection$1.execute(AvaticaConnection.java:669)
    at org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:636)
    at org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:677)
    at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:157)
    at org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:228)
    at ru.spi2.modules.infosetquery.InfosetQueryPlugin.executeQuery(InfosetQueryPlugin.java:101)

The error occurs because col1 has some rows with null values. If I order only by col1 or col2, all works well.


Solution

  • I found problem in my code, I created RelDataType and had set it to not nullable.

    Therefore, I added to org.apache.calcite.schema.Table#getRowType when describing RelDataType for column next code:

    RelDataType sqlType = relDataTypeFactory.createTypeWithNullability(relDataTypeFactory.createSqlType(sqlTypeName), true); 
    

    And now all works fine, thanks.