Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 906
To-om Avatar asked Jan 26 '26 10:01

To-om


1 Answers

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.

like image 172
stephen mallette Avatar answered Jan 29 '26 11:01

stephen mallette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!