added index parsing

This commit is contained in:
Nikita Vasilev 2018-12-25 21:45:08 +03:00
parent 6f986495dd
commit fcd49afc2a
5 changed files with 105 additions and 25 deletions

View File

@ -19,6 +19,7 @@ public:
IAST * primary_key = nullptr;
IAST * order_by = nullptr;
IAST * sample_by = nullptr;
std::vector<IAST *> indexes;
ASTSetQuery * settings = nullptr;
String getID(char) const override { return "Storage definition"; }
@ -38,6 +39,12 @@ public:
res->set(res->order_by, order_by->clone());
if (sample_by)
res->set(res->sample_by, sample_by->clone());
for (const auto& index : indexes) {
res->indexes.emplace_back(nullptr);
res->set(res->indexes.back(), index->clone());
}
if (settings)
res->set(res->settings, settings->clone());
@ -71,6 +78,10 @@ public:
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "SAMPLE BY " << (s.hilite ? hilite_none : "");
sample_by->formatImpl(s, state, frame);
}
for (const auto& index : indexes) {
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "INDEX " << (s.hilite ? hilite_none : "");
index->formatImpl(s, state, frame);
}
if (settings)
{
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "SETTINGS " << (s.hilite ? hilite_none : "");

View File

@ -11,40 +11,37 @@
namespace DB
{
/** Index name(expr) TYPE typename(args) in create query
*/
class ASTIndexQuery : public IAST
class ASTIndexDeclaration : public IAST
{
public:
struct Index
{
String name;
ASTPtr expression_list;
ASTFunction type;
};
using Indexes = std::vector<Index>;
Indexes indexes;
String name;
IAST * expr;
ASTFunction * type;
//TODO: params (GRANULARITY number or SETTINGS a=b, c=d, ..)?
/** Get the text that identifies this element. */
String getID(char) const override { return "Index"; }
ASTPtr clone() const override { return std::make_shared<ASTIndexQuery>(*this); }
ASTPtr clone() const override {
auto res = std::make_shared<ASTIndexDeclaration>(*this);
res->children.clear();
if (expr)
res->set(res->expr, expr->clone());
if (type)
res->set(res->type, type->clone());
return res;
}
void formatImpl(const FormatSettings & s, FormatState &state, FormatStateStacked frame) const override
{
for (ASTIndexQuery::Indexes::const_iterator it = indexes.begin(); it != indexes.end(); ++it)
{
if (it != indexes.begin())
s.ostr << s.nl_or_ws;
s.ostr << (s.hilite ? hilite_keyword : "") << "INDEX" << (s.hilite ? hilite_none : "") << " " << it->name;
s.ostr << "(";
it->expression_list->formatImpl(s, state, frame);
s.ostr << ") " << (s.hilite ? hilite_keyword : "") << "TYPE" << (s.hilite ? hilite_none : "");
it->type.formatImpl(s, state, frame);
}
s.ostr << name;
s.ostr << (s.hilite ? hilite_keyword : "") << " BY " << (s.hilite ? hilite_none : "");
expr->formatImpl(s, state, frame);
s.ostr << (s.hilite ? hilite_keyword : "") << " TYPE " << (s.hilite ? hilite_none : "");
type->formatImpl(s, state, frame);
}
};

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTIndexDeclaration.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ExpressionListParsers.h>
@ -90,6 +91,43 @@ bool ParserColumnDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected &
.parse(pos, node, expected);
}
bool ParserIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_by("BY");
ParserKeyword s_type("TYPE");
ParserIdentifier name_p;
ParserIdentifierWithOptionalParameters ident_with_optional_params_p;
ParserExpression expression_p;
ASTPtr name;
ASTPtr expr;
ASTPtr type;
if (!name_p.parse(pos, name, expected))
return false;
if (!s_by.ignore(pos, expected))
return false;
if (!expression_p.parse(pos, expr, expected))
return false;
if (!s_type.ignore(pos, expected))
return false;
if (!ident_with_optional_params_p.parse(pos, type, expected))
return false;
auto index = std::make_shared<ASTIndexDeclaration>();
index->name = typeid_cast<const ASTIdentifier &>(*name).name;
index->set(index->expr, expr);
index->set(index->type, type);
node = index;
return true;
}
bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
@ -99,17 +137,20 @@ bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
ParserKeyword s_primary_key("PRIMARY KEY");
ParserKeyword s_order_by("ORDER BY");
ParserKeyword s_sample_by("SAMPLE BY");
ParserKeyword s_index("INDEX");
ParserKeyword s_settings("SETTINGS");
ParserIdentifierWithOptionalParameters ident_with_optional_params_p;
ParserExpression expression_p;
ParserSetQuery settings_p(/* parse_only_internals_ = */ true);
ParserIndexDeclaration index_p;
ASTPtr engine;
ASTPtr partition_by;
ASTPtr primary_key;
ASTPtr order_by;
ASTPtr sample_by;
ASTs indexes;
ASTPtr settings;
if (!s_engine.ignore(pos, expected))
@ -154,6 +195,14 @@ bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
return false;
}
if (s_index.ignore(pos, expected)) {
indexes.emplace_back(nullptr);
if (index_p.parse(pos, indexes.back(), expected))
continue;
else
return false;
}
if (s_settings.ignore(pos, expected))
{
if (!settings_p.parse(pos, settings, expected))
@ -169,6 +218,12 @@ bool ParserStorage::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
storage->set(storage->primary_key, primary_key);
storage->set(storage->order_by, order_by);
storage->set(storage->sample_by, sample_by);
for (const auto& index : indexes) {
storage->indexes.emplace_back(nullptr);
storage->set(storage->indexes.back(), index);
}
storage->set(storage->settings, settings);
node = storage;

View File

@ -199,7 +199,24 @@ protected:
};
/** ENGINE = name [PARTITION BY expr] [ORDER BY expr] [PRIMARY KEY expr] [SAMPLE BY expr] [SETTINGS name = value, ...] */
/**
* INDEX name(column1, column2, ...) TYPE typename(arg1, arg2, ...)
*/
class ParserIndexDeclaration : public IParserBase
{
public:
ParserIndexDeclaration() {}
protected:
const char * getName() const override { return "INDEX query"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
/**
* ENGINE = name [PARTITION BY expr] [ORDER BY expr] [PRIMARY KEY expr] [SAMPLE BY expr]
* [INDEX name(expr) TYPE type(args) ...] [SETTINGS name = value, ...]
*/
class ParserStorage : public IParserBase
{
protected:

View File

@ -10,7 +10,7 @@ int main(int, char **)
{
using namespace DB;
std::string input = "CREATE TABLE hits (URL String, UserAgentMinor2 FixedString(2), EventTime DateTime) ENGINE = Log";
std::string input = "CREATE TABLE hits (URL String, UserAgentMinor2 FixedString(2), EventTime DateTime) ENGINE = MergeTree() ORDER BY EventTime INDEX minmax1 BY (lower(URL), EventTime) TYPE MINMAX(1,2,3)";
ParserCreateQuery parser;
ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0);