|
|
from dataclasses import dataclass |
|
|
import copy |
|
|
from typing import * |
|
|
|
|
|
|
|
|
@dataclass |
|
|
class Table: |
|
|
name: str |
|
|
columns: Set[str] |
|
|
column_types: Dict[str, str] = None |
|
|
|
|
|
|
|
|
@dataclass |
|
|
class DerivedColumn: |
|
|
name: str |
|
|
provenance_sql: str |
|
|
provenance_tables: Dict[str, Table] |
|
|
|
|
|
|
|
|
@dataclass |
|
|
class DataBase: |
|
|
name: str |
|
|
tables: Dict[str, Table] |
|
|
derived_columns: List[DerivedColumn] = None |
|
|
|
|
|
def get_column_count(self, include_derived_columns: bool = False): |
|
|
|
|
|
if include_derived_columns: |
|
|
temp_self = copy.deepcopy(self) |
|
|
if temp_self.derived_columns is not None: |
|
|
for dc in temp_self.derived_columns: |
|
|
for tabname, table in dc.provenance_tables.items(): |
|
|
if tabname not in temp_self.tables: |
|
|
temp_self.tables[tabname] = Table(name=tabname, columns=set()) |
|
|
temp_self.tables[tabname].columns |= table.columns |
|
|
else: |
|
|
temp_self = self |
|
|
return sum([len(tab.columns) for _, tab in temp_self.tables.items()]) |
|
|
|
|
|
def compare_column_overlap(self, other_db, include_derived_columns=False): |
|
|
count = 0 |
|
|
if include_derived_columns: |
|
|
temp_self = copy.deepcopy(self) |
|
|
temp_other = copy.deepcopy(other_db) |
|
|
if temp_self.derived_columns is not None: |
|
|
for dc in temp_self.derived_columns: |
|
|
for tabname, table in dc.provenance_tables.items(): |
|
|
if tabname not in temp_self.tables: |
|
|
temp_self.tables[tabname] = Table(name=tabname, columns=set()) |
|
|
temp_self.tables[tabname].columns |= table.columns |
|
|
if temp_other.derived_columns is not None: |
|
|
for dc in temp_other.derived_columns: |
|
|
for tabname, table in dc.provenance_tables.items(): |
|
|
if tabname not in temp_other.tables: |
|
|
temp_other.tables[tabname] = Table(name=tabname, columns=set()) |
|
|
temp_other.tables[tabname].columns |= table.columns |
|
|
else: |
|
|
temp_self = self |
|
|
temp_other = other_db |
|
|
|
|
|
for tabname, table in temp_self.tables.items(): |
|
|
if tabname not in temp_other.tables: |
|
|
continue |
|
|
if tabname in other_db.tables: |
|
|
count += len(table.columns.intersection(other_db.tables[tabname].columns)) |
|
|
return count |
|
|
|
|
|
def get_schema_for_sqlglot(self): |
|
|
return { |
|
|
tabname: { |
|
|
col: coltype |
|
|
for col, coltype in table.column_types.items() |
|
|
} |
|
|
for tabname, table in self.tables.items() |
|
|
} |
|
|
|
|
|
|
|
|
def filter_real_table_columns(real_db: DataBase, filter_db: DataBase, debug=False): |
|
|
bad_tables = [] |
|
|
for tabname, tab in filter_db.tables.items(): |
|
|
if tabname not in real_db.tables: |
|
|
bad_tables.append(tabname) |
|
|
else: |
|
|
if debug and tab.columns-real_db.tables[tabname].columns: |
|
|
print(f"bad cols ", tab.columns-real_db.tables[tabname].columns) |
|
|
tab.columns = tab.columns.intersection(real_db.tables[tabname].columns) |
|
|
if filter_db.derived_columns: |
|
|
for dc in filter_db.derived_columns: |
|
|
dc_bad_tables = [] |
|
|
for tabname, table in dc.provenance_tables.items(): |
|
|
if tabname not in real_db.tables: |
|
|
dc_bad_tables.append(tabname) |
|
|
else: |
|
|
cols = table.columns |
|
|
if debug: |
|
|
if cols.intersection(real_db.tables[tabname].columns) != cols: |
|
|
print(real_db.name, tabname, cols - real_db.tables[tabname].columns) |
|
|
dc.provenance_tables[tabname].columns = cols.intersection(real_db.tables[tabname].columns) |
|
|
for tab in dc_bad_tables: |
|
|
dc.provenance_tables.pop(tab) |
|
|
for tab in bad_tables: |
|
|
if debug: |
|
|
print(f"bad table ", tab) |
|
|
filter_db.tables.pop(tab, None) |
|
|
return filter_db |