Skip to content

Commit 74b64ae

Browse files
committed
Check for empty aggregation result
1 parent 9a05f35 commit 74b64ae

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222

2323
## Improvements
2424

25+
* Improved the error message if `gds.graph.project.cypher` produces an empty graph.
26+
2527

2628
## Other changes

graphdatascience/graph/graph_cypher_runner.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from __future__ import annotations
2+
13
import re
24
from itertools import chain, zip_longest
35
from typing import Any, Optional
46

7+
from pandas import Series
8+
59
from ..caller_base import CallerBase
610
from ..query_runner.query_runner import QueryRunner
711
from ..server_version.server_version import ServerVersion
@@ -41,7 +45,10 @@ def project(
4145

4246
GraphCypherRunner._verify_query_ends_with_return_clause(self._namespace, query)
4347

44-
result = self._query_runner.run_cypher(query, params, database, False).squeeze()
48+
result: Optional[Series[Any]] = self._query_runner.run_cypher(query, params, database, False).squeeze()
49+
50+
if result is None or result.empty:
51+
raise ValueError("Projected graph cannot be empty.")
4552

4653
try:
4754
graph_name = str(result["graphName"])

graphdatascience/tests/integration/test_graph_ops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def test_cypher_projection(gds: GraphDataScience) -> None:
9494
assert result["exists"]
9595

9696

97+
@pytest.mark.filterwarnings("ignore: One of the labels in your query is not available in the database")
98+
def test_cypher_projection_empty_graph(gds: GraphDataScience) -> None:
99+
with pytest.raises(ValueError, match="Projected graph cannot be empty"):
100+
gds.graph.cypher.project("MATCH (n:MISSING_LABEL) RETURN gds.graph.project('some-graph', n, null)")
101+
102+
97103
def test_beta_project_subgraph(runner: QueryRunner, gds: GraphDataScience) -> None:
98104
from_G, _ = gds.graph.project(GRAPH_NAME, {"Node": {"properties": "x"}}, "*")
99105

0 commit comments

Comments
 (0)