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

COALESCE throws ClassCastException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.33.0
    • 1.34.0
    • None
    • None

    Description

      A call to the COALESCE function throws ClassCastException. This is a regression caused by CALCITE-5424.

      After updating to calcite 1.33.0, I found that validation of the following query fails with a ClassCastException. Query:

      SELECT COALESCE(DATE '2021-07-08', DATE '2020-01-01’)
      

      Error:

      org.apache.calcite.tools.ValidationException: java.lang.ClassCastException: class org.apache.calcite.sql.SqlLiteral cannot be cast to class org.apache.calcite.sql.SqlUnknownLiteral (org.apache.calcite.sql.SqlLiteral and org.apache.calcite.sql.SqlUnknownLiteral are in unnamed module of loader 'app')
      
      	at org.apache.calcite.prepare.PlannerImpl.validate(PlannerImpl.java:226)
      	at org.apache.calcite.rex.RexSqlStandardConvertletTableTest.convertSqlToRel(RexSqlStandardConvertletTableTest.java:101)
      	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
      	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
      	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
      	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
      	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
      Caused by: java.lang.ClassCastException: class org.apache.calcite.sql.SqlLiteral cannot be cast to class org.apache.calcite.sql.SqlUnknownLiteral (org.apache.calcite.sql.SqlLiteral and org.apache.calcite.sql.SqlUnknownLiteral are in unnamed module of loader 'app')
      	at org.apache.calcite.sql.validate.SqlValidatorImpl.resolveLiteral(SqlValidatorImpl.java:6152)
      	at org.apache.calcite.sql.validate.SqlValidatorImpl$Expander.visit(SqlValidatorImpl.java:6617)
      	at org.apache.calcite.sql.validate.SqlValidatorImpl$Expander.visit(SqlValidatorImpl.java:6590)
      

      When I move a commit pointer prior to the commit that introduces SqlUnknownLiteral, CALCITE-5424, the following test case passes successfully.

      The reproducer (added to RexSqlStandardConvertletTableTest):

      @Test void testCoalesceWithUnknownLiteral() {
          final Project project = (Project) convertSqlToRel(
              "SELECT COALESCE(DATE '2021-07-08', DATE '2020-01-01')", false);
      
          final RexNode rex = project.getProjects().get(0);
          final RexToSqlNodeConverter rexToSqlNodeConverter = rexToSqlNodeConverter();
          final SqlNode convertedSql = rexToSqlNodeConverter.convertNode(rex);
      
          assertEquals(
              "CASE WHEN DATE '2021-07-08' IS NOT NULL THEN DATE '2021-07-08' ELSE DATE '2020-01-01' END",
              convertedSql.toString());
        }
      

      Attachments

        Issue Links

          Activity

            People

              julianhyde Julian Hyde
              mzhuravkov Maksim Zhuravkov
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: