AT
Apache TinkerPopValentyn Kahamlyk

Question about `local` step

Sometimes I don't understand how local step works. I want to count how many elements in each array, this query looks correct but gives unexpected result. How to fix it? g.inject([1,2],[1],[1,2]).local(unfold().count())
S
spmallette372d ago
if you just want to count each, the most Gremlinesque way is to do:
gremlin> g.inject([1,2],[1],[1,2]).count(local)
==>2
==>2
==>1
gremlin> g.inject([1,2],[1],[1,2]).count(local)
==>2
==>2
==>1
local() step has some nuances to it explored here: http://stephen.genoprime.com/snippet/2020/04/25/snippet-10.html and more recently here: https://discord.com/channels/838910279550238720/888507863847284766/1080592362297901088
stephen mallette
Use of local()
One of the more misunderstood, misused or simply unknown parts of the Gremlin language is local()-step. Its purpose is to execute a child traversal on a single element within the stream. In the following example limit(2) is applied to the stream in a global fashion and thus only two results are returned:
C
ColeGreer372d ago
There appears to be a subtle difference between .count(local) and .local(unfold().count()). Based on my observations, .count(local) performs a separate count on each incoming array. .local(unfold().count()) takes each incoming traverser, and runs is through an unfold().count(). The subtle difference is regarding bulked traversers. A bulked traverser can contain 2 copies of the same object, but it still gets fed into a single execution of the unfold().count() child traversal. g.inject([1,2],[1],[1,2]) appears to spawn 2 traversers: {t = [1], bulk=1}, {t = [1,2], bulk=2}. There are 3 arrays but only 2 traversers. This creates the following results:
gremlin> g.inject([1,2],[1],[1,2]).count(local)
==>2
==>2
==>1
gremlin> g.inject([1,2],[1],[1,2]).local(unfold().count())
==>4
==>1
gremlin> g.inject([1,2],[1],[1,2]).count(local)
==>2
==>2
==>1
gremlin> g.inject([1,2],[1],[1,2]).local(unfold().count())
==>4
==>1
It appears that even when disabling bulking, inject() still only spawns 2 traversers in this case.
gremlin> g.inject([1,2],[1],[1,2]).local(unfold().count())
==>4
==>1
gremlin> g.withBulk(false).inject([1,2],[1],[1,2]).local(unfold().count())
==>2
==>1
gremlin> g.inject([1,2],[1],[1,2]).local(unfold().count())
==>4
==>1
gremlin> g.withBulk(false).inject([1,2],[1],[1,2]).local(unfold().count())
==>2
==>1
I believe this may be a bug in the inject step. Thoughts @spmallette ?
S
spmallette371d ago
I'm definitely turning that thing i wrote in the discord link above into a blog post today. if i didn't re-read that i'd have probably gone down into a hole of debugging with this example. sorry i didn't understand if this was the real question here. anyway, no, not a bug. that's just how local() works. a global count() (not count(local)) occurs over the same objects in the stream. maybe this is easier to see:
gremlin> g.inject([1],[1],[1]).local(unfold().count())
==>3
gremlin> g.inject([1],[1],[1]).local(unfold().count())
==>3
The objects are all List with a 1 in there so they all count under the same traverser object. kinda weird but there are some use cases where you want to do object-local traversal computations. this example with inject() is fairly contrived so it looks especially strange. whenever i see local() in a traversal, i always stop to ask if it really needs to be there. often, it should be replaced with a Scope.local argument (like count(local) in this case) or a direct replacement with map() or flatMap().
S
spmallette371d ago
hope i didn't jump to "mark solution" too quickly....wanted that link in Answer Overflow so that i could complete the blog post: https://stephen.genoprime.com/snippet/2023/04/13/snippet-15.html
stephen mallette
Use of local() Revisited
In recent weeks there have been many questions about the local()-step both in Discord and in TinkerPop’s JIRA. I’d written on this topic once before in Use of local() but it perhaps begs a further revisit. The example that arose from Discord helped inspire this post as it was sufficiently primitive to hopefully clarify usage.
Want results from more Discord servers?
Add your server
More Posts
Recommendation of a cache methodI have a complex query that involves ordering and traversing over several node types to obtain sometMultiple labels not working in mergeV() syntax for conditional inserts in AWS NeptuneI know that multiple labels are an AWS Neptune specific feature and TinkerPop model does not allow fUsing a SeedStrategy in javascript to control shuffle.On my project we are trying to select random vertices from the graph and use a seed to keep the resuHorizontal cache for gremlin server deployment.I have gremlin deployment, which has 3 pods within it. On top of it I have a load balancer to accessworker thread pool of 1, gremlin pool of 1 and boss thread pool of 1 ?Hello - I have an remote gremlin setup backend as cassandra, In my gremlin remote logs I see a log sMultiple next() steps in a traversalIs there a way to exit traversals at different steps? I tried using the `next()` step on a static, btell me writing query this patternvartex(orange): question: {code:string, type: string } vartex(purple):LeaningElement What I want toVersion Update differencesHi guys, We are upgrading from gremlin_python 3.4.11 to 3.6.2. We had some functions break and it What is the ordering of group?For instance (on Tinkerpop modern) g.V().group().by('age').by('name') gives [{32=[josh], 35=[peter],Export to graphml/graphson with PythonHello everyone, I hope this question has not been asked yet, if so I'm sorry for redundant content.Publish all Q&A on the webTo my best knowledge, Discord contents are not indexed by search engine crawlers. If we can export aOut edge of vertex is slowWhen i run this below query: "g.V().has("guid", "6203620951906330066").limit(10).out("matching").proTranslating bytecode into JupyterLabs compatible script.I've found that the gremlin syntax in jupyter is different from every other language, including pythQuery if else in gremlinI have a query and that return a list vertex. I want to do a query from those vertexes like this (ifDoes Gremlin do DFS or BFS?Does Gremlin perform a Depth First Search (DFS) or Breadth First Search (BFS) when traversing?Self-service roles for providers and areas of expertiseWe have roles for providers right now as listed in #roles - they are handed out by moderators. So faMore elaborate sack examples in the docsThere are a few examples for the `sack` step, but it would be good to have a more concrete one. For Isolated vertices vs connected vertices with no join benefitIs there any downside to storing an isolated vertex with references to other nodes? Creating relatio