Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map comprehensions #352

Open
Mats-SX opened this issue Feb 22, 2019 · 0 comments
Open

Map comprehensions #352

Mats-SX opened this issue Feb 22, 2019 · 0 comments
Labels

Comments

@Mats-SX
Copy link
Member

Mats-SX commented Feb 22, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant