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

Conditional queries (WHEN) #1175

Merged
merged 24 commits into from
Feb 21, 2025

Conversation

JPryce-Aklundh
Copy link
Collaborator

This PR also moves Queries/Expressions/*.adoc to a new "Expressions" section.

This PR also subsumes #1109 (and therefore makes it redundant)

@JPryce-Aklundh JPryce-Aklundh changed the title Conditional queries (`WHEN) Conditional queries (WHEN) Jan 28, 2025
Comment on lines +3 to +9
In Cypher, an expression is any combination of components that evaluates to a result.
Expressions are used to perform operations like calculations, comparisons, and data transformations within a query.

This section includes:

* xref:expressions/expressions-overview.adoc[]
* xref:expressions/conditional-expressions.adoc[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future consideration, EXISTS/COUNT/COLLECT might make more sense under here and moving CALL and CALL in transactions into the Clauses section.

Comment on lines 1 to 11
= Composed queries
:description: Overview about how to use `UNION` and `WHEN` to construct combined or conditional queries in Cypher.

`UNION` and `WHEN` enable the composition of multiple separate query branches within a single query.
`UNION` allows for combining the results of different queries, while `WHEN` enables conditional queries, where different query branches can be made to execute depending on a set of criteria.
As such, `UNION` and `WHEN` manage in different ways the execution flow and logic of queries and cannot be used in queries as regular clauses.

For more information, see:

* xref:queries/composed-queries/combined-queries.adoc[]
* xref:queries/composed-queries/conditional-queries.adoc[] label:new[Introduced in Neo4j 2025.02]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good!

[}]]
----

The first predicate that evaluates to `true` will be executed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The first predicate that evaluates to `true` will be executed.
The first branch with a predicate that evaluates to `true` will be executed.

Comment on lines 58 to 60
WHEN false THEN RETURN 1 AS x
WHEN true THEN RETURN 2 AS x
ELSE RETURN 3 AS x
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WHEN false THEN RETURN 1 AS x
WHEN true THEN RETURN 2 AS x
ELSE RETURN 3 AS x
WHEN false THEN RETURN 1 AS x
WHEN true THEN RETURN 2 AS x
WHEN true THEN RETURN 3 AS x
ELSE RETURN 3 AS x

ELSE RETURN 3 AS x
----

Since the second `WHEN` branch is `true`, it will execute, while the preceding branch (which is `false`) and the succeeding `ELSE` branch will be skipped.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Since the second `WHEN` branch is `true`, it will execute, while the preceding branch (which is `false`) and the succeeding `ELSE` branch will be skipped.
Since the second `WHEN` branch is `true`, it will execute, while the preceding branch (which is `false`) and the succeeding `WHEN` branch (which is `true`) and the `ELSE` branch will be skipped.

Comment on lines 81 to 83
ELSE { MATCH (n:Person)
RETURN n.name AS name
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ELSE { MATCH (n:Person)
RETURN n.name AS name
}
ELSE {
MATCH (n:Person)
RETURN n.name AS name
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe part of a larger discussion but how does the docs communicate that conditional query is only available in CYPHER 25

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be part of the 25 versions of the Manual, and not the 5 version. Together with the version label (which is now probably incorrect) and the page about selecting Cypher versions (see PR: #1123) I hope it will be clear to our readers.

@JPryce-Aklundh JPryce-Aklundh marked this pull request as ready for review January 30, 2025 14:16
Copy link
Contributor

@gem-neo4j gem-neo4j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great :D


This page contains examples of allowed expressions in Cypher.
This page contains an overview of the allowed expressions in Cypher.

[[general]]
== General
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really this PR, but I see the below list mentions dynamic properties, it could also now mention dynamic labels/types 😼

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - will add at some point in the ongoing expressions work

Comment on lines 229 to 235
CALL (*) {
WHEN m.age > n.age THEN {
RETURN collect([m.name, m.ageGroup]) AS manager
}
ELSE {
RETURN collect([m.name, m.ageGroup]) AS manager
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this query wrong? It doesn't make sense, I assume the second collect wanted to use n in the collect instead?

n.age AS age
----

`Alice` and `Charlie` are both older than 40, so they are returned by the `WHEN` branch, while `Bob` is returned by the `ELSE` branch.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention that some aren't matched in either branch, and are therefore left out?

1+d| Rows: 2
|===

If the queries combined by the `UNION` does not begin with a `WHEN` branch, then enclosing curly braces are not necessary.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the queries combined by the `UNION` does not begin with a `WHEN` branch, then enclosing curly braces are not necessary.
If the queries combined by the `UNION` do not begin with a `WHEN` branch, then enclosing curly braces are not necessary.

RETURN person, message
UNION
CALL (*) {
WHEN n.age < 40 THEN {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahahah everyone over 40 are gonna be sad 😂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Older" is at least better than "Old" :)

Comment on lines 4 to 5
*Last updated*: 23 January October 2025 +
*Neo4j version*: 2025.02
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this will be updated before merge :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that - yep.

@neo-technology-commit-status-publisher
Copy link
Collaborator

Thanks for the documentation updates.

The preview documentation has now been torn down - reopening this PR will republish it.

@JPryce-Aklundh JPryce-Aklundh merged commit fbd5696 into neo4j:cypher-25 Feb 21, 2025
4 checks passed
@JPryce-Aklundh JPryce-Aklundh deleted the conditional_clause branch February 21, 2025 12:46
JPryce-Aklundh added a commit that referenced this pull request Feb 28, 2025
Builds on work in this PR:
#1175

This PR does the following:

- New "Predicate expressions" section including:
-- "Predicate operators" (inclucing boolean, string, comparison, and
list operators - content taken from syntax/operators.adoc, and several
examples taken from clauses/where.adoc). **Redirect required**
-- "Path pattern expressions "(content taken from clauses/where.adoc)
-- "Type predicate expressions" (page moved from Values and types
chapter) **Redirect required**

- New page: "Equality, ordering, and comparison of value types" (content
taken from syntax/operators.adoc)
- Rewrite of WHERE page (significantly shorter due to several examples
moved to new pages listed above)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants