Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParserAlias ignores backticks whaen checking for restricted keywords #49324

Closed
zhicwu opened this issue Apr 29, 2023 · 1 comment · Fixed by #49360
Closed

ParserAlias ignores backticks whaen checking for restricted keywords #49324

zhicwu opened this issue Apr 29, 2023 · 1 comment · Fixed by #49360
Assignees

Comments

@zhicwu
Copy link
Contributor

zhicwu commented Apr 29, 2023

Is the behavior below expected? Looks like SQL parsing issue? select null `tuple` works but select null `array` won't, unless as is used explicitly.

$ clickhouse-client
ClickHouse client version 23.3.2.37 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 23.3.2 revision 54462.

ch-server :) select range(1,2) `array`

Syntax error: failed at position 19 ('`array`'):

select range(1,2) `array`

Expected one of: token, OpeningRoundBracket, FILTER, OVER, OR, AND, BETWEEN, NOT BETWEEN, LIKE, ILIKE, NOT LIKE, NOT ILIKE, REGEXP, IN, NOT IN, GLOBAL IN, GLOBAL NOT IN, MOD, DIV, IS NULL, IS NOT NULL, alias, AS, identifier, Comma, FROM, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, ORDER BY, LIMIT, OFFSET, SETTINGS, UNION, EXCEPT, INTERSECT, INTO OUTFILE, FORMAT, end of query

ch-server :) select range(1,2) as `array`

SELECT range(1, 2) AS array

Query id: 19ea4aa4-80ee-4dc2-9b9b-d6329d86bfa5

┌─array─┐
│ [1]   │
└───────┘

1 row in set. Elapsed: 0.004 sec. 
@tavplubix
Copy link
Member

Looks like it's because ARRAY is a restricted keyword (TUPLE is not) and ParserAlias::parseImpl ignores backticks (it should not):

ParserKeyword s_as("AS");
ParserIdentifier id_p;
bool has_as_word = s_as.ignore(pos, expected);
if (!allow_alias_without_as_keyword && !has_as_word)
return false;
if (!id_p.parse(pos, node, expected))
return false;
if (!has_as_word)
{
/** In this case, the alias can not match the keyword -
* so that in the query "SELECT x FROM t", the word FROM was not considered an alias,
* and in the query "SELECT x FR FROM t", the word FR was considered an alias.
*/
const String name = getIdentifierName(node);
for (const char ** keyword = restricted_keywords; *keyword != nullptr; ++keyword)
if (0 == strcasecmp(name.data(), *keyword))
return false;

@tavplubix tavplubix changed the title SQL parsing issue? ParserAlias ignores backticks whaen checking for restricted keywords May 1, 2023
@evillique evillique self-assigned this May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants