dargolith
dargolith

Reputation: 311

OrientDB SQL Check if multiple pairs of vertices are connected

I haven't been able to find an answer for the SQL for this.

Given pairs of vertices (record ids) and edge types between them, I want to check if all pairs exists.

V1 --E1--> V2

V3 --E2--> V4

... and so on. The answer I want is true / false or something equivalent. ALL connections must be present in order to evaluate to true, so at least one edge (of correct type) must exist for each pair.

Pseudo, the question would be:

Does V1 have edge <E1EdgeType> to V2?
AND
Does V3 have edge <E2EdgeType> to V4?
AND
... and so on

Does anyone know what the orientDB SQL would be to achieve this?

UPDATE

I did already have one way of checking if one single edge exists between known vertices. It's perhaps not very pretty either, but it works:

SELECT FROM (
    SELECT EXPAND(out('TestEdge')) FROM #12:0
) WHERE @rid=#12:1

This will return the destination record (#12:0) if an edge of type 'TestEdge' exists from #12:0 to #12:1. However, if I have two of those, how can I query for one single result for both queries. Something like:

SELECT <something with $c> LET
    $a = (SELECT FROM (SELECT EXPAND(out('TestEdge')) FROM #12:0) WHERE @rid=#12:1)
    $b = (SELECT FROM (SELECT EXPAND(out('AnotherTestEdge')) FROM #12:2) WHERE @rid=#12:3)
    $c = <something that checks that both a and b yield results>

That's what I aim towards doing. Please tell me if I'm solving this the wrong way. I'm not even sure what the gain is to merge queries like this compared to just repeat queries.

Upvotes: 0

Views: 531

Answers (2)

peak
peak

Reputation: 116977

Given a pair of vertices, say #11:0 and #12:0, the following query will effectively check whether there is an edge of type E from #11:0 to #12:0

select from (select @this, out(E) from #11:0 unwind out) where out = #12:0

----+------+-----+-----
#   |@CLASS|this |out  
----+------+-----+-----
0   |null  |#11:0|#12:0
----+------+-----+-----

This is highly inelegant and I would encourage you to think about formulating an enhancement request accordingly at https://github.com/orientechnologies/orientdb/issues

One way to incorporate the boolean tests you have in mind is illustrated by the following:

select from 
  (select $a.size() as a, $b.size() as b
   let a=(select count(*) as e from (select out(E) from #11:0 unwind out)
                                     where out = #12:0),
       b=(select count(*) as e from (select out(E) from #11:1 unwind out)
                                     where out = #12:2))
where a > 0 and b > 0

Yes, inelegance again :-(

Upvotes: 2

Alessandro Rota
Alessandro Rota

Reputation: 3570

It might be useful to you the following query

SELECT eval('sum($a.size(),$b.size())==2') as existing_edges
let $a = ( SELECT from TestEdge where out = #12:0 and in = #12:1 limit 1),
    $b = ( SELECT from AnotherTestEdge where out = #12:2 and in = #12:3 limit 1)

Hope it helps.

Upvotes: 1

Related Questions