From 1e58adaac98a5abcbb7a9be5b3777d978a6ce990 Mon Sep 17 00:00:00 2001 From: peter-rich Date: Tue, 2 Aug 2022 01:54:37 -0700 Subject: [PATCH] Parser2 (#4478) * 5 * 6 * 5 --- .linters/cpp/checkKeyword.py | 1 + src/parser/TraverseSentences.cpp | 21 +++++++ src/parser/TraverseSentences.h | 16 ++++- src/parser/parser.yy | 63 ++++++++++++------- src/parser/scanner.lex | 89 +++++++++++++------------- src/parser/test/CMakeLists.txt | 13 ++++ src/parser/test/ParserIso.cpp | 105 +++++++++++++++++++++++++++++++ 7 files changed, 239 insertions(+), 69 deletions(-) create mode 100644 src/parser/test/ParserIso.cpp diff --git a/.linters/cpp/checkKeyword.py b/.linters/cpp/checkKeyword.py index 4055255051d..9bc71f61754 100755 --- a/.linters/cpp/checkKeyword.py +++ b/.linters/cpp/checkKeyword.py @@ -13,6 +13,7 @@ reserved_key_words = [ 'KW_GO', + 'KW_ISOMOR', 'KW_AS', 'KW_TO', 'KW_OR', diff --git a/src/parser/TraverseSentences.cpp b/src/parser/TraverseSentences.cpp index 9114bed6124..489492a7dee 100644 --- a/src/parser/TraverseSentences.cpp +++ b/src/parser/TraverseSentences.cpp @@ -7,8 +7,29 @@ #include "common/base/Base.h" +#include + namespace nebula { + +IsomorSentence::~IsomorSentence() { + printf("Deconstuct. \n"); +} + +std::string IsomorSentence::toString() const { + std::string buf; + buf.reserve(256); + buf += "ISOMOR"; + buf += " "; + if (tags_->empty()) { + buf += "*"; + } else { + buf += tags_->toString(); + } + printf("Here is the string: %s \n", buf.c_str()); + return buf; +} + std::string GoSentence::toString() const { std::string buf; buf.reserve(256); diff --git a/src/parser/TraverseSentences.h b/src/parser/TraverseSentences.h index f4044e0aae1..0719061f3d0 100644 --- a/src/parser/TraverseSentences.h +++ b/src/parser/TraverseSentences.h @@ -9,9 +9,23 @@ #include "parser/EdgeKey.h" #include "parser/MutateSentences.h" #include "parser/Sentence.h" - namespace nebula { +class IsomorSentence final : public Sentence { + public: + explicit IsomorSentence(NameLabelList* tags) { + tags_.reset(tags); + } + const NameLabelList* tags() const { + return tags_->empty() ? nullptr : tags_.get(); + } + std::string toString() const override; + ~IsomorSentence(); + + private: + std::unique_ptr tags_; +}; + class GoSentence final : public Sentence { public: GoSentence(StepClause* step, diff --git a/src/parser/parser.yy b/src/parser/parser.yy index 05e96dfa713..090b3125307 100644 --- a/src/parser/parser.yy +++ b/src/parser/parser.yy @@ -12,7 +12,7 @@ %parse-param { std::string &errmsg } %parse-param { nebula::Sentence** sentences } %parse-param { nebula::graph::QueryContext* qctx } - + %code requires { #include #include @@ -173,7 +173,8 @@ using namespace nebula; %token KW_BOOL KW_INT8 KW_INT16 KW_INT32 KW_INT64 KW_INT KW_FLOAT KW_DOUBLE %token KW_STRING KW_FIXED_STRING KW_TIMESTAMP KW_DATE KW_TIME KW_DATETIME KW_DURATION %token KW_GO KW_AS KW_TO KW_USE KW_SET KW_FROM KW_WHERE KW_ALTER -%token KW_MATCH KW_INSERT KW_VALUE KW_VALUES KW_YIELD KW_RETURN KW_CREATE KW_VERTEX KW_VERTICES KW_IGNORE_EXISTED_INDEX +%token KW_MATCH KW_INSERT KW_VALUE KW_VALUES KW_YIELD KW_RETURN KW_CREATE KW_VERTEX KW_VERTICES KW_IGNORE_EXISTED_INDEX +%token KW_ISOMOR %token KW_EDGE KW_EDGES KW_STEPS KW_OVER KW_UPTO KW_REVERSELY KW_SPACE KW_DELETE KW_FIND %token KW_TAG KW_TAGS KW_UNION KW_INTERSECT KW_MINUS %token KW_NO KW_OVERWRITE KW_IN KW_DESCRIBE KW_DESC KW_SHOW KW_HOST KW_HOSTS KW_PART KW_PARTS KW_ADD @@ -391,6 +392,7 @@ using namespace nebula; %type download_sentence ingest_sentence %type traverse_sentence +%type isomor_sentence %type go_sentence match_sentence lookup_sentence find_path_sentence get_subgraph_sentence %type group_by_sentence order_by_sentence limit_sentence %type fetch_sentence fetch_vertices_sentence fetch_edges_sentence @@ -1390,11 +1392,20 @@ truncate_clause go_sentence : KW_GO step_clause from_clause over_clause where_clause yield_clause truncate_clause { - auto go = new GoSentence($2, $3, $4, $5, $7); - go->setYieldClause($6); - $$ = go; + auto go = new GoSentence($2, $3, $4, $5, $7); + go->setYieldClause($6); + $$ = go; } ; + + + isomor_sentence + : KW_ISOMOR name_label_list { + auto isomor = new IsomorSentence($2); + $$ = isomor; + } + ; + step_clause : %empty { $$ = new StepClause(); } @@ -1406,19 +1417,19 @@ step_clause } ; -from_clause - : KW_FROM vid_list { - $$ = new FromClause($2); - } - | KW_FROM vid_ref_expression { - if(graph::ExpressionUtils::findAny($2,{Expression::Kind::kVar})) { - throw nebula::GraphParser::syntax_error(@2, "Parameter is not supported in from clause"); - } - $$ = new FromClause($2); - } - ; +from_clause + : KW_FROM vid_list { + $$ = new FromClause($2); + } + | KW_FROM vid_ref_expression { + if(graph::ExpressionUtils::findAny($2,{Expression::Kind::kVar})) { + throw nebula::GraphParser::syntax_error(@2, "Parameter is not supported in from clause"); + } + $$ = new FromClause($2); + } + ; -vid_list +vid_list : vid { $$ = new VertexIDList(); $$->add($1); @@ -1455,9 +1466,9 @@ vid throw nebula::GraphParser::syntax_error(@1, "Variable is not supported in vid"); } } - ; - -unary_integer + ; + +unary_integer : PLUS legal_integer { $$ = $2; } @@ -1468,7 +1479,7 @@ unary_integer $$ = $1; } ; - + vid_ref_expression : input_prop_expression { $$ = $1; @@ -1480,7 +1491,7 @@ vid_ref_expression $$ = $1; } ; - + over_edge : name_label { $$ = new OverEdge($1); @@ -1691,6 +1702,9 @@ match_clause } ; + + + reading_clause : unwind_clause { $$ = $1; @@ -1782,13 +1796,13 @@ match_path $$ = $3; $$->setAlias($1); } - ; + ; match_path_list : match_path { $$ = new MatchPathList($1); } - | match_path_list COMMA match_path { + | match_path_list COMMA match_path { $$ = $1; $$->add($3); } @@ -2922,6 +2936,7 @@ desc_zone_sentence traverse_sentence : L_PAREN set_sentence R_PAREN { $$ = $2; } | go_sentence { $$ = $1; } + | isomor_sentence { $$ = $1; } | lookup_sentence { $$ = $1; } | group_by_sentence { $$ = $1; } | order_by_sentence { $$ = $1; } diff --git a/src/parser/scanner.lex b/src/parser/scanner.lex index 2e9f794df92..e1a26f502c9 100644 --- a/src/parser/scanner.lex +++ b/src/parser/scanner.lex @@ -94,50 +94,51 @@ LABEL_FULL_WIDTH {CN_EN_FULL_WIDTH}{CN_EN_NUM_FULL_WIDTH}* "FROM" { return TokenType::KW_FROM; } "WHERE" { return TokenType::KW_WHERE; } "MATCH" { return TokenType::KW_MATCH; } -"INSERT" { return TokenType::KW_INSERT; } -"YIELD" { return TokenType::KW_YIELD; } -"RETURN" { return TokenType::KW_RETURN; } -"DESCRIBE" { return TokenType::KW_DESCRIBE; } -"DESC" { return TokenType::KW_DESC; } -"VERTEX" { return TokenType::KW_VERTEX; } -"VERTICES" { return TokenType::KW_VERTICES; } -"EDGE" { return TokenType::KW_EDGE; } -"EDGES" { return TokenType::KW_EDGES; } -"UPDATE" { return TokenType::KW_UPDATE; } -"UPSERT" { return TokenType::KW_UPSERT; } -"WHEN" { return TokenType::KW_WHEN; } -"DELETE" { return TokenType::KW_DELETE; } -"FIND" { return TokenType::KW_FIND; } -"PATH" { return TokenType::KW_PATH; } -"LOOKUP" { return TokenType::KW_LOOKUP; } -"ALTER" { return TokenType::KW_ALTER; } -"STEPS" { return TokenType::KW_STEPS; } -"STEP" { return TokenType::KW_STEPS; } -"OVER" { return TokenType::KW_OVER; } -"UPTO" { return TokenType::KW_UPTO; } -"REVERSELY" { return TokenType::KW_REVERSELY; } -"INDEX" { return TokenType::KW_INDEX; } -"INDEXES" { return TokenType::KW_INDEXES; } -"REBUILD" { return TokenType::KW_REBUILD; } -"BOOL" { return TokenType::KW_BOOL; } -"INT8" { return TokenType::KW_INT8; } -"INT16" { return TokenType::KW_INT16; } -"INT32" { return TokenType::KW_INT32; } -"INT64" { return TokenType::KW_INT64; } -"INT" { return TokenType::KW_INT; } -"FLOAT" { return TokenType::KW_FLOAT; } -"DOUBLE" { return TokenType::KW_DOUBLE; } -"STRING" { return TokenType::KW_STRING; } -"FIXED_STRING" { return TokenType::KW_FIXED_STRING; } -"TIMESTAMP" { return TokenType::KW_TIMESTAMP; } -"DATE" { return TokenType::KW_DATE; } -"TIME" { return TokenType::KW_TIME; } -"DATETIME" { return TokenType::KW_DATETIME; } -"TAG" { return TokenType::KW_TAG; } -"TAGS" { return TokenType::KW_TAGS; } -"UNION" { return TokenType::KW_UNION; } -"INTERSECT" { return TokenType::KW_INTERSECT; } -"MINUS" { return TokenType::KW_MINUS; } +"ISOMOR" { return TokenType::KW_ISOMOR; } +"INSERT" { return TokenType::KW_INSERT; } +"YIELD" { return TokenType::KW_YIELD; } +"RETURN" { return TokenType::KW_RETURN; } +"DESCRIBE" { return TokenType::KW_DESCRIBE; } +"DESC" { return TokenType::KW_DESC; } +"VERTEX" { return TokenType::KW_VERTEX; } +"VERTICES" { return TokenType::KW_VERTICES; } +"EDGE" { return TokenType::KW_EDGE; } +"EDGES" { return TokenType::KW_EDGES; } +"UPDATE" { return TokenType::KW_UPDATE; } +"UPSERT" { return TokenType::KW_UPSERT; } +"WHEN" { return TokenType::KW_WHEN; } +"DELETE" { return TokenType::KW_DELETE; } +"FIND" { return TokenType::KW_FIND; } +"PATH" { return TokenType::KW_PATH; } +"LOOKUP" { return TokenType::KW_LOOKUP; } +"ALTER" { return TokenType::KW_ALTER; } +"STEPS" { return TokenType::KW_STEPS; } +"STEP" { return TokenType::KW_STEPS; } +"OVER" { return TokenType::KW_OVER; } +"UPTO" { return TokenType::KW_UPTO; } +"REVERSELY" { return TokenType::KW_REVERSELY; } +"INDEX" { return TokenType::KW_INDEX; } +"INDEXES" { return TokenType::KW_INDEXES; } +"REBUILD" { return TokenType::KW_REBUILD; } +"BOOL" { return TokenType::KW_BOOL; } +"INT8" { return TokenType::KW_INT8; } +"INT16" { return TokenType::KW_INT16; } +"INT32" { return TokenType::KW_INT32; } +"INT64" { return TokenType::KW_INT64; } +"INT" { return TokenType::KW_INT; } +"FLOAT" { return TokenType::KW_FLOAT; } +"DOUBLE" { return TokenType::KW_DOUBLE; } +"STRING" { return TokenType::KW_STRING; } +"FIXED_STRING" { return TokenType::KW_FIXED_STRING; } +"TIMESTAMP" { return TokenType::KW_TIMESTAMP; } +"DATE" { return TokenType::KW_DATE; } +"TIME" { return TokenType::KW_TIME; } +"DATETIME" { return TokenType::KW_DATETIME; } +"TAG" { return TokenType::KW_TAG; } +"TAGS" { return TokenType::KW_TAGS; } +"UNION" { return TokenType::KW_UNION; } +"INTERSECT" { return TokenType::KW_INTERSECT; } +"MINUS" { return TokenType::KW_MINUS; } "NO" { return TokenType::KW_NO; } "OVERWRITE" { return TokenType::KW_OVERWRITE; } "SHOW" { return TokenType::KW_SHOW; } diff --git a/src/parser/test/CMakeLists.txt b/src/parser/test/CMakeLists.txt index e244923d29e..3b18ec7b56e 100644 --- a/src/parser/test/CMakeLists.txt +++ b/src/parser/test/CMakeLists.txt @@ -65,6 +65,19 @@ nebula_add_test( LIBRARIES gtest gtest_main ${THRIFT_LIBRARIES} ${PROXYGEN_LIBRARIES} ) +nebula_add_test( + NAME parser_iso + SOURCES ParserIso.cpp + OBJECTS + ${PARSER_TEST_LIBS} + LIBRARIES + gtest gtest_main + ${THRIFT_LIBRARIES} + ${PROXYGEN_LIBRARIES} + ${Ldap_LIBRARIES} + ${ROCKSDB_LIBRARIES} +) + nebula_add_test( NAME scanner_test SOURCES ScannerTest.cpp diff --git a/src/parser/test/ParserIso.cpp b/src/parser/test/ParserIso.cpp new file mode 100644 index 00000000000..29ed0361236 --- /dev/null +++ b/src/parser/test/ParserIso.cpp @@ -0,0 +1,105 @@ +/* Copyright (c) 2018 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License. + */ + +#include + +#include "common/base/Base.h" +#include "graph/util/AstUtils.h" +#include "parser/GQLParser.h" + +namespace nebula { + +using graph::QueryContext; +class ParserIso : public ::testing::Test { + public: + void SetUp() override { + qctx_ = std::make_unique(); + } + void TearDown() override { + qctx_.reset(); + } + + protected: + StatusOr> parse(const std::string& query) { + auto* qctx = qctx_.get(); + GQLParser parser(qctx); + auto result = parser.parse(query); + NG_RETURN_IF_ERROR(result); + NG_RETURN_IF_ERROR(graph::AstUtils::reprAstCheck(*result.value(), qctx)); + return result; + } + + protected: + std::unique_ptr qctx_; +}; + +TEST_F(ParserIso, TestSchemaCreation) { + // All type + { + std::string query = + "ISOMOR A.graph,b.graph"; + auto result = parse(query); + // ASSERT_TRUE(result.ok()) << result.status(); + } +} +/* +TEST_F(ParserIso, Set) { + { + std::string query = + "GO FROM \"1\" OVER friend INTERSECT " + "GO FROM \"2\" OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } + { + std::string query = + "GO FROM \"1\" OVER friend UNION " + "GO FROM \"2\" OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } + { + std::string query = + "GO FROM \"1\" OVER friend MINUS " + "GO FROM \"2\" OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } + { + std::string query = + "GO FROM \"1\" OVER friend MINUS " + "GO FROM \"2\" OVER friend UNION " + "GO FROM \"2\" OVER friend INTERSECT " + "GO FROM \"3\" OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } + { + std::string query = + "(GO FROM \"1\" OVER friend | " + "GO FROM \"2\" OVER friend) UNION " + "GO FROM \"3\" OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } + { + // pipe have priority over set + std::string query = + "GO FROM \"1\" OVER friend | " + "GO FROM \"2\" OVER friend UNION " + "GO FROM \"3\" OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } + { + std::string query = + "(GO FROM \"1\" OVER friend YIELD friend._dst AS id UNION " + "GO FROM \"2\" OVER friend YIELD friend._dst AS id) | " + "GO FROM $-.id OVER friend"; + auto result = parse(query); + ASSERT_TRUE(result.ok()) << result.status(); + } +} */ +} // namespace nebula