Optional Path with ACCUM Clause Depending on the Existence of the Path

Hello, I came across a use case in which I have an optional path and would want to adjust my ACCUM clause for both cases. How could I achieve that?
The following is a simple example describing the situation:

TYPEDEF TUPLE <STRING UserGroupID, STRING MessageID, BOOL MentionedTopic> mention;
SetAccum<mention> @@topic_mentions;

res = SELECT u FROM User:u-(PARTICIPATES_IN)-UserGroup:ug-(SENT_TO*..1)-Message:m-(MENTIONS)-Topic:t
ACCUM 
// this is the part where I am unsure about how to check for the existence of the path / subpath that starts with the SENT_TO
// I thought about checking the length, but that did not give the results I was hoping for
IF exists(m.id) THEN
@@topic_mentions += mention(ug.id, m.id, True)
ELSE
@@topic_mentions += mention(ug.id, "", False)
END;
PRINT @@topic_mentions;

Thanks!

Can you say a bit more about what you are trying to achieve with the query and with the ACCUM?

Right now it appears to me that you will always have the Message vertex (and therefore the m.id) since it is explicitly included in your query. If you wanted to use the syntax that just finds connections between vertexes, regardless of the number of hops, then you would omit the intervening vertex and then of course would not be able to access any attributes from it

1 Like

Hi @markmegerian, thanks for your reply! I have a path which might be fully or partially matched, so a message might have been sent to a UserGroup or not. And I would like to use ACCUM accordingly for both cases: If the path exists and a message was sent to the respective UserGroup, then I would want to record details of the message in the ACCUM part and if it is not the case I would want to use default values for example. Thanks!

This sounds like a relational database LEFT OUTER JOIN - which isn’t a concept that translates to a graph database but you can achieve something similar with the following approach - please comment on whether this is helpful. I am working under the assumption that you want a list of user groups, and optionally include any messages with topics

SetAccum<STRING>  @topic_mentions;

USER_GROUPS = SELECT ug FROM User:u-(PARTICIPATES_IN)-UserGroup:ug;
TOPICS = SELECT ug FROM USER_GROUPS:ug -(SENT_TO) - Message:m -(MENTIONS) - Topic:t
                   ACCUM ug.@topic_mentions += m.id;

// Print all user groups, with topic mentions, user groups with no topic mentions will have an empty set
PRINT USER_GROUPS[USER_GROUPS.id, USR_GROUPS.@topic_mentions];
1 Like

Thank you! Yes, that actually helps a lot :slight_smile: