Hi,
To explain my requirement, let’s take the following example.
Let there be 3 entities
-
User
-
Device
-
Event
User - (used) - Device
User - (occurred) - Event
Now I want to get all users, device and event combinations for which some conditions are true. The query for this in Cypher is below
MATCH (u:User)-[occ:occurred]-(ev:Event), (u:User)-[ud:used]-(d:Device) WHERE
RETURN u,ev,d
This returns each combination of user, event and device for which conditions are true.
The conditions can be dynamic and can contain AND/OR grouping.
How can I achieve this in GSQL?
Hi Raghav,
Thanks for your question.
For dynamic condition evaluation please follow this link
https://docs.tigergraph.com/dev/gsql-ref/querying/operators-functions-and-expressions#dynamic-expressions-with-evaluate
To return each combination, we can store the vertex in a MaxAccum.
So, in the end, your query would look like this (pseudo code, I didn’t test it):
CREAT QUERY q1 (string cond1, string cond2) FOR GRAPH graph1 {
MaxAccum @event, @device;
users = {User.*};
users = select s from users:s-( occ:occurred )-:t
where evaluate(cond1, "bool")
accum s.@event += t;
users = select s from users:s-( ud:used )-:t
where evaluate(cond2, "bool");
accum s.@device += t;
print users;
}
Please be noted that evaluate() is slower than normal expressions. Also, another option for the dynamic condition is interpreted query.
https://docs.tigergraph.com/dev/gsql-ref/querying/query-operations#interpret-query
Thanks.
Hi Xinyu Chang,
Thanks for the reply.
CREAT QUERY q1 (string cond1, string cond2) FOR GRAPH graph1 {
MaxAccum @event, @device;
users = {User.*};
users = select s from users:s-( occ:occurred )-:t
where evaluate(cond1, "bool")
accum s.@event += t;
users = select s from users:s-( ud:used )-:t
where evaluate(cond2, "bool");
accum s.@device += t;
print users;
}
This query gives me users for which cond1 AND cond2 are true. what if I want where cond1 OR cond2 is true?
I will have additional entities like Event-(has)-EventDetails and my condition could be on any entity.
I may receive an expression, where conditions are not separated by paths, like
(Device.name == “one” OR Event.name == “event_1”) AND (Device.name == “two” OR EventDetails.location == “US”)
So how do I get users for which above is true?
I even thought of creating a pattern like
User:u1 - (used) - Device - (used) - User:u2 - (occurred)- Event:e-(occurred)-User:u3 Where u1.primary_id=u2.primary_id AND u2.primary_id=u3.primary_id and ( condition expression )
but the query throws an error.