diff --git a/src/query/frontend/ast/ast.hpp b/src/query/frontend/ast/ast.hpp index 323f57b96..cd68418f5 100644 --- a/src/query/frontend/ast/ast.hpp +++ b/src/query/frontend/ast/ast.hpp @@ -12,6 +12,21 @@ #include "query/typed_value.hpp" #include "utils/assert.hpp" +// Hash function for the key in pattern atom property maps. +namespace std { +template <> +struct hash> { + size_t operator()( + const std::pair &pair) const { + return string_hash(pair.first) ^ property_hash(pair.second); + }; + + private: + std::hash string_hash{}; + std::hash property_hash{}; +}; +} + namespace query { #define CLONE_BINARY_EXPRESSION \ @@ -644,14 +659,16 @@ class MapLiteral : public BaseLiteral { } // maps (property_name, property) to expressions - std::map, Expression *> + std::unordered_map, + Expression *> elements_; protected: MapLiteral(int uid) : BaseLiteral(uid) {} - MapLiteral(int uid, - const std::map, - Expression *> &elements) + MapLiteral( + int uid, + const std::unordered_map, + Expression *> &elements) : BaseLiteral(uid), elements_(elements) {} }; @@ -954,8 +971,8 @@ class NodeAtom : public PatternAtom { std::vector labels_; // maps (property_name, property) to an expression - // TODO: change to unordered_map - std::map, Expression *> + std::unordered_map, + Expression *> properties_; protected: @@ -1005,8 +1022,8 @@ class EdgeAtom : public PatternAtom { Direction direction_ = Direction::BOTH; std::vector edge_types_; // maps (property_name, property) to an expression - // TODO: change to unordered_map - std::map, Expression *> + std::unordered_map, + Expression *> properties_; bool has_range_ = false; Expression *lower_bound_ = nullptr; @@ -1584,4 +1601,4 @@ class CreateIndex : public Clause { #undef CLONE_BINARY_EXPRESSION #undef CLONE_UNARY_EXPRESSION -} +} // namespace query diff --git a/src/query/frontend/ast/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp index fa2e06ca1..3f55df8d4 100644 --- a/src/query/frontend/ast/cypher_main_visitor.cpp +++ b/src/query/frontend/ast/cypher_main_visitor.cpp @@ -5,9 +5,9 @@ #include #include #include -#include #include #include +#include #include #include @@ -273,11 +273,11 @@ antlrcpp::Any CypherMainVisitor::visitNodePattern( ctx->nodeLabels()->accept(this).as>(); } if (ctx->properties()) { - node->properties_ = - ctx->properties() - ->accept(this) - .as, - Expression *>>(); + node->properties_ = ctx->properties() + ->accept(this) + .as, + Expression *>>(); } return node; } @@ -306,7 +306,9 @@ antlrcpp::Any CypherMainVisitor::visitProperties( antlrcpp::Any CypherMainVisitor::visitMapLiteral( CypherParser::MapLiteralContext *ctx) { - std::map, Expression *> map; + std::unordered_map, + Expression *> + map; for (int i = 0; i < static_cast(ctx->propertyKeyName().size()); ++i) { std::pair key = ctx->propertyKeyName()[i]->accept(this); @@ -509,8 +511,9 @@ antlrcpp::Any CypherMainVisitor::visitRelationshipPattern( edge->properties_ = properties[0] ->accept(this) - .as, - Expression *>>(); + .as, + Expression *>>(); break; } default: @@ -887,8 +890,9 @@ antlrcpp::Any CypherMainVisitor::visitLiteral( return static_cast(storage_.Create( ctx->mapLiteral() ->accept(this) - .as, - Expression *>>())); + .as, + Expression *>>())); } return visitChildren(ctx); } diff --git a/tests/unit/query_common.hpp b/tests/unit/query_common.hpp index 55fbd3dc3..13091ca50 100644 --- a/tests/unit/query_common.hpp +++ b/tests/unit/query_common.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -477,10 +478,10 @@ auto GetMerge(AstTreeStorage &storage, Pattern *pattern, OnMatch on_match, #define LIST(...) \ storage.Create( \ std::vector{__VA_ARGS__}) -#define MAP(...) \ - storage.Create( \ - std::map, \ - query::Expression *>{__VA_ARGS__}) +#define MAP(...) \ + storage.Create( \ + std::unordered_map, \ + query::Expression *>{__VA_ARGS__}) #define PROPERTY_PAIR(property_name) \ std::make_pair(property_name, dba->Property(property_name)) #define PROPERTY_LOOKUP(...) \ diff --git a/tests/unit/query_expression_evaluator.cpp b/tests/unit/query_expression_evaluator.cpp index 118e88d16..659856b6b 100644 --- a/tests/unit/query_expression_evaluator.cpp +++ b/tests/unit/query_expression_evaluator.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "gmock/gmock.h" @@ -374,7 +375,8 @@ TEST(ExpressionEvaluator, MapIndexing) { Dbms dbms; auto dba = dbms.active(); auto *map_literal = storage.Create( - std::map, Expression *>{ + std::unordered_map, + Expression *>{ {PROPERTY_PAIR("a"), storage.Create(1)}, {PROPERTY_PAIR("b"), storage.Create(2)}, {PROPERTY_PAIR("c"), storage.Create(3)}});