Skip to content

Commit

Permalink
New ancestor/descendant algo (#5326)
Browse files Browse the repository at this point in the history
* New ancestor/descendant algo

* changelog
  • Loading branch information
iknox-fa committed Jun 2, 2022
1 parent 189c06d commit eea872c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .changes/unreleased/Fixes-20220601-135908.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: Fixes
body: Change node ancestor/descendant algo, fixes issue where downstream models aren't
run when using networkx >= 2.8.1
time: 2022-06-01T13:59:08.886215-05:00
custom:
Author: iknox-fa
Issue: "5286"
PR: "5326"
14 changes: 5 additions & 9 deletions core/dbt/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@ def ancestors(self, node: UniqueId, max_depth: Optional[int] = None) -> Set[Uniq
"""Returns all nodes having a path to `node` in `graph`"""
if not self.graph.has_node(node):
raise InternalException(f"Node {node} not found in the graph!")
# This used to use nx.utils.reversed(self.graph), but that is deprecated,
# so changing to use self.graph.reverse(copy=False) as recommeneded
G = self.graph.reverse(copy=False) if self.graph.is_directed() else self.graph
anc = nx.single_source_shortest_path_length(G=G, source=node, cutoff=max_depth).keys()
return anc - {node}
return {
child
for _, child in nx.bfs_edges(self.graph, node, reverse=True, depth_limit=max_depth)
}

def descendants(self, node: UniqueId, max_depth: Optional[int] = None) -> Set[UniqueId]:
"""Returns all nodes reachable from `node` in `graph`"""
if not self.graph.has_node(node):
raise InternalException(f"Node {node} not found in the graph!")
des = nx.single_source_shortest_path_length(
G=self.graph, source=node, cutoff=max_depth
).keys()
return des - {node}
return {child for _, child in nx.bfs_edges(self.graph, node, depth_limit=max_depth)}

def select_childrens_parents(self, selected: Set[UniqueId]) -> Set[UniqueId]:
ancestors_for = self.select_children(selected) | selected
Expand Down

0 comments on commit eea872c

Please sign in to comment.