Skip to content

update NodeListGetRelationshipQuery to be more efficient #6387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
allow filters for inbound/outbound/bidirectional rel identifiers
  • Loading branch information
ajtmccarty committed May 1, 2025
commit aece5b1e703586a603c48e1d66e97f416a79f6c5
21 changes: 16 additions & 5 deletions backend/infrahub/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,20 +1229,31 @@ async def _enrich_node_dicts_with_relationships(
if not prefetch_relationships and not fields:
return
cardinality_one_identifiers_by_kind: dict[str, dict[str, RelationshipDirection]] | None = None
all_identifiers: list[str] | None = None
outbound_identifiers: set[str] | None = None
inbound_identifiers: set[str] | None = None
bidirectional_identifiers: set[str] | None = None
if not prefetch_relationships:
cardinality_one_identifiers_by_kind = _get_cardinality_one_identifiers_by_kind(
nodes=nodes_by_id.values(), fields=fields or {}
)
all_identifiers_set: set[str] = set()
outbound_identifiers = set()
inbound_identifiers = set()
bidirectional_identifiers = set()
for identifier_direction_map in cardinality_one_identifiers_by_kind.values():
all_identifiers_set.update(identifier_direction_map.keys())
all_identifiers = list(all_identifiers_set)
for identifier, direction in identifier_direction_map.items():
if direction is RelationshipDirection.OUTBOUND:
outbound_identifiers.add(identifier)
elif direction is RelationshipDirection.INBOUND:
inbound_identifiers.add(identifier)
elif direction is RelationshipDirection.BIDIR:
bidirectional_identifiers.add(identifier)

query = await NodeListGetRelationshipsQuery.init(
db=db,
ids=list(nodes_by_id.keys()),
relationship_identifiers=all_identifiers,
outbound_identifiers=None if outbound_identifiers is None else list(outbound_identifiers),
inbound_identifiers=None if inbound_identifiers is None else list(inbound_identifiers),
bidirectional_identifiers=None if bidirectional_identifiers is None else list(bidirectional_identifiers),
branch=branch,
at=at,
branch_agnostic=branch_agnostic,
Expand Down
23 changes: 17 additions & 6 deletions backend/infrahub/core/query/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,25 @@ class NodeListGetRelationshipsQuery(Query):
type: QueryType = QueryType.READ
insert_return: bool = False

def __init__(self, ids: list[str], relationship_identifiers: list[str] | None = None, **kwargs):
def __init__(
self,
ids: list[str],
outbound_identifiers: list[str] | None = None,
inbound_identifiers: list[str] | None = None,
bidirectional_identifiers: list[str] | None = None,
**kwargs,
):
self.ids = ids
self.relationship_identifiers = relationship_identifiers
self.outbound_identifiers = outbound_identifiers
self.inbound_identifiers = inbound_identifiers
self.bidirectional_identifiers = bidirectional_identifiers
super().__init__(**kwargs)

async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG002
self.params["ids"] = self.ids
self.params["relationship_identifiers"] = self.relationship_identifiers
self.params["outbound_identifiers"] = self.outbound_identifiers
self.params["inbound_identifiers"] = self.inbound_identifiers
self.params["bidirectional_identifiers"] = self.bidirectional_identifiers

rels_filter, rels_params = self.branch.get_query_filter_path(at=self.at, branch_agnostic=self.branch_agnostic)
self.params.update(rels_params)
Expand All @@ -666,7 +677,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
CALL {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to remember that we'll need to convert this one for 1.3, maybe we can open an issue and tag all impacted PRs on it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Fatih already made this issue to go over all the CALL subqueries before the 1.3 release. I'm not sure which issue it is though

WITH n
MATCH (n)<-[:IS_RELATED]-(rel:Relationship)<-[:IS_RELATED]-(peer)
WHERE ($relationship_identifiers IS NULL OR rel.name in $relationship_identifiers)
WHERE ($inbound_identifiers IS NULL OR rel.name in $inbound_identifiers)
AND n.uuid <> peer.uuid
WITH DISTINCT n, rel, peer
CALL {
Expand All @@ -691,7 +702,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
UNION
WITH n
MATCH (n)-[:IS_RELATED]->(rel:Relationship)-[:IS_RELATED]->(peer)
WHERE ($relationship_identifiers IS NULL OR rel.name in $relationship_identifiers)
WHERE ($outbound_identifiers IS NULL OR rel.name in $outbound_identifiers)
AND n.uuid <> peer.uuid
WITH DISTINCT n, rel, peer
CALL {
Expand All @@ -716,7 +727,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
UNION
WITH n
MATCH (n)-[:IS_RELATED]->(rel:Relationship)<-[:IS_RELATED]-(peer)
WHERE ($relationship_identifiers IS NULL OR rel.name in $relationship_identifiers)
WHERE ($bidirectional_identifiers IS NULL OR rel.name in $bidirectional_identifiers)
AND n.uuid <> peer.uuid
WITH DISTINCT n, rel, peer
CALL {
Expand Down
42 changes: 42 additions & 0 deletions backend/tests/unit/core/test_node_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,48 @@ async def test_query_NodeListGetRelationshipsQuery_hierarchical(
)
assert parent_peer_ids == {europe_id}

# check with inbound only filter
query = await NodeListGetRelationshipsQuery.init(
db=db,
ids=node_ids,
branch=default_branch,
outbound_identifiers=[],
inbound_identifiers=["parent__child"],
bidirectional_identifiers=[],
)
await query.execute(db=db)
grouped_peer_nodes = query.get_peers_group_by_node()
assert grouped_peer_nodes.has_node(paris_id)
child_peer_ids = grouped_peer_nodes.get_peer_ids(
node_id=paris_id, rel_name="parent__child", direction=RelationshipDirection.INBOUND
)
assert child_peer_ids == {paris_r1_id, paris_r2_id}
parent_peer_ids = grouped_peer_nodes.get_peer_ids(
node_id=paris_id, rel_name="parent__child", direction=RelationshipDirection.OUTBOUND
)
assert not parent_peer_ids

# check with outbound only filter
query = await NodeListGetRelationshipsQuery.init(
db=db,
ids=node_ids,
branch=default_branch,
outbound_identifiers=["parent__child"],
inbound_identifiers=[],
bidirectional_identifiers=[],
)
await query.execute(db=db)
grouped_peer_nodes = query.get_peers_group_by_node()
assert grouped_peer_nodes.has_node(paris_id)
child_peer_ids = grouped_peer_nodes.get_peer_ids(
node_id=paris_id, rel_name="parent__child", direction=RelationshipDirection.INBOUND
)
assert not child_peer_ids
parent_peer_ids = grouped_peer_nodes.get_peer_ids(
node_id=paris_id, rel_name="parent__child", direction=RelationshipDirection.OUTBOUND
)
assert parent_peer_ids == {europe_id}


async def test_query_NodeDeleteQuery(
db: InfrahubDatabase,
Expand Down
Loading