How do I write a triangle query with filters?

Hello everyone, I am new to TigerGraph and I am writing some test queries using gsql. One of them is to count triangles whose vertex belong to 3 community in a social network. The query is like this.

CREATE QUERY CommunityTriangle() SYNTAX v2  {
  TYPEDEF TUPLE <uint a, uint b, uint c> triplet;
  SetAccum<VERTEX<Person>> @p13set,@p23set;

  ListAccum<triplet> @@triplet_set;
  SumAccum<INT> @@triplet_count;

  C = SELECT p1
        FROM 
             Person:p1 -(knows*0..1)- Person:p3 
        WHERE p1.community==0 AND p3.community==2
        ACCUM p1.@p13set += p3;

  C = SELECT p2
        FROM 
             Person:p2 -(knows*0..1)- Person:p3 
        WHERE p2.community==1 AND p3.community==2
        ACCUM p2.@p23set += p3;

  C = SELECT p2
    FROM 
         Person:p1 -(knows*0..1)- Person:p2
    WHERE p1.community == 0 AND p2.community == 1
    ACCUM @@triplet_count += COUNT(p1.@p13set INTERSECT p2.@p23set);

  PRINT @@triplet_count;
}

When I tried to load this query, gsql told me

Semantic Check Error in query CommunityTriangle (SEM-1201): line [20, 24]
Accessing the accumulator of the loop variable 'p1' is not supported.
Failed to create queries: [CommunityTriangle].

I tried to search SEM-1201 but get nothing. What happened here? How to make this query work?

Not sure on the specific error, but here are a few suggestions to simplify your query and make it work.
Overall, the approach seems to work, and I have done similar queries and they work fine.

  1. Since you are already getting a vertex set for community 0 and community 1 in the first 2 steps of the query, you should then use those vertex sets in the final step. Something like this:
  1. I am not sure what benefit you are getting with the knows*0…1 edge specification. If you just want a direct relationship, why not use -(knows)-
1 Like

Thank you very much. I will try it then! :smiley:

1 Like