ClickHouse/src/Parsers/ParserCreateQuery.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1610 lines
47 KiB
C++
Raw Normal View History

#include <IO/ReadHelpers.h>
#include <Parsers/ASTConstraintDeclaration.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
2018-12-25 18:45:08 +00:00
#include <Parsers/ASTIndexDeclaration.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTProjectionDeclaration.h>
2019-03-23 22:45:28 +00:00
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/ASTCreateNamedCollectionQuery.h>
#include <Parsers/ASTTableOverrides.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/ParserCreateQuery.h>
2019-10-09 20:09:10 +00:00
#include <Parsers/ParserDictionary.h>
#include <Parsers/ParserDictionaryAttributeDeclaration.h>
#include <Parsers/ParserProjectionSelectQuery.h>
#include <Parsers/ParserSelectWithUnionQuery.h>
#include <Parsers/ParserSetQuery.h>
#include <Common/typeid_cast.h>
#include <Parsers/ASTColumnDeclaration.h>
2011-08-18 18:48:00 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
namespace
{
2022-08-28 00:00:09 +00:00
ASTPtr parseComment(IParser::Pos & pos, Expected & expected)
{
ParserKeyword s_comment("COMMENT");
ParserStringLiteral string_literal_parser;
ASTPtr comment;
s_comment.ignore(pos, expected) && string_literal_parser.parse(pos, comment, expected);
return comment;
}
2022-08-28 00:00:09 +00:00
}
2022-08-28 00:00:09 +00:00
bool ParserNestedTable::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserToken open(TokenType::OpeningRoundBracket);
ParserToken close(TokenType::ClosingRoundBracket);
ParserIdentifier name_p;
ParserNameTypePairList columns_p;
ASTPtr name;
ASTPtr columns;
2017-04-02 17:37:49 +00:00
/// For now `name == 'Nested'`, probably alternative nested data structures will appear
if (!name_p.parse(pos, name, expected))
return false;
if (!open.ignore(pos))
return false;
if (!columns_p.parse(pos, columns, expected))
return false;
if (!close.ignore(pos))
return false;
2018-02-26 03:37:08 +00:00
auto func = std::make_shared<ASTFunction>();
2019-08-08 20:02:30 +00:00
tryGetIdentifierNameInto(name, func->name);
// FIXME(ilezhankin): func->no_empty_args = true; ?
func->arguments = columns;
func->children.push_back(columns);
node = func;
return true;
}
bool ParserIdentifierWithParameters::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
2020-06-18 12:52:05 +00:00
return ParserFunction().parse(pos, node, expected);
}
2011-08-18 18:48:00 +00:00
bool ParserNameTypePairList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserNameTypePair>(), std::make_unique<ParserToken>(TokenType::Comma), false)
.parse(pos, node, expected);
}
bool ParserColumnDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserColumnDeclaration>(require_type, allow_null_modifiers, check_keywords_after_name), std::make_unique<ParserToken>(TokenType::Comma), false)
.parse(pos, node, expected);
}
bool ParserNameList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserCompoundIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma), false)
.parse(pos, node, expected);
}
2018-12-25 18:45:08 +00:00
bool ParserIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_type("TYPE");
2019-01-02 16:04:44 +00:00
ParserKeyword s_granularity("GRANULARITY");
2018-12-25 18:45:08 +00:00
ParserIdentifier name_p;
2020-06-18 12:52:05 +00:00
ParserDataType data_type_p;
2019-01-20 15:02:19 +00:00
ParserExpression expression_p;
2019-01-03 08:10:25 +00:00
ParserUnsignedInteger granularity_p;
2018-12-25 18:45:08 +00:00
ASTPtr name;
ASTPtr expr;
ASTPtr type;
2019-01-02 16:04:44 +00:00
ASTPtr granularity;
2018-12-25 18:45:08 +00:00
if (!name_p.parse(pos, name, expected))
return false;
if (!expression_p.parse(pos, expr, expected))
return false;
if (!s_type.ignore(pos, expected))
return false;
2020-06-18 12:52:05 +00:00
if (!data_type_p.parse(pos, type, expected))
2018-12-25 18:45:08 +00:00
return false;
2023-01-19 20:52:38 +00:00
if (s_granularity.ignore(pos, expected))
{
if (!granularity_p.parse(pos, granularity, expected))
return false;
}
2019-01-02 16:04:44 +00:00
2018-12-25 18:45:08 +00:00
auto index = std::make_shared<ASTIndexDeclaration>();
index->name = name->as<ASTIdentifier &>().name();
2018-12-25 18:45:08 +00:00
index->set(index->expr, expr);
index->set(index->type, type);
2023-06-08 08:10:40 +00:00
if (granularity)
index->granularity = granularity->as<ASTLiteral &>().value.safeGet<UInt64>();
else
{
if (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->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
}
2018-12-25 18:45:08 +00:00
node = index;
return true;
}
2019-05-12 11:36:02 +00:00
bool ParserConstraintDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_check("CHECK");
2021-01-03 15:02:00 +00:00
ParserKeyword s_assume("ASSUME");
2019-05-12 11:36:02 +00:00
ParserIdentifier name_p;
2022-09-07 21:41:37 +00:00
ParserExpression expression_p;
2019-05-12 11:36:02 +00:00
ASTPtr name;
ASTPtr expr;
2021-01-03 15:02:00 +00:00
ASTConstraintDeclaration::Type type = ASTConstraintDeclaration::Type::CHECK;
2019-01-20 09:19:34 +00:00
2019-05-12 11:36:02 +00:00
if (!name_p.parse(pos, name, expected))
return false;
if (!s_check.ignore(pos, expected))
2021-01-03 15:02:00 +00:00
{
if (s_assume.ignore(pos, expected))
type = ASTConstraintDeclaration::Type::ASSUME;
else
return false;
}
2019-05-12 11:36:02 +00:00
if (!expression_p.parse(pos, expr, expected))
return false;
auto constraint = std::make_shared<ASTConstraintDeclaration>();
constraint->name = name->as<ASTIdentifier &>().name();
2021-01-03 15:02:00 +00:00
constraint->type = type;
2019-05-12 11:36:02 +00:00
constraint->set(constraint->expr, expr);
node = constraint;
return true;
}
bool ParserProjectionDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserIdentifier name_p;
ParserProjectionSelectQuery query_p;
ParserToken s_lparen(TokenType::OpeningRoundBracket);
ParserToken s_rparen(TokenType::ClosingRoundBracket);
ASTPtr name;
ASTPtr query;
if (!name_p.parse(pos, name, expected))
return false;
if (!s_lparen.ignore(pos, expected))
return false;
if (!query_p.parse(pos, query, expected))
return false;
if (!s_rparen.ignore(pos, expected))
return false;
auto projection = std::make_shared<ASTProjectionDeclaration>();
projection->name = name->as<ASTIdentifier &>().name();
projection->set(projection->query, query);
node = projection;
return true;
}
2019-05-12 11:36:02 +00:00
bool ParserTablePropertyDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2019-01-20 09:19:34 +00:00
{
ParserKeyword s_index("INDEX");
2019-05-12 11:36:02 +00:00
ParserKeyword s_constraint("CONSTRAINT");
ParserKeyword s_projection("PROJECTION");
ParserKeyword s_primary_key("PRIMARY KEY");
2019-01-20 09:19:34 +00:00
ParserIndexDeclaration index_p;
2019-05-12 11:36:02 +00:00
ParserConstraintDeclaration constraint_p;
ParserProjectionDeclaration projection_p;
2020-06-14 16:17:22 +00:00
ParserColumnDeclaration column_p{true, true};
ParserExpression primary_key_p;
2019-01-20 09:19:34 +00:00
2019-01-20 15:02:19 +00:00
ASTPtr new_node = nullptr;
2019-01-20 09:19:34 +00:00
if (s_index.ignore(pos, expected))
{
2019-01-20 15:02:19 +00:00
if (!index_p.parse(pos, new_node, expected))
2019-01-20 09:19:34 +00:00
return false;
}
2019-05-12 11:36:02 +00:00
else if (s_constraint.ignore(pos, expected))
{
if (!constraint_p.parse(pos, new_node, expected))
return false;
}
else if (s_projection.ignore(pos, expected))
{
if (!projection_p.parse(pos, new_node, expected))
return false;
}
else if (s_primary_key.ignore(pos, expected))
{
if (!primary_key_p.parse(pos, new_node, expected))
return false;
}
2019-01-20 09:19:34 +00:00
else
{
2019-01-20 15:02:19 +00:00
if (!column_p.parse(pos, new_node, expected))
2019-01-20 09:19:34 +00:00
return false;
}
2019-01-20 15:02:19 +00:00
node = new_node;
2019-01-20 09:19:34 +00:00
return true;
}
2019-01-08 11:04:25 +00:00
bool ParserIndexDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserIndexDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false)
2019-05-25 14:07:45 +00:00
.parse(pos, node, expected);
}
bool ParserConstraintDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserConstraintDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false)
2019-01-08 11:04:25 +00:00
.parse(pos, node, expected);
}
bool ParserProjectionDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserProjectionDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false)
.parse(pos, node, expected);
}
2019-05-12 11:36:02 +00:00
bool ParserTablePropertiesDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2019-01-20 09:19:34 +00:00
{
ASTPtr list;
2019-05-12 11:36:02 +00:00
if (!ParserList(
std::make_unique<ParserTablePropertyDeclaration>(),
std::make_unique<ParserToken>(TokenType::Comma), false)
2019-01-20 09:19:34 +00:00
.parse(pos, list, expected))
return false;
ASTPtr columns = std::make_shared<ASTExpressionList>();
ASTPtr indices = std::make_shared<ASTExpressionList>();
2019-05-12 11:36:02 +00:00
ASTPtr constraints = std::make_shared<ASTExpressionList>();
ASTPtr projections = std::make_shared<ASTExpressionList>();
ASTPtr primary_key;
2023-07-05 12:28:27 +00:00
ASTPtr primary_key_from_columns;
2019-01-20 09:19:34 +00:00
for (const auto & elem : list->children)
{
if (auto * cd = elem->as<ASTColumnDeclaration>())
2023-07-05 12:28:27 +00:00
{
2023-07-06 10:34:34 +00:00
if (cd->primary_key_specifier)
2023-07-05 12:28:27 +00:00
{
2023-07-06 10:34:34 +00:00
if (!primary_key_from_columns)
2023-07-05 12:28:27 +00:00
primary_key_from_columns = makeASTFunction("tuple");
auto column_identifier = std::make_shared<ASTIdentifier>(cd->name);
2023-07-06 09:50:44 +00:00
primary_key_from_columns->children[0]->as<ASTExpressionList>()->children.push_back(column_identifier);
2023-07-05 12:28:27 +00:00
}
2019-01-20 09:19:34 +00:00
columns->children.push_back(elem);
2023-07-05 12:28:27 +00:00
}
2019-03-11 13:22:51 +00:00
else if (elem->as<ASTIndexDeclaration>())
2019-01-20 09:19:34 +00:00
indices->children.push_back(elem);
2019-05-12 11:36:02 +00:00
else if (elem->as<ASTConstraintDeclaration>())
constraints->children.push_back(elem);
else if (elem->as<ASTProjectionDeclaration>())
projections->children.push_back(elem);
else if (elem->as<ASTIdentifier>() || elem->as<ASTFunction>())
{
if (primary_key)
{
2021-01-04 21:28:57 +00:00
/// Multiple primary keys are not allowed.
return false;
}
primary_key = elem;
}
2019-01-20 09:19:34 +00:00
else
return false;
}
auto res = std::make_shared<ASTColumns>();
if (!columns->children.empty())
res->set(res->columns, columns);
if (!indices->children.empty())
res->set(res->indices, indices);
2019-05-12 11:36:02 +00:00
if (!constraints->children.empty())
res->set(res->constraints, constraints);
if (!projections->children.empty())
res->set(res->projections, projections);
if (primary_key)
res->set(res->primary_key, primary_key);
2023-07-05 12:28:27 +00:00
if (primary_key_from_columns)
res->set(res->primary_key_from_columns, primary_key_from_columns);
2019-01-20 09:19:34 +00:00
node = res;
return true;
}
bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2011-11-01 15:16:04 +00:00
{
ParserKeyword s_engine("ENGINE");
ParserToken s_eq(TokenType::Equals);
ParserKeyword s_partition_by("PARTITION BY");
ParserKeyword s_primary_key("PRIMARY KEY");
ParserKeyword s_order_by("ORDER BY");
ParserKeyword s_sample_by("SAMPLE BY");
ParserKeyword s_ttl("TTL");
ParserKeyword s_settings("SETTINGS");
2011-11-01 15:16:04 +00:00
ParserIdentifierWithOptionalParameters ident_with_optional_params_p;
ParserExpression expression_p;
ParserSetQuery settings_p(/* parse_only_internals_ = */ true);
2019-10-09 13:02:05 +00:00
ParserTTLExpressionList parser_ttl_list;
2021-04-23 12:18:23 +00:00
ParserStringLiteral string_literal_parser;
2011-11-01 15:16:04 +00:00
ASTPtr engine;
ASTPtr partition_by;
ASTPtr primary_key;
ASTPtr order_by;
ASTPtr sample_by;
ASTPtr ttl_table;
ASTPtr settings;
2022-01-31 17:10:59 +00:00
bool storage_like = false;
2022-04-25 19:00:39 +00:00
bool parsed_engine_keyword = s_engine.ignore(pos, expected);
2022-04-25 19:00:39 +00:00
if (parsed_engine_keyword)
2022-01-31 17:10:59 +00:00
{
s_eq.ignore(pos, expected);
2022-01-31 17:10:59 +00:00
if (!ident_with_optional_params_p.parse(pos, engine, expected))
return false;
storage_like = true;
}
2011-11-01 15:16:04 +00:00
while (true)
2011-11-01 15:16:04 +00:00
{
if (!partition_by && s_partition_by.ignore(pos, expected))
{
if (expression_p.parse(pos, partition_by, expected))
2022-01-31 17:10:59 +00:00
{
storage_like = true;
continue;
2022-01-31 17:10:59 +00:00
}
else
return false;
}
2011-11-01 15:16:04 +00:00
if (!primary_key && s_primary_key.ignore(pos, expected))
{
if (expression_p.parse(pos, primary_key, expected))
2022-01-31 17:10:59 +00:00
{
storage_like = true;
continue;
2022-01-31 17:10:59 +00:00
}
else
return false;
}
if (!order_by && s_order_by.ignore(pos, expected))
{
if (expression_p.parse(pos, order_by, expected))
2022-01-31 17:10:59 +00:00
{
storage_like = true;
continue;
2022-01-31 17:10:59 +00:00
}
else
return false;
}
if (!sample_by && s_sample_by.ignore(pos, expected))
{
if (expression_p.parse(pos, sample_by, expected))
2022-01-31 17:10:59 +00:00
{
storage_like = true;
continue;
2022-01-31 17:10:59 +00:00
}
else
return false;
}
if (!ttl_table && s_ttl.ignore(pos, expected))
{
2019-10-09 13:02:05 +00:00
if (parser_ttl_list.parse(pos, ttl_table, expected))
2022-01-31 17:10:59 +00:00
{
storage_like = true;
continue;
2022-01-31 17:10:59 +00:00
}
else
return false;
}
2022-04-25 19:00:39 +00:00
/// Do not allow SETTINGS clause without ENGINE,
/// because we cannot distinguish engine settings from query settings in this case.
/// And because settings for each engine are different.
if (parsed_engine_keyword && s_settings.ignore(pos, expected))
{
if (!settings_p.parse(pos, settings, expected))
return false;
2022-01-31 17:10:59 +00:00
storage_like = true;
}
break;
2011-11-01 15:16:04 +00:00
}
2022-01-31 17:10:59 +00:00
// If any part of storage definition is found create storage node
if (!storage_like)
return false;
2011-11-01 15:16:04 +00:00
if (engine)
{
switch (engine_kind)
{
case EngineKind::TABLE_ENGINE:
engine->as<ASTFunction &>().kind = ASTFunction::Kind::TABLE_ENGINE;
break;
case EngineKind::DATABASE_ENGINE:
engine->as<ASTFunction &>().kind = ASTFunction::Kind::DATABASE_ENGINE;
break;
}
}
2018-02-26 03:37:08 +00:00
auto storage = std::make_shared<ASTStorage>();
storage->set(storage->engine, engine);
storage->set(storage->partition_by, partition_by);
storage->set(storage->primary_key, primary_key);
storage->set(storage->order_by, order_by);
storage->set(storage->sample_by, sample_by);
storage->set(storage->ttl_table, ttl_table);
storage->set(storage->settings, settings);
node = storage;
2011-11-01 15:16:04 +00:00
return true;
}
2019-10-09 20:09:10 +00:00
bool ParserCreateTableQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2011-08-18 18:48:00 +00:00
{
ParserKeyword s_create("CREATE");
ParserKeyword s_attach("ATTACH");
2020-12-25 16:28:10 +00:00
ParserKeyword s_replace("REPLACE");
ParserKeyword s_or_replace("OR REPLACE");
2019-10-23 13:46:38 +00:00
ParserKeyword s_temporary("TEMPORARY");
ParserKeyword s_table("TABLE");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
2023-08-15 18:37:39 +00:00
ParserCompoundIdentifier table_name_p(/*table_name_with_optional_uuid*/ true, /*allow_query_parameter*/ true);
ParserKeyword s_from("FROM");
2019-10-23 13:46:38 +00:00
ParserKeyword s_on("ON");
ParserToken s_dot(TokenType::Dot);
2022-06-26 13:28:45 +00:00
ParserToken s_comma(TokenType::Comma);
ParserToken s_lparen(TokenType::OpeningRoundBracket);
ParserToken s_rparen(TokenType::ClosingRoundBracket);
ParserStorage storage_p{ParserStorage::TABLE_ENGINE};
2011-08-18 18:48:00 +00:00
ParserIdentifier name_p;
2019-05-12 11:36:02 +00:00
ParserTablePropertiesDeclarationList table_properties_p;
2018-02-25 00:50:53 +00:00
ParserSelectWithUnionQuery select_p;
2019-07-18 18:29:49 +00:00
ParserFunction table_function_p;
ParserNameList names_p;
2020-04-06 23:22:44 +00:00
ASTPtr table;
2019-01-21 17:17:21 +00:00
ASTPtr columns_list;
2011-08-18 18:48:00 +00:00
ASTPtr storage;
2011-10-31 17:30:44 +00:00
ASTPtr as_database;
ASTPtr as_table;
2019-07-18 18:29:49 +00:00
ASTPtr as_table_function;
2011-11-01 15:16:04 +00:00
ASTPtr select;
ASTPtr from_path;
2017-04-21 12:39:28 +00:00
String cluster_str;
2011-08-18 18:48:00 +00:00
bool attach = false;
2020-12-25 16:28:10 +00:00
bool replace = false;
bool or_replace = false;
2011-08-19 18:31:14 +00:00
bool if_not_exists = false;
bool is_temporary = false;
2022-06-21 13:03:58 +00:00
bool is_create_empty = false;
2020-12-25 16:28:10 +00:00
if (s_create.ignore(pos, expected))
2011-08-18 18:48:00 +00:00
{
2020-12-25 16:28:10 +00:00
if (s_or_replace.ignore(pos, expected))
replace = or_replace = true;
2011-08-18 18:48:00 +00:00
}
2020-12-25 16:28:10 +00:00
else if (s_attach.ignore(pos, expected))
attach = true;
else if (s_replace.ignore(pos, expected))
replace = true;
else
return false;
2020-12-25 16:28:10 +00:00
if (!replace && !or_replace && s_temporary.ignore(pos, expected))
{
is_temporary = true;
}
2019-10-09 20:09:10 +00:00
if (!s_table.ignore(pos, expected))
return false;
2020-12-25 16:28:10 +00:00
if (!replace && !or_replace && s_if_not_exists.ignore(pos, expected))
2019-10-09 20:09:10 +00:00
if_not_exists = true;
2020-04-06 23:22:44 +00:00
if (!table_name_p.parse(pos, table, expected))
2019-10-09 20:09:10 +00:00
return false;
if (attach && s_from.ignore(pos, expected))
{
2021-10-05 23:34:48 +00:00
ParserStringLiteral from_path_p;
if (!from_path_p.parse(pos, from_path, expected))
return false;
}
2019-10-23 13:46:38 +00:00
if (s_on.ignore(pos, expected))
2019-10-09 20:09:10 +00:00
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
2021-10-21 12:50:31 +00:00
auto * table_id = table->as<ASTTableIdentifier>();
2020-04-06 23:22:44 +00:00
2019-10-09 20:09:10 +00:00
// Shortcut for ATTACH a previously detached table
bool short_attach = attach && !from_path;
if (short_attach && (!pos.isValid() || pos.get().type == TokenType::Semicolon))
2019-10-09 20:09:10 +00:00
{
auto query = std::make_shared<ASTCreateQuery>();
node = query;
2019-10-09 20:09:10 +00:00
query->attach = attach;
query->if_not_exists = if_not_exists;
query->cluster = cluster_str;
2021-10-12 18:07:09 +00:00
query->database = table_id->getDatabase();
query->table = table_id->getTable();
query->uuid = table_id->uuid;
if (query->database)
query->children.push_back(query->database);
if (query->table)
query->children.push_back(query->table);
2019-10-09 20:09:10 +00:00
return true;
}
2022-06-21 13:03:58 +00:00
auto need_parse_as_select = [&is_create_empty, &pos, &expected]()
{
if (ParserKeyword{"EMPTY AS"}.ignore(pos, expected))
{
is_create_empty = true;
return true;
}
return ParserKeyword{"AS"}.ignore(pos, expected);
};
2019-10-09 20:09:10 +00:00
/// List of columns.
if (s_lparen.ignore(pos, expected))
{
2023-07-05 12:28:27 +00:00
/// Columns and all table properties (indices, constraints, projections, primary_key)
2019-10-09 20:09:10 +00:00
if (!table_properties_p.parse(pos, columns_list, expected))
return false;
2022-06-29 06:46:45 +00:00
/// We allow a trailing comma in the columns list for user convenience.
/// Although it diverges from the SQL standard slightly.
2022-06-27 05:17:22 +00:00
s_comma.ignore(pos, expected);
2022-06-26 13:28:45 +00:00
2019-10-09 20:09:10 +00:00
if (!s_rparen.ignore(pos, expected))
return false;
auto storage_parse_result = storage_p.parse(pos, storage, expected);
if ((storage_parse_result || is_temporary) && need_parse_as_select())
{
if (!select_p.parse(pos, select, expected))
return false;
}
if (!storage_parse_result && !is_temporary)
{
2022-06-21 13:03:58 +00:00
if (need_parse_as_select() && !table_function_p.parse(pos, as_table_function, expected))
return false;
}
2022-02-10 20:35:03 +00:00
/// Will set default table engine if Storage clause was not parsed
2019-10-09 20:09:10 +00:00
}
/** Create queries without list of columns:
* - CREATE|ATTACH TABLE ... AS ...
* - CREATE|ATTACH TABLE ... ENGINE = engine
*/
2019-10-09 20:09:10 +00:00
else
{
storage_p.parse(pos, storage, expected);
/// CREATE|ATTACH TABLE ... AS ...
2022-06-21 13:03:58 +00:00
if (need_parse_as_select())
2019-10-09 20:09:10 +00:00
{
if (!select_p.parse(pos, select, expected)) /// AS SELECT ...
2011-11-01 15:16:04 +00:00
{
/// ENGINE can not be specified for table functions.
if (storage || !table_function_p.parse(pos, as_table_function, expected))
2011-11-05 23:31:19 +00:00
{
/// AS [db.]table
if (!name_p.parse(pos, as_table, expected))
2011-11-05 23:31:19 +00:00
return false;
2019-10-09 20:09:10 +00:00
if (s_dot.ignore(pos, expected))
{
as_database = as_table;
if (!name_p.parse(pos, as_table, expected))
return false;
}
/// Optional - ENGINE can be specified.
if (!storage)
storage_p.parse(pos, storage, expected);
}
2011-11-01 15:16:04 +00:00
}
2011-10-31 17:30:44 +00:00
}
2014-04-24 18:49:07 +00:00
}
auto comment = parseComment(pos, expected);
2019-10-09 20:09:10 +00:00
auto query = std::make_shared<ASTCreateQuery>();
node = query;
query->attach = attach;
2020-12-25 16:28:10 +00:00
query->replace_table = replace;
query->create_or_replace = or_replace;
2019-10-09 20:09:10 +00:00
query->if_not_exists = if_not_exists;
query->temporary = is_temporary;
2021-10-12 18:07:09 +00:00
query->database = table_id->getDatabase();
query->table = table_id->getTable();
query->uuid = table_id->uuid;
2019-10-09 20:09:10 +00:00
query->cluster = cluster_str;
2021-10-12 18:07:09 +00:00
if (query->database)
query->children.push_back(query->database);
if (query->table)
query->children.push_back(query->table);
2019-10-09 20:09:10 +00:00
query->set(query->columns_list, columns_list);
query->set(query->storage, storage);
query->set(query->as_table_function, as_table_function);
2019-10-09 20:09:10 +00:00
if (comment)
query->set(query->comment, comment);
2022-02-10 20:35:03 +00:00
if (query->columns_list && query->columns_list->primary_key)
{
2022-02-10 20:35:03 +00:00
/// If engine is not set will use default one
if (!query->storage)
query->set(query->storage, std::make_shared<ASTStorage>());
else if (query->storage->primary_key)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Multiple primary keys are not allowed.");
2022-02-10 20:35:03 +00:00
query->storage->primary_key = query->columns_list->primary_key;
2023-07-06 09:50:44 +00:00
}
2023-07-05 12:28:27 +00:00
if (query->columns_list && (query->columns_list->primary_key_from_columns))
{
/// If engine is not set will use default one
if (!query->storage)
query->set(query->storage, std::make_shared<ASTStorage>());
else if (query->storage->primary_key)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Multiple primary keys are not allowed.");
query->storage->primary_key = query->columns_list->primary_key_from_columns;
}
2019-10-09 20:09:10 +00:00
tryGetIdentifierNameInto(as_database, query->as_database);
tryGetIdentifierNameInto(as_table, query->as_table);
query->set(query->select, select);
2022-06-21 13:03:58 +00:00
query->is_create_empty = is_create_empty;
2019-10-09 20:09:10 +00:00
if (from_path)
query->attach_from_path = from_path->as<ASTLiteral &>().value.get<String>();
2019-10-09 20:09:10 +00:00
return true;
}
bool ParserCreateLiveViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_create("CREATE");
ParserKeyword s_attach("ATTACH");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
2023-08-15 18:37:39 +00:00
ParserCompoundIdentifier table_name_p(/*table_name_with_optional_uuid*/ true, /*allow_query_parameter*/ true);
2019-10-09 20:09:10 +00:00
ParserKeyword s_as("AS");
ParserKeyword s_view("VIEW");
ParserKeyword s_live("LIVE");
ParserToken s_dot(TokenType::Dot);
ParserToken s_lparen(TokenType::OpeningRoundBracket);
ParserToken s_rparen(TokenType::ClosingRoundBracket);
ParserStorage storage_p{ParserStorage::TABLE_ENGINE};
ParserStorage storage_inner{ParserStorage::TABLE_ENGINE};
2019-10-09 20:09:10 +00:00
ParserTablePropertiesDeclarationList table_properties_p;
ParserSelectWithUnionQuery select_p;
2020-04-06 23:22:44 +00:00
ASTPtr table;
ASTPtr to_table;
2019-10-09 20:09:10 +00:00
ASTPtr columns_list;
ASTPtr as_database;
ASTPtr as_table;
ASTPtr select;
ASTPtr live_view_periodic_refresh;
2019-10-09 20:09:10 +00:00
String cluster_str;
bool attach = false;
bool if_not_exists = false;
bool with_and = false;
bool with_timeout = false;
bool with_periodic_refresh = false;
2019-10-09 20:09:10 +00:00
if (!s_create.ignore(pos, expected))
{
2019-10-09 20:09:10 +00:00
if (s_attach.ignore(pos, expected))
attach = true;
else
2019-10-09 20:09:10 +00:00
return false;
}
if (!s_live.ignore(pos, expected))
return false;
if (!s_view.ignore(pos, expected))
return false;
if (s_if_not_exists.ignore(pos, expected))
if_not_exists = true;
2020-04-06 23:22:44 +00:00
if (!table_name_p.parse(pos, table, expected))
2019-10-09 20:09:10 +00:00
return false;
if (ParserKeyword{"WITH"}.ignore(pos, expected))
2020-02-14 18:20:20 +00:00
{
if (ParserKeyword{"REFRESH"}.ignore(pos, expected) || ParserKeyword{"PERIODIC REFRESH"}.ignore(pos, expected))
{
if (!ParserNumber{}.parse(pos, live_view_periodic_refresh, expected))
live_view_periodic_refresh = std::make_shared<ASTLiteral>(static_cast<UInt64>(DEFAULT_PERIODIC_LIVE_VIEW_REFRESH_SEC));
with_periodic_refresh = true;
}
else if (with_and)
return false;
if (!with_timeout && !with_periodic_refresh)
return false;
2020-02-14 18:20:20 +00:00
}
2019-10-09 20:09:10 +00:00
if (ParserKeyword{"ON"}.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
// TO [db.]table
if (ParserKeyword{"TO"}.ignore(pos, expected))
{
2020-04-06 23:22:44 +00:00
if (!table_name_p.parse(pos, to_table, expected))
2019-10-09 20:09:10 +00:00
return false;
}
2019-10-09 20:09:10 +00:00
/// Optional - a list of columns can be specified. It must fully comply with SELECT.
if (s_lparen.ignore(pos, expected))
{
if (!table_properties_p.parse(pos, columns_list, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!s_rparen.ignore(pos, expected))
return false;
}
2019-08-23 05:08:06 +00:00
2019-10-09 20:09:10 +00:00
/// AS SELECT ...
if (!s_as.ignore(pos, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!select_p.parse(pos, select, expected))
return false;
auto comment = parseComment(pos, expected);
2019-10-09 20:09:10 +00:00
auto query = std::make_shared<ASTCreateQuery>();
node = query;
query->attach = attach;
query->if_not_exists = if_not_exists;
query->is_live_view = true;
2021-10-21 12:50:31 +00:00
auto * table_id = table->as<ASTTableIdentifier>();
2021-10-12 18:07:09 +00:00
query->database = table_id->getDatabase();
query->table = table_id->getTable();
query->uuid = table_id->uuid;
2019-10-09 20:09:10 +00:00
query->cluster = cluster_str;
2021-10-12 18:07:09 +00:00
if (query->database)
query->children.push_back(query->database);
if (query->table)
query->children.push_back(query->table);
2020-04-07 14:05:51 +00:00
if (to_table)
2020-10-26 15:49:00 +00:00
query->to_table_id = to_table->as<ASTTableIdentifier>()->getTableId();
2019-10-09 20:09:10 +00:00
query->set(query->columns_list, columns_list);
tryGetIdentifierNameInto(as_database, query->as_database);
tryGetIdentifierNameInto(as_table, query->as_table);
query->set(query->select, select);
if (live_view_periodic_refresh)
query->live_view_periodic_refresh.emplace(live_view_periodic_refresh->as<ASTLiteral &>().value.safeGet<UInt64>());
2020-02-14 18:20:20 +00:00
if (comment)
query->set(query->comment, comment);
2019-10-09 20:09:10 +00:00
return true;
}
2020-01-14 03:07:31 +00:00
bool ParserCreateWindowViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_create("CREATE");
ParserKeyword s_temporary("TEMPORARY");
ParserKeyword s_attach("ATTACH");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
2023-08-15 18:37:39 +00:00
ParserCompoundIdentifier table_name_p(/*table_name_with_optional_uuid*/ true, /*allow_query_parameter*/ true);
2020-01-14 03:07:31 +00:00
ParserKeyword s_as("AS");
ParserKeyword s_view("VIEW");
ParserKeyword s_window("WINDOW");
2022-05-05 15:31:15 +00:00
ParserKeyword s_populate("POPULATE");
2020-01-14 03:07:31 +00:00
ParserToken s_dot(TokenType::Dot);
2020-02-14 08:07:03 +00:00
ParserToken s_eq(TokenType::Equals);
2020-01-14 03:07:31 +00:00
ParserToken s_lparen(TokenType::OpeningRoundBracket);
ParserToken s_rparen(TokenType::ClosingRoundBracket);
ParserStorage storage_p{ParserStorage::TABLE_ENGINE};
ParserStorage storage_inner{ParserStorage::TABLE_ENGINE};
2020-01-14 03:07:31 +00:00
ParserTablePropertiesDeclarationList table_properties_p;
2022-09-15 21:54:37 +00:00
ParserExpression watermark_p;
ParserExpression lateness_p;
2020-01-14 03:07:31 +00:00
ParserSelectWithUnionQuery select_p;
ASTPtr table;
ASTPtr to_table;
2020-06-09 08:48:04 +00:00
ASTPtr columns_list;
2020-01-14 03:07:31 +00:00
ASTPtr storage;
2022-05-14 16:46:49 +00:00
ASTPtr inner_storage;
2020-02-14 08:07:03 +00:00
ASTPtr watermark;
2020-03-03 04:42:12 +00:00
ASTPtr lateness;
2020-01-14 03:07:31 +00:00
ASTPtr as_database;
ASTPtr as_table;
ASTPtr select;
String cluster_str;
bool attach = false;
2020-03-01 18:08:52 +00:00
bool is_watermark_strictly_ascending = false;
bool is_watermark_ascending = false;
bool is_watermark_bounded = false;
2020-03-03 04:42:12 +00:00
bool allowed_lateness = false;
2020-01-14 03:07:31 +00:00
bool if_not_exists = false;
2022-05-05 15:31:15 +00:00
bool is_populate = false;
2022-06-21 13:03:58 +00:00
bool is_create_empty = false;
2020-01-14 03:07:31 +00:00
if (!s_create.ignore(pos, expected))
{
if (s_attach.ignore(pos, expected))
attach = true;
else
return false;
}
if (!s_window.ignore(pos, expected))
return false;
if (!s_view.ignore(pos, expected))
return false;
if (s_if_not_exists.ignore(pos, expected))
if_not_exists = true;
2020-06-09 08:48:04 +00:00
if (!table_name_p.parse(pos, table, expected))
2020-01-14 03:07:31 +00:00
return false;
if (ParserKeyword{"ON"}.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
// TO [db.]table
if (ParserKeyword{"TO"}.ignore(pos, expected))
{
2020-06-09 08:48:04 +00:00
if (!table_name_p.parse(pos, to_table, expected))
2020-01-14 03:07:31 +00:00
return false;
}
/// Optional - a list of columns can be specified. It must fully comply with SELECT.
if (s_lparen.ignore(pos, expected))
{
if (!table_properties_p.parse(pos, columns_list, expected))
return false;
if (!s_rparen.ignore(pos, expected))
return false;
}
2022-05-14 16:46:49 +00:00
if (ParserKeyword{"INNER"}.ignore(pos, expected))
{
/// Inner table ENGINE for WINDOW VIEW
storage_inner.parse(pos, inner_storage, expected);
}
if (!to_table)
{
/// Target table ENGINE for WINDOW VIEW
storage_p.parse(pos, storage, expected);
}
2020-02-12 17:39:57 +00:00
2020-02-14 08:07:03 +00:00
// WATERMARK
if (ParserKeyword{"WATERMARK"}.ignore(pos, expected))
{
s_eq.ignore(pos, expected);
2020-03-01 18:08:52 +00:00
if (ParserKeyword("STRICTLY_ASCENDING").ignore(pos,expected))
is_watermark_strictly_ascending = true;
else if (ParserKeyword("ASCENDING").ignore(pos,expected))
is_watermark_ascending = true;
else if (watermark_p.parse(pos, watermark, expected))
is_watermark_bounded = true;
else
2020-02-14 08:07:03 +00:00
return false;
}
2020-03-03 04:42:12 +00:00
// ALLOWED LATENESS
if (ParserKeyword{"ALLOWED_LATENESS"}.ignore(pos, expected))
{
s_eq.ignore(pos, expected);
allowed_lateness = true;
if (!lateness_p.parse(pos, lateness, expected))
return false;
}
2022-05-05 15:31:15 +00:00
if (s_populate.ignore(pos, expected))
is_populate = true;
2022-06-21 13:03:58 +00:00
else if (ParserKeyword{"EMPTY"}.ignore(pos, expected))
is_create_empty = true;
2022-05-05 15:31:15 +00:00
2020-01-14 03:07:31 +00:00
/// AS SELECT ...
if (!s_as.ignore(pos, expected))
return false;
if (!select_p.parse(pos, select, expected))
return false;
auto query = std::make_shared<ASTCreateQuery>();
node = query;
query->attach = attach;
query->if_not_exists = if_not_exists;
query->is_window_view = true;
2023-08-15 18:37:39 +00:00
auto * table_id = table->as<ASTTableIdentifier>();
query->database = table_id->getDatabase();
query->table = table_id->getTable();
query->uuid = table_id->uuid;
2020-01-14 03:07:31 +00:00
query->cluster = cluster_str;
2023-08-15 18:37:39 +00:00
if (query->database)
query->children.push_back(query->database);
if (query->table)
query->children.push_back(query->table);
2020-06-09 08:48:04 +00:00
if (to_table)
2021-11-19 13:09:12 +00:00
query->to_table_id = to_table->as<ASTTableIdentifier>()->getTableId();
2020-01-14 03:07:31 +00:00
query->set(query->columns_list, columns_list);
2020-02-12 17:39:57 +00:00
query->set(query->storage, storage);
2022-05-14 16:46:49 +00:00
query->set(query->inner_storage, inner_storage);
2020-03-01 18:08:52 +00:00
query->is_watermark_strictly_ascending = is_watermark_strictly_ascending;
query->is_watermark_ascending = is_watermark_ascending;
query->is_watermark_bounded = is_watermark_bounded;
2020-02-14 08:07:03 +00:00
query->watermark_function = watermark;
2020-03-03 04:42:12 +00:00
query->allowed_lateness = allowed_lateness;
query->lateness_function = lateness;
2022-05-05 15:31:15 +00:00
query->is_populate = is_populate;
2022-06-21 13:03:58 +00:00
query->is_create_empty = is_create_empty;
2020-01-14 03:07:31 +00:00
tryGetIdentifierNameInto(as_database, query->as_database);
tryGetIdentifierNameInto(as_table, query->as_table);
query->set(query->select, select);
return true;
}
bool ParserTableOverrideDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_table_override("TABLE OVERRIDE");
ParserIdentifier table_name_p;
ParserToken lparen_p(TokenType::OpeningRoundBracket);
ParserToken rparen_p(TokenType::ClosingRoundBracket);
ParserTablePropertiesDeclarationList table_properties_p;
ParserExpression expression_p;
ParserTTLExpressionList parser_ttl_list;
ParserKeyword s_columns("COLUMNS");
ParserKeyword s_partition_by("PARTITION BY");
ParserKeyword s_primary_key("PRIMARY KEY");
ParserKeyword s_order_by("ORDER BY");
ParserKeyword s_sample_by("SAMPLE BY");
ParserKeyword s_ttl("TTL");
ASTPtr table_name;
ASTPtr columns;
ASTPtr partition_by;
ASTPtr primary_key;
ASTPtr order_by;
ASTPtr sample_by;
ASTPtr ttl_table;
if (is_standalone)
{
if (!s_table_override.ignore(pos, expected))
return false;
if (!table_name_p.parse(pos, table_name, expected))
return false;
if (!lparen_p.ignore(pos, expected))
return false;
}
while (true)
{
if (!columns && s_columns.ignore(pos, expected))
{
if (!lparen_p.ignore(pos, expected))
return false;
if (!table_properties_p.parse(pos, columns, expected))
return false;
if (!rparen_p.ignore(pos, expected))
return false;
}
if (!partition_by && s_partition_by.ignore(pos, expected))
{
if (expression_p.parse(pos, partition_by, expected))
continue;
else
return false;
}
if (!primary_key && s_primary_key.ignore(pos, expected))
{
if (expression_p.parse(pos, primary_key, expected))
continue;
else
return false;
}
if (!order_by && s_order_by.ignore(pos, expected))
{
if (expression_p.parse(pos, order_by, expected))
continue;
else
return false;
}
if (!sample_by && s_sample_by.ignore(pos, expected))
{
if (expression_p.parse(pos, sample_by, expected))
continue;
else
return false;
}
if (!ttl_table && s_ttl.ignore(pos, expected))
{
if (parser_ttl_list.parse(pos, ttl_table, expected))
continue;
else
return false;
}
break;
}
if (is_standalone && !rparen_p.ignore(pos, expected))
return false;
auto storage = std::make_shared<ASTStorage>();
storage->set(storage->partition_by, partition_by);
storage->set(storage->primary_key, primary_key);
storage->set(storage->order_by, order_by);
storage->set(storage->sample_by, sample_by);
storage->set(storage->ttl_table, ttl_table);
auto res = std::make_shared<ASTTableOverride>();
if (table_name)
res->table_name = table_name->as<ASTIdentifier>()->name();
res->is_standalone = is_standalone;
res->set(res->storage, storage);
if (columns)
res->set(res->columns, columns);
node = res;
return true;
}
bool ParserTableOverridesDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserTableOverrideDeclaration table_override_p;
ParserToken s_comma(TokenType::Comma);
auto res = std::make_shared<ASTTableOverrideList>();
auto parse_element = [&]
{
ASTPtr element;
if (!table_override_p.parse(pos, element, expected))
return false;
auto * table_override = element->as<ASTTableOverride>();
if (!table_override)
return false;
res->setTableOverride(table_override->table_name, element);
return true;
};
if (!ParserList::parseUtil(pos, expected, parse_element, s_comma, true))
return false;
if (!res->children.empty())
node = res;
return true;
}
2019-10-09 20:09:10 +00:00
bool ParserCreateDatabaseQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_create("CREATE");
ParserKeyword s_attach("ATTACH");
ParserKeyword s_database("DATABASE");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
ParserStorage storage_p{ParserStorage::DATABASE_ENGINE};
ParserIdentifier name_p(true);
ParserTableOverridesDeclarationList table_overrides_p;
2019-10-09 20:09:10 +00:00
ASTPtr database;
ASTPtr storage;
ASTPtr table_overrides;
2020-07-02 20:39:31 +00:00
UUID uuid = UUIDHelpers::Nil;
2019-10-09 20:09:10 +00:00
String cluster_str;
bool attach = false;
bool if_not_exists = false;
2019-10-09 20:09:10 +00:00
if (!s_create.ignore(pos, expected))
{
if (s_attach.ignore(pos, expected))
attach = true;
else
2019-08-23 05:08:06 +00:00
return false;
}
2019-10-09 20:09:10 +00:00
if (!s_database.ignore(pos, expected))
return false;
2019-10-09 20:09:10 +00:00
if (s_if_not_exists.ignore(pos, expected))
if_not_exists = true;
if (!name_p.parse(pos, database, expected))
return false;
2020-07-02 20:39:31 +00:00
if (ParserKeyword("UUID").ignore(pos, expected))
{
ParserStringLiteral uuid_p;
ASTPtr ast_uuid;
if (!uuid_p.parse(pos, ast_uuid, expected))
return false;
uuid = parseFromString<UUID>(ast_uuid->as<ASTLiteral>()->value.get<String>());
}
2019-10-09 20:09:10 +00:00
if (ParserKeyword{"ON"}.ignore(pos, expected))
{
2019-10-09 20:09:10 +00:00
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
storage_p.parse(pos, storage, expected);
auto comment = parseComment(pos, expected);
2019-10-09 20:09:10 +00:00
if (!table_overrides_p.parse(pos, table_overrides, expected))
return false;
2019-10-09 20:09:10 +00:00
auto query = std::make_shared<ASTCreateQuery>();
node = query;
query->attach = attach;
query->if_not_exists = if_not_exists;
2020-07-02 20:39:31 +00:00
query->uuid = uuid;
2019-10-09 20:09:10 +00:00
query->cluster = cluster_str;
query->database = database;
if (database)
query->children.push_back(database);
2019-10-09 20:09:10 +00:00
query->set(query->storage, storage);
if (comment)
query->set(query->comment, comment);
if (table_overrides && !table_overrides->children.empty())
query->set(query->table_overrides, table_overrides);
2019-10-09 20:09:10 +00:00
return true;
}
bool ParserCreateViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_create("CREATE");
ParserKeyword s_attach("ATTACH");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
2023-08-15 18:37:39 +00:00
ParserCompoundIdentifier table_name_p(/*table_name_with_optional_uuid*/ true, /*allow_query_parameter*/ true);
ParserCompoundIdentifier to_table_name_p(/*table_name_with_optional_uuid*/ true, /*allow_query_parameter*/ false);
2019-10-09 20:09:10 +00:00
ParserKeyword s_as("AS");
ParserKeyword s_view("VIEW");
ParserKeyword s_materialized("MATERIALIZED");
ParserKeyword s_populate("POPULATE");
ParserKeyword s_or_replace("OR REPLACE");
ParserToken s_dot(TokenType::Dot);
ParserToken s_lparen(TokenType::OpeningRoundBracket);
ParserToken s_rparen(TokenType::ClosingRoundBracket);
ParserStorage storage_p{ParserStorage::TABLE_ENGINE};
2019-10-09 20:09:10 +00:00
ParserIdentifier name_p;
ParserTablePropertiesDeclarationList table_properties_p;
ParserSelectWithUnionQuery select_p;
ParserNameList names_p;
2020-04-06 23:22:44 +00:00
ASTPtr table;
ASTPtr to_table;
2021-03-08 17:26:38 +00:00
ASTPtr to_inner_uuid;
2019-10-09 20:09:10 +00:00
ASTPtr columns_list;
ASTPtr storage;
ASTPtr as_database;
ASTPtr as_table;
ASTPtr select;
2019-10-09 20:09:10 +00:00
String cluster_str;
bool attach = false;
bool if_not_exists = false;
bool is_ordinary_view = false;
2019-10-09 20:09:10 +00:00
bool is_materialized_view = false;
bool is_populate = false;
2022-06-21 13:03:58 +00:00
bool is_create_empty = false;
2019-10-09 20:09:10 +00:00
bool replace_view = false;
if (!s_create.ignore(pos, expected))
{
if (s_attach.ignore(pos, expected))
attach = true;
else
return false;
2019-10-09 20:09:10 +00:00
}
2019-10-09 20:09:10 +00:00
/// VIEW or MATERIALIZED VIEW
if (s_or_replace.ignore(pos, expected))
{
replace_view = true;
}
2019-10-09 20:09:10 +00:00
if (!replace_view && s_materialized.ignore(pos, expected))
{
is_materialized_view = true;
}
2014-04-24 18:49:07 +00:00
else
is_ordinary_view = true;
2019-03-11 16:50:31 +00:00
2019-10-09 20:09:10 +00:00
if (!s_view.ignore(pos, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!replace_view && s_if_not_exists.ignore(pos, expected))
if_not_exists = true;
2020-04-06 23:22:44 +00:00
if (!table_name_p.parse(pos, table, expected))
2019-10-09 20:09:10 +00:00
return false;
2019-10-09 20:09:10 +00:00
if (ParserKeyword{"ON"}.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
2023-02-10 13:56:47 +00:00
if (is_materialized_view && ParserKeyword{"TO INNER UUID"}.ignore(pos, expected))
2021-03-08 17:26:38 +00:00
{
ParserStringLiteral literal_p;
2021-03-08 17:26:38 +00:00
if (!literal_p.parse(pos, to_inner_uuid, expected))
return false;
}
2023-02-10 13:56:47 +00:00
else if (is_materialized_view && ParserKeyword{"TO"}.ignore(pos, expected))
2019-10-09 20:09:10 +00:00
{
2021-03-08 17:26:38 +00:00
// TO [db.]table
2020-04-06 23:22:44 +00:00
if (!table_name_p.parse(pos, to_table, expected))
2019-10-09 20:09:10 +00:00
return false;
}
2019-10-09 20:09:10 +00:00
/// Optional - a list of columns can be specified. It must fully comply with SELECT.
if (s_lparen.ignore(pos, expected))
{
if (!table_properties_p.parse(pos, columns_list, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!s_rparen.ignore(pos, expected))
return false;
2019-10-09 20:09:10 +00:00
}
2020-04-06 23:22:44 +00:00
if (is_materialized_view && !to_table)
2019-10-09 20:09:10 +00:00
{
/// Internal ENGINE for MATERIALIZED VIEW must be specified.
/// Actually check it in Interpreter as default_table_engine can be set
storage_p.parse(pos, storage, expected);
2019-10-09 20:09:10 +00:00
if (s_populate.ignore(pos, expected))
is_populate = true;
2022-06-21 13:03:58 +00:00
else if (ParserKeyword{"EMPTY"}.ignore(pos, expected))
is_create_empty = true;
2011-10-31 17:30:44 +00:00
}
2019-10-09 20:09:10 +00:00
/// AS SELECT ...
if (!s_as.ignore(pos, expected))
return false;
if (!select_p.parse(pos, select, expected))
return false;
auto comment = parseComment(pos, expected);
2019-10-09 20:09:10 +00:00
2018-02-26 03:37:08 +00:00
auto query = std::make_shared<ASTCreateQuery>();
2011-08-18 18:48:00 +00:00
node = query;
2011-08-18 18:48:00 +00:00
query->attach = attach;
2011-08-19 18:31:14 +00:00
query->if_not_exists = if_not_exists;
query->is_ordinary_view = is_ordinary_view;
query->is_materialized_view = is_materialized_view;
query->is_populate = is_populate;
2022-06-21 13:03:58 +00:00
query->is_create_empty = is_create_empty;
2019-03-11 16:50:31 +00:00
query->replace_view = replace_view;
2021-10-21 12:50:31 +00:00
auto * table_id = table->as<ASTTableIdentifier>();
2021-10-12 18:07:09 +00:00
query->database = table_id->getDatabase();
query->table = table_id->getTable();
query->uuid = table_id->uuid;
2017-04-21 12:39:28 +00:00
query->cluster = cluster_str;
2021-10-12 18:07:09 +00:00
if (query->database)
query->children.push_back(query->database);
if (query->table)
query->children.push_back(query->table);
2020-04-07 14:05:51 +00:00
if (to_table)
2020-10-26 15:49:00 +00:00
query->to_table_id = to_table->as<ASTTableIdentifier>()->getTableId();
2021-03-08 17:26:38 +00:00
if (to_inner_uuid)
query->to_inner_uuid = parseFromString<UUID>(to_inner_uuid->as<ASTLiteral>()->value.get<String>());
2019-01-21 17:17:21 +00:00
query->set(query->columns_list, columns_list);
query->set(query->storage, storage);
if (comment)
query->set(query->comment, comment);
2019-08-08 20:02:30 +00:00
tryGetIdentifierNameInto(as_database, query->as_database);
tryGetIdentifierNameInto(as_table, query->as_table);
query->set(query->select, select);
2019-10-09 20:09:10 +00:00
return true;
}
bool ParserCreateNamedCollectionQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_create("CREATE");
ParserKeyword s_named_collection("NAMED COLLECTION");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
ParserKeyword s_on("ON");
ParserKeyword s_as("AS");
ParserIdentifier name_p;
ParserToken s_comma(TokenType::Comma);
String cluster_str;
bool if_not_exists = false;
ASTPtr collection_name;
if (!s_create.ignore(pos, expected))
return false;
if (!s_named_collection.ignore(pos, expected))
return false;
if (s_if_not_exists.ignore(pos, expected))
if_not_exists = true;
if (!name_p.parse(pos, collection_name, expected))
return false;
if (s_on.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
if (!s_as.ignore(pos, expected))
return false;
2022-11-15 14:49:25 +00:00
SettingsChanges changes;
while (true)
{
if (!changes.empty() && !s_comma.ignore(pos))
break;
changes.push_back(SettingChange{});
if (!ParserSetQuery::parseNameValuePair(changes.back(), pos, expected))
return false;
}
auto query = std::make_shared<ASTCreateNamedCollectionQuery>();
tryGetIdentifierNameInto(collection_name, query->collection_name);
query->if_not_exists = if_not_exists;
2022-11-15 14:49:25 +00:00
query->changes = changes;
query->cluster = std::move(cluster_str);
node = query;
return true;
}
2019-10-09 20:09:10 +00:00
bool ParserCreateDictionaryQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_create("CREATE");
ParserKeyword s_attach("ATTACH");
ParserKeyword s_replace("REPLACE");
ParserKeyword s_or_replace("OR REPLACE");
2019-10-09 20:09:10 +00:00
ParserKeyword s_dictionary("DICTIONARY");
ParserKeyword s_if_not_exists("IF NOT EXISTS");
ParserKeyword s_on("ON");
2023-08-15 18:37:39 +00:00
ParserCompoundIdentifier dict_name_p(/*table_name_with_optional_uuid*/ true, /*allow_query_parameter*/ true);
2019-10-09 20:09:10 +00:00
ParserToken s_left_paren(TokenType::OpeningRoundBracket);
ParserToken s_right_paren(TokenType::ClosingRoundBracket);
ParserToken s_dot(TokenType::Dot);
ParserDictionaryAttributeDeclarationList attributes_p;
ParserDictionary dictionary_p;
bool if_not_exists = false;
bool replace = false;
bool or_replace = false;
2019-10-09 20:09:10 +00:00
ASTPtr name;
ASTPtr attributes;
ASTPtr dictionary;
String cluster_str;
2019-10-09 20:09:10 +00:00
bool attach = false;
if (s_create.ignore(pos, expected))
{
if (s_or_replace.ignore(pos, expected))
{
replace = true;
or_replace = true;
}
}
else if (s_attach.ignore(pos, expected))
attach = true;
else if (s_replace.ignore(pos, expected))
replace = true;
else
return false;
2019-10-09 20:09:10 +00:00
if (!s_dictionary.ignore(pos, expected))
return false;
2019-12-04 15:11:36 +00:00
if (s_if_not_exists.ignore(pos, expected))
if_not_exists = true;
2020-07-13 21:21:01 +00:00
if (!dict_name_p.parse(pos, name, expected))
2019-10-09 20:09:10 +00:00
return false;
if (s_on.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
if (!attach)
{
if (!s_left_paren.ignore(pos, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!attributes_p.parse(pos, attributes, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!s_right_paren.ignore(pos, expected))
return false;
2019-10-09 20:09:10 +00:00
if (!dictionary_p.parse(pos, dictionary, expected))
return false;
}
2019-10-09 20:09:10 +00:00
auto comment = parseComment(pos, expected);
2019-10-09 20:09:10 +00:00
auto query = std::make_shared<ASTCreateQuery>();
node = query;
query->is_dictionary = true;
query->attach = attach;
query->create_or_replace = or_replace;
query->replace_table = replace;
2019-10-09 20:09:10 +00:00
2021-10-21 12:50:31 +00:00
auto * dict_id = name->as<ASTTableIdentifier>();
2021-10-12 18:07:09 +00:00
query->database = dict_id->getDatabase();
query->table = dict_id->getTable();
query->uuid = dict_id->uuid;
if (query->database)
query->children.push_back(query->database);
if (query->table)
query->children.push_back(query->table);
2019-10-09 20:09:10 +00:00
query->if_not_exists = if_not_exists;
query->set(query->dictionary_attributes_list, attributes);
query->set(query->dictionary, dictionary);
query->cluster = cluster_str;
2019-10-09 20:09:10 +00:00
if (comment)
query->set(query->comment, comment);
2011-08-18 18:48:00 +00:00
return true;
}
2019-10-09 20:09:10 +00:00
bool ParserCreateQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserCreateTableQuery table_p;
ParserCreateDatabaseQuery database_p;
ParserCreateViewQuery view_p;
ParserCreateDictionaryQuery dictionary_p;
ParserCreateLiveViewQuery live_view_p;
2020-01-14 03:07:31 +00:00
ParserCreateWindowViewQuery window_view_p;
2019-10-09 20:09:10 +00:00
return table_p.parse(pos, node, expected)
|| database_p.parse(pos, node, expected)
|| view_p.parse(pos, node, expected)
|| dictionary_p.parse(pos, node, expected)
2020-01-14 03:07:31 +00:00
|| live_view_p.parse(pos, node, expected)
2022-11-15 14:49:25 +00:00
|| window_view_p.parse(pos, node, expected);
2019-10-09 20:09:10 +00:00
}
2011-08-18 18:48:00 +00:00
}