'Cypher - is it possible to declare common table expression over synthetized data?

To play with SQL or explain some language feature, it is often unnecessary to have data in real database table, only common table expression suffices:

with my_sample (id, name, age) as (values
  (1, 'Alice', 17),
  (2, 'Bob', 19)
), can_drive_car (name) as (
  select name from my_sample where age >= 18
)
select * from can_drive_car

Does anything equivalent exist in Cypher? I mean create graph on-the-fly, query it and return result without impact to data in database. Pseudocode:

WITH CREATE (:Person {name:'Alice'})-[:FRIEND]->(:Person {name:'Bob'})-[:FRIEND]->(:Person {name:'Charlie'})
MATCH p=shortestPath((a {name:'Alice'})-[*]->(c {name:'Charlie'}))
RETURN p

I am aware of Cypher WITH clause. Despite same naming it seems to me that in Cypher - unlike SQL - the clause only passes result of previous query part (which was invoked on real graph data in Neo4j database) to subsequent streaming, AFAIK it cannot fabricate anything beyond as in my SQL example.



Solution 1:[1]

For a quick (and dirty) solution, I usually approach this problem by adding an extra test label to the nodes I'm creating, eg:

CREATE (:Person:Test {name:'Alice'})-[:WORKS_FOR]->(:Manager:Test {name:'Bob'})-[:DRIVES]->(:Car:Test {name:'Ford Cortina'})

Then, when I've finised MATCH(x:Test) DETACH DELETE x clears everything up.

Not ideal, but might work for you if your testing is simple.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marj