Skip to content

Commit

Permalink
refactor: simplified the code
Browse files Browse the repository at this point in the history
  • Loading branch information
jincejames committed Apr 23, 2024
1 parent 70cfffd commit 52357bb
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/databricks/labs/ucx/hive_metastore/view_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _next_batch(self, views: set[ViewToMigrate]) -> set[ViewToMigrate]:
result: set[ViewToMigrate] = set()
for view in views:
view_deps = set(view.dependencies)
self.check_circular_dependency(view, views)
self._check_circular_dependency(view, views)
if len(view_deps) == 0:
result.add(view)
else:
Expand All @@ -132,28 +132,26 @@ def _next_batch(self, views: set[ViewToMigrate]) -> set[ViewToMigrate]:
raise ValueError(f"Invalid table references are preventing migration: {views}")
return result

def check_circular_dependency(self, initial_view, views):
def _check_circular_dependency(self, initial_view, views):
queue = []
queue.extend(dep for dep in initial_view.dependencies)
print(queue)
while queue:
current_view = self._get_view_instance(queue.pop(0).key, views)
if current_view:
if current_view == initial_view:
raise ValueError(
f"Circular dependency detected between {initial_view.src.name} and {current_view.src.name} "
)
queue.extend(dep for dep in current_view.dependencies)
if not current_view:
continue
if current_view == initial_view:
raise ValueError(
f"Circular dependency detected between {initial_view.src.name} and {current_view.src.name} "
)
queue.extend(dep for dep in current_view.dependencies)

def _get_view_instance(self, key: str, views: set[ViewToMigrate]) -> ViewToMigrate | None:
# This method acts as a mapper between TableView and ViewToMigrate. We check if the key passed matches with
# any of the views in the list of views. This means the circular dependency will be identified only
# if the dependencies are present in the list of views passed to _next_batch() or the _result_view_list
# ToDo: see if a mapper between TableView and ViewToMigrate can be implemented
instance = None
all_views = list(views) + self._result_view_list
for view in all_views:
if view.src.key == key:
instance = view
break
return instance
return view
return None

0 comments on commit 52357bb

Please sign in to comment.