[ https://issues.apache.org/jira/browse/CALCITE-5104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17526089#comment-17526089 ]
Julian Hyde edited comment on CALCITE-5104 at 4/21/22 9:28 PM: --------------------------------------------------------------- The [Postgres doc|https://www.postgresql.org/docs/current/sql-keywords-appendix.html] says {quote} Even reserved key words are not completely reserved in PostgreSQL, but can be used as column labels (for example, {{SELECT 55 AS CHECK}}, even though {{CHECK}} is a reserved key word). {quote} When I read that, it sounds like they're embarrassed to have made things so complicated. was (Author: julianhyde): The [Postgres doc|https://www.postgresql.org/docs/current/sql-keywords-appendix.html] says {quote}Even reserved key words are not completely reserved in PostgreSQL, but can be used as column labels (for example, {{{}SELECT 55 AS CHECK{}}}, even though {{CHECK}} is a reserved key word).\{quote} When I read that, it sounds like they're embarrassed to have made things so complicated. > 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)