From 1bc93b64f40d3daa364cfa7e53479d3768d49d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= Date: Mon, 13 Feb 2023 23:32:33 +0100 Subject: [PATCH] Make it possible to compile v2 and v3 interpreter into a single binary --- src/expr/ast/cypher_main_visitor.hpp | 8 ++++---- src/parser/CMakeLists.txt | 2 +- src/parser/opencypher/parser.hpp | 19 ++++++++----------- src/query/CMakeLists.txt | 18 ++++++++++-------- src/query/v2/cypher_query_interpreter.cpp | 8 ++++---- src/query/v2/cypher_query_interpreter.hpp | 8 ++++---- src/query/v2/plan/rewrite/index_lookup.cpp | 4 ++-- src/query/v2/plan/rewrite/index_lookup.hpp | 6 +++--- src/query/v2/plan/variable_start_planner.cpp | 4 ++-- src/query/v2/plan/variable_start_planner.hpp | 6 +++--- 10 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/expr/ast/cypher_main_visitor.hpp b/src/expr/ast/cypher_main_visitor.hpp index 17d2167b2..bbe5d2d06 100644 --- a/src/expr/ast/cypher_main_visitor.hpp +++ b/src/expr/ast/cypher_main_visitor.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -51,7 +51,7 @@ constexpr char kId[] = "ID"; namespace MG_INJECTED_NAMESPACE_NAME { namespace detail { -using antlropencypher::MemgraphCypher; +using antlropencypher::v2::MemgraphCypher; template std::optional> VisitMemoryLimit(MemgraphCypher::MemoryLimitContext *memory_limit_ctx, @@ -211,13 +211,13 @@ inline std::string_view ToString(const PulsarConfigKey key) { } } // namespace detail -using antlropencypher::MemgraphCypher; +using antlropencypher::v2::MemgraphCypher; struct ParsingContext { bool is_query_cached = false; }; -class CypherMainVisitor : public antlropencypher::MemgraphCypherBaseVisitor { +class CypherMainVisitor : public antlropencypher::v2::MemgraphCypherBaseVisitor { public: explicit CypherMainVisitor(ParsingContext context, AstStorage *storage) : context_(context), storage_(storage) {} diff --git a/src/parser/CMakeLists.txt b/src/parser/CMakeLists.txt index 7575b0529..b7ae43178 100644 --- a/src/parser/CMakeLists.txt +++ b/src/parser/CMakeLists.txt @@ -23,7 +23,7 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E make_directory ${opencypher_generated} COMMAND java -jar ${CMAKE_SOURCE_DIR}/libs/antlr-4.10.1-complete.jar - -Dlanguage=Cpp -visitor -package antlropencypher + -Dlanguage=Cpp -visitor -package antlropencypher::v2 -o ${opencypher_generated} ${opencypher_lexer_grammar} ${opencypher_parser_grammar} WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" diff --git a/src/parser/opencypher/parser.hpp b/src/parser/opencypher/parser.hpp index 9a57bc65b..25fdabf4b 100644 --- a/src/parser/opencypher/parser.hpp +++ b/src/parser/opencypher/parser.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -14,10 +14,10 @@ #include #include "antlr4-runtime.h" -#include "utils/exceptions.hpp" #include "parser/opencypher/generated/MemgraphCypher.h" #include "parser/opencypher/generated/MemgraphCypherLexer.h" #include "utils/concepts.hpp" +#include "utils/exceptions.hpp" namespace memgraph::frontend::opencypher { @@ -32,11 +32,9 @@ class SyntaxException : public utils::BasicException { * This thing must me a class since parser.cypher() returns pointer and there is * no way for us to get ownership over the object. */ -enum class ParserOpTag : uint8_t { - CYPHER, EXPRESSION -}; +enum class ParserOpTag : uint8_t { CYPHER, EXPRESSION }; -template +template class Parser { public: /** @@ -46,10 +44,9 @@ class Parser { Parser(const std::string query) : query_(std::move(query)) { parser_.removeErrorListeners(); parser_.addErrorListener(&error_listener_); - if constexpr(Tag == ParserOpTag::CYPHER) { + if constexpr (Tag == ParserOpTag::CYPHER) { tree_ = parser_.cypher(); - } - else { + } else { tree_ = parser_.expression(); } if (parser_.getNumberOfSyntaxErrors()) { @@ -75,11 +72,11 @@ class Parser { FirstMessageErrorListener error_listener_; std::string query_; antlr4::ANTLRInputStream input_{query_}; - antlropencypher::MemgraphCypherLexer lexer_{&input_}; + antlropencypher::v2::MemgraphCypherLexer lexer_{&input_}; antlr4::CommonTokenStream tokens_{&lexer_}; // generate ast - antlropencypher::MemgraphCypher parser_{&tokens_}; + antlropencypher::v2::MemgraphCypher parser_{&tokens_}; antlr4::tree::ParseTree *tree_ = nullptr; }; } // namespace memgraph::frontend::opencypher diff --git a/src/query/CMakeLists.txt b/src/query/CMakeLists.txt index 0303f2fa5..d5545cd95 100644 --- a/src/query/CMakeLists.txt +++ b/src/query/CMakeLists.txt @@ -48,18 +48,20 @@ add_dependencies(mg-query generate_lcp_query) target_include_directories(mg-query PUBLIC ${CMAKE_SOURCE_DIR}/include) target_link_libraries(mg-query dl cppitertools Boost::headers) target_link_libraries(mg-query mg-integrations-pulsar mg-integrations-kafka mg-storage-v2 mg-license mg-utils mg-kvstore mg-memory) + if(NOT "${MG_PYTHON_PATH}" STREQUAL "") set(Python3_ROOT_DIR "${MG_PYTHON_PATH}") endif() + if("${MG_PYTHON_VERSION}" STREQUAL "") find_package(Python3 3.5 REQUIRED COMPONENTS Development) else() find_package(Python3 "${MG_PYTHON_VERSION}" EXACT REQUIRED COMPONENTS Development) endif() + target_link_libraries(mg-query Python3::Python) # Generate Antlr openCypher parser - set(opencypher_frontend ${CMAKE_CURRENT_SOURCE_DIR}/frontend/opencypher) set(opencypher_generated ${opencypher_frontend}/generated) set(opencypher_lexer_grammar ${opencypher_frontend}/grammar/MemgraphCypherLexer.g4) @@ -82,15 +84,15 @@ add_custom_command( OUTPUT ${antlr_opencypher_generated_src} ${antlr_opencypher_generated_include} COMMAND ${CMAKE_COMMAND} -E make_directory ${opencypher_generated} COMMAND - java -jar ${CMAKE_SOURCE_DIR}/libs/antlr-4.10.1-complete.jar - -Dlanguage=Cpp -visitor -package antlropencypher - -o ${opencypher_generated} - ${opencypher_lexer_grammar} ${opencypher_parser_grammar} + java -jar ${CMAKE_SOURCE_DIR}/libs/antlr-4.10.1-complete.jar + -Dlanguage=Cpp -visitor -package antlropencypher + -o ${opencypher_generated} + ${opencypher_lexer_grammar} ${opencypher_parser_grammar} WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" DEPENDS - ${opencypher_lexer_grammar} ${opencypher_parser_grammar} - ${opencypher_frontend}/grammar/CypherLexer.g4 - ${opencypher_frontend}/grammar/Cypher.g4) + ${opencypher_lexer_grammar} ${opencypher_parser_grammar} + ${opencypher_frontend}/grammar/CypherLexer.g4 + ${opencypher_frontend}/grammar/Cypher.g4) add_custom_target(generate_opencypher_parser DEPENDS ${antlr_opencypher_generated_src} ${antlr_opencypher_generated_include}) diff --git a/src/query/v2/cypher_query_interpreter.cpp b/src/query/v2/cypher_query_interpreter.cpp index f3f8e48d7..6ac6b36fa 100644 --- a/src/query/v2/cypher_query_interpreter.cpp +++ b/src/query/v2/cypher_query_interpreter.cpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -14,9 +14,9 @@ #include "query/v2/request_router.hpp" // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables) -DEFINE_HIDDEN_bool(query_cost_planner, true, "Use the cost-estimating query planner."); +DEFINE_HIDDEN_bool(query_v2_cost_planner, true, "Use the cost-estimating query planner."); // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables) -DEFINE_VALIDATED_int32(query_plan_cache_ttl, 60, "Time to live for cached query plans, in seconds.", +DEFINE_VALIDATED_int32(query_v2_plan_cache_ttl, 60, "Time to live for cached query plans, in seconds.", FLAG_IN_RANGE(0, std::numeric_limits::max())); namespace memgraph::query::v2 { @@ -123,7 +123,7 @@ std::unique_ptr MakeLogicalPlan(AstStorage ast_storage, CypherQuery auto vertex_counts = plan::MakeVertexCountCache(request_router); auto symbol_table = expr::MakeSymbolTable(query, predefined_identifiers); auto planning_context = plan::MakePlanningContext(&ast_storage, &symbol_table, query, &vertex_counts); - auto [root, cost] = plan::MakeLogicalPlan(&planning_context, parameters, FLAGS_query_cost_planner); + auto [root, cost] = plan::MakeLogicalPlan(&planning_context, parameters, FLAGS_query_v2_cost_planner); return std::make_unique(std::move(root), cost, std::move(ast_storage), std::move(symbol_table)); } diff --git a/src/query/v2/cypher_query_interpreter.hpp b/src/query/v2/cypher_query_interpreter.hpp index 688e52fed..18505820f 100644 --- a/src/query/v2/cypher_query_interpreter.hpp +++ b/src/query/v2/cypher_query_interpreter.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -22,9 +22,9 @@ #include "utils/timer.hpp" // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables) -DECLARE_bool(query_cost_planner); +DECLARE_bool(query_v2_cost_planner); // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables) -DECLARE_int32(query_plan_cache_ttl); +DECLARE_int32(query_v2_plan_cache_ttl); namespace memgraph::query::v2 { @@ -58,7 +58,7 @@ class CachedPlan { bool IsExpired() const { // NOLINTNEXTLINE (modernize-use-nullptr) - return cache_timer_.Elapsed() > std::chrono::seconds(FLAGS_query_plan_cache_ttl); + return cache_timer_.Elapsed() > std::chrono::seconds(FLAGS_query_v2_plan_cache_ttl); }; private: diff --git a/src/query/v2/plan/rewrite/index_lookup.cpp b/src/query/v2/plan/rewrite/index_lookup.cpp index 795f15fa4..5141e92df 100644 --- a/src/query/v2/plan/rewrite/index_lookup.cpp +++ b/src/query/v2/plan/rewrite/index_lookup.cpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -13,7 +13,7 @@ #include "utils/flag_validation.hpp" -DEFINE_VALIDATED_HIDDEN_int64(query_vertex_count_to_expand_existing, 10, +DEFINE_VALIDATED_HIDDEN_int64(query_v2_vertex_count_to_expand_existing, 10, "Maximum count of indexed vertices which provoke " "indexed lookup and then expand to existing, instead of " "a regular expand. Default is 10, to turn off use -1.", diff --git a/src/query/v2/plan/rewrite/index_lookup.hpp b/src/query/v2/plan/rewrite/index_lookup.hpp index 0b9b9cb97..c8d2b3720 100644 --- a/src/query/v2/plan/rewrite/index_lookup.hpp +++ b/src/query/v2/plan/rewrite/index_lookup.hpp @@ -30,7 +30,7 @@ #include "query/v2/plan/preprocess.hpp" #include "storage/v3/id_types.hpp" -DECLARE_int64(query_vertex_count_to_expand_existing); +DECLARE_int64(query_v2_vertex_count_to_expand_existing); namespace memgraph::query::v2::plan { @@ -100,7 +100,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { return true; } ScanAll dst_scan(expand.input(), expand.common_.node_symbol, expand.view_); - auto indexed_scan = GenScanByIndex(dst_scan, FLAGS_query_vertex_count_to_expand_existing); + auto indexed_scan = GenScanByIndex(dst_scan, FLAGS_query_v2_vertex_count_to_expand_existing); if (indexed_scan) { expand.set_input(std::move(indexed_scan)); expand.common_.existing_node = true; @@ -129,7 +129,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { // unconditionally creating an indexed scan. indexed_scan = GenScanByIndex(dst_scan); } else { - indexed_scan = GenScanByIndex(dst_scan, FLAGS_query_vertex_count_to_expand_existing); + indexed_scan = GenScanByIndex(dst_scan, FLAGS_query_v2_vertex_count_to_expand_existing); } if (indexed_scan) { expand.set_input(std::move(indexed_scan)); diff --git a/src/query/v2/plan/variable_start_planner.cpp b/src/query/v2/plan/variable_start_planner.cpp index b6d15f73d..6fd6e0ad6 100644 --- a/src/query/v2/plan/variable_start_planner.cpp +++ b/src/query/v2/plan/variable_start_planner.cpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -17,7 +17,7 @@ #include "utils/flag_validation.hpp" #include "utils/logging.hpp" -DEFINE_VALIDATED_HIDDEN_uint64(query_max_plans, 1000U, "Maximum number of generated plans for a query.", +DEFINE_VALIDATED_HIDDEN_uint64(query_v2_max_plans, 1000U, "Maximum number of generated plans for a query.", FLAG_IN_RANGE(1, std::numeric_limits::max())); namespace memgraph::query::v2::plan::impl { diff --git a/src/query/v2/plan/variable_start_planner.hpp b/src/query/v2/plan/variable_start_planner.hpp index 27722b6b2..abb7f269f 100644 --- a/src/query/v2/plan/variable_start_planner.hpp +++ b/src/query/v2/plan/variable_start_planner.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -18,7 +18,7 @@ #include "query/v2/plan/rule_based_planner.hpp" -DECLARE_uint64(query_max_plans); +DECLARE_uint64(query_v2_max_plans); namespace memgraph::query::v2::plan { @@ -310,7 +310,7 @@ class VariableStartPlanner { for (const auto &query_part : query_parts) { alternative_query_parts.emplace_back(impl::VaryQueryPartMatching(query_part, symbol_table)); } - return iter::slice(MakeCartesianProduct(std::move(alternative_query_parts)), 0UL, FLAGS_query_max_plans); + return iter::slice(MakeCartesianProduct(std::move(alternative_query_parts)), 0UL, FLAGS_query_v2_max_plans); } public: