Using Multiple Vertex Sets In SELECT Statement

Hi, I was wondering if there is any way to use more than one vertex set in a SELECT statement.

I would think it should be possible because… why not?

For an example, say we have this basic query:

CREATE QUERY coolQuery(VERTEX<Foo> foo, String bar, String biz) FOR GRAPH cool_graph SYNTAX v2 {

f = {foo};

x = SELECT i
FROM SomeVertex:i -(PathType1>)- f

y = SELECT i
FROM x:i -(<PathType2)- BarVertex:br
WHERE [br.id](http://br.id/) == bar;

z = SELECT i
FROM y:i -(PathType3>.PathType4>)- BizVertex:bz
WHERE [bz.id](http://bz.id/) == biz;

PRINT z;
}

Now, that’s all fine and dandy, but what if I know the other vertices whose ids are bar and biz?

Can I use more than one known vertex set in a SELECT statement?

The goal here is to arrive to the final SomeVertex set as quickly as possible via using the indexed vertex id values.

This is what I’m thinking:

CREATE QUERY coolQuery2(VERTEX<FooVertex> foo, VERTEX<BarVertex> bar, Vertex<BizVertex> biz) FOR GRAPH cool_graph SYNTAX v2 {

f = {foo};

  br = {bar};

  bz = {biz};

  x = SELECT i
FROM SomeVertex:i -(PathType1>)- f

y = SELECT i
FROM x:i -(<PathType2)- br

z = SELECT i
FROM y:i -(PathType3>.PathType4>)- bz

PRINT z;
}

I get syntax errors with this and I can’t find anything in the docs that does this type

of thing where more than one known vertex set is used in a SELECT statement.

Hi Brett,

Thanks for your great question.

In your case, it is recommended to write the query this way:

Version 1:

CREATE QUERY coolQuery2(VERTEX<FooVertex> foo, VERTEX<BarVertex> bar, Vertex<BizVertex> biz) FOR GRAPH cool_graph SYNTAX v2 {

OrAccum<BOOL> @hasFoo, @hasBar, @hasBiz;

f = {foo, bar, biz};

  result = select t from f:s-((PathType1|PathType2|PathType3):e)-:t

               accum case when s == foo then t.@hasFoo += true end,

                          case when s == bar then t.@hasBar += true end,

                           case when s == biz then t.@hasBiz += true end

               having t.@hasFoo and t.@hasBar and t.@hasBiz;

  print result;

}

Version 2:

CREATE QUERY coolQuery2(VERTEX<FooVertex> foo, VERTEX<BarVertex> bar, Vertex<BizVertex> biz) FOR GRAPH cool_graph SYNTAX v2 {

OrAccum<BOOL> @hasFoo, @hasBar, @hasBiz;

f = {foo};

  br = {bar};

  bz = {biz};

  fooSet = select t from f-(PathType1)-:t;

  barSet = select t from br-(PathType1)-:t;

  bizSet = select t from bz-(PathType1)-:t;

  result = fooSet intersect barSet intersect bizSet;

  print result;

}

In this case version, 1 is more recommended since it has better concurrency and only does only one SELECT.

Thank you!

I believe that is exactly what I was looking for!

I was pretty sure there was a way to do,

thank you for showing me that way. :slight_smile:

I believe I have one that is more complicated now, and I am not sure how to go about it.

Say that I have the above graph schema. (all edges also have reverse edges).

In the query, parameters for VERTEX, VERTEX, VERTEX, and VERTEX would be provided.

The result of the query should return the set of v4 vertices.

(if in fact the vertex params for v1, v2, v3, and v6 actually have connecting edges).

The vertex v5 is ignored, v4 should have some path connecting any e4 and e5 to v6.

How can I go about this type of query using the vertex ids as the params?

So in this case, v3 should be connected by both input v1 and v2?

And the v4 in the result should be connected to qualified v3?

And v6 should be connected to v4 via any v5?

Thanks.

That is correct.

I can put the query together with multiple SELECT statements

(but only using v3 param as VERTEX type whereas other params are STRING),

but I’d really like to know how to chain this all together using one SELECT (if possible)

Hi Brett,

Sorry for the late reply. For that query we have to write them in syntax v1, which is to use multiple select.

I don’t think we can chain all of them in one statement for now.

Thanks.

Thank you for the info, that is helpful.
I think with neo4j Cypher queries this type of specifying middle vertices in a chain can be done.

Do you know if this chaining will be a feature in a future TigerGraph release?

Yes, I just confirmed this with the language team. we will support this very soon.

Awesome, that is great to know, thanks!

I Appreciate the helpful info as always. :slight_smile:

I have a tree graph where there is a 1 to many relation between each descending Vertex. Meaning, a Contract has many accounts, but an account only has one contract, this type of relationship goes all the way down the tree for each of the Vertex types.

Below is a snippet of my query where I’m trying to find which companies are under a contract:

start = {Contract.*};
contractRS = select s from start:s - (hasAccount.hasAccGroup.hasCompany) - Company:tgt
WHERE lower(tgt.company_name) LIKE company_name;

The query identifies the Contract, but I need to also need to know which Account, AccGroup and the “Company” or Companies if the LIKE statement files similar named companies that the Contract is associated with. The option 1, using ACCM will not work since Contract is the only Vertex that is selected.

Is there any other way to select all the Vertex’s in the graph without having to perform separate Select statements (option 2)? If not, is there any plans to support multi-selection of Vertex? I’m currently using “product release_3.3.0_11-01-2021.”

Ok, I found my issue…I needed to break out the path to represent the Vertices.

contractRS = select s from (Contract):s - (hasAccount:ae) - Account:ac - (hasAccGroup) - AccGroup:ag - (hasCompany:sge) - Company:tgt
WHERE lower(tgt.name) LIKE company_name
ACCUM file1.println(s.contract_id, ac.name, ag.name, tgt.company_id, tgt.name);