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

JDBC adapter should generate "CAST(NULL AS type)" rather than "NULL"

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.21.0
    • Fix Version/s: 1.21.0
    • Component/s: core
    • Environment:

      mac os

      Description

      I have as sql as follows: 

      select cast(null as varchar) as a

      The process step is : 

      1. parse sql to SqlNode;
      2. convert SqlNode to RelNode;
      3. convert RelNode to SqlNode;
      4. convert SqlNode to string.

      The result string is:

      "SELECT NULL AS `A`\n"
                  + "FROM (VALUES  (0)) AS `t` (`ZERO`)"

      Finally, I found that the result string is absent of cast statement for  "null" which causes the result sql to be invalid.

      Actually, the result I expect is like:

      "SELECT CAST(NULL AS VARCHAR) AS `A`\n"
                  + "FROM (VALUES  (0)) AS `t` (`ZERO`)"

       

      Testing code is as follows

      // code placeholder
      @Test
      public void testSeletNull() throws SqlParseException {
          String sql = "select cast(null as varchar) as a";
          SqlNode sqlNode = parserContext.parseStmt(sql);
          RelNode relNode = parserContext.getSqlToRelConverter().convertQuery(sqlNode, true, true).rel;
          SqlNode sqlNodeNew = toSqlNode(relNode);
          Assert.assertEquals(sqlNodeNew.toString(), "SELECT NULL AS `A`\n"
                  + "FROM (VALUES  (0)) AS `t` (`ZERO`)");
      }
      
      private static SqlNode toSqlNode(RelNode root) {
        SqlDialect dialect = MysqlSqlDialect.DEFAULT;
        RelToSqlConverter converter = new RelToSqlConverter(dialect == null ? dialect : dialect);
        return converter.visitChild(0, root).asStatement();
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                wangweidong Wang Weidong
                Reporter:
                wangweidong Wang Weidong
              • Votes:
                0 Vote for this issue
                Watchers:
                4 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 - 9h 40m
                  9h 40m