Gremlin Query for Root-to-Leaf Paths Fails in AWS Neptune: How to Fix Grouping Issue?

48 views
Skip to first unread message

Laura Poss

unread,
Jan 17, 2025, 1:40:38 PMJan 17
to Gremlin-users

I'm trying to write a Tinkerpop Gremlin query that returns data about each path between a particular root node and all of its leaves. In particular, I'd like to know:

  • details about the leaf node
  • the minimum value of an edge property across all edges in the path between leaf node and root (if there is no such value along a path, report null)
  • how many edges are in the path

Ideally this data would be grouped by the name of the leaf node, as doing so allows me to paginate the results by leaf node.

I've managed to write a query that works well enough against the tinkerpop/gremlin-server:3.6.5 Docker container (the result is not nearly as succinct as would be ideal, but it was something I could at least work with). My problem is that I'm ultimately writing this query to run against an AWS Neptune instance, and the query does not work when running against Neptune. I'm quite new to Gremlin and was hoping someone might be able to assist me in updating this query so that it works against Neptune (and making the result more succinct would be an awesome added bonus).

The following diagram is a basic example:

example_graph.drawio.png

In this example, an ideal result would be something similar to:

==> x1: {"leafNode": {result of running elementMap on the leaf node x1}, "paths": [{"edgeCount": 1, "minVal": null}]}
==> x2: {"leafNode": {result of running elementMap on the leaf node x2}, "paths": [{"edgeCount": 1, "minVal": 20}]}
==> x3: {"leafNode": {result of running elementMap on the leaf node x3}, "paths": [{"edgeCount": 3, "minVal": 17}, {"edgeCount": 2, "minVal": 16}]}

The query that I've written is (assuming 4 is the ID of the root node):

g.V(4).repeat(__.inE('member').outV()).until(hasLabel('X')).emit().filter(hasLabel('X')).order().by('name').
  path().
  project('name', 'leafNode', 'minEVal', 'edgeCount').
    by(unfold().where(label().is('X')).values('name')).
    by(unfold().where(label().is('X')).elementMap()).
    by(
      unfold().
      where(label().is('member')).
      values('e').
      fold().
      coalesce(unfold().min(), constant(null))
    ).
    by(unfold().where(label().is('member')).count()).
  group().by('name').unfold()

which yields, for the example diagram above:

==>X1=[{name=X1, leafNode={id=1, label=X, name=X1}, minEVal=null, edgeCount=1}] ==>X2=[{name=X2, leafNode={id=2, label=X, name=X2}, minEVal=20, edgeCount=1}] ==>X3=[{name=X3, leafNode={id=3, label=X, name=X3}, minEVal=16, edgeCount=2}, {name=X3, leafNode={id=3, label=X, name=X3}, minEVal=17, edgeCount=3}]

As I mentioned above, this result is a bit repetitive because it duplicates the leaf node details for leaf nodes that have multiple paths between themselves and the root node, but it was workable.

AWS Neptune does not like the last line group().by('name').unfold() and returns the error:

"code":"UnsupportedOperationException","message":"java.util.LinkedHashMap cannot be cast to org.apache.tinkerpop.gremlin.structure.Element"

If I don't include the group by line, the result of the query looks like:

==>[name:X1,leafNode:[id:1,label:X,name:X1],minEVal:null,edgeCount:1]
==>[name:X2,leafNode:[id:2,label:X,name:X2],minEVal:20,edgeCount:1]
==>[name:X3,leafNode:[id:3,label:X,name:X3],minEVal:16,edgeCount:2]
==>[name:X3,leafNode:[id:3,label:X,name:X3],minEVal:17,edgeCount:3]

which complicates performing pagination.


Setup code to replicate the example graph in Gremlin:

g.addV('X').property(id,1).property('name', 'X1').as('X1').
  addV('X').property(id,2).property('name', 'X2').as('X2').
  addV('X').property(id,3).property('name', 'X3').as('X3').
  addV('A').property(id,4).property('name', 'A1').as('A1').
  addV('A').property(id,5).property('name', 'A2').as('A2').
  addV('A').property(id,6).property('name', 'A3').as('A3').
  addV('A').property(id,7).property('name', 'A4').as('A4').
  addE('member').from('X1').to('A1').
  addE('member').property('e', 20).from('X2').to('A1').
  addE('member').property('e', 17).from('X3').to('A3').
  addE('member').property('e', 19).from('X3').to('A4').
  addE('member').from('A2').to('A1').
  addE('member').from('A3').to('A2').
  addE('member').property('e', 16).from('A4').to('A1').
  iterate()

Andrea Child

unread,
Jan 17, 2025, 6:40:40 PMJan 17
to gremli...@googlegroups.com

Hello Laura,

 

Thanks for the detailed question and steps to reproduce. I just have a few follow up questions.

Did you use the same gremlin query to insert the sample data into both the gremlin-server and AWS Neptune db?

How were you querying Neptune? Was it using gremlin-console or one of the drivers such as the python or java driver? Or possibly the Neptune notebook?

 

I would like to help you troubleshoot this issue and hope to have some capacity next week.

 

Thanks,

 

Andrea

 

From: 'Laura Poss' via Gremlin-users <gremli...@googlegroups.com>
Date: Friday, January 17, 2025 at 10:40
AM
To: Gremlin-users <gremli...@googlegroups.com>
Subject: [TinkerPop] Gremlin Query for Root-to-Leaf Paths Fails in AWS Neptune: How to Fix Grouping Issue?

I'm trying to write a Tinkerpop Gremlin query that returns data about each path between a particular root node and all of its leaves. In particular, I'd like to know:

·         details about the leaf node

·         the minimum value of an edge property across all edges in the path between leaf node and root (if there is no such value along a path, report null)

·         how many edges are in the path

Ideally this data would be grouped by the name of the leaf node, as doing so allows me to paginate the results by leaf node.

I've managed to write a query that works well enough against the tinkerpop/gremlin-server:3.6.5 Docker container (the result is not nearly as succinct as would be ideal, but it was something I could at least work with). My problem is that I'm ultimately writing this query to run against an AWS Neptune instance, and the query does not work when running against Neptune. I'm quite new to Gremlin and was hoping someone might be able to assist me in updating this query so that it works against Neptune (and making the result more succinct would be an awesome added bonus).

The following diagram is a basic example:

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/gremlin-users/7ab486fb-c348-43c2-965f-58401efe1fd1n%40googlegroups.com.

Laura Poss

unread,
Jan 21, 2025, 12:49:50 PMJan 21
to Gremlin-users
Hi Andrea,

I was using the same query for the gremlin-server and with Neptune. I was testing my query against Neptune using a Neptune notebook.

Separately, one of our AWS reps reached out to me directly and said that this bug is an issue that has already been fixed in Neptune. His recommendation was to upgrade my Neptune cluster to version 1.3.4.0 and that he had tested the query against a cluster running that version and it works.

Haven't verified myself yet, but I plan to soon!

Andrea Child

unread,
Jan 22, 2025, 7:03:23 PMJan 22
to gremli...@googlegroups.com

Good to hear you received a potential solution from AWS. Hope it works out!

 

Image removed by sender.

Reply all
Reply to author
Forward
0 new messages