Skip to content

Commit

Permalink
including primary keys when sinking vertex
Browse files Browse the repository at this point in the history
Committed-by: [email protected] from Dev container
  • Loading branch information
zhanglei1949 committed Jan 21, 2025
1 parent 17ceda1 commit 963680b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
13 changes: 12 additions & 1 deletion flex/engines/graph_db/runtime/common/rt_any.cc
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,19 @@ void sink_vertex(const GraphReadInterface& graph, const VertexRecord& vertex,
results::Vertex* v) {
v->mutable_label()->set_id(vertex.label_);
v->set_id(encode_unique_vertex_id(vertex.label_, vertex.vid_));
// TODO: add properties
const auto& names = graph.schema().get_vertex_property_names(vertex.label_);
// Add primary keys first, since primary keys are also the properties of a
// vertex
auto& primary_key = graph.schema().get_vertex_primary_key(vertex.label_);
if (primary_key.size() > 1) {
LOG(ERROR) << "Currently only support single primary key";
}
auto pk_name = std::get<1>(primary_key[0]);
auto pk_prop = v->add_properties();
pk_prop->mutable_key()->set_name(pk_name);
sink_any(graph.GetVertexId(vertex.label_, vertex.vid_),
pk_prop->mutable_value());

for (size_t i = 0; i < names.size(); ++i) {
auto prop = v->add_properties();
prop->mutable_key()->set_name(names[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,21 @@ public static QueryContext get_simple_match_query_18_test() {
List<String> expected = Arrays.asList("Record<{$f0: 39783}>");
return new QueryContext(query, expected);
}

public static QueryContext get_simple_match_query_19_test() {
String query = "MATCH(a: PLACE) return a ORDER BY a.id limit 5;";
List<String> expected =
Arrays.asList(
"[a: {name=India, id=0, type=country,"
+ " url=http://dbpedia.org/resource/India}]",
"[a: {name=China, id=1, type=country,"
+ " url=http://dbpedia.org/resource/China}]",
"[a: {name=Angola, id=2, type=country,"
+ " url=http://dbpedia.org/resource/Angola}]",
"[a: {name=Austria, id=3, type=country,"
+ " url=http://dbpedia.org/resource/Austria}]",
"[a: {name=Azerbaijan, id=4, type=country,"
+ " url=http://dbpedia.org/resource/Azerbaijan}]");
return new QueryContext(query, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Value;
import org.neo4j.driver.internal.types.InternalTypeSystem;
import org.neo4j.driver.types.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SimpleMatchTest {

private static final Logger logger = LoggerFactory.getLogger(SimpleMatchTest.class);

private static Session session;

@BeforeClass
Expand Down Expand Up @@ -169,6 +181,37 @@ public void run_simple_match_18_test() {
Assert.assertEquals(testQuery.getExpectedResult().toString(), result.list().toString());
}

@Test
public void run_simple_match_19_test() {
QueryContext testQuery = SimpleMatchQueries.get_simple_match_query_19_test();
Result result = session.run(testQuery.getQuery());
List<Record> records = result.list();
List<String> properties = new ArrayList<>();
records.forEach(
record -> {
properties.add(fetchAllProperties(record));
});
logger.info(properties.toString());
Assert.assertEquals(testQuery.getExpectedResult().toString(), properties.toString());
}

private static String fetchAllProperties(Record record) {
List<String> properties = new ArrayList<>();
record.keys()
.forEach(
key -> {
Value v = record.get(key);
if (v.hasType(InternalTypeSystem.TYPE_SYSTEM.NODE())) {
Node node = v.asNode();
Map<String, Object> nodeProperties = node.asMap();
properties.add(key + ": " + nodeProperties.toString());
} else {
properties.add(key + ": " + record.get(key).toString());
}
});
return properties.toString();
}

@AfterClass
public static void afterClass() {
if (session != null) {
Expand Down

0 comments on commit 963680b

Please sign in to comment.