What is the ordering of group?

For instance (on Tinkerpop modern) g.V().group().by('age').by('name') gives [{32=[josh], 35=[peter], 27=[vadas], 29=[marko]}] There must be some logic to it? (Understanding this would help me for unit testing, allowing me to compare results that are actually the same)
Solution:
Gremlin doesn't offer too many order guarantees. I think group() by default uses a regular old HashMap for it's data structure. If you want an order you would want to specifically specify an order() - for example:
gremlin> g.V().group().by('age').by('name').order(local).by(keys)
==>[27:[vadas],29:[marko],32:[josh],35:[peter]]
gremlin> g.V().group().by('age').by('name').order(local).by(keys)
==>[27:[vadas],29:[marko],32:[josh],35:[peter]]
I suppose you could also force use of a LinkedHashMap: ```gremlin> g.withSideEffect('m', [:]).V().hasLabel('person').group('m').by('age').by('name').cap('m')...
Solution
S
spmallette393d ago
Gremlin doesn't offer too many order guarantees. I think group() by default uses a regular old HashMap for it's data structure. If you want an order you would want to specifically specify an order() - for example:
gremlin> g.V().group().by('age').by('name').order(local).by(keys)
==>[27:[vadas],29:[marko],32:[josh],35:[peter]]
gremlin> g.V().group().by('age').by('name').order(local).by(keys)
==>[27:[vadas],29:[marko],32:[josh],35:[peter]]
I suppose you could also force use of a LinkedHashMap:
gremlin> g.withSideEffect('m', [:]).V().hasLabel('person').group('m').by('age').by('name').cap('m')
==>[29:[marko],27:[vadas],32:[josh],35:[peter]]
gremlin> g.withSideEffect('m', [:]).V().order().by('age').group('m').by('age').by('name').cap('m')
==>[27:[vadas],29:[marko],32:[josh],35:[peter]]
gremlin> g.withSideEffect('m', [:]).V().hasLabel('person').group('m').by('age').by('name').cap('m')
==>[29:[marko],27:[vadas],32:[josh],35:[peter]]
gremlin> g.withSideEffect('m', [:]).V().order().by('age').group('m').by('age').by('name').cap('m')
==>[27:[vadas],29:[marko],32:[josh],35:[peter]]
The first being an insertion order given however the underlying graph iterates vertices from V() or with an explicit order() as in the second. I'd say it's possible better to do the explicit order against vertices rather than than my first example where it does a in-memory sort of the Map as it's possible you might get some performance improvement there depending on your graph. Hard to say which is nicer as the withSideEffect() approach isn't quite as readable. (Note that in my Groovy examples above, the [:] will resolve to a LinkedHashMap by default.)
W
wiebe393d ago
Ah, thanks! A hash map does explain it. I assumed the maps where ordered (unique keys, ordered by insertion). So I guess I can now refactor (my own implementation I'm testing) back to my native maps. They use = and > for it's binary tree. But I'll keep your solution for examination. The easy way out is to simply overwrite my unit test results, but not knowing the 'why' kept me awake...
S
spmallette393d ago
good question - thanks gremlin
W
wiebe384d ago
A small follow up question... If the maps use hashtables, how does ordering them work: gremlin>g.V().group().by('age').order(local).by(keys) ==>[{27=[v[2]], 29=[v[1]], 32=[v[4]], 35=[v[6]]}] The result appears to be a map (based on the {}), but how can a (hashtable) map have order?
S
spmallette384d ago
the TinkerPop processing engine uses LinkedHashMap internally. graph implementations should abide to similar order requirements if they don't use the TinkerPop engine.
W
wiebe380d ago
Thanks. I assumed LinkedHashMap couldn't be ordered, but apparently you can order by insertion. Still, with query "g.V().groupCount().by(inE().count()).order(local).by(values, desc) " g.V()'s console returns (ordered) [{1=3, 0=2, 3=1}] yet g.V()'s JSON returns (unordered) [ {"0": 2, "1": 3, "3": 1 } ] I'm not even sure if g.V() uses the TinkerPop engine. Probably so obvious it's hard to find.
S
spmallette380d ago
when you say "g.V()'s" are you referring to https://gdotv.com/ ?
gdotv Ltd
gdotv Ltd
Gremlin Graph Database IDE, Debugging & Visualization Tool
A powerful IDE to query, debug and visualize your Apache TinkerPop™ graph DBs. Compatible with Azure Cosmos DB, JanusGraph, Amazon Neptune & more. Free to try!
W
wiebe380d ago
Ha, right in my face. Guess it's just not easy to google or time to take a break. So, TinkerPop orders the map by reinserting the elements in order? Why is the JSON output not ordered?
S
spmallette380d ago
So, TinkerPop orders the map by reinserting the elements in order?
yes
Why is the JSON output not ordered?
i'd agree that's a bit strange. i confirmed the serializer should be writing the JSON in entrySet() iterator order, but this is why i asked about https://gdotv.com/ i.e. i wanted to know how you are getting that JSON.
gdotv Ltd
gdotv Ltd
Gremlin Graph Database IDE, Debugging & Visualization Tool
A powerful IDE to query, debug and visualize your Apache TinkerPop™ graph DBs. Compatible with Azure Cosmos DB, JanusGraph, Amazon Neptune & more. Free to try!
G
gdotv376d ago
hey, so just to chime in on this it appears the JSON serialized version of the results gdotv displays is somehow losing the order of the results (and instead ordering by key name) im guessing this is due to how serializing maps to JSON works in java, it may somehow not preserve the order the console output is the accurate one in this instance, the JSON representation is more so for convenience in gdotv (just a brief update, I've identified the issue in gdotv and fixed it, it will be out in the next release)
Want results from more Discord servers?
Add your server
More Posts
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 relatioSubgraph Strategy with vertexProperties + project().by("field name") = crashRunning the following query: `g.withStrategies(new SubgraphStrategy(vertexProperties: constant(true)Custom MutationListener on TransactionHello everyone, I'm a beginner regarding tinkerpop and i'm trying to fire my custom listener after Gremlin Query to give all related items in versioned graphI am working on a requirement where I need to store all version of a record in a Graph Database say Order group count result alphabeticallyHi! Given the following query and results in the enclosed image: how would I sort the labels alphabTransactions - tx.commit vs tx.closeI have a question related to transactions. I'm having issues with tx.commit() hanging locally when rExtracting the ProjectStep of a GraphTraversal instance during unit testing**Tl;dr** Given an instance of `GraphTraversal<?, Map<String, Object>>`, is it possible to extract tWhen can we get a non-RC release for Python 3.11 support in Production Envs?There was a bug where the version of aiohttp was too strict and blocked Python 3.11 support. ( httpsSubgraph query returns String instead of TinkerGraph Object in Fluent Java APIHi guys. I am running the following query in the console and it works fine: `g.V().has('user', 'id'Multiple Graphs in Gremlin Server?How do I manage multiple graphs? Is there an option where I can select which graph to use for query Has anyone else encountered "NoSuchElementException" when calling getLeafTrees() on a tree objectDid a little bit of debugging and it seems that the issue has to do with a cast to Tree before calliThe query gets slower as the number of vertices that already exist in JanusGraph gets bigger and bigWhen i start JanusGraph and perform any query like : - g.V().has(properties).limit(10).toList() - g.