I have a graph as follows
---- Graph Social
Vertex Types:
- VERTEX Person(name STRING primary key, age UINT, gender STRING) WITH STATS="NONE"
Edge Types:
- DIRECTED EDGE Friendship(FROM Person, TO Person, closeness DOUBLE)
Graphs:
- Graph Social(Person:v, Friendship:e)
I want to get a sorted list of edges, sorted by the closeness value. The following does not work
SELECT e FROM (s:Person)-[e:Friendship]-(t:Person) ORDER BY e.closeness ASC
The error shown is -
Semantic Check Error in query (SEM-44): line [0, 0]
The provided heap sort key 'closeness' is not a valid field name for tuple:
TupTy_4_Result_Table
I can get a list of sorted nodes using a similar query. So how do I do this for edges?
This worked
CREATE QUERY sort_edge_attribute_value() FOR GRAPH workflow_graph SYNTAX v3 {
TYPEDEF TUPLE <src_node_id INT, dst_node_id INT, weight FLOAT> myTuple;
HeapAccum<myTuple>({heap_size}, weight ASC) @@sorted_edges;
res = SELECT s FROM (s:Node)-[e:Edge]-(t:gsbNode) ACCUM @@sorted_edges += myTuple(s.node_id, t.node_id, e.weight);
PRINT @@sorted_edges;
}
Here heap size represents the number of rows we need to sort.
I wonder though, why this roundabout way of sorting edges when sorting nodes is so easy. Cypher allows sorting both nodes and edges very easily
1 Like
Hi @ameyap2 ,
Glad that you found the workaround for sorting the edges!
You may have noticed that in TigerGraph, we actually don’t have a direct way to SELECT the edges from a specific pattern.
For instance, if you are selecting the vertices, you can do:
res = SELECT s FROM (s:Node) -[e:Edge]- (t:AnotherNode);
But If you want to print the edges, you can collect the edges into a container-type accumulator, and then print the container-type accumulator out. Or in your case, use a HeapAccum with a default tuple type:
SetAccum<EDGE> @@edges_to_display;
res = SELECT s FROM (s:Node) -[e:Edge]- (t:AnotherNode)
ACCUM @@edges_to_display;
PRINT @@edges_to_display;
I think the current limitation was somewhat related to the features in GSQL about collecting the edges itself, but the GSQL language is flexible enough such that there is a workaround. I’d say there are several things you can do in GSQL that would be harder or impossible to do in Cypher. Likewise, there are several things in Cypher that is easier to do (e.g. sorting the edges like you mentioned) than in GSQL.
Best,
Supawish Limprasert (Jim)
Solution Engineer, TigerGraph
But I am able to print edges using this syntax in the GSQL shell.
SELECT e FROM (s:Node) -[e:Edge]- (t:AnotherNode);
The problem occurs when I try to include this statement inside a CREATE QUERY. It doesnt for some reason return a list of edges for the same command as above as it does when executed standalone in the GSQL shell.
Or am I missing something?