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

Code generation incorrect (does not compile) for DATE, TIME and TIMESTAMP fields



    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.16.0
    • Component/s: core
    • Labels:


      The generated code for sql queries that filter on DATE, TIME or TIMESTAMP fields is incorrect.

      The problem can easily be reproduced with the csv example using a statement such as (patch with unit test in attachment).

      select EMPNO from "DATE" where JOINEDAT is not null;

      or (similar but not exactly the same problem)

      select EMPNO from "DATE" where JOINEDAT > {d '1990-01-01'};

      In case of the not-null filter, the generated code contains

      if (org.apache.calcite.runtime.SqlFunctions.internalToDate(((Object[]) inputEnumerator.current())[1]) != null) {...}

      This seems to be generated in the assumption that the internal representation is a long, rather than a java.sql.Date, java.sql.Time or java.sql.Timestamp.

      So first question: is the enumerator supposed to return java.sql.Date, java.sql.Time and java.sql.Timestamp values or simply longs (epoch millis). Currently it returns java.sql.* types and that is definitely how it used to work in older calcite versions.

      In that case the generated code should probably not contain the internalToDate function?

      However the mere existence of that function makes me think that maybe the enumerators are supposed to return long values. But in that case the generated code is also wrong, it should probably be (added cast):

      if (org.apache.calcite.runtime.SqlFunctions.internalToDate((Long) ((Object[]) inputEnumerator.current())[1]) != null) {...}

      With the second sql query (greater than filter), the generated code actually does contain a cast to Integer (which fails):

      final Integer inp1_ = (Integer) ((Object[]) inputEnumerator.current())[1];
      if (inp1_ != null && inp1_.intValue() > 7305) {...}

      The cast fails with java.sql.Date cannot be cast to java.lang.Integer


        1. CALCITE_1427_unit_tests.patch
          3 kB
          Jan Van Besien

          Issue Links



              • Assignee:
                julianhyde Julian Hyde
                janvanbesien Jan Van Besien
              • Votes:
                0 Vote for this issue
                4 Start watching this issue


                • Created: