Search code examples
gremlintinkerpop3gremlin-server

Gremlin: Can't sum property from other node


I have a graph with post vertices, which have edges to 1 or more tag vertices.

I'm trying to compute the total weight for each tag:

// Create empty graph
graph = TinkerGraph.open()
g = graph.traversal()

// Create some tag vertices
g.inject('food', 'drink').addV('tag').property(id, identity())

// Create some posts with 'food' tag
posts = g.inject(10, 20).addV('post').property('weight', identity()).toList()
g.V(posts).addE('tagged').to(g.V('food'))

// Create some posts with 'drink' tag
posts = g.inject(5).addV('post').property('weight', identity()).toList()
g.V(posts).addE('tagged').to(g.V('drink'))

// Attempt to calculate total weight for each tag
g.V().
  hasLabel('post').as('p').
  out('tagged').
  group('g').
    by(id).
    by(select('p').values('weight').sum()).
  cap('g').next()

but I get this error:

java.lang.Long cannot be cast to org.apache.tinkerpop.gremlin.process.traversal.traverser.util.TraverserSet

Note that computing the total weight for all the posts works fine:

g.V().hasLabel('post').values('weight').sum()

Any suggestions?

Using Gremlin Server 3.3.4


Solution

  • This is a bug in 3.3.4 (and earlier versions). Here's a workaround:

    gremlin> g.V().hasLabel('post').as('p').
    ......1>   out('tagged').
    ......2>   group().
    ......3>     by(id).
    ......4>     by(select('p').by('weight').sum())
    ==>[food:30,drink:5]