You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by "Wang Weidong (JIRA)" <ji...@apache.org> on 2019/07/24 08:46:00 UTC

[jira] [Created] (CALCITE-3210) RelToSqlConverter convert "cast(null as $type)" just as null

Wang Weidong created CALCITE-3210:
-------------------------------------

             Summary: RelToSqlConverter convert "cast(null as $type)" just as  null 
                 Key: CALCITE-3210
                 URL: https://issues.apache.org/jira/browse/CALCITE-3210
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.21.0
         Environment: mac os
            Reporter: Wang Weidong
             Fix For: next


I have as sql as follows: 
{code:java}
select cast(null as varchar) as a{code}
The process step is : 
 # parse sql to SqlNode;
 # convert SqlNode to RelNode;
 # convert RelNode to SqlNode;
 # convert SqlNode to string.

The result string is:
{code:java}
"SELECT NULL AS `A`\n"
            + "FROM (VALUES  (0)) AS `t` (`ZERO`)"{code}
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:
{code:java}
SELECT CAST(NULL AS VARCHAR) AS `A`\n"
            + "FROM (VALUES  (0)) AS `t` (`ZERO`){code}
 

Testing code is as follows
{code:java}
// 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();
}
{code}



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)