Search code examples
mathgremlin

math() step in gremlin throwing exception even if the attribute passed has type long


I have a vertex (Ticket) in my graph DB having attribute as createdOn and closedOn having values in millisecond. I'm trying to find average open time for ticket grouping by name but getting exception as 'The variable createdOn for math() step must resolve to a Number - it is instead of type null with value null'. I verified the value for each ticket's attribute is number only but i keep getting this error message.

Tried

g.V().hasLabel('Tickets').has('closedOn').has('createdOn').project('name','duration').by(math('closedOn - createdOn').by(select('duration').mean())

Expecting

[{"name":"ticket1","duration":16745200000}]

Solution

  • The error is because the math step will not look "inside" the vertex for properties with those names. They have to be available either from prior as steps or a map in the traversal stream. So you could do something like:

    g.V().hasLabel('Tickets').
          has('closedOn').
          has('createdOn').
          project('closed','created').
            by(values('closedOn')).
            by(values('createdOn')).
          project('duration').
            by(math('closed - created'))    
    

    Note that (I think) you also had a closing paren in the wrong place in your example query.

    Lastly, you may need to calculate the mean after the second project is completed as values from one part of the project will not be visible to another part most likely.