2021-12-03 09:35:36 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <Parsers/ASTConstraintDeclaration.h>
|
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2018-12-25 18:45:08 +00:00
|
|
|
#include <Parsers/ASTIndexDeclaration.h>
|
2021-11-26 16:54:57 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2021-12-03 09:35:36 +00:00
|
|
|
#include <Parsers/ASTProjectionDeclaration.h>
|
2019-03-23 22:45:28 +00:00
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2021-12-03 09:35:36 +00:00
|
|
|
#include <Parsers/ASTSetQuery.h>
|
|
|
|
#include <Parsers/ASTTableOverrides.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2019-10-09 20:09:10 +00:00
|
|
|
#include <Parsers/ParserDictionary.h>
|
|
|
|
#include <Parsers/ParserDictionaryAttributeDeclaration.h>
|
2021-02-10 14:12:49 +00:00
|
|
|
#include <Parsers/ParserProjectionSelectQuery.h>
|
2021-12-03 09:35:36 +00:00
|
|
|
#include <Parsers/ParserSelectWithUnionQuery.h>
|
|
|
|
#include <Parsers/ParserSetQuery.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2022-02-01 07:03:50 +00:00
|
|
|
#include <Parsers/ASTColumnDeclaration.h>
|
2011-08-18 18:48:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2020-10-12 13:55:41 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:07:11 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserNestedTable::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2013-07-11 17:35:56 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken open(TokenType::OpeningRoundBracket);
|
|
|
|
ParserToken close(TokenType::ClosingRoundBracket);
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifier name_p;
|
|
|
|
ParserNameTypePairList columns_p;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr name;
|
|
|
|
ASTPtr columns;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// For now `name == 'Nested'`, probably alternative nested data structures will appear
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, name, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!open.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!columns_p.parse(pos, columns, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!close.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
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);
|
2020-12-04 02:15:44 +00:00
|
|
|
// FIXME(ilezhankin): func->no_empty_args = true; ?
|
2017-04-01 07:20:54 +00:00
|
|
|
func->arguments = columns;
|
|
|
|
func->children.push_back(columns);
|
|
|
|
node = func;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2013-07-11 17:35:56 +00:00
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserIdentifierWithParameters::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2013-07-11 17:35:56 +00:00
|
|
|
{
|
2020-06-18 12:52:05 +00:00
|
|
|
return ParserFunction().parse(pos, node, expected);
|
2013-07-11 17:35:56 +00:00
|
|
|
}
|
2011-08-18 18:48:00 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserNameTypePairList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2013-07-11 17:35:56 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
return ParserList(std::make_unique<ParserNameTypePair>(), std::make_unique<ParserToken>(TokenType::Comma), false)
|
|
|
|
.parse(pos, node, expected);
|
2013-07-11 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserColumnDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2014-09-24 14:44:57 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
return ParserList(std::make_unique<ParserColumnDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false)
|
|
|
|
.parse(pos, node, expected);
|
2014-09-24 14:44:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 21:17:48 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-02 16:04:44 +00:00
|
|
|
if (!s_granularity.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!granularity_p.parse(pos, granularity, expected))
|
|
|
|
return false;
|
|
|
|
|
2018-12-25 18:45:08 +00:00
|
|
|
auto index = std::make_shared<ASTIndexDeclaration>();
|
2020-10-24 18:46:10 +00:00
|
|
|
index->name = name->as<ASTIdentifier &>().name();
|
2020-02-14 18:20:20 +00:00
|
|
|
index->granularity = granularity->as<ASTLiteral &>().value.safeGet<UInt64>();
|
2018-12-25 18:45:08 +00:00
|
|
|
index->set(index->expr, expr);
|
|
|
|
index->set(index->type, type);
|
|
|
|
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;
|
2019-05-18 05:16:33 +00:00
|
|
|
ParserLogicalOrExpression 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>();
|
2020-10-24 18:46:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
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();
|
2021-06-02 15:09:55 +00:00
|
|
|
projection->set(projection->query, query);
|
2021-02-10 14:12:49 +00:00
|
|
|
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");
|
2021-02-10 14:12:49 +00:00
|
|
|
ParserKeyword s_projection("PROJECTION");
|
2020-10-12 13:55:41 +00:00
|
|
|
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;
|
2021-02-10 14:12:49 +00:00
|
|
|
ParserProjectionDeclaration projection_p;
|
2020-06-14 16:17:22 +00:00
|
|
|
ParserColumnDeclaration column_p{true, true};
|
2020-10-12 13:55:41 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-02-10 14:12:49 +00:00
|
|
|
else if (s_projection.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!projection_p.parse(pos, new_node, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-12 13:55:41 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
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>();
|
2021-02-10 14:12:49 +00:00
|
|
|
ASTPtr projections = std::make_shared<ASTExpressionList>();
|
2020-10-12 13:55:41 +00:00
|
|
|
ASTPtr primary_key;
|
2019-01-20 09:19:34 +00:00
|
|
|
|
|
|
|
for (const auto & elem : list->children)
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (elem->as<ASTColumnDeclaration>())
|
2019-01-20 09:19:34 +00:00
|
|
|
columns->children.push_back(elem);
|
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);
|
2021-02-10 14:12:49 +00:00
|
|
|
else if (elem->as<ASTProjectionDeclaration>())
|
|
|
|
projections->children.push_back(elem);
|
2020-10-12 13:55:41 +00:00
|
|
|
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;
|
2020-10-12 13:55:41 +00:00
|
|
|
}
|
|
|
|
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);
|
2021-02-10 14:12:49 +00:00
|
|
|
if (!projections->children.empty())
|
|
|
|
res->set(res->projections, projections);
|
2020-10-12 13:55:41 +00:00
|
|
|
if (primary_key)
|
|
|
|
res->set(res->primary_key, primary_key);
|
2019-01-20 09:19:34 +00:00
|
|
|
|
|
|
|
node = res;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-11-01 15:16:04 +00:00
|
|
|
{
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_engine("ENGINE");
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_eq(TokenType::Equals);
|
2017-09-18 14:18:29 +00:00
|
|
|
ParserKeyword s_partition_by("PARTITION BY");
|
2018-10-10 18:24:11 +00:00
|
|
|
ParserKeyword s_primary_key("PRIMARY KEY");
|
2017-09-18 14:18:29 +00:00
|
|
|
ParserKeyword s_order_by("ORDER BY");
|
|
|
|
ParserKeyword s_sample_by("SAMPLE BY");
|
2019-04-15 09:30:45 +00:00
|
|
|
ParserKeyword s_ttl("TTL");
|
2017-09-18 14:18:29 +00:00
|
|
|
ParserKeyword s_settings("SETTINGS");
|
2011-11-01 15:16:04 +00:00
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
ParserIdentifierWithOptionalParameters ident_with_optional_params_p;
|
2017-09-18 14:18:29 +00:00
|
|
|
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
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
ASTPtr engine;
|
2017-09-18 14:18:29 +00:00
|
|
|
ASTPtr partition_by;
|
2018-10-10 18:24:11 +00:00
|
|
|
ASTPtr primary_key;
|
2017-09-18 14:18:29 +00:00
|
|
|
ASTPtr order_by;
|
|
|
|
ASTPtr sample_by;
|
2019-04-15 09:30:45 +00:00
|
|
|
ASTPtr ttl_table;
|
2017-09-18 14:18:29 +00:00
|
|
|
ASTPtr settings;
|
2017-09-17 18:49:43 +00:00
|
|
|
|
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);
|
2017-09-17 18:49:43 +00:00
|
|
|
|
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);
|
2017-09-17 18:49:43 +00:00
|
|
|
|
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
|
|
|
|
2017-09-18 14:18:29 +00:00
|
|
|
while (true)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-09-18 14:18:29 +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;
|
2017-09-18 14:18:29 +00:00
|
|
|
continue;
|
2022-01-31 17:10:59 +00:00
|
|
|
}
|
2017-09-18 14:18:29 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-01 15:16:04 +00:00
|
|
|
|
2018-10-10 18:24:11 +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;
|
2018-10-10 18:24:11 +00:00
|
|
|
continue;
|
2022-01-31 17:10:59 +00:00
|
|
|
}
|
2018-10-10 18:24:11 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-18 14:18:29 +00:00
|
|
|
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;
|
2017-09-18 14:18:29 +00:00
|
|
|
continue;
|
2022-01-31 17:10:59 +00:00
|
|
|
}
|
2017-09-18 14:18:29 +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;
|
2017-09-18 14:18:29 +00:00
|
|
|
continue;
|
2022-01-31 17:10:59 +00:00
|
|
|
}
|
2017-09-18 14:18:29 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
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;
|
2019-04-15 09:30:45 +00:00
|
|
|
continue;
|
2022-01-31 17:10:59 +00:00
|
|
|
}
|
2019-04-15 09:30:45 +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))
|
2017-09-18 14:18:29 +00:00
|
|
|
{
|
|
|
|
if (!settings_p.parse(pos, settings, expected))
|
|
|
|
return false;
|
2022-01-31 17:10:59 +00:00
|
|
|
storage_like = true;
|
2017-09-18 14:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2017-04-01 07:20:54 +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
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto storage = std::make_shared<ASTStorage>();
|
2017-09-17 18:49:43 +00:00
|
|
|
storage->set(storage->engine, engine);
|
2017-09-18 14:18:29 +00:00
|
|
|
storage->set(storage->partition_by, partition_by);
|
2018-10-10 18:24:11 +00:00
|
|
|
storage->set(storage->primary_key, primary_key);
|
2017-09-18 14:18:29 +00:00
|
|
|
storage->set(storage->order_by, order_by);
|
|
|
|
storage->set(storage->sample_by, sample_by);
|
2019-04-15 09:30:45 +00:00
|
|
|
storage->set(storage->ttl_table, ttl_table);
|
2018-12-25 18:45:08 +00:00
|
|
|
|
2017-09-18 14:18:29 +00:00
|
|
|
storage->set(storage->settings, settings);
|
2017-09-17 18:49:43 +00:00
|
|
|
|
|
|
|
node = storage;
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2011-11-01 15:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-06-18 03:07:03 +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");
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_table("TABLE");
|
|
|
|
ParserKeyword s_if_not_exists("IF NOT EXISTS");
|
2021-10-12 18:07:09 +00:00
|
|
|
ParserCompoundIdentifier table_name_p(true, true);
|
2020-12-08 14:13:35 +00:00
|
|
|
ParserKeyword s_from("FROM");
|
2019-10-23 13:46:38 +00:00
|
|
|
ParserKeyword s_on("ON");
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_dot(TokenType::Dot);
|
|
|
|
ParserToken s_lparen(TokenType::OpeningRoundBracket);
|
|
|
|
ParserToken s_rparen(TokenType::ClosingRoundBracket);
|
2017-09-17 18:49:43 +00:00
|
|
|
ParserStorage storage_p;
|
2017-04-01 07:20:54 +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;
|
2019-05-28 21:17:48 +00:00
|
|
|
ParserNameList names_p;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-06 23:22:44 +00:00
|
|
|
ASTPtr table;
|
2019-01-21 17:17:21 +00:00
|
|
|
ASTPtr columns_list;
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr storage;
|
|
|
|
ASTPtr as_database;
|
|
|
|
ASTPtr as_table;
|
2019-07-18 18:29:49 +00:00
|
|
|
ASTPtr as_table_function;
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr select;
|
2020-12-08 14:13:35 +00:00
|
|
|
ASTPtr from_path;
|
2019-05-28 21:17:48 +00:00
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
String cluster_str;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool attach = false;
|
2020-12-25 16:28:10 +00:00
|
|
|
bool replace = false;
|
|
|
|
bool or_replace = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool if_not_exists = false;
|
|
|
|
bool is_temporary = false;
|
2022-06-21 13:03:58 +00:00
|
|
|
bool is_create_empty = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-12-25 16:28:10 +00:00
|
|
|
if (s_create.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-12-25 16:28:10 +00:00
|
|
|
if (s_or_replace.ignore(pos, expected))
|
|
|
|
replace = or_replace = true;
|
2017-04-01 07:20:54 +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;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-12-25 16:28:10 +00:00
|
|
|
if (!replace && !or_replace && s_temporary.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
is_temporary = true;
|
|
|
|
}
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!s_table.ignore(pos, expected))
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-12-08 14:13:35 +00:00
|
|
|
if (attach && s_from.ignore(pos, expected))
|
|
|
|
{
|
2021-10-05 23:34:48 +00:00
|
|
|
ParserStringLiteral from_path_p;
|
2020-12-08 14:13:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-04-13 13:42:29 +00:00
|
|
|
|
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
|
2020-12-08 14:13:35 +00:00
|
|
|
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;
|
2017-10-21 20:38:39 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
query->attach = attach;
|
|
|
|
query->if_not_exists = if_not_exists;
|
|
|
|
query->cluster = cluster_str;
|
2017-10-21 20:38:39 +00:00
|
|
|
|
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);
|
2017-10-21 20:38:39 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-21 20:38:39 +00:00
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
if (!table_properties_p.parse(pos, columns_list, expected))
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!s_rparen.ignore(pos, expected))
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-12-14 09:30:08 +00:00
|
|
|
auto storage_parse_result = storage_p.parse(pos, storage, expected);
|
|
|
|
|
2022-06-21 13:03:58 +00:00
|
|
|
if (storage_parse_result && need_parse_as_select())
|
2020-12-14 09:30:08 +00:00
|
|
|
{
|
2020-12-14 09:33:16 +00:00
|
|
|
if (!select_p.parse(pos, select, expected))
|
|
|
|
return false;
|
2020-12-14 09:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!storage_parse_result && !is_temporary)
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2022-06-21 13:03:58 +00:00
|
|
|
if (need_parse_as_select() && !table_function_p.parse(pos, as_table_function, expected))
|
2020-10-14 12:19:29 +00:00
|
|
|
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
|
|
|
}
|
2021-12-15 11:30:57 +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);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-12-15 11:30:57 +00:00
|
|
|
/// 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
|
|
|
{
|
2021-12-15 11:30:57 +00:00
|
|
|
if (!select_p.parse(pos, select, expected)) /// AS SELECT ...
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-12-15 11:30:57 +00:00
|
|
|
/// ENGINE can not be specified for table functions.
|
|
|
|
if (storage || !table_function_p.parse(pos, as_table_function, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-12-15 11:30:57 +00:00
|
|
|
/// AS [db.]table
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, as_table, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2021-12-15 11:30:57 +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);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 10:07:11 +00:00
|
|
|
auto comment = parseComment(pos, expected);
|
2019-10-09 20:09:10 +00:00
|
|
|
|
|
|
|
auto query = std::make_shared<ASTCreateQuery>();
|
|
|
|
node = query;
|
|
|
|
|
|
|
|
if (as_table_function)
|
|
|
|
query->as_table_function = as_table_function;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-10-08 10:07:11 +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)
|
2020-10-12 13:55:41 +00:00
|
|
|
{
|
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)
|
2020-10-12 13:55:41 +00:00
|
|
|
throw Exception("Multiple primary keys are not allowed.", ErrorCodes::BAD_ARGUMENTS);
|
2022-02-10 20:35:03 +00:00
|
|
|
|
2020-10-12 13:55:41 +00:00
|
|
|
query->storage->primary_key = query->columns_list->primary_key;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-08 14:13:35 +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");
|
2021-10-12 18:07:09 +00:00
|
|
|
ParserCompoundIdentifier table_name_p(true, 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);
|
|
|
|
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;
|
2020-02-14 18:20:20 +00:00
|
|
|
ASTPtr live_view_timeout;
|
2020-09-14 19:39:33 +00:00
|
|
|
ASTPtr live_view_periodic_refresh;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
|
|
|
String cluster_str;
|
|
|
|
bool attach = false;
|
|
|
|
bool if_not_exists = false;
|
2020-09-14 19:39:33 +00:00
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
{
|
2019-10-09 20:09:10 +00:00
|
|
|
if (s_attach.ignore(pos, expected))
|
|
|
|
attach = true;
|
2019-05-28 21:17:48 +00:00
|
|
|
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;
|
2019-05-28 21:17:48 +00:00
|
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
|
2020-09-14 19:39:33 +00:00
|
|
|
if (ParserKeyword{"WITH"}.ignore(pos, expected))
|
2020-02-14 18:20:20 +00:00
|
|
|
{
|
2020-09-14 19:39:33 +00:00
|
|
|
if (ParserKeyword{"TIMEOUT"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!ParserNumber{}.parse(pos, live_view_timeout, expected))
|
|
|
|
{
|
|
|
|
live_view_timeout = std::make_shared<ASTLiteral>(static_cast<UInt64>(DEFAULT_TEMPORARY_LIVE_VIEW_TIMEOUT_SEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Optional - AND
|
|
|
|
if (ParserKeyword{"AND"}.ignore(pos, expected))
|
|
|
|
with_and = true;
|
|
|
|
|
|
|
|
with_timeout = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!select_p.parse(pos, select, expected))
|
|
|
|
return false;
|
2019-05-28 21:17:48 +00:00
|
|
|
|
2021-11-03 20:32:29 +00:00
|
|
|
auto comment = parseComment(pos, expected);
|
2019-05-28 21:17:48 +00:00
|
|
|
|
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);
|
|
|
|
|
2020-02-14 18:20:20 +00:00
|
|
|
if (live_view_timeout)
|
|
|
|
query->live_view_timeout.emplace(live_view_timeout->as<ASTLiteral &>().value.safeGet<UInt64>());
|
|
|
|
|
2020-09-14 19:39:33 +00:00
|
|
|
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
|
|
|
|
2021-11-03 20:32:29 +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");
|
2020-06-09 08:48:04 +00:00
|
|
|
ParserCompoundIdentifier table_name_p(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;
|
2022-05-14 16:46:49 +00:00
|
|
|
ParserStorage storage_inner;
|
2020-01-14 03:07:31 +00:00
|
|
|
ParserTablePropertiesDeclarationList table_properties_p;
|
2020-02-14 08:07:03 +00:00
|
|
|
ParserIntervalOperatorExpression watermark_p;
|
2020-03-03 04:42:12 +00:00
|
|
|
ParserIntervalOperatorExpression 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;
|
|
|
|
|
2021-11-19 13:09:12 +00:00
|
|
|
StorageID table_id = table->as<ASTTableIdentifier>()->getTableId();
|
2021-11-20 10:10:16 +00:00
|
|
|
query->setDatabase(table_id.database_name);
|
|
|
|
query->setTable(table_id.table_name);
|
2021-11-20 09:03:39 +00:00
|
|
|
query->uuid = table_id.uuid;
|
2020-01-14 03:07:31 +00:00
|
|
|
query->cluster = cluster_str;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-03 09:35:36 +00:00
|
|
|
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;
|
|
|
|
|
2021-12-16 08:26:37 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-12-03 09:35:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:26:37 +00:00
|
|
|
if (is_standalone && !rparen_p.ignore(pos, expected))
|
2021-12-03 09:35:36 +00:00
|
|
|
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>();
|
2021-12-16 08:26:37 +00:00
|
|
|
if (table_name)
|
|
|
|
res->table_name = table_name->as<ASTIdentifier>()->name();
|
|
|
|
res->is_standalone = is_standalone;
|
2021-12-03 09:35:36 +00:00
|
|
|
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;
|
2021-09-06 22:13:54 +00:00
|
|
|
ParserIdentifier name_p(true);
|
2021-12-03 09:35:36 +00:00
|
|
|
ParserTableOverridesDeclarationList table_overrides_p;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr storage;
|
2021-12-03 09:35:36 +00:00
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
|
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-05-28 21:17:48 +00:00
|
|
|
}
|
2019-10-09 20:09:10 +00:00
|
|
|
|
|
|
|
if (!s_database.ignore(pos, expected))
|
2017-10-25 19:52:32 +00:00
|
|
|
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))
|
2017-10-25 19:52:32 +00:00
|
|
|
{
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
storage_p.parse(pos, storage, expected);
|
2021-10-08 10:07:11 +00:00
|
|
|
auto comment = parseComment(pos, expected);
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2021-12-03 09:35:36 +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;
|
2021-09-06 22:13:54 +00:00
|
|
|
query->database = database;
|
|
|
|
|
|
|
|
if (database)
|
|
|
|
query->children.push_back(database);
|
2019-10-09 20:09:10 +00:00
|
|
|
|
|
|
|
query->set(query->storage, storage);
|
2021-10-08 10:07:11 +00:00
|
|
|
if (comment)
|
|
|
|
query->set(query->comment, comment);
|
2021-12-03 09:35:36 +00:00
|
|
|
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");
|
2021-10-12 18:07:09 +00:00
|
|
|
ParserCompoundIdentifier table_name_p(true, true);
|
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;
|
|
|
|
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;
|
2017-10-25 19:52:32 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
String cluster_str;
|
|
|
|
bool attach = false;
|
|
|
|
bool if_not_exists = false;
|
2020-12-16 03:19:38 +00:00
|
|
|
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
|
2017-10-25 19:52:32 +00:00
|
|
|
return false;
|
2019-10-09 20:09:10 +00:00
|
|
|
}
|
2017-10-25 19:52:32 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
/// VIEW or MATERIALIZED VIEW
|
|
|
|
if (s_or_replace.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
replace_view = true;
|
|
|
|
}
|
2017-10-25 19:52:32 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!replace_view && s_materialized.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
is_materialized_view = true;
|
2017-10-25 19:52:32 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2020-12-16 03:19:38 +00:00
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!replace_view && s_if_not_exists.ignore(pos, expected))
|
|
|
|
if_not_exists = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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;
|
2017-04-01 07:20:54 +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;
|
|
|
|
}
|
2017-08-02 20:54:41 +00:00
|
|
|
|
2021-03-08 17:26:38 +00:00
|
|
|
|
|
|
|
if (ParserKeyword{"TO INNER UUID"}.ignore(pos, expected))
|
|
|
|
{
|
2021-10-07 13:43:49 +00:00
|
|
|
ParserStringLiteral literal_p;
|
2021-03-08 17:26:38 +00:00
|
|
|
if (!literal_p.parse(pos, to_inner_uuid, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (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;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
if (!s_rparen.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2019-10-09 20:09:10 +00:00
|
|
|
}
|
2017-08-18 20:56:19 +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.
|
2022-02-02 18:07:03 +00:00
|
|
|
/// 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;
|
2017-04-01 07:20:54 +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;
|
|
|
|
|
2021-10-08 10:07:11 +00:00
|
|
|
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>();
|
2017-04-01 07:20:54 +00:00
|
|
|
node = query;
|
|
|
|
|
|
|
|
query->attach = attach;
|
|
|
|
query->if_not_exists = if_not_exists;
|
2020-12-16 03:19:38 +00:00
|
|
|
query->is_ordinary_view = is_ordinary_view;
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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>());
|
2017-10-30 17:53:01 +00:00
|
|
|
|
2019-01-21 17:17:21 +00:00
|
|
|
query->set(query->columns_list, columns_list);
|
2017-09-17 18:49:43 +00:00
|
|
|
query->set(query->storage, storage);
|
2021-10-08 10:07:11 +00:00
|
|
|
if (comment)
|
|
|
|
query->set(query->comment, comment);
|
2019-01-14 18:15:04 +00:00
|
|
|
|
2019-08-08 20:02:30 +00:00
|
|
|
tryGetIdentifierNameInto(as_database, query->as_database);
|
|
|
|
tryGetIdentifierNameInto(as_table, query->as_table);
|
2017-09-17 18:49:43 +00:00
|
|
|
query->set(query->select, select);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-09 20:09:10 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserCreateDictionaryQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserKeyword s_create("CREATE");
|
2019-10-11 13:21:52 +00:00
|
|
|
ParserKeyword s_attach("ATTACH");
|
2021-06-17 22:29:41 +00:00
|
|
|
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");
|
2019-12-12 10:27:02 +00:00
|
|
|
ParserKeyword s_on("ON");
|
2021-10-12 18:07:09 +00:00
|
|
|
ParserCompoundIdentifier dict_name_p(true, 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;
|
2021-06-17 22:29:41 +00:00
|
|
|
bool replace = false;
|
|
|
|
bool or_replace = false;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
|
|
|
ASTPtr name;
|
|
|
|
ASTPtr attributes;
|
|
|
|
ASTPtr dictionary;
|
2019-12-12 10:27:02 +00:00
|
|
|
String cluster_str;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2019-10-11 13:21:52 +00:00
|
|
|
bool attach = false;
|
2021-06-17 22:29:41 +00:00
|
|
|
|
|
|
|
if (s_create.ignore(pos, expected))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
2021-06-17 22:29:41 +00:00
|
|
|
if (s_or_replace.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
replace = true;
|
|
|
|
or_replace = true;
|
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2021-06-17 22:29:41 +00:00
|
|
|
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;
|
|
|
|
|
2019-12-12 10:27:02 +00:00
|
|
|
if (s_on.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-11 13:21:52 +00:00
|
|
|
if (!attach)
|
|
|
|
{
|
|
|
|
if (!s_left_paren.ignore(pos, expected))
|
|
|
|
return false;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2019-10-11 13:21:52 +00:00
|
|
|
if (!attributes_p.parse(pos, attributes, expected))
|
|
|
|
return false;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2019-10-11 13:21:52 +00:00
|
|
|
if (!s_right_paren.ignore(pos, expected))
|
|
|
|
return false;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2019-10-11 13:21:52 +00:00
|
|
|
if (!dictionary_p.parse(pos, dictionary, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2021-10-08 10:07:11 +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;
|
2019-10-11 13:21:52 +00:00
|
|
|
query->attach = attach;
|
2021-06-17 22:29:41 +00:00
|
|
|
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);
|
2019-12-12 10:27:02 +00:00
|
|
|
query->cluster = cluster_str;
|
2019-10-09 20:09:10 +00:00
|
|
|
|
2021-10-08 10:07:11 +00:00
|
|
|
if (comment)
|
|
|
|
query->set(query->comment, comment);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2011-08-18 18:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|| window_view_p.parse(pos, node, expected);
|
2019-10-09 20:09:10 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
}
|