From 6a027ea3b294c02158b599f6145192799b29e26e Mon Sep 17 00:00:00 2001 From: Mislav Bradac Date: Fri, 19 May 2017 16:15:30 +0200 Subject: [PATCH] Throw on key appearing twice in map literal Reviewers: teon.banek Reviewed By: teon.banek Differential Revision: https://phabricator.memgraph.io/D388 --- src/query/frontend/ast/cypher_main_visitor.cpp | 9 ++++++--- tests/unit/cypher_main_visitor.cpp | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/query/frontend/ast/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp index 002860920..f5ab0c273 100644 --- a/src/query/frontend/ast/cypher_main_visitor.cpp +++ b/src/query/frontend/ast/cypher_main_visitor.cpp @@ -268,9 +268,12 @@ antlrcpp::Any CypherMainVisitor::visitProperties( antlrcpp::Any CypherMainVisitor::visitMapLiteral( CypherParser::MapLiteralContext *ctx) { std::map map; - for (int i = 0; i < (int)ctx->propertyKeyName().size(); ++i) { - map[ctx->propertyKeyName()[i]->accept(this)] = - ctx->expression()[i]->accept(this); + for (int i = 0; i < static_cast(ctx->propertyKeyName().size()); ++i) { + GraphDbTypes::Property key = ctx->propertyKeyName()[i]->accept(this); + Expression *value = ctx->expression()[i]->accept(this); + if (!map.insert({key, value}).second) { + throw SemanticException("Same key can't appear twice in map literal"); + } } return map; } diff --git a/tests/unit/cypher_main_visitor.cpp b/tests/unit/cypher_main_visitor.cpp index 443cdb387..733832614 100644 --- a/tests/unit/cypher_main_visitor.cpp +++ b/tests/unit/cypher_main_visitor.cpp @@ -674,6 +674,10 @@ TEST(CypherMainVisitorTest, NodePattern) { Pair(ast_generator.db_accessor_->property("b"), 10))); } +TEST(CypherMainVisitorTest, PropertyMapSameKeyAppearsTwice) { + EXPECT_THROW(AstGenerator("MATCH ({a : 1, a : 2})"), SemanticException); +} + TEST(CypherMainVisitorTest, NodePatternIdentifier) { AstGenerator ast_generator("MATCH (var) RETURN 1"); auto *query = ast_generator.query_;