Skip to content
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

Detect cycles (For issue #30) #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions src/bqva/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def __init__(
view = self._get_table(project_id, dataset_id, view_id)
assert view.table_type == "VIEW"
self.view = view
self.views_in_cycle: List[TableNode] = []

def __str__(self):
return self.view.full_table_id
Expand All @@ -153,7 +154,8 @@ def _get_table(self, project_id: str, dataset_id: str, table_id: str) -> Table:
view_ref = dataset_ref.table(table_id)
return self.client.get_table(view_ref)

def _build_tree(self, node: TableNode) -> TableNode:
def _build_tree(self, node: TableNode, ancestors: Optional[List[Table]] = None) -> TableNode:
if ancestors is None: ancestors = []
table = node.table
log.info(f"{node.name}")
log.info(f"{node.name}: object is of type {table.table_type}")
Expand All @@ -170,8 +172,13 @@ def _build_tree(self, node: TableNode) -> TableNode:
child_node = TableNode(
client=self.client, table=child_table, parent=node
)
log.info(f"{node.name}: analyzing '{child_node.name}' ({i}/{count})")
self._build_tree(child_node)
# Check whether this child table is an ancestor
if child_table in ancestors:
log.info(f"{node.name}: found cycle '{child_node.name}' ({i}/{count})")
self.views_in_cycle.append(child_node)
else:
log.info(f"{node.name}: analyzing '{child_node.name}' ({i}/{count})")
self._build_tree(child_node, ancestors + [table])
return node

def apply_permissions(self):
Expand Down
5 changes: 5 additions & 0 deletions src/bqva/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def format_tree(view: ViewAnalyzer, show_key=False, show_status=False):
output.append(format_key())
for pre, _, node in RenderTree(view.tree):
output.append(pre + format_node(node, show_status=show_status))
if len(view.views_in_cycle) > 0:
cycle_view_names = [format_node(x) for x in view.views_in_cycle]
output.append(f"Found a cycle! This cycle(s) include (but are not necessarily limited to):")
for s in cycle_view_names:
output.append("\t" + s)
return "\n".join(output)


Expand Down