Datasets:

Languages:
English
ArXiv:
License:
File size: 4,117 Bytes
116cc62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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