-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from neo4j/208-collect-subqueries
Add Collect subqueries
- Loading branch information
Showing
8 changed files
with
188 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
"@neo4j/cypher-builder": minor | ||
--- | ||
|
||
Add support for Collect subqueries: | ||
|
||
```js | ||
const dog = new Cypher.Node({ labels: ["Dog"] }); | ||
const person = new Cypher.Node({ labels: ["Person"] }); | ||
|
||
const subquery = new Cypher.Match( | ||
new Cypher.Pattern(person).related(new Cypher.Relationship({ type: "HAS_DOG" })).to(dog) | ||
).return(dog.property("name")); | ||
|
||
const match = new Cypher.Match(person) | ||
.where(Cypher.in(new Cypher.Literal("Ozzy"), new Cypher.Collect(subquery))) | ||
.return(person); | ||
``` | ||
|
||
```cypher | ||
MATCH (this0:Person) | ||
WHERE "Ozzy" IN COLLECT { | ||
MATCH (this0:Person)-[this1:HAS_DOG]->(this2:Dog) | ||
RETURN this2.name | ||
} | ||
RETURN this0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Cypher from "../.."; | ||
|
||
describe("Collect Subquery", () => { | ||
test("Collect expression with subclause", () => { | ||
const dog = new Cypher.Node({ labels: ["Dog"] }); | ||
const person = new Cypher.Node({ labels: ["Person"] }); | ||
|
||
const subquery = new Cypher.Match( | ||
new Cypher.Pattern(person).related(new Cypher.Relationship({ type: "HAS_DOG" })).to(dog) | ||
).return(dog.property("name")); | ||
|
||
const match = new Cypher.Match(person) | ||
.where(Cypher.in(new Cypher.Literal("Ozzy"), new Cypher.Collect(subquery))) | ||
.return(person); | ||
|
||
const queryResult = match.build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot(` | ||
"MATCH (this0:Person) | ||
WHERE \\"Ozzy\\" IN COLLECT { | ||
MATCH (this0:Person)-[this1:HAS_DOG]->(this2:Dog) | ||
RETURN this2.name | ||
} | ||
RETURN this0" | ||
`); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`{}`); | ||
}); | ||
|
||
test("Return collect subquery with an union", () => { | ||
const dog = new Cypher.Node({ labels: ["Dog"] }); | ||
const cat = new Cypher.Node({ labels: ["Cat"] }); | ||
const person = new Cypher.Node({ labels: ["Person"] }); | ||
|
||
const matchDog = new Cypher.Match( | ||
new Cypher.Pattern(person).related(new Cypher.Relationship({ type: "HAS_DOG" })).to(dog) | ||
).return([dog.property("name"), "petName"]); | ||
const matchCat = new Cypher.Match( | ||
new Cypher.Pattern(person).related(new Cypher.Relationship({ type: "HAS_CAT" })).to(cat) | ||
).return([cat.property("name"), "petName"]); | ||
|
||
const subquery = new Cypher.Union(matchDog, matchCat); | ||
|
||
const match = new Cypher.Match(person).return(person, [new Cypher.Collect(subquery), "petNames"]); | ||
|
||
const queryResult = match.build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot(` | ||
"MATCH (this0:Person) | ||
RETURN this0, COLLECT { | ||
MATCH (this0:Person)-[this1:HAS_DOG]->(this2:Dog) | ||
RETURN this2.name AS petName | ||
UNION | ||
MATCH (this0:Person)-[this3:HAS_CAT]->(this4:Cat) | ||
RETURN this4.name AS petName | ||
} AS petNames" | ||
`); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`{}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { CypherEnvironment } from "../../Environment"; | ||
import { padBlock } from "../../utils/pad-block"; | ||
import { Subquery } from "./Subquery"; | ||
|
||
/** | ||
* @see [Cypher Documentation](https://neo4j.com/docs/cypher-manual/current/subqueries/collect/) | ||
* @group Other | ||
*/ | ||
export class Collect extends Subquery { | ||
/** | ||
* @internal | ||
*/ | ||
public getCypher(env: CypherEnvironment): string { | ||
const subQueryStr = this.subquery.getCypher(env); | ||
const paddedSubQuery = padBlock(subQueryStr); | ||
return `COLLECT {\n${paddedSubQuery}\n}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { CypherASTNode } from "../../CypherASTNode"; | ||
import type { Clause } from "../../clauses/Clause"; | ||
|
||
export abstract class Subquery extends CypherASTNode { | ||
protected subquery: CypherASTNode; | ||
|
||
constructor(subquery: Clause) { | ||
super(); | ||
const rootQuery = subquery.getRoot(); | ||
this.addChildren(rootQuery); | ||
this.subquery = rootQuery; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters