Throw on key appearing twice in map literal

Reviewers: teon.banek

Reviewed By: teon.banek

Differential Revision: https://phabricator.memgraph.io/D388
This commit is contained in:
Mislav Bradac 2017-05-19 16:15:30 +02:00
parent aa6601407d
commit 6a027ea3b2
2 changed files with 10 additions and 3 deletions

View File

@ -268,9 +268,12 @@ antlrcpp::Any CypherMainVisitor::visitProperties(
antlrcpp::Any CypherMainVisitor::visitMapLiteral(
CypherParser::MapLiteralContext *ctx) {
std::map<GraphDbTypes::Property, Expression *> 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<int>(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;
}

View File

@ -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_;