| 10 | |
| 11 | |
| 12 | class QueryEngine: |
| 13 | def __init__(self, *query_backends: SimilarityIndex): |
| 14 | """ |
| 15 | Create a new querying engine to perform data discovery in datalakes. |
| 16 | Parameters |
| 17 | ---------- |
| 18 | query_backends : SimilarityIndex |
| 19 | A variable number of similarity indexes. |
| 20 | """ |
| 21 | |
| 22 | self.query_backends = query_backends |
| 23 | |
| 24 | @staticmethod |
| 25 | def group_results_by_table( |
| 26 | target_id: str, |
| 27 | results: Iterable[Tuple[str, Iterable[float]]], |
| 28 | table_groups: Optional[Dict] = None, |
| 29 | ) -> Dict: |
| 30 | """ |
| 31 | Groups column-based results by table. |
| 32 | For a given query column, at most one candidate column is considered for each candidate table. |
| 33 | This candidate column is the one with the highest sum of similarity scores. |
| 34 | |
| 35 | Parameters |
| 36 | ---------- |
| 37 | target_id : str |
| 38 | Typically the target column name used to get the results. |
| 39 | results : Iterable[Tuple[str, Iterable[float]]] |
| 40 | One or more pairs of column names (including the table names) and backend similarity scores. |
| 41 | table_groups: Optional[Dict] |
| 42 | Iteratively created table groups. |
| 43 | If None, a new dict is created and populated with the current results. |
| 44 | |
| 45 | Returns |
| 46 | ------- |
| 47 | Dict |
| 48 | A mapping of table names to similarity scores. |
| 49 | """ |
| 50 | |
| 51 | if table_groups is None: |
| 52 | table_groups = defaultdict(list) |
| 53 | candidate_scores = {} |
| 54 | for result_item, result_scores in results: |
| 55 | name_components = result_item.split(".") |
| 56 | table_name, column_name = ( |
| 57 | ".".join(name_components[:-1]), |
| 58 | name_components[-1:][0], |
| 59 | ) |
| 60 | |
| 61 | candidate_column, existing_scores = candidate_scores.get( |
| 62 | table_name, (None, None) |
| 63 | ) |
| 64 | if existing_scores is None or sum(existing_scores) < sum(result_scores): |
| 65 | candidate_scores[table_name] = (column_name, result_scores) |
| 66 | |
| 67 | for table_name, (candidate_column, result_scores) in candidate_scores.items(): |
| 68 | table_groups[table_name].append( |
| 69 | ((target_id, candidate_column), result_scores) |
no outgoing calls
no test coverage detected