Search code examples
gremlintinkerpop

gremlin order by with coalesce duplicates some values


In some cases, I get inexplicable result when I use order().by(...) with coalesce(...). Using the standard Modern graph,

gremlin> g.V()
          .hasLabel("person")
          .out("created")
          .coalesce(values("name"), constant("x"))
          .fold()
==>[lop,lop,ripple,lop]

But if I sort by name before the coalesce I get 9 lop instead of 3:

gremlin> g.V()
          .hasLabel("person")
          .out("created")
          .order().by("name")
          .coalesce(values("name"), constant("x"))
          .fold()
==>[lop,lop,lop,lop,lop,lop,lop,lop,lop,ripple]

Why the number of elements differs between the two queries ?


Solution

  • That looks like a bug - I've created an issue in JIRA. There is a workaround but first consider that your traversal isn't really going to work even with the bug set aside, order() will fail because you're referencing a key that possibly doesn't exist in the by() modulator. So you need to account for that differently:

    g.V().
      hasLabel("person").
      out("created").
      order().by(coalesce(values('name'),constant('x')))
    

    I then used choose() to do what coalesce() is supposed to do:

    g.V().
      hasLabel("person").
      out("created").
      order().by(coalesce(values('name'),constant('x'))).
      choose(has("name"),values('name'),constant('x')).
      fold()
    

    and that seems to work fine.