[
https://issues.apache.org/jira/browse/CALCITE-5104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17525155#comment-17525155
]
Julian Hyde commented on CALCITE-5104:
--------------------------------------
I checked, and I see that CURRENT_DATE is a [reserved
keyword|https://github.com/apache/calcite/blob/bf56743554ea27d250d41db2eb83806f9e626b55/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java#L195].
I think the check should be on whether the alias is a reserved keyword, not
whether it is a function.
> 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
> Priority: Major
>
> 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:*
> 1) 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}
> 2) 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)