Search code examples
gremlintinkerpop3gremlinpython

gremlin python clone traversal


I'm using gremlin-python to connect to gremlin-server and I'm trying to build up a query incrementally but I'm getting stuck. I have an initial part of my query like the following:

query = g.V().hasLabel('<some_label>')

Now I would like to do multiple things with this query, firstly I just want a count:

query.count().next()

Now if I do anything else using the query variable the count step is on the traversal, so something like the following doesn't work:

query.out('<some_edge_label>').valueMap().toList()

Looking at the docs it seems like I need to clone the traversal so I replaced the above with:

query = g.V().hasLabel('<some_label>')

count_query = query.clone()
count_query.count().next()

But query still has the count() step on it, when I print the bytecode even though I cloned it. Is this the expected behaviour for gremlin-python? Here is a complete example of what I'm talking about, printing the bytecode at each step:

query = g.V().hasLabel('alabel')
print(query)
q_count = query.clone()
print(q_count.count())
print(query)

[['V'], ['hasLabel', 'alabel']]
[['V'], ['hasLabel', 'alabel'], ['count']]
[['V'], ['hasLabel', 'alabel'], ['count']]

What do I do to clone/copy the start of the traversal so I can reuse it in gremlin-python?


Solution

  • There were some fixes in the area of deep cloning traversals in the 3.4.7 (3.3.11) [1] [2] Apache TinkerPop release (June 2020). Installing one of those drivers should help.

    [1] https://github.com/apache/tinkerpop/blob/master/CHANGELOG.asciidoc

    [2] https://issues.apache.org/jira/browse/TINKERPOP-2350