-
Notifications
You must be signed in to change notification settings - Fork 150
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
Aggregations #218
base: master
Are you sure you want to change the base?
Aggregations #218
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
= CIP2017-04-13 Aggregations | ||
:numbered: | ||
:toc: | ||
:toc-placement: macro | ||
:source-highlighter: codemirror | ||
|
||
*Author:* Tobias Lindaaker <[email protected]> | ||
|
||
toc::[] | ||
|
||
== Aggregations | ||
|
||
=== Syntax | ||
|
||
[source, ebnf] | ||
---- | ||
Return = 'RETURN', ['DISTINCT'], ReturnBody, Filter ; | ||
With = 'WITH', ['DISTINCT'], ReturnBody, Filter ; | ||
SingleValueReturn = 'RETURN', (Expression | ProjectedMap | Aggregation), Filter ; | ||
|
||
ReturnBody = ('*' | ReturnItem), {',', ReturnItem} ; | ||
ReturnItem = (Expression, ['AS', Variable]) | ||
| (Aggregation, ['AS', Variable]) | ||
| (ProjectedMap, 'AS', Variable) | ||
; | ||
|
||
Filter = [Where], [Order], [Skip], [Limit] ; | ||
|
||
Aggregation = Aggregator, 'OF', Expression ; | ||
Aggregator = SymbolicName | ExtensionName ; | ||
ExtensionName = {SymbolicName, '.'}-, SymbolicName ; | ||
---- | ||
|
||
=== Examples | ||
|
||
[source, cypher] | ||
.Aggregation using `avg` | ||
---- | ||
MATCH (employee:Employee) | ||
RETURN avg OF employee.salary | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `collect` | ||
---- | ||
MATCH (person:Person)-[:FRIEND]-(friend) | ||
RETURN person.email, collect OF friend {.name,.email} AS friends | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `count` | ||
---- | ||
MATCH (nodes) RETURN count OF nodes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had not thought of that, but that seems sensible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been wondering if we shouldn't just mandate always writing a kind-of list comprehension syntax for aggregation, to make more visible what's going on
this would allow re-using aggregation function calling syntax with regular lists!
or even shorter
I agree, we should just use But I think it's important to have syntax that allows us to call aggregation functions for aggregations as well as over ordinary lists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That syntax seems pretty far away from what this CIP suggests. Could we adapt it to a better fit? Just allow any expression after the
Although this is not ideal when considering list properties... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some similar ideas are discussed in the Python generator expression PEP -> https://www.python.org/dev/peps/pep-0289/ |
||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `max` | ||
---- | ||
MATCH (person:Person)-[:LIVES_IN]->({country:$country}) | ||
RETURN max OF person.age | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `min` | ||
---- | ||
MATCH (movie:Movie) | ||
RETURN min OF movie.duration | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `percentileCont` | ||
---- | ||
BREAKS DOWN IN THIS SYNTAX | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we could consider allowing arguments to the aggregator on the left-hand side of the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice ^^ How about - so we have a few options to give food for thought - a parentheses-free version:
I also played around with using If we ever have aggregations with more than one argument of this type, e.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I thought about similar things on my way home after having pushed this. What I thought of then was that, at least in this case, the parameter describes which particular aggregate value to get, so perhaps a subscript operator would be appropriate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, the general - or multi-arg - case would be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you bracket the expression on the RHS of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @technige Interesting idea. Are you considering the case when both I kind of like using |
||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `percentileDisc` | ||
---- | ||
BREAKS DOWN IN THIS SYNTAX | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `stDev` | ||
---- | ||
MATCH (person:Person) | ||
RETURN stDev OF n.age | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `stDevP` | ||
---- | ||
MATCH (person:Person) | ||
WHERE person.name IN $names | ||
RETURN stDevP OF n.age | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using `sum` | ||
---- | ||
MATCH (sale:Sale) | ||
WHERE date({quarterOf:date()}) = date({quarterOf:sale.date}) | ||
RETURN sum OF sale.value AS `sales this quarter` | ||
---- | ||
|
||
[source, cypher] | ||
.Aggregation using user-defined aggregator | ||
---- | ||
MATCH (node) | ||
RETURN org.thobe.FancyComputation OF node | ||
---- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the
OF
syntax, butcollect
is the operation I would like to change if we proceed withOF
.collect
is a verb, but a noun fits better here, which makes me think we should rename this tocollection
, or evenlist
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having just read through, I was thinking exactly the same thing, both "nounifying" the term and switching to
list
. It strikes me that this also highlights the return type of the operation.