Skip to content

Commit 952a7c5

Browse files
adamnschFlorentinD
andcommitted
Warn when limiting with from_neo4j
Co-Authored-By: Florentin Dörre <[email protected]>
1 parent c9c4b9f commit 952a7c5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

python-wrapper/src/neo4j_viz/neo4j.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import warnings
34
from typing import Optional, Union
45

56
import neo4j.graph
@@ -59,6 +60,15 @@ def from_neo4j(
5960
elif isinstance(data, neo4j.graph.Graph):
6061
graph = data
6162
elif isinstance(data, Driver):
63+
rel_count = data.execute_query(
64+
"MATCH ()-[r]->() RETURN count(r) as count",
65+
routing_=RoutingControl.READ,
66+
result_transformer_=Result.single,
67+
).get("count") # type: ignore[union-attr]
68+
if rel_count > row_limit:
69+
warnings.warn(
70+
f"Database relationship count exceeds row limit {row_limit}, so limiting will be applied. Increase the `row_limit` if needed"
71+
)
6272
graph = data.execute_query(
6373
f"MATCH (n)-[r]->(m) RETURN n,r,m LIMIT {row_limit}",
6474
routing_=RoutingControl.READ,

python-wrapper/tests/test_neo4j.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,14 @@ def test_from_neo4j_graph_driver(neo4j_session: Session, neo4j_driver: Driver) -
249249
(node_ids[0], node_ids[1], "KNOWS"),
250250
(node_ids[1], node_ids[0], "RELATED"),
251251
]
252+
253+
254+
@pytest.mark.requires_neo4j_and_gds
255+
def test_from_neo4j_graph_row_limit_warning(neo4j_session: Session, neo4j_driver: Driver) -> None:
256+
neo4j_session.run("MATCH (a:_CI_A|_CI_B)-[r]->(b) RETURN a, b, r ORDER BY a").graph()
257+
258+
with pytest.warns(
259+
UserWarning,
260+
match="Database relationship count exceeds row limit 1, so limiting will be applied. Increase the `row_limit` if needed",
261+
):
262+
from_neo4j(neo4j_driver, row_limit=1)

0 commit comments

Comments
 (0)