limit() function execution order in Gremlin? - gremlin

I have encountered a problem when using Gremlin. Vertex JobDefinition1 has edges named JobDefinitionToJobHistory, and the target of these edges are the job history vertices. In 2022-02-12, JobDefinition1 has two job history vertices, the first status of job history vertex is failed and the second status of job history vertex is succeed.
So, when I execute the Gremlin query below, I get the expected result which is a vertex with a succeed status(2 means succeed).
g.V("JobDefinition1").out('JobDefinitionToJobHistory').has("ExecutionStartTime", between("2022-02-12T00:00:00Z", "2022-02-13T00:00:00Z")).order().by('ExecutionStartTime', decr).limit(1)
But when I execute the Gremlin query below, I get the incomprehensible result which is a vertex with a failed status (1 means failed). The expected result of this query I think should be empty or null.
g.V("JobDefinition1").out('JobDefinitionToJobHistory').has("ExecutionStartTime", between("2022-02-12T00:00:00Z", "2022-02-13T00:00:00Z")).order().by('ExecutionStartTime', decr).limit(1).has("Status", 1)
I am very confused about the second Gremlin query result. Is the execution order of the limit function different from what we believe? Or is this result is caused by other reasons I don't know about? Hope you can give me some advice, Thanks!

Related

Gremlin OLAP traversal query error regarding local star-graph

I'm trying to execute an OLAP traversal on a query that needs to check if a vertex has a neighbour of certain type.
i keep getting
Local traversals may not traverse past the local star-graph on GraphComputer
my query looks something like:
g.V().hasLabel('label1').
where(_.out().hasLable('label2'))
I'm using the TraversalVertexProgram.
needless to say, when running the same query in oltp mode there is no problem
is there a way to execute such logic?
That is limitation of TinkerPop OLAP GraphComputer. It operate on 'star-graph' objects. The vertex and connected edges only. It uses message passing engine inside. So you have to rewrite you query.
Option 1: start from label2 and go t label1. This should return the same result
g.V().hasLabel('label2').in().hasLabel('label1')
Option2: try to use unique edge labels and check edge label instead
g.V().hasLabel('label1').where(_.outE('label1_label2'))

How to get the list of all incoming intermediate vertices between a source vertex and target vertex of specific label using Gremlin query?

The Gremlin query that I use to get the list of all outgoing vertices (with edge label “has”) from a selected vertex “P1” until it reaches the vertex with specific label “L3” is this:
g.V().has("id”,”P1”).repeat(out(“has”)).until(hasLabel(“L3”)).path().by("id")
As expected, the query above returns me the list of all intermediate nodes between selected vertex and target vertex labeled “L3”.
However, when using the same query (changing the ‘out’ to ‘in’) in opposite direction, i.e., to get the list of all incoming vertices from a selected vertex to the target vertex with specific label, i get a gremlin query error straight away.
Here is the query:
g.V().has("id”,”P3”).repeat(in(“has”)).until(hasLabel(“L1”)).path().by("id")
The error looks like this:
Failure in submitting query:
Error: Script compile error: Missing ')'
I don’t see any missing brackets in the query though and the only change between the queries for incoming or outgoing vertices i made is using ‘in’ instead of ‘out’.
In the official tinkerpop documentation (https://tinkerpop.apache.org/docs/3.2.9/reference/#_traversal_strategies_2), in traversal strategies, I cannot find any example with repeat(in()), only with repeat(out()). Is there a special query or method to get all the incoming vertices from a selected vertex until it reaches the vertex with a specific label?
I'm not sure if this is your problem or not, but I could see where you would get an error because "in" is a reserved word in Groovy so, you have to explicitly spawn it using the anonymous traversal class with: __.in(), therefore:
g.V().has("id","P3").repeat(__.in("has")).until(hasLabel("L1")).path().by("id")
This issue is documented in a number of places in the Reference Documentation, but perhaps you missed it (you referenced a fairly old version of the documentation as well) - described here in a NOTE in the Vertex Steps for example.

Fetch nodes recursively with Gremlin query

I'd like to define a Gremlin query which returns all the nodes recursively till there are no more nodes available:
Node: ProductionEvent
Node: Product
What I've tried so far is the following...however, I'm not sure if the output is correct. Also, is it possible to print it out using the tree() function? And can I do the repeat function with the times function to get all nodes?
g.V().hasLabel('ProductionEvent').
repeat(__.outE('consumes').simplePath()).times(3).
emit().dedup()
Im expecting an output like this
Product1: consumed <--- ProductionEvent1 --> produced :Product2: consumed <--- ProductionEvent2 --> produced :Product3
I achieved results which am satisfied with. The query is
g.V().hasLabel('ProductionEvent').has('orderID', 'XYZ')
.repeat(__.out('consumes').in_('produces').simplePath()).emit().times(5)
.dedup().limit(6).next()
From my understanding, the limit() and times() is necessary in order to improve the query execution time if the number of results is known in advance. And the simplepath() ensures that already visited paths are not revisited again.

Gremlin bredcrumb query

I'm new to Gremlin and still learning.
I'd like to include the starting vertex in the results of the following:
g.V('leafNode').repeat(out()).emit()
This gives me a collection of vertexes starting from an arbitrary leaf node "upwards" to the root vertex. However this collection excludes the V('leafNode') vertex itself.
How do I include the V('leafNode') in this collection?
Thanks
-John
There are two places for the emit in this statement: either before the repeat or after. If it comes before the repeat, it will be performed before evaluating the next loop.
Source: http://tinkerpop.apache.org/docs/current/reference/#repeat-step
So the following should take care of what your request.
g.V('leafNode').emit().repeat(out())

Simple outer-join like gremlin query not returning any results

I wrote the simple query below to traversal between Person to Country but it’s not returning any results.
g.V().hasLabel("Person").as("p").out("from").hasLabel("Country").as("c").select("p", "c")
In the actual data, only Person vertices exists and no Country vertices or from edges exist. I expected to at least return p - basically I want to do a left outer join. However, if I have Country and from data as well, the query returns results.
I tried another query using match as well but still no results unless there are actual data:
g.V().hasLabel("Person").has("name","bob").match(__.as("p").out("from").hasLabel("Country").as("c")).select("p", "c")
I'm running these queries against Datastax Enterpise Graph.
Any idea why it’s returning no results?
The result you are getting is expected. If there are no "from" edges then the traverser essentially dies and does not proceed any further. Perhaps you could consider using project():
g.V().hasLabel("Person").
project('name','country').
by('name')
by(out('from').hasLabel('Country').values('name').fold())
With the "modern" toy graph in TinkerPop, the output looks like this:
gremlin> g.V().hasLabel('person').project('name','knows').by().by(out('knows').values('name').fold())
==>[name:v[1],knows:[vadas,josh]]
==>[name:v[2],knows:[]]
==>[name:v[4],knows:[]]
==>[name:v[6],knows:[]]
In the future, when you submit questions about Gremlin, please include a Gremlin script that can be pasted into a Gremlin Console which makes it easier to try to more directly answer your specific question.

Resources