Expose ID

Reviewers: buda, teon.banek

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1400
This commit is contained in:
Dominik Gleich 2018-05-23 10:16:54 +02:00
parent 2c49487eae
commit 63f90a453d
2 changed files with 34 additions and 0 deletions

View File

@ -622,6 +622,23 @@ TypedValue WorkerId(const std::vector<TypedValue> &args,
throw QueryRuntimeException("workerId argument must be a vertex or edge");
}
}
TypedValue Id(const std::vector<TypedValue> &args,
database::GraphDbAccessor &) {
if (args.size() != 1U) {
throw QueryRuntimeException("Id takes one argument");
}
auto &arg = args[0];
switch (arg.type()) {
case TypedValue::Type::Vertex:
return static_cast<int64_t>(arg.ValueVertex().gid());
case TypedValue::Type::Edge:
return static_cast<int64_t>(arg.ValueEdge().gid());
default:
throw QueryRuntimeException("Id argument must be a vertex or edge");
}
}
} // namespace
std::function<TypedValue(const std::vector<TypedValue> &,
@ -672,6 +689,7 @@ NameToFunction(const std::string &function_name) {
if (function_name == "COUNTERSET") return CounterSet;
if (function_name == "INDEXINFO") return IndexInfo;
if (function_name == "WORKERID") return WorkerId;
if (function_name == "ID") return Id;
return nullptr;
}
} // namespace query

View File

@ -1363,4 +1363,20 @@ TEST(ExpressionEvaluator, FunctionIndexInfo) {
EXPECT_THAT(info, testing::UnorderedElementsAre(":l1", ":l1(prop)"));
}
}
TEST(ExpressionEvaluator, FunctionUid) {
database::SingleNode db;
EXPECT_THROW(EvaluateFunction("ID", {}, db), QueryRuntimeException);
database::GraphDbAccessor dba(db);
auto va = dba.InsertVertex();
auto ea = dba.InsertEdge(va, va, dba.EdgeType("edge"));
EXPECT_EQ(EvaluateFunction("ID", {va}, db).Value<int64_t>(), 0);
EXPECT_EQ(EvaluateFunction("ID", {ea}, db).Value<int64_t>(), 0);
auto vb = dba.InsertVertex();
auto eb = dba.InsertEdge(vb, vb, dba.EdgeType("edge"));
EXPECT_EQ(EvaluateFunction("ID", {vb}, db).Value<int64_t>(), 1024);
EXPECT_EQ(EvaluateFunction("ID", {eb}, db).Value<int64_t>(), 1024);
}
} // namespace