Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-3536

NPE when executing plan with Coalesce due to wrong NullAs strategy

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.22.0
    • None

    Description

      Currently, COALESCE expression specified in sql string will be rewrite to CASE by SqlValidator [1], thus CoalesceImplementor is almost no use. But it is actually used somewhere, for example Sum0Splitter. When running AggregateRemove and split expression SUM0, a COALESCE is actually created and no chance to be rewrite to CASE anymore.

      In current code, CoalesceImplementor doesn't handle NullAs properly by semantics.

      If we disable the rewrite from COALESCE to CASE in Validator and run below sql in JDBC Test:

      select grocery_sqft from store where coalesce(grocery_sqft, 0) >= 20000

      We will get below exception: 

      Caused by: java.lang.RuntimeException: Error while compiling generated Java code:
      public org.apache.calcite.linq4j.Enumerable bind(final org.apache.calcite.DataContext root) {
        final org.apache.calcite.linq4j.Enumerable _inputEnumerable = org.apache.calcite.schema.Schemas.queryable(root, root.getRootSchema().getSubSchema("foodmart2"), java.lang.Object[].class, "store").asEnumerable();
        return new org.apache.calcite.linq4j.AbstractEnumerable(){
            public org.apache.calcite.linq4j.Enumerator enumerator() {
              return new org.apache.calcite.linq4j.Enumerator(){
                  public final org.apache.calcite.linq4j.Enumerator inputEnumerator = _inputEnumerable.enumerator();
                  public void reset() {
                    inputEnumerator.reset();
                  }            public boolean moveNext() {
                    while (inputEnumerator.moveNext()) {
                      final Integer inp16_ = (Integer) ((Object[]) inputEnumerator.current())[16];
                      if ((inp16_ ? inp16_.intValue() : 0) >= 20000) {
                        return true;
                      }
                    }
                    return false;
                  }            public void close() {
                    inputEnumerator.close();
                  }            public Object current() {
                    return (Integer) ((Object[]) inputEnumerator.current())[16];
                  }          };
            }    };
      } 
      at org.apache.calcite.avatica.Helper.wrap(Helper.java:37) 
      at org.apache.calcite.avatica.Helper.wrap(Helper.java:37) 
      at org.apache.calcite.adapter.enumerable.EnumerableInterpretable.toBindable(EnumerableInterpretable.java:128) at org.apache.calcite.prepare.CalcitePrepareImpl$CalcitePreparingStmt.implement(CalcitePrepareImpl.java:1111) at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:332) 
      at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:231) 
      at org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:638) 
      at org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:502) 
      at org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:472) 
      at org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:231) 
      at org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:550) 
      at org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:675) 
      at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:156) ... 29 more
      
      Caused by: org.codehaus.commons.compiler.CompileException: Line 14, Column 28: Not a boolean expression 
      at org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:12211) 
      at org.codehaus.janino.UnitCompiler.compileBoolean2(UnitCompiler.java:3994) 
      at org.codehaus.janino.UnitCompiler.access$6300(UnitCompiler.java:215)

      This issue was found when I try to resolve CALCITE-3124 and CALCITE-3505

      [1] https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L1210

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jinxing6042@126.com Jin Xing
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 3h 50m
                  3h 50m