You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by "Vyacheslav Puzakov (Jira)" <ji...@apache.org> on 2022/04/20 07:27:00 UTC

[jira] [Created] (CALCITE-5104) Select with CURRENT_DATE without alias produces alias that can't be parsed by Calcite itself

Vyacheslav Puzakov created CALCITE-5104:
-------------------------------------------

             Summary: Select with CURRENT_DATE without alias produces alias that can't be parsed by Calcite itself
                 Key: CALCITE-5104
                 URL: https://issues.apache.org/jira/browse/CALCITE-5104
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.30.0
            Reporter: Vyacheslav Puzakov


Validator configured with 
{code:java}
validatorConfig.withIdentifierExpansion(true){code}
In case of this original query:

 
{code:java}
select current_date from emp{code}
 

validator produces next sql node:

 
{code:java}
SELECT CURRENT_DATE AS CURRENT_DATE
FROM "CATALOG"."SALES"."EMP" AS "EMP"{code}
 

Where CURRENT_DATE used as simple *Alias*

If you try to parse that syntax, Calcite will fail with:

 
{code:java}
Caused by: org.apache.calcite.sql.parser.SqlParseException: Incorrect syntax near the keyword 'CURRENT_DATE' at line 1, column 24.
Was expecting one of:
    <QUOTED_STRING> ...
    <BRACKET_QUOTED_IDENTIFIER> ...
    <QUOTED_IDENTIFIER> ...
    <BACK_QUOTED_IDENTIFIER> ...
    <BIG_QUERY_BACK_QUOTED_IDENTIFIER> ...
    <HYPHENATED_IDENTIFIER> ...
    <IDENTIFIER> ...
    <UNICODE_QUOTED_IDENTIFIER> ...{code}
 

 

I tracked down a problem causing it:

SqlValidatorImpl.java

Line 435:

 
{code:java}
final String alias =
    deriveAliasNonNull(
        selectItem,
        aliases.size()); {code}
produces "current_date" as alias

 

And then on the line 448 it applies:

 
{code:java}
expanded =
    SqlStdOperatorTable.AS.createCall(
        selectItem.getParserPosition(),
        expanded,
        new SqlIdentifier(alias, SqlParserPos.ZERO)); {code}
When this identifier *unparses* (SqlUtil.java)

 
{code:java}
public static void unparseSqlIdentifierSyntax(
...
final SqlOperator operator = isUnquotedSimple
    ? SqlValidatorUtil.lookupSqlFunctionByID(SqlStdOperatorTable.instance(), identifier, null)
    : null;{code}
 

It finds that this identifier has *operator* as its *isUnquotedSimple* and putting it without quotes.

Possible solutions:
 # Change to SqlParserPos.QUOTED_ZERO which will produce valid syntax after unparsing, but will affect a lot results
in this case result will be (which parsed succesfully):

{code:java}
SELECT CURRENT_DATE AS "CURRENT_DATE"
FROM "CATALOG"."SALES"."EMP" AS "EMP"{code}

 #  A bit smarter move - check that this identifier is reserved keyword  (function which parser can't handle in AS operator) and force quotes only in this case.

 

 



--
This message was sent by Atlassian Jira
(v8.20.7#820007)