From e1cda4b760549d58a8cb38d951fadad83a5b5467 Mon Sep 17 00:00:00 2001 From: artpaul Date: Sat, 12 Nov 2016 22:55:40 +0500 Subject: [PATCH] move impl of common parsers to cpp --- dbms/CMakeLists.txt | 1 + .../DB/Interpreters/InterpreterSelectQuery.h | 20 +-- dbms/include/DB/Parsers/CommonParsers.h | 112 +++---------- dbms/include/DB/Parsers/ParserSelectQuery.h | 4 +- .../Interpreters/InterpreterSelectQuery.cpp | 22 +++ dbms/src/Parsers/CommonParsers.cpp | 152 ++++++++++++++++++ 6 files changed, 198 insertions(+), 113 deletions(-) create mode 100644 dbms/src/Parsers/CommonParsers.cpp diff --git a/dbms/CMakeLists.txt b/dbms/CMakeLists.txt index fc5b866caf..5939dc4950 100644 --- a/dbms/CMakeLists.txt +++ b/dbms/CMakeLists.txt @@ -829,6 +829,7 @@ add_library (dbms src/Parsers/ASTIdentifier.cpp src/Parsers/ASTSampleRatio.cpp src/Parsers/ASTTablesInSelectQuery.cpp + src/Parsers/CommonParsers.cpp src/Parsers/IAST.cpp src/Parsers/IParserBase.cpp src/Parsers/ExpressionElementParsers.cpp diff --git a/dbms/include/DB/Interpreters/InterpreterSelectQuery.h b/dbms/include/DB/Interpreters/InterpreterSelectQuery.h index 1e6adec494..73c015c46b 100644 --- a/dbms/include/DB/Interpreters/InterpreterSelectQuery.h +++ b/dbms/include/DB/Interpreters/InterpreterSelectQuery.h @@ -144,25 +144,11 @@ private: void executeSubqueriesInSetsAndJoins(std::unordered_map & subqueries_for_sets); template - void transformStreams(Transform && transform) - { - for (auto & stream : streams) - transform(stream); + void transformStreams(Transform && transform); - if (stream_with_non_joined_data) - 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 hasNoData() const; + bool hasMoreThanOneStream() const; void ignoreWithTotals(); diff --git a/dbms/include/DB/Parsers/CommonParsers.h b/dbms/include/DB/Parsers/CommonParsers.h index 3bcf043347..99518df389 100644 --- a/dbms/include/DB/Parsers/CommonParsers.h +++ b/dbms/include/DB/Parsers/CommonParsers.h @@ -1,14 +1,10 @@ #pragma once -#include /// strncmp, strncasecmp - #include - namespace DB { - /** Если прямо сейчас не s, то ошибка. * Если word_boundary установлен в true, и последний символ строки - словарный (\w), * то проверяется, что последующий символ строки не словарный. @@ -22,26 +18,12 @@ private: bool case_insensitive; public: - 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_) {} + ParserString(const char * s_, bool word_boundary_ = false, bool case_insensitive_ = false); protected: - const char * getName() const { return s; } - - bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) - { - if (static_cast(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; - } - } + const char * getName() const override; + + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; @@ -50,74 +32,32 @@ protected: class ParserWhiteSpace : public IParserBase { public: - ParserWhiteSpace(bool allow_newlines_ = true) : allow_newlines(allow_newlines_) {} + ParserWhiteSpace(bool allow_newlines_ = true); protected: bool allow_newlines; - const char * getName() const { return "white space"; } - - 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; + const char * getName() const override; - return pos != begin; - } + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; class ParserCStyleComment : public IParserBase { protected: - const char * getName() const { return "C-style comment"; } - - bool 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 * getName() const override; + + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; class ParserSQLStyleComment : public IParserBase { protected: - const char * getName() const { return "SQL-style comment"; } - - bool 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 * getName() const override; + + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; @@ -126,39 +66,23 @@ protected: class ParserComment : public IParserBase { 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) - { - ParserCStyleComment p1; - ParserSQLStyleComment p2; - - return p1.ignore(pos, end, max_parsed_pos, expected) - || p2.ignore(pos, end, max_parsed_pos, expected); - } + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; class ParserWhiteSpaceOrComments : public IParserBase { public: - ParserWhiteSpaceOrComments(bool allow_newlines_outside_comments_ = true) : allow_newlines_outside_comments(allow_newlines_outside_comments_) {} + ParserWhiteSpaceOrComments(bool allow_newlines_outside_comments_ = true); protected: bool allow_newlines_outside_comments; - const char * getName() const { return "white space or comments"; } - - bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) - { - ParserWhiteSpace p1(allow_newlines_outside_comments); - ParserComment p2; + const char * getName() const override; - bool res = false; - while (p1.ignore(pos, end, max_parsed_pos, expected) || p2.ignore(pos, end, max_parsed_pos, expected)) - res = true; - return res; - } + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; } diff --git a/dbms/include/DB/Parsers/ParserSelectQuery.h b/dbms/include/DB/Parsers/ParserSelectQuery.h index bda7d72b35..482af380b9 100644 --- a/dbms/include/DB/Parsers/ParserSelectQuery.h +++ b/dbms/include/DB/Parsers/ParserSelectQuery.h @@ -10,8 +10,8 @@ namespace DB class ParserSelectQuery : public ParserQueryWithOutput { protected: - const char * getName() const { return "SELECT query"; } - bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected); + const char * getName() const override { return "SELECT query"; } + bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override; }; } diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index 449909c416..bddac96e4e 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -1229,6 +1229,28 @@ void InterpreterSelectQuery::executeSubqueriesInSetsAndJoins(SubqueriesForSets & streams[0] = std::make_shared(streams[0], subqueries_for_sets, settings.limits); } +template +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() { diff --git a/dbms/src/Parsers/CommonParsers.cpp b/dbms/src/Parsers/CommonParsers.cpp new file mode 100644 index 0000000000..4bbb98a66c --- /dev/null +++ b/dbms/src/Parsers/CommonParsers.cpp @@ -0,0 +1,152 @@ +#include + +#include /// 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(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; +} + +} -- GitLab