2022-06-16 09:58:14 +00:00
|
|
|
#include <Parsers/ParserCreateIndexQuery.h>
|
|
|
|
|
|
|
|
#include <Parsers/ASTCreateIndexQuery.h>
|
2022-07-01 20:35:20 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2022-06-16 09:58:14 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTIndexDeclaration.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ParserDataType.h>
|
|
|
|
#include <Parsers/parseDatabaseAndTableName.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool ParserCreateIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserKeyword s_type("TYPE");
|
|
|
|
ParserKeyword s_granularity("GRANULARITY");
|
2023-07-12 09:29:36 +00:00
|
|
|
ParserToken open(TokenType::OpeningRoundBracket);
|
|
|
|
ParserToken close(TokenType::ClosingRoundBracket);
|
|
|
|
ParserOrderByExpressionList order_list;
|
2022-06-16 09:58:14 +00:00
|
|
|
ParserDataType data_type_p;
|
|
|
|
ParserExpression expression_p;
|
|
|
|
ParserUnsignedInteger granularity_p;
|
|
|
|
|
|
|
|
ASTPtr expr;
|
2023-07-12 09:29:36 +00:00
|
|
|
ASTPtr order;
|
2022-06-16 09:58:14 +00:00
|
|
|
ASTPtr type;
|
|
|
|
ASTPtr granularity;
|
|
|
|
|
2023-07-12 12:47:24 +00:00
|
|
|
/// Skip name parser for SQL-standard CREATE INDEX
|
|
|
|
if (expression_p.parse(pos, expr, expected))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (open.ignore(pos, expected))
|
2023-07-12 09:29:36 +00:00
|
|
|
{
|
|
|
|
if (!order_list.parse(pos, order, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!close.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-16 09:58:14 +00:00
|
|
|
|
2023-07-12 09:29:36 +00:00
|
|
|
if (s_type.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!data_type_p.parse(pos, type, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-16 09:58:14 +00:00
|
|
|
|
2023-06-07 09:01:20 +00:00
|
|
|
if (s_granularity.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!granularity_p.parse(pos, granularity, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-16 09:58:14 +00:00
|
|
|
|
|
|
|
auto index = std::make_shared<ASTIndexDeclaration>();
|
2022-06-29 11:10:30 +00:00
|
|
|
index->part_of_create_index_query = true;
|
2022-06-16 09:58:14 +00:00
|
|
|
index->set(index->expr, expr);
|
2023-07-12 09:29:36 +00:00
|
|
|
if (type)
|
|
|
|
index->set(index->type, type);
|
2023-06-08 08:10:40 +00:00
|
|
|
|
|
|
|
if (granularity)
|
|
|
|
index->granularity = granularity->as<ASTLiteral &>().value.safeGet<UInt64>();
|
|
|
|
else
|
|
|
|
{
|
2023-07-12 09:29:36 +00:00
|
|
|
if (index->type && index->type->name == "annoy")
|
2023-06-12 20:06:57 +00:00
|
|
|
index->granularity = ASTIndexDeclaration::DEFAULT_ANNOY_INDEX_GRANULARITY;
|
2023-08-16 13:28:16 +00:00
|
|
|
else if (index->type && index->type->name == "usearch")
|
|
|
|
index->granularity = ASTIndexDeclaration::DEFAULT_USEARCH_INDEX_GRANULARITY;
|
2023-06-08 08:10:40 +00:00
|
|
|
else
|
2023-06-12 20:06:57 +00:00
|
|
|
index->granularity = ASTIndexDeclaration::DEFAULT_INDEX_GRANULARITY;
|
2023-06-08 08:10:40 +00:00
|
|
|
}
|
2022-06-16 09:58:14 +00:00
|
|
|
node = index;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserCreateIndexQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
auto query = std::make_shared<ASTCreateIndexQuery>();
|
|
|
|
node = query;
|
|
|
|
|
|
|
|
ParserKeyword s_create("CREATE");
|
2023-07-19 12:23:39 +00:00
|
|
|
ParserKeyword s_unique("UNIQUE");
|
2022-06-16 09:58:14 +00:00
|
|
|
ParserKeyword s_index("INDEX");
|
|
|
|
ParserKeyword s_if_not_exists("IF NOT EXISTS");
|
|
|
|
ParserKeyword s_on("ON");
|
|
|
|
ParserIdentifier index_name_p;
|
|
|
|
ParserCreateIndexDeclaration parser_create_idx_decl;
|
|
|
|
|
|
|
|
ASTPtr index_name;
|
|
|
|
ASTPtr index_decl;
|
|
|
|
|
|
|
|
String cluster_str;
|
|
|
|
bool if_not_exists = false;
|
2023-07-19 12:23:39 +00:00
|
|
|
bool unique = false;
|
2022-06-16 09:58:14 +00:00
|
|
|
|
2022-06-17 07:46:44 +00:00
|
|
|
if (!s_create.ignore(pos, expected))
|
2022-06-16 09:58:14 +00:00
|
|
|
return false;
|
|
|
|
|
2023-07-19 12:23:39 +00:00
|
|
|
if (s_unique.ignore(pos, expected))
|
|
|
|
unique = true;
|
|
|
|
|
2022-06-16 09:58:14 +00:00
|
|
|
if (!s_index.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (s_if_not_exists.ignore(pos, expected))
|
|
|
|
if_not_exists = true;
|
|
|
|
|
|
|
|
if (!index_name_p.parse(pos, index_name, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/// ON [db.] table_name
|
|
|
|
if (!s_on.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
2022-07-01 20:35:20 +00:00
|
|
|
if (!parseDatabaseAndTableAsAST(pos, expected, query->database, query->table))
|
2022-06-29 11:10:30 +00:00
|
|
|
return false;
|
2022-06-16 09:58:14 +00:00
|
|
|
|
|
|
|
/// [ON cluster_name]
|
2022-07-01 20:35:20 +00:00
|
|
|
if (s_on.ignore(pos, expected))
|
|
|
|
{
|
2022-06-29 11:10:30 +00:00
|
|
|
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
|
|
|
|
return false;
|
2022-07-01 20:35:20 +00:00
|
|
|
}
|
2022-06-16 09:58:14 +00:00
|
|
|
|
|
|
|
if (!parser_create_idx_decl.parse(pos, index_decl, expected))
|
|
|
|
return false;
|
|
|
|
|
2022-06-29 11:10:30 +00:00
|
|
|
auto & ast_index_decl = index_decl->as<ASTIndexDeclaration &>();
|
|
|
|
ast_index_decl.name = index_name->as<ASTIdentifier &>().name();
|
|
|
|
|
2022-06-16 09:58:14 +00:00
|
|
|
query->index_name = index_name;
|
|
|
|
query->children.push_back(index_name);
|
|
|
|
|
|
|
|
query->index_decl = index_decl;
|
|
|
|
query->children.push_back(index_decl);
|
|
|
|
|
|
|
|
query->if_not_exists = if_not_exists;
|
2023-07-19 12:23:39 +00:00
|
|
|
query->unique = unique;
|
2022-06-16 09:58:14 +00:00
|
|
|
query->cluster = cluster_str;
|
|
|
|
|
|
|
|
if (query->database)
|
|
|
|
query->children.push_back(query->database);
|
|
|
|
|
|
|
|
if (query->table)
|
|
|
|
query->children.push_back(query->table);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|