You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "yanjing.wang (Jira)" <ji...@apache.org> on 2022/04/20 12:31:00 UTC
[jira] [Commented] (CALCITE-5104) Select with CURRENT_DATE without alias produces alias that can't be parsed by Calcite itself
[ https://issues.apache.org/jira/browse/CALCITE-5104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17524962#comment-17524962 ]
yanjing.wang commented on CALCITE-5104:
---------------------------------------
Hi [~FDoKE] , the current_date is a sql call derived from a sql identifier, I think its alias should be generated rather than derived from an identifier. It may be better use 'newAlias' rather 'alias' when SqlStdOperatorTable.AS.createCall, how do you think?
> 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)