提交 e1cda4b7 编写于 作者: A artpaul

move impl of common parsers to cpp

上级 c617565f
...@@ -829,6 +829,7 @@ add_library (dbms ...@@ -829,6 +829,7 @@ add_library (dbms
src/Parsers/ASTIdentifier.cpp src/Parsers/ASTIdentifier.cpp
src/Parsers/ASTSampleRatio.cpp src/Parsers/ASTSampleRatio.cpp
src/Parsers/ASTTablesInSelectQuery.cpp src/Parsers/ASTTablesInSelectQuery.cpp
src/Parsers/CommonParsers.cpp
src/Parsers/IAST.cpp src/Parsers/IAST.cpp
src/Parsers/IParserBase.cpp src/Parsers/IParserBase.cpp
src/Parsers/ExpressionElementParsers.cpp src/Parsers/ExpressionElementParsers.cpp
......
...@@ -144,25 +144,11 @@ private: ...@@ -144,25 +144,11 @@ private:
void executeSubqueriesInSetsAndJoins(std::unordered_map<String, SubqueryForSet> & subqueries_for_sets); void executeSubqueriesInSetsAndJoins(std::unordered_map<String, SubqueryForSet> & subqueries_for_sets);
template <typename Transform> template <typename Transform>
void transformStreams(Transform && transform) void transformStreams(Transform && transform);
{
for (auto & stream : streams)
transform(stream);
if (stream_with_non_joined_data) bool hasNoData() const;
transform(stream_with_non_joined_data);
}
bool hasNoData() const
{
return streams.empty() && !stream_with_non_joined_data;
}
bool hasMoreThanOneStream() const
{
return streams.size() + (stream_with_non_joined_data ? 1 : 0) > 1;
}
bool hasMoreThanOneStream() const;
void ignoreWithTotals(); void ignoreWithTotals();
......
#pragma once #pragma once
#include <string.h> /// strncmp, strncasecmp
#include <DB/Parsers/IParserBase.h> #include <DB/Parsers/IParserBase.h>
namespace DB namespace DB
{ {
/** Если прямо сейчас не s, то ошибка. /** Если прямо сейчас не s, то ошибка.
* Если word_boundary установлен в true, и последний символ строки - словарный (\w), * Если word_boundary установлен в true, и последний символ строки - словарный (\w),
* то проверяется, что последующий символ строки не словарный. * то проверяется, что последующий символ строки не словарный.
...@@ -22,26 +18,12 @@ private: ...@@ -22,26 +18,12 @@ private:
bool case_insensitive; bool case_insensitive;
public: public:
ParserString(const char * s_, bool word_boundary_ = false, bool case_insensitive_ = false) ParserString(const char * s_, bool word_boundary_ = false, bool case_insensitive_ = false);
: s(s_), s_size(strlen(s)), word_boundary(word_boundary_), case_insensitive(case_insensitive_) {}
protected: protected:
const char * getName() const { return s; } const char * getName() const override;
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
{
if (static_cast<ssize_t>(s_size) > end - pos || (case_insensitive ? strncasecmp : strncmp)(pos, s, s_size))
return false;
else
{
if (word_boundary && s_size && isWordCharASCII(s[s_size - 1])
&& pos + s_size != end && isWordCharASCII(pos[s_size]))
return false;
pos += s_size;
return true;
}
}
}; };
...@@ -50,74 +32,32 @@ protected: ...@@ -50,74 +32,32 @@ protected:
class ParserWhiteSpace : public IParserBase class ParserWhiteSpace : public IParserBase
{ {
public: public:
ParserWhiteSpace(bool allow_newlines_ = true) : allow_newlines(allow_newlines_) {} ParserWhiteSpace(bool allow_newlines_ = true);
protected: protected:
bool allow_newlines; bool allow_newlines;
const char * getName() const { return "white space"; } const char * getName() const override;
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
Pos begin = pos;
while (pos < end && (*pos == ' ' || *pos == '\t' || (allow_newlines && *pos == '\n') || *pos == '\r' || *pos == '\f'))
++pos;
return pos != begin; bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
}
}; };
class ParserCStyleComment : public IParserBase class ParserCStyleComment : public IParserBase
{ {
protected: protected:
const char * getName() const { return "C-style comment"; } const char * getName() const override;
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
{
if (end - pos >= 4 && pos[0] == '/' && pos[1] == '*')
{
pos += 2;
while (end - pos >= 2 && (pos[0] != '*' || pos[1] != '/'))
++pos;
if (end - pos < 2)
{
expected = "closing of C-style comment '*/'";
return false;
}
else
{
pos += 2;
return true;
}
}
else
return false;
}
}; };
class ParserSQLStyleComment : public IParserBase class ParserSQLStyleComment : public IParserBase
{ {
protected: protected:
const char * getName() const { return "SQL-style comment"; } const char * getName() const override;
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
{
if (end - pos >= 2 && pos[0] == '-' && pos[1] == '-')
{
pos += 2;
while (pos != end && *pos != '\n')
++pos;
if (pos != end)
++pos;
return true;
}
else
return false;
}
}; };
...@@ -126,39 +66,23 @@ protected: ...@@ -126,39 +66,23 @@ protected:
class ParserComment : public IParserBase class ParserComment : public IParserBase
{ {
protected: protected:
const char * getName() const { return "comment"; } const char * getName() const override;
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
{
ParserCStyleComment p1;
ParserSQLStyleComment p2;
return p1.ignore(pos, end, max_parsed_pos, expected)
|| p2.ignore(pos, end, max_parsed_pos, expected);
}
}; };
class ParserWhiteSpaceOrComments : public IParserBase class ParserWhiteSpaceOrComments : public IParserBase
{ {
public: public:
ParserWhiteSpaceOrComments(bool allow_newlines_outside_comments_ = true) : allow_newlines_outside_comments(allow_newlines_outside_comments_) {} ParserWhiteSpaceOrComments(bool allow_newlines_outside_comments_ = true);
protected: protected:
bool allow_newlines_outside_comments; bool allow_newlines_outside_comments;
const char * getName() const { return "white space or comments"; } const char * getName() const override;
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
ParserWhiteSpace p1(allow_newlines_outside_comments);
ParserComment p2;
bool res = false; bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
while (p1.ignore(pos, end, max_parsed_pos, expected) || p2.ignore(pos, end, max_parsed_pos, expected))
res = true;
return res;
}
}; };
} }
...@@ -10,8 +10,8 @@ namespace DB ...@@ -10,8 +10,8 @@ namespace DB
class ParserSelectQuery : public ParserQueryWithOutput class ParserSelectQuery : public ParserQueryWithOutput
{ {
protected: protected:
const char * getName() const { return "SELECT query"; } const char * getName() const override { return "SELECT query"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected); bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
}; };
} }
...@@ -1229,6 +1229,28 @@ void InterpreterSelectQuery::executeSubqueriesInSetsAndJoins(SubqueriesForSets & ...@@ -1229,6 +1229,28 @@ void InterpreterSelectQuery::executeSubqueriesInSetsAndJoins(SubqueriesForSets &
streams[0] = std::make_shared<CreatingSetsBlockInputStream>(streams[0], subqueries_for_sets, settings.limits); streams[0] = std::make_shared<CreatingSetsBlockInputStream>(streams[0], subqueries_for_sets, settings.limits);
} }
template <typename Transform>
void InterpreterSelectQuery::transformStreams(Transform && transform)
{
for (auto & stream : streams)
transform(stream);
if (stream_with_non_joined_data)
transform(stream_with_non_joined_data);
}
bool InterpreterSelectQuery::hasNoData() const
{
return streams.empty() && !stream_with_non_joined_data;
}
bool InterpreterSelectQuery::hasMoreThanOneStream() const
{
return streams.size() + (stream_with_non_joined_data ? 1 : 0) > 1;
}
void InterpreterSelectQuery::ignoreWithTotals() void InterpreterSelectQuery::ignoreWithTotals()
{ {
......
#include <DB/Parsers/CommonParsers.h>
#include <string.h> /// strncmp, strncasecmp
namespace DB {
ParserString::ParserString(const char * s_, bool word_boundary_, bool case_insensitive_)
: s(s_)
, s_size(strlen(s))
, word_boundary(word_boundary_)
, case_insensitive(case_insensitive_)
{
}
const char * ParserString::getName() const
{
return s;
}
bool ParserString::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
if (static_cast<ssize_t>(s_size) > end - pos || (case_insensitive ? strncasecmp : strncmp)(pos, s, s_size))
return false;
else
{
if (word_boundary && s_size && isWordCharASCII(s[s_size - 1])
&& pos + s_size != end && isWordCharASCII(pos[s_size]))
return false;
pos += s_size;
return true;
}
}
ParserWhiteSpace::ParserWhiteSpace(bool allow_newlines_)
: allow_newlines(allow_newlines_)
{
}
const char * ParserWhiteSpace::getName() const
{
return "white space";
}
bool ParserWhiteSpace::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
Pos begin = pos;
while (pos < end && (*pos == ' ' || *pos == '\t' || (allow_newlines && *pos == '\n') || *pos == '\r' || *pos == '\f'))
++pos;
return pos != begin;
}
const char * ParserCStyleComment::getName() const
{
return "C-style comment";
}
bool ParserCStyleComment::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
if (end - pos >= 4 && pos[0] == '/' && pos[1] == '*')
{
pos += 2;
while (end - pos >= 2 && (pos[0] != '*' || pos[1] != '/'))
++pos;
if (end - pos < 2)
{
expected = "closing of C-style comment '*/'";
return false;
}
else
{
pos += 2;
return true;
}
}
else
return false;
}
const char * ParserSQLStyleComment::getName() const
{
return "SQL-style comment";
}
bool ParserSQLStyleComment::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
if (end - pos >= 2 && pos[0] == '-' && pos[1] == '-')
{
pos += 2;
while (pos != end && *pos != '\n')
++pos;
if (pos != end)
++pos;
return true;
}
else
return false;
}
const char * ParserComment::getName() const
{
return "comment";
}
bool ParserComment::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
ParserCStyleComment p1;
ParserSQLStyleComment p2;
return p1.ignore(pos, end, max_parsed_pos, expected)
|| p2.ignore(pos, end, max_parsed_pos, expected);
}
ParserWhiteSpaceOrComments::ParserWhiteSpaceOrComments(bool allow_newlines_outside_comments_)
: allow_newlines_outside_comments(allow_newlines_outside_comments_)
{
}
const char * ParserWhiteSpaceOrComments::getName() const
{
return "white space or comments";
}
bool ParserWhiteSpaceOrComments::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
ParserWhiteSpace p1(allow_newlines_outside_comments);
ParserComment p2;
bool res = false;
while (p1.ignore(pos, end, max_parsed_pos, expected) || p2.ignore(pos, end, max_parsed_pos, expected))
res = true;
return res;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册