Skip to content

Commit 1a91893

Browse files
committed
[WIP] node properties alias
1 parent d7affa8 commit 1a91893

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

graphdatascience/graph/graph_cypher_runner.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ def project(
195195

196196
match_part = str(match_part)
197197

198+
print("nodes", nodes)
199+
print("labels", label_mappings)
200+
198201
case_part = []
199202
if label_mappings:
200203
with_rel = f", {rel_var}" if rel_var else ""
@@ -273,14 +276,21 @@ def _node_properties_spec(self, spec: Any, name: Optional[str] = None) -> NodePr
273276
if isinstance(spec, str):
274277
return NodeProperty(name=name or spec, property_key=spec)
275278

276-
if name is None:
277-
raise ValueError(f"Node properties spec must be used with the dict syntax: {spec}")
279+
if isinstance(spec, dict):
280+
name = spec.pop("name", name)
281+
if name is None:
282+
raise ValueError(
283+
f"Node properties must specify either a name in the outer dict or by using the `name` key: {spec}"
284+
)
285+
property_key = spec.pop("property_key", name)
286+
287+
return NodeProperty(name=name, property_key=property_key, **spec)
278288

279289
if spec is True:
280-
return NodeProperty(name=name, property_key=name)
290+
if name is None:
291+
raise ValueError(f"Node properties spec must be used with the dict syntax: {spec}")
281292

282-
if isinstance(spec, dict):
283-
return NodeProperty(name=name, property_key=name, **spec)
293+
return NodeProperty(name=name, property_key=name)
284294

285295
raise TypeError(f"Invalid node property specification: {spec}")
286296

graphdatascience/tests/unit/test_graph_cypher.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ def test_multiple_multi_graph(runner: CollectingQueryRunner, gds: GraphDataScien
220220

221221
@pytest.mark.parametrize("server_version", [ServerVersion(2, 4, 0)])
222222
def test_node_properties(runner: CollectingQueryRunner, gds: GraphDataScience) -> None:
223+
# G, _ = gds.graph.cypher.project(
224+
# "g", nodes=dict(L1=["prop1"], L2=["prop2", "prop3"], L3=dict(prop4=True, prop5=dict()))
225+
# )
223226
G, _ = gds.graph.cypher.project(
224-
"g", nodes=dict(L1=["prop1"], L2=["prop2", "prop3"], L3=dict(prop4=True, prop5=dict()))
227+
"g", nodes={"L1": ["prop1"], "L2": ["prop2", "prop3"], "L3": {"prop4": True, "prop5": {}}}
225228
)
226229

227230
assert G.name() == "g"
@@ -247,3 +250,25 @@ def test_node_properties(runner: CollectingQueryRunner, gds: GraphDataScience) -
247250
"sourceNodeProperties: sourceNodeProperties, "
248251
"targetNodeProperties: targetNodeProperties})"
249252
)
253+
254+
255+
@pytest.mark.parametrize("server_version", [ServerVersion(2, 4, 0)])
256+
def test_node_properties_alias(runner: CollectingQueryRunner, gds: GraphDataScience) -> None:
257+
G, _ = gds.graph.cypher.project(
258+
"g", nodes=dict(A=dict(target_prop1="source_prop1", target_prop2=dict(property_key="source_prop2")))
259+
)
260+
261+
assert G.name() == "g"
262+
assert runner.last_params() == dict(graph_name="g")
263+
264+
assert runner.last_query() == (
265+
"""MATCH (source:A)-->(target:A)
266+
WITH source, target, """
267+
"[{target_prop1: source.source_prop1, target_prop1: source.source_prop2}] AS sourceNodeProperties"
268+
"""[{target_prop1: target.source_prop1, target_prop1: target.source_prop2}] AS targetNodeProperties
269+
RETURN gds.graph.project($graph_name, source, target, {"""
270+
"sourceNodeLabels: labels(source), "
271+
"targetNodeLabels: labels(target), "
272+
"sourceNodeProperties: sourceNodeProperties, "
273+
"targetNodeProperties: targetNodeProperties})"
274+
)

0 commit comments

Comments
 (0)