|
| 1 | +import logging |
| 2 | +from functools import cached_property |
| 3 | + |
| 4 | +from databricks.labs.ucx.framework.owners import ( |
| 5 | + Ownership, |
| 6 | + AdministratorLocator, |
| 7 | + LegacyQueryOwnership, |
| 8 | + WorkspacePathOwnership, |
| 9 | +) |
| 10 | +from databricks.labs.ucx.hive_metastore import TablesCrawler |
| 11 | +from databricks.labs.ucx.hive_metastore.grants import GrantsCrawler |
| 12 | +from databricks.labs.ucx.hive_metastore.table_migration_status import TableMigrationStatus |
| 13 | +from databricks.labs.ucx.hive_metastore.tables import Table |
| 14 | +from databricks.labs.ucx.source_code.base import UsedTable |
| 15 | +from databricks.labs.ucx.source_code.used_table import UsedTablesCrawler |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class TableOwnership(Ownership[Table]): |
| 21 | + """Determine ownership of tables in the inventory based on the following rules: |
| 22 | + - If a table is owned by a principal in the grants table, then that principal is the owner. |
| 23 | + - If a table is written to by a query, then the owner of that query is the owner of the table. |
| 24 | + - If a table is written to by a notebook or file, then the owner of the path is the owner of the table. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + administrator_locator: AdministratorLocator, |
| 30 | + grants_crawler: GrantsCrawler, |
| 31 | + used_tables_in_paths: UsedTablesCrawler, |
| 32 | + used_tables_in_queries: UsedTablesCrawler, |
| 33 | + legacy_query_ownership: LegacyQueryOwnership, |
| 34 | + workspace_path_ownership: WorkspacePathOwnership, |
| 35 | + ) -> None: |
| 36 | + super().__init__(administrator_locator) |
| 37 | + self._grants_crawler = grants_crawler |
| 38 | + self._used_tables_in_paths = used_tables_in_paths |
| 39 | + self._used_tables_in_queries = used_tables_in_queries |
| 40 | + self._legacy_query_ownership = legacy_query_ownership |
| 41 | + self._workspace_path_ownership = workspace_path_ownership |
| 42 | + |
| 43 | + def _maybe_direct_owner(self, record: Table) -> str | None: |
| 44 | + owner = self._maybe_from_grants(record) |
| 45 | + if owner: |
| 46 | + return owner |
| 47 | + return self._maybe_from_sources(record) |
| 48 | + |
| 49 | + def _maybe_from_sources(self, record: Table) -> str | None: |
| 50 | + used_table = self._used_tables_snapshot.get((record.catalog, record.database, record.name)) |
| 51 | + if not used_table: |
| 52 | + return None |
| 53 | + # If something writes to a table, then it's an owner of it |
| 54 | + if not used_table.is_write: |
| 55 | + return None |
| 56 | + if used_table.source_type == 'QUERY' and used_table.query_id: |
| 57 | + return self._legacy_query_ownership.owner_of(used_table.query_id) |
| 58 | + if used_table.source_type in {'NOTEBOOK', 'FILE'}: |
| 59 | + return self._workspace_path_ownership.owner_of_path(used_table.source_id) |
| 60 | + logger.warning(f"Unknown source type {used_table.source_type} for {used_table.source_id}") |
| 61 | + return None |
| 62 | + |
| 63 | + @cached_property |
| 64 | + def _used_tables_snapshot(self) -> dict[tuple[str, str, str], UsedTable]: |
| 65 | + index = {} |
| 66 | + for collection in (self._used_tables_in_paths.snapshot(), self._used_tables_in_queries.snapshot()): |
| 67 | + for used_table in collection: |
| 68 | + key = used_table.catalog_name, used_table.schema_name, used_table.table_name |
| 69 | + index[key] = used_table |
| 70 | + return index |
| 71 | + |
| 72 | + def _maybe_from_grants(self, record: Table) -> str | None: |
| 73 | + for grant in self._grants_snapshot: |
| 74 | + if not grant.action_type == 'OWN': |
| 75 | + continue |
| 76 | + object_type, full_name = grant.this_type_and_key() |
| 77 | + if object_type == 'TABLE' and full_name == record.key: |
| 78 | + return grant.principal |
| 79 | + if object_type in {'DATABASE', 'SCHEMA'} and full_name == f"{record.catalog}.{record.database}": |
| 80 | + return grant.principal |
| 81 | + return None |
| 82 | + |
| 83 | + @cached_property |
| 84 | + def _grants_snapshot(self): |
| 85 | + return self._grants_crawler.snapshot() |
| 86 | + |
| 87 | + |
| 88 | +class TableMigrationOwnership(Ownership[TableMigrationStatus]): |
| 89 | + """Determine ownership of table migration records in the inventory. |
| 90 | +
|
| 91 | + This is the owner of the source table, if (and only if) the source table is present in the inventory. |
| 92 | + """ |
| 93 | + |
| 94 | + def __init__(self, tables_crawler: TablesCrawler, table_ownership: TableOwnership) -> None: |
| 95 | + super().__init__(table_ownership._administrator_locator) # TODO: Fix this |
| 96 | + self._tables_crawler = tables_crawler |
| 97 | + self._table_ownership = table_ownership |
| 98 | + self._indexed_tables: dict[tuple[str, str], Table] | None = None |
| 99 | + |
| 100 | + def _tables_snapshot_index(self, reindex: bool = False) -> dict[tuple[str, str], Table]: |
| 101 | + index = self._indexed_tables |
| 102 | + if index is None or reindex: |
| 103 | + snapshot = self._tables_crawler.snapshot() |
| 104 | + index = {(table.database, table.name): table for table in snapshot} |
| 105 | + self._indexed_tables = index |
| 106 | + return index |
| 107 | + |
| 108 | + def _maybe_direct_owner(self, record: TableMigrationStatus) -> str | None: |
| 109 | + index = self._tables_snapshot_index() |
| 110 | + source_table = index.get((record.src_schema, record.src_table), None) |
| 111 | + return self._table_ownership.owner_of(source_table) if source_table is not None else None |
0 commit comments