opencypher / openCypher

Specification of the Cypher property graph query language
http://www.opencypher.org
Apache License 2.0
855 stars 150 forks source link

Shorthand for coalesce #22

Open mbyrne00 opened 8 years ago

mbyrne00 commented 8 years ago

CIR-2016-22

Since null handling can be common within a statement, writing coalesce every time can be a bit verbose. It would be nice to have some syntactic shorthand sugar for this.

Oracle, for example, has the nvl function which is a more concise alternative to coalesce, which they also have (don't read this in any way as me being a fan of Oracle .... it's one of the only times their syntax is "concise").

This all started from the casual slack conversation on the neo4j-users slack channel #help-cypher. Not a massive priority, but raised issue this at the request of Michael Hunger ...

Kevin Turner [2:58 AM] 
makes me want something like `(x IS NULL) ? 0 : x` sometimes.

Matt Byrne [6:23 AM] 
Like `coalesce(x, 0)` ?

Kevin Turner [6:56 AM] 
oh hey that is what coalesce does, isn’t it. okay, that’d be readable enough.

Matt Byrne [7:12 AM] 
:simple_smile: ... yea comes in handy

Eve Freeman [7:13 AM] 
if you have more complex stuff `... ? ... : ...` is basically `case when x is null then 0 else x end`

Matt Byrne [7:14 AM] 
I'm not a massive fan of Oracle but they do have a more concise equivalent (as well as `coalesce`) called `nvl` ... fewer letters ... when you have a few of these in one statement it's more readable with 3 letters.

new messages
Michael Hunger [12:33 PM] 
ya, true

[12:33] 
can you raise it as issue on the openCypher repo @mattbyrne ?

Matt Byrne [1:19 PM] 
sure @michael.neo
thobe commented 8 years ago

I don't think having two ways of spelling the same thing is beneficial, it only creates more things to learn in the language without any benefit. It would be better to instead propose changing the name of coalesce to something shorter, at which point I think nvl is a really poor proposal since that name doesn't provide a direct insight into what the operator does, it is another thing one has to learn by heart, which adds to the complexity of the language.

I'm not saying coalesce is a perfect word, the effect of the operator is not a perfect match for the dictionary definition of the word, but it is an actual word and it hints in the direction of what the operator does, and manages to do that while being pretty short. If we can come up with a word that does a better job by those criterions, changing it would probably be a great idea.

Just my 2¢.

boggle commented 8 years ago

I can see that sometimes an infix operator form may feel more natural as it gets less in the way of the intent of an expression. Scala uses OR ELSE for chaining options, perhaps that would work here?

expr1 OR ELSE expr2 OR ELSE expr3

Alternatively, we could decide to spend an operator for this but the downside of that would be that it is much harder to read that without knowing it and it seems to small an issue to spend a special character on. If we go down that route, ? might be a natural character to use since it is also used in the type system to indicate nullability.

mbyrne00 commented 8 years ago

@thobe I see your point about confusion in having two functions that do the same thing. Although the word coalesce is in the dictionary, however, it's not a common word that you'd use every day. I know to use this function because it's common in other databases (ANSI standard), but other than that I wouldn't think to search for a function with this name.

Databases tend to require more special null handling since null doesn't equal null like in some languages. The point of the ticket was to consider a shorthand since it could be used quite often.

It's commonplace for many languages to offer a shorthand for this, such as the elvis operator ?:

See here for examples: https://en.wikipedia.org/wiki/Null_coalescing_operator

mbyrne00 commented 8 years ago

That being said it was something raised in passing slack conversation and not a burning issue ;-)

ADTC commented 8 years ago

How about |?

So instead of COALESCE(v1, v2, v3 ...) you could do v1|v2|v3.... Since OR is used for boolean arithmetic, the use of | would not conflict with anything. And the syntax would read "v1 or v2 or v3, whichever comes first as non-null".

petraselmer commented 7 years ago

Updated the original comment to include the name of the CIR

boggle commented 7 years ago

Throwing in some more operator suggestions: v1 // v2 // v3 or v1 :+: v2 :+: v3 (monoidal combine operator)