Added constraints to parsers

This commit is contained in:
Gleb Novikov 2019-05-12 14:36:02 +03:00
parent 83a35d7644
commit f24bedd2b8
5 changed files with 118 additions and 10 deletions

View File

@ -0,0 +1,38 @@
#pragma once
#include <Parsers/IAST.h>
namespace DB
{
/** name CHECK logical_expr
*/
class ASTConstraintDeclaration : public IAST {
public:
String name;
IAST *expr;
String getID(char) const override { return "Constraint"; }
ASTPtr clone() const override {
auto res = std::make_shared<ASTConstraintDeclaration>();
res->name = name;
if (expr)
res->set(res->expr, expr->clone());
return res;
}
void formatImpl(const FormatSettings &s, FormatState &state, FormatStateStacked frame) const override {
frame.need_parens = false;
std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' ');
s.ostr << s.nl_or_ws << indent_str;
s.ostr << backQuoteIfNeed(name);
s.ostr << (s.hilite ? hilite_keyword : "") << " CHECK " << (s.hilite ? hilite_none : "");
expr->formatImpl(s, state, frame);
}
};
}

View File

@ -128,6 +128,8 @@ ASTPtr ASTColumns::clone() const
res->set(res->columns, columns->clone());
if (indices)
res->set(res->indices, indices->clone());
if (constraints)
res->set(res->constraints, constraints->clone());
return res;
}
@ -156,6 +158,16 @@ void ASTColumns::formatImpl(const FormatSettings & s, FormatState & state, Forma
list.children.push_back(elem);
}
}
if (constraints)
{
for (const auto & constraint : constraints->children)
{
auto elem = std::make_shared<ASTColumnsElement>();
elem->prefix = "CONSTRAINT";
elem->set(elem->elem, constraint->clone());
list.children.push_back(elem);
}
}
if (!list.children.empty())
list.formatImpl(s, state, frame);

View File

@ -36,6 +36,7 @@ class ASTColumns : public IAST
public:
ASTExpressionList * columns = nullptr;
ASTExpressionList * indices = nullptr;
ASTExpressionList * constraints = nullptr;
String getID(char) const override { return "Columns definition"; }

View File

@ -10,6 +10,7 @@
#include <Parsers/ParserCreateQuery.h>
#include <Parsers/ParserSelectWithUnionQuery.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTConstraintDeclaration.h>
namespace DB
@ -137,12 +138,41 @@ bool ParserIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expe
return true;
}
bool ParserConstraintDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_check("CHECK");
bool ParserColumnAndIndexDeclaraion::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
ParserIdentifier name_p;
ParserLogicalOrExpression expression_p;
ASTPtr name;
ASTPtr expr;
if (!name_p.parse(pos, name, expected))
return false;
if (!s_check.ignore(pos, expected))
return false;
if (!expression_p.parse(pos, expr, expected))
return false;
auto constraint = std::make_shared<ASTConstraintDeclaration>();
constraint->name = name->as<ASTIdentifier &>().name;
constraint->set(constraint->expr, expr);
node = constraint;
return true;
}
bool ParserTablePropertyDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_index("INDEX");
ParserKeyword s_constraint("CONSTRAINT");
ParserIndexDeclaration index_p;
ParserConstraintDeclaration constraint_p;
ParserColumnDeclaration column_p;
ASTPtr new_node = nullptr;
@ -152,6 +182,11 @@ bool ParserColumnAndIndexDeclaraion::parseImpl(Pos & pos, ASTPtr & node, Expecte
if (!index_p.parse(pos, new_node, expected))
return false;
}
else if (s_constraint.ignore(pos, expected))
{
if (!constraint_p.parse(pos, new_node, expected))
return false;
}
else
{
if (!column_p.parse(pos, new_node, expected))
@ -168,16 +203,18 @@ bool ParserIndexDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected &
.parse(pos, node, expected);
}
bool ParserColumnsOrIndicesDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
bool ParserTablePropertiesDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ASTPtr list;
if (!ParserList(std::make_unique<ParserColumnAndIndexDeclaraion>(), std::make_unique<ParserToken>(TokenType::Comma), false)
if (!ParserList(
std::make_unique<ParserTablePropertyDeclaration>(),
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>();
ASTPtr constraints = std::make_shared<ASTExpressionList>();
for (const auto & elem : list->children)
{
@ -185,6 +222,8 @@ bool ParserColumnsOrIndicesDeclarationList::parseImpl(Pos & pos, ASTPtr & node,
columns->children.push_back(elem);
else if (elem->as<ASTIndexDeclaration>())
indices->children.push_back(elem);
else if (elem->as<ASTConstraintDeclaration>())
constraints->children.push_back(elem);
else
return false;
}
@ -195,6 +234,8 @@ bool ParserColumnsOrIndicesDeclarationList::parseImpl(Pos & pos, ASTPtr & node,
res->set(res->columns, columns);
if (!indices->children.empty())
res->set(res->indices, indices);
if (!constraints->children.empty())
res->set(res->constraints, constraints);
node = res;
@ -317,7 +358,7 @@ bool ParserCreateQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
ParserToken s_rparen(TokenType::ClosingRoundBracket);
ParserStorage storage_p;
ParserIdentifier name_p;
ParserColumnsOrIndicesDeclarationList columns_or_indices_p;
ParserTablePropertiesDeclarationList table_properties_p;
ParserSelectWithUnionQuery select_p;
ASTPtr database;
@ -391,7 +432,7 @@ bool ParserCreateQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
/// List of columns.
if (s_lparen.ignore(pos, expected))
{
if (!columns_or_indices_p.parse(pos, columns_list, expected))
if (!table_properties_p.parse(pos, columns_list, expected))
return false;
if (!s_rparen.ignore(pos, expected))
@ -498,7 +539,7 @@ bool ParserCreateQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
/// Optional - a list of columns can be specified. It must fully comply with SELECT.
if (s_lparen.ignore(pos, expected))
{
if (!columns_or_indices_p.parse(pos, columns_list, expected))
if (!table_properties_p.parse(pos, columns_list, expected))
return false;
if (!s_rparen.ignore(pos, expected))

View File

@ -244,11 +244,20 @@ protected:
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserConstraintDeclaration : public IParserBase
{
public:
ParserConstraintDeclaration() {}
class ParserColumnAndIndexDeclaraion : public IParserBase
protected:
const char * getName() const override { return "constraint declaration"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserTablePropertyDeclaration : public IParserBase
{
protected:
const char * getName() const override { return "column or index declaration"; }
const char * getName() const override { return "table propery (column, index, constraint) declaration"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
@ -260,8 +269,15 @@ protected:
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserConstraintDeclarationList : public IParserBase
{
protected:
const char * getName() const override { return "constraint declaration list"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserColumnsOrIndicesDeclarationList : public IParserBase
class ParserTablePropertiesDeclarationList : public IParserBase
{
protected:
const char * getName() const override { return "columns or indices declaration list"; }