Handle zero increment in counters

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2208
This commit is contained in:
Matej Ferencevic 2019-07-16 10:35:23 +02:00
parent e7f897984f
commit d5106c7233
2 changed files with 8 additions and 1 deletions

View File

@ -702,7 +702,12 @@ TypedValue Counter(TypedValue *args, int64_t nargs,
"Third argument of 'counter' must be an integer."); "Third argument of 'counter' must be an integer.");
int64_t step = 1; int64_t step = 1;
if (nargs == 3) step = args[2].ValueInt(); if (nargs == 3) {
step = args[2].ValueInt();
if (step == 0)
throw QueryRuntimeException(
"Third argument of 'counter' must not be zero.");
}
auto [it, inserted] = auto [it, inserted] =
context.counters.emplace(args[0].ValueString(), args[1].ValueInt()); context.counters.emplace(args[0].ValueString(), args[1].ValueInt());

View File

@ -1494,6 +1494,8 @@ TEST_F(FunctionTest, Counter) {
EXPECT_EQ(EvaluateFunction("COUNTER", "c5", 0, -5).ValueInt(), 0); EXPECT_EQ(EvaluateFunction("COUNTER", "c5", 0, -5).ValueInt(), 0);
EXPECT_EQ(EvaluateFunction("COUNTER", "c5", 0, -5).ValueInt(), -5); EXPECT_EQ(EvaluateFunction("COUNTER", "c5", 0, -5).ValueInt(), -5);
EXPECT_EQ(EvaluateFunction("COUNTER", "c5", 0, -5).ValueInt(), -10); EXPECT_EQ(EvaluateFunction("COUNTER", "c5", 0, -5).ValueInt(), -10);
EXPECT_THROW(EvaluateFunction("COUNTER", "c6", 0, 0), QueryRuntimeException);
} }
TEST_F(FunctionTest, Id) { TEST_F(FunctionTest, Id) {