Search code examples
gremlin

How to set default values or return null value for a property which is missing in the graph and is been queried in the project() step


Referring to this previously asked question: Default value when property is missing with project() step in Gremlin? Is there a more efficient way to do the same thing or preferably returning "null" in the latest gremlin version as each coalesce step takes 2 additional traversals?


Solution

  • Gremlin will not have a notion of a null traverser until 3.5.0 is released (there is no schedule for release at this time). Here are some of examples of what to expect from 3.5.0:

    gremlin> g = TinkerGraph.open().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> g.addV('person').property('name','allen')
    ==>v[0]
    gremlin> g.V().has('person','name','allen').project('name','age').by('name').by('age')
    ==>[name:allen,age:null]
    gremlin> g.V().has('person','name','allen').elementMap()
    ==>[id:0,label:person,name:allen]
    gremlin> g.V().has('person','name','allen').elementMap('name','age')
    ==>[id:0,label:person,name:allen]
    gremlin> g.V().has('person','name','allen').values('name','age')
    ==>allen
    gremlin> g.V().has('person','name','allen').union(values('name'),values('age'))
    ==>allen
    gremlin> g.V().has('person','name','allen').union(values('name'),coalesce(values('age'),constant(null)))
    ==>allen
    ==>null
    

    There shouldn't be much loss in efficiency for using coalesce() even if you must use many of them. The cost of constant() is not an issue and values(String) assuming that's all you're doing will compile to a fast TokenTraversal. I suppose the big loss is having to type a lot. I'm not aware of any graph databases (maybe sqlg since it's based on relational backends?) that support default values as most don't have notion of a schema.