added useful parsers and ast classes

This commit is contained in:
Nikita Vasilev 2019-01-20 12:19:34 +03:00
parent 4a19523698
commit 4bf0ac886b
3 changed files with 129 additions and 0 deletions

View File

@ -90,6 +90,44 @@ public:
};
class ASTColumns : public IAST
{
public:
ASTExpressionList * columns = nullptr;
ASTExpressionList * indices = nullptr;
String getID(char) const override { return "Columns definition"; }
ASTPtr clone() const override
{
auto res = std::make_shared<ASTColumns>();
if (columns)
res->set(res->columns, columns->clone());
if (indices)
res->set(res->indices, indices->clone());
return res;
}
void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const override
{
ASTExpressionList list;
if (columns)
for (const auto & column : columns->children)
list.children.push_back(column);
if (indices)
for (const auto & index : indices->children)
list.children.push_back(index);
if (!list.children.empty())
list.formatImpl(s, state, frame;
}
};
/// CREATE TABLE or ATTACH TABLE query
class ASTCreateQuery : public ASTQueryWithTableAndOutput, public ASTQueryWithOnCluster
{
@ -100,6 +138,7 @@ public:
bool is_materialized_view{false};
bool is_populate{false};
ASTExpressionList * columns = nullptr;
ASTColumns * columns_list = nullptr;
String to_database; /// For CREATE MATERIALIZED VIEW mv TO table.
String to_table;
ASTStorage * storage = nullptr;
@ -115,6 +154,8 @@ public:
auto res = std::make_shared<ASTCreateQuery>(*this);
res->children.clear();
if (columns_list)
res->set(res->columns_list, columns_list->clone());
if (columns)
res->set(res->columns, columns->clone());
if (storage)
@ -184,6 +225,15 @@ protected:
<< (!as_database.empty() ? backQuoteIfNeed(as_database) + "." : "") << backQuoteIfNeed(as_table);
}
if (columns_list)
{
settings.ostr << (settings.one_line ? " (" : "\n(");
FormatStateStacked frame_nested = frame;
++frame_nested.indent;
columns_list->formatImpl(settings, state, frame_nested);
settings.ostr << (settings.one_line ? ")" : "\n)");
}
if (columns)
{
settings.ostr << (settings.one_line ? " (" : "\n(");

View File

@ -139,6 +139,35 @@ bool ParserIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expe
return true;
}
bool ParserColumnAndIndexDeclaraion::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_index("INDEX");
ParserIndexDeclaration index_p;
ParserColumnDeclaration column_p;
ASTPtr column = nullptr;
ASTPtr index = nullptr;
if (s_index.ignore(pos, expected))
{
if (!index_p.parse(pos, index, expected))
return false;
}
else
{
if (!column_p.parse(pos, column, expected))
return false;
}
if (column)
node = column;
else
node = index;
return true;
}
bool ParserIndexDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
return ParserList(std::make_unique<ParserIndexDeclaration>(), std::make_unique<ParserToken>(TokenType::Comma), false)
@ -146,6 +175,39 @@ bool ParserIndexDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected &
}
bool ParserColumnsOrIndicesDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ASTPtr list;
if (!ParserList(std::make_unique<ParserColumnAndIndexDeclaraion>(), std::make_unique<ParserToken>(TokenType::Comma), false)
.parse(pos, list, expected))
return false;
ASTPtr columns = std::make_shared<ASTExpressionList>();
ASTPtr indices = std::make_shared<ASTExpressionList>();
for (const auto & elem : list->children)
{
if (dynamic_cast<const ASTColumnDeclaration *>(elem.get()))
columns->children.push_back(elem);
else if (dynamic_cast<const ASTIndexDeclaration *>(elem.get()))
indices->children.push_back(elem);
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);
node = res;
return true;
}
bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_engine("ENGINE");

View File

@ -231,6 +231,15 @@ protected:
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserColumnAndIndexDeclaraion : public IParserBase
{
protected:
const char * getName() const override { return "column or index declaration"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserIndexDeclarationList : public IParserBase
{
protected:
@ -239,6 +248,14 @@ protected:
};
class ParserColumnsOrIndicesDeclaration : public IParserBase
{
protected:
const char * getName() const override { return "columns or indices declaration"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
/**
* ENGINE = name [PARTITION BY expr] [ORDER BY expr] [PRIMARY KEY expr] [SAMPLE BY expr]
* [INDEXES name BY expr TYPE type(args) GRANULARITY value, ...] [SETTINGS name = value, ...]