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

Exception while evaluating ROUND/TRUNCATE functions

    XMLWordPrintableJSON

Details

    Description

      The following test in CalciteSqlOperatorTest:

       @Test void testRoundFunc() {
          final SqlOperatorFixture f = fixture();
          f.checkScalar("round(42, CAST(2 as BIGINT))", 42, "INTEGER NOT NULL");
        }
      

      causes an exception; here is the relevant part of the stack trace:

      java.sql.SQLException: Error while executing SQL "values (round(42, CAST(2 as BIGINT)))": Unable to implement EnumerableCalc(expr#0=[{inputs}], expr#1=[42], expr#2=[2:BIGINT], expr#3=[ROUND($t1, $t2)], EXPR$0=[$t3]): rowcount = 1.0, cumulative cost = {2.0 rows, 6.0 cpu, 0.0 io}, id = 20
        EnumerableValues(tuples=[[{ 0 }]]): rowcount = 1.0, cumulative cost = {1.0 rows, 1.0 cpu, 0.0 io}, id = 13
      ...
      	Suppressed: java.lang.RuntimeException: while resolving method 'sround[int, long]' in class class org.apache.calcite.runtime.SqlFunctions
      		at org.apache.calcite.adapter.enumerable.EnumUtils.call(EnumUtils.java:679)
      		at org.apache.calcite.adapter.enumerable.RexImpTable$MethodImplementor.call(RexImpTable.java:2818)
      		at org.apache.calcite.adapter.enumerable.RexImpTable$MethodImplementor.implementSafe(RexImpTable.java:2799)
      		at org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:3857)
      		at org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:3819)
      

      And indeed, SqlFunctions does not have a function sround with this signature.

      There are several possible fixes:

      • reject calls to ROUND that have a BIGINT second argument
      • have the validator insert an implicit cast for the second argument to INTEGER
      • implement more Java versions of the SROUND function in SqlFunctions. Probably many more.

      Which one of these is the right one? I suspect this problem applies to other SQL functions as well.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              mbudiu Mihai Budiu
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: