Avoid generating named tuple for special keywords

This commit is contained in:
Amos Bird 2024-07-17 18:47:33 +08:00
parent 2c54b5cab4
commit eb129232ff
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
4 changed files with 26 additions and 1 deletions

View File

@ -1,5 +1,6 @@
#include <Parsers/isUnquotedIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/Lexer.h>
namespace DB
@ -7,6 +8,18 @@ namespace DB
bool isUnquotedIdentifier(const String & name)
{
auto is_keyword = [&name](Keyword keyword)
{
auto s = toStringView(keyword);
if (name.size() != s.size())
return false;
return strncasecmp(s.data(), name.data(), s.size()) == 0;
};
/// Special keywords are parsed as literals instead of identifiers.
if (is_keyword(Keyword::NULL_KEYWORD) || is_keyword(Keyword::TRUE_KEYWORD) || is_keyword(Keyword::FALSE_KEYWORD))
return false;
Lexer lexer(name.data(), name.data() + name.size());
auto maybe_ident = lexer.nextToken();

View File

@ -5,6 +5,14 @@
namespace DB
{
/// Checks if the input string @name is a valid unquoted identifier.
///
/// Example Usage:
/// abc -> true (valid unquoted identifier)
/// 123 -> false (identifiers cannot start with digits)
/// `123` -> false (quoted identifiers are not considered)
/// `abc` -> false (quoted identifiers are not considered)
/// null -> false (reserved literal keyword)
bool isUnquotedIdentifier(const String & name);
}

View File

@ -7,3 +7,4 @@ Tuple(\n k UInt8,\n j Int32)
Tuple(Int32, Int32, Int32, Int32)
['1','2','3','4']
(1,2,3)
Tuple(Nullable(Nothing)) Tuple(Bool) Tuple(Bool)

View File

@ -28,4 +28,7 @@ create table tbl (x Tuple(a Int32, b Int32, c Int32)) engine MergeTree order by
insert into tbl values (tuple(1, 2, 3)); -- without tuple it's interpreted differently inside values block.
select * from tbl;
drop table tbl
drop table tbl;
-- Avoid generating named tuple for special keywords
select toTypeName(tuple(null)), toTypeName(tuple(true)), toTypeName(tuple(false));