Skip to content

Commit f890a18

Browse files
committed
fix: handle missing query results in graph ingestion example
1 parent b581806 commit f890a18

2 files changed

Lines changed: 58 additions & 24 deletions

File tree

bindings/python/examples/16_import_database_vs_transactional_graph_ingest.py

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import shutil
5858
import time
5959
from pathlib import Path
60-
from typing import Any, Dict, List, Tuple
60+
from typing import Dict, List, Tuple
6161

6262
import arcadedb_embedded as arcadedb
6363

@@ -150,10 +150,24 @@ def sample_ids(total: int) -> List[int]:
150150
return sorted(values)
151151

152152

153+
def query_one_or_none(result_set):
154+
try:
155+
return result_set.one()
156+
except ValueError as exc:
157+
if str(exc) == "Query returned no results":
158+
return None
159+
raise
160+
161+
153162
def collect_vertex_sample(
154163
db, vertex_type: str, vertex_id: int, props: List[ColumnDef]
155164
) -> dict:
156-
row = db.query("sql", f"SELECT FROM {vertex_type} WHERE Id = {vertex_id}").one()
165+
row = query_one_or_none(
166+
db.query("sql", f"SELECT FROM {vertex_type} WHERE Id = {vertex_id}")
167+
)
168+
if row is None:
169+
return {"Id": vertex_id, "missing": True}
170+
157171
sample = {"Id": int(row.get("Id"))}
158172
for column_name, _ in props:
159173
sample[column_name] = row.get(column_name)
@@ -169,14 +183,18 @@ def collect_edge_sample(
169183
"r.Id AS Id",
170184
] + [f"r.{column_name} AS {column_name}" for column_name, _ in props]
171185

172-
row = db.query(
173-
"opencypher",
174-
(
175-
f"MATCH (a)-[r:{edge_type}]->(b) "
176-
f"WHERE r.Id = {edge_id} "
177-
f"RETURN {', '.join(projections)}"
178-
),
179-
).one()
186+
row = query_one_or_none(
187+
db.query(
188+
"opencypher",
189+
(
190+
f"MATCH (a)-[r:{edge_type}]->(b) "
191+
f"WHERE r.Id = {edge_id} "
192+
f"RETURN {', '.join(projections)}"
193+
),
194+
)
195+
)
196+
if row is None:
197+
return {"Id": edge_id, "missing": True}
180198

181199
sample = {
182200
"from_id": int(row.get("from_id")),
@@ -216,21 +234,33 @@ def collect_graph_signature(
216234
"max(Id) AS max_id",
217235
] + [f"sum({name}) AS sum_{name}" for name in vertex_int_props]
218236

219-
edge_aggregate_fields = [
220-
"count(*) AS count",
221-
"sum(Id) AS sum_id",
222-
"min(Id) AS min_id",
223-
"max(Id) AS max_id",
224-
] + [f"sum({name}) AS sum_{name}" for name in edge_int_props]
237+
edge_match_aggregate_fields = [
238+
"count(r) AS count",
239+
"sum(r.Id) AS sum_id",
240+
"min(r.Id) AS min_id",
241+
"max(r.Id) AS max_id",
242+
] + [f"sum(r.{name}) AS sum_{name}" for name in edge_int_props]
243+
244+
vertex_aggregate = query_one_or_none(
245+
db.query(
246+
"sql",
247+
f"SELECT {', '.join(vertex_aggregate_fields)} FROM {vertex_type}",
248+
)
249+
)
250+
edge_aggregate = query_one_or_none(
251+
db.query(
252+
"opencypher",
253+
(
254+
f"MATCH ()-[r:{edge_type}]->() "
255+
f"RETURN {', '.join(edge_match_aggregate_fields)}"
256+
),
257+
)
258+
)
225259

226-
vertex_aggregate = db.query(
227-
"sql",
228-
f"SELECT {', '.join(vertex_aggregate_fields)} FROM {vertex_type}",
229-
).one()
230-
edge_aggregate = db.query(
231-
"sql",
232-
f"SELECT {', '.join(edge_aggregate_fields)} FROM {edge_type}",
233-
).one()
260+
if vertex_aggregate is None:
261+
vertex_aggregate = {}
262+
if edge_aggregate is None:
263+
edge_aggregate = {}
234264

235265
return {
236266
"vertex_aggregate": {

bindings/python/tests/test_docs_examples.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
DOCS_ROOT = Path(__file__).resolve().parents[1] / "docs"
1313
PYTHON_BLOCK_RE = re.compile(r"```python\n(.*?)```", re.DOTALL)
14+
pytestmark = pytest.mark.skipif(
15+
not DOCS_ROOT.exists(),
16+
reason="bindings/python/docs is not present on this branch",
17+
)
1418

1519

1620
def _doc_path(relative_path: str) -> Path:

0 commit comments

Comments
 (0)