-
Notifications
You must be signed in to change notification settings - Fork 157
Description
CIR-2019-352
Motivation
There are list comprehensions:
[x IN xs WHERE p(x) | f(x)]
It would also be nice to have map comprehensions, which allow the construction of maps based on key-value pairs in another map, similar to how list comprehensions allow construction of lists based on elements in another list.
Proposal
Borrowing the syntax of list comprehensions, this CIR suggests map comprehensions like the following example:
{k, v IN map WHERE p(k, v) | f(k) : g(v)}
Example use cases
This could be useful to find partial property sets of entities (which may be viewed as maps with respect to their properties):
MATCH (n:Node)
RETURN {(k, v) IN n WHERE abs(v) > 10 | k: v } AS onlyLargeProperties
Or to remove certain properties from a set of nodes:
MATCH (n:Node)
WITH {(k, v) IN n WHERE myPredicate(v) | k: v } AS filteredProperties, n
SET n = filteredProperties
Or to change the name of a property key (only if considering dynamic property keys):
MATCH (n:Node)
WITH {(k, v) IN n | toUpper(k): v } AS upperCaseProperties, n
SET n = upperCaseProperties
Open questions
Several aspects of this CIR consider making map keys dynamic (the result of evaluating an expression, rather than a direct token in the query text), which has wider consequences for the language. This proposal could be amended to only function over the values of the maps, while retaining the key for each tuple that passes the predicate.
Originally suggested in neo4j/neo4j#12152.
Since LOAD CSV is non-standard Cypher, the example was replaced when creating this CIR.