Ian Bertolacci created CALCITE-5273:
---------------------------------------

             Summary: RelToSqlConverter allows unparsing of invalid CASE 
expression
                 Key: CALCITE-5273
                 URL: https://issues.apache.org/jira/browse/CALCITE-5273
             Project: Calcite
          Issue Type: Bug
            Reporter: Ian Bertolacci


RelToSqlConverter will unparse the invalid† CASE expression {{`CASE ELSE 1 
END`}} (which can be constructed using either RelBuilder or RexBuilder, see 
CALCITE-5272).
(† Or at least and expression which Calcite then cannot parse.)

Given this RelNode tree with invalid case statements
{code}
1:LogicalProject($f0=[CASE(1)], $f1=[CASE(1:BIGINT)])
  0:LogicalValues(tuples=[[{ 0 }]])
{code}

Unparsing with RelToSqlConverter using MysqlSqlDialect.DEFAULT‡ gives the Sql:
{code}
SELECT CASE ELSE 1 END AS `$f0`, CASE ELSE 1 END AS `$f1`
{code}

Running that sql back through the parser using Lex.MYSQL‡ and 
SqlConformanceEnum.MYSQL_5‡ throws the exception:
{code}
Incorrect syntax near the keyword 'CASE' at line 1, column 8.
Was expecting one of:
    "ALL" ...
    "CURSOR" ...
    "DISTINCT" ...
    ...
    org.apache.calcite.sql.parser.SqlParseException: Incorrect syntax near the 
keyword 'CASE' at line 1, column 8.
Was expecting one of:
    "ALL" ...
    "CURSOR" ...
    "DISTINCT" ...
    ...
        at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:389)
        at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:153)
        at 
org.apache.calcite.sql.parser.SqlParser.handleException(SqlParser.java:145)
        at 
org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:160)
        at org.apache.calcite.sql.parser.SqlParser.parseStmt(SqlParser.java:185)
        at org.apache.calcite.prepare.PlannerImpl.parse(PlannerImpl.java:214)
        at org.apache.calcite.tools.Planner.parse(Planner.java:50)
        ..
Caused by: org.apache.calcite.sql.parser.impl.ParseException: Incorrect syntax 
near the keyword 'CASE' at line 1, column 8.
Was expecting one of:
    "ALL" ...
    "CURSOR" ...
    "DISTINCT" ...
    ...
        at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:349)
        ... 59 more
{code}

‡ I don't believe the specific dialect/lex/conformance parameters affect the 
outcome.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to