Inconsistent Behavior with WhereStep with labels

I'm getting some weird behavior when using labels on an anonymous traversal within the WhereStep. Below are some code examples - excuse the fact that they're kind of silly they're just meant to demonstrate the issue
final var tinkerg = TinkerFactory.createModern();
tinkerg.traversal().V().drop().iterate();
var g = tinkerg.traversal();
g.addV("v1").iterate();
g.addV("v2").iterate();
g.addE("test").from(v1).to(v2).iterate();
g.V().hasLabel("v1").as("testLabel")
.coalesce(
__.outE().where(__.inV()),
__.addE("edge").from(v1).to(v2)
).iterate();
// The above does not add a second edge (as expected)
g.V().hasLabel("v1").as("testLabel")
.coalesce(
__.outE().where(__.inV().as("testLabel"),
__.addE("edge").from(v1).to(v2)
).iterate();
// The above does add a second edge when it shouldn't (?)
final var tinkerg = TinkerFactory.createModern();
tinkerg.traversal().V().drop().iterate();
var g = tinkerg.traversal();
g.addV("v1").iterate();
g.addV("v2").iterate();
g.addE("test").from(v1).to(v2).iterate();
g.V().hasLabel("v1").as("testLabel")
.coalesce(
__.outE().where(__.inV()),
__.addE("edge").from(v1).to(v2)
).iterate();
// The above does not add a second edge (as expected)
g.V().hasLabel("v1").as("testLabel")
.coalesce(
__.outE().where(__.inV().as("testLabel"),
__.addE("edge").from(v1).to(v2)
).iterate();
// The above does add a second edge when it shouldn't (?)
What makes this even more interesting, is that on a more complex traversal with coalesce, the label doesn't seem to cause any problems...
2 Replies
Rice
RiceOP2mo ago
The more complex traversal in question:
graphTraversalSource
.V().hasLabel(FIRST_PARTY_IDENTIFIER).has("value", identifier).as("src")
.V().hasLabel(IPV6_VERTEX).has("value", ipv6)
.out(CONNECTED)
.hasLabel(FIRST_PARTY_IDENTIFIER)
.has("value", P.neq(identifier)).as("target")
.select("src", "target")
.coalesce(
__.select("src").outE(IDENTIFIER_CONNECTED).where(__.inV().as("target")),
__.addE(IDENTIFIER_CONNECTED).from("src").to("target")
)
.coalesce(
__.select("target").outE(IDENTIFIER_CONNECTED).where(__.inV().as("src")),
__.addE(IDENTIFIER_CONNECTED).from("target").to("src")
)
.iterate();
graphTraversalSource
.V().hasLabel(FIRST_PARTY_IDENTIFIER).has("value", identifier).as("src")
.V().hasLabel(IPV6_VERTEX).has("value", ipv6)
.out(CONNECTED)
.hasLabel(FIRST_PARTY_IDENTIFIER)
.has("value", P.neq(identifier)).as("target")
.select("src", "target")
.coalesce(
__.select("src").outE(IDENTIFIER_CONNECTED).where(__.inV().as("target")),
__.addE(IDENTIFIER_CONNECTED).from("src").to("target")
)
.coalesce(
__.select("target").outE(IDENTIFIER_CONNECTED).where(__.inV().as("src")),
__.addE(IDENTIFIER_CONNECTED).from("target").to("src")
)
.iterate();
ColeGreer
ColeGreer2mo ago
I won't time to dig deep into this today, but a bit of a clue can be found in the traversal explanations for the first 2.
gremlin> g.V().hasLabel("v1").as("testLabel").coalesce(__.outE().where(__.inV()), __.addE("edge").from("v1").to("v2")).explain()
==>Traversal Explanation
==============================================================================================================================================================================
Original Traversal [GraphStep(vertex,[]), HasStep([~label.eq(v1)])@[testLabel], CoalesceStep([[VertexStep(OUT,edge), TraversalFilterStep([EdgeVertexStep(IN
)])], [AddEdgeStep({~from=[[SelectOneStep(last,v1,null)]], label=[edge], ~to=[[SelectOneStep(last,v2,null)]]})]])]
...
gremlin> g.V().hasLabel("v1").as("testLabel").coalesce(__.outE().where(__.inV()), __.addE("edge").from("v1").to("v2")).explain()
==>Traversal Explanation
==============================================================================================================================================================================
Original Traversal [GraphStep(vertex,[]), HasStep([~label.eq(v1)])@[testLabel], CoalesceStep([[VertexStep(OUT,edge), TraversalFilterStep([EdgeVertexStep(IN
)])], [AddEdgeStep({~from=[[SelectOneStep(last,v1,null)]], label=[edge], ~to=[[SelectOneStep(last,v2,null)]]})]])]
...
gremlin> g.V().hasLabel("v1").as("testLabel").coalesce(__.outE().where(__.inV().as("testLabel")), __.addE("edge").from("v1").to("v2")).explain()
==>Traversal Explanation
===========================================================================================================================================================================================================================================================
Original Traversal [GraphStep(vertex,[]), HasStep([~label.eq(v1)])@[testLabel], CoalesceStep([[VertexStep(OUT,edge), WhereTraversalStep([WhereStartStep(null), EdgeVertexStep(IN), WhereEndStep(testLabel)])], [AddEdgeStep({~from=[[Sel
ectOneStep(last,v1,null)]], label=[edge], ~to=[[SelectOneStep(last,v2,null)]]})]])]
...
gremlin> g.V().hasLabel("v1").as("testLabel").coalesce(__.outE().where(__.inV().as("testLabel")), __.addE("edge").from("v1").to("v2")).explain()
==>Traversal Explanation
===========================================================================================================================================================================================================================================================
Original Traversal [GraphStep(vertex,[]), HasStep([~label.eq(v1)])@[testLabel], CoalesceStep([[VertexStep(OUT,edge), WhereTraversalStep([WhereStartStep(null), EdgeVertexStep(IN), WhereEndStep(testLabel)])], [AddEdgeStep({~from=[[Sel
ectOneStep(last,v1,null)]], label=[edge], ~to=[[SelectOneStep(last,v2,null)]]})]])]
...
As you can see in the code (https://github.com/apache/tinkerpop/blob/86cb67fd34d4278a514553d4a99aa32e880debb9/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.java#L2125-L2130), the presence of the variable in the where traversal determines if the where() step should become a TraversalFilterStep or a WhereTraversalStep.
GitHub
tinkerpop/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/p...
Apache TinkerPop - a graph computing framework. Contribute to apache/tinkerpop development by creating an account on GitHub.

Did you find this page helpful?