|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import List, Generator, Any |
| 3 | +import copy |
4 | 4 | import fnmatch
|
| 5 | +from typing import List |
| 6 | + |
5 | 7 | import pyarrow as pa
|
6 | 8 |
|
7 | 9 | from cloudquery.sdk.schema import arrow
|
@@ -87,17 +89,90 @@ def tables_to_arrow_schemas(tables: List[Table]):
|
87 | 89 |
|
88 | 90 |
|
89 | 91 | def filter_dfs(
|
90 |
| - tables: List[Table], include_tables: List[str], skip_tables: List[str] |
| 92 | + tables: List[Table], |
| 93 | + include_tables: List[str], |
| 94 | + skip_tables: List[str], |
| 95 | + skip_dependent_tables: bool = False, |
91 | 96 | ) -> List[Table]:
|
92 |
| - filtered: List[Table] = [] |
| 97 | + flattened_tables = flatten_tables(tables) |
| 98 | + for include_pattern in include_tables: |
| 99 | + matched = any( |
| 100 | + fnmatch.fnmatch(table.name, include_pattern) for table in flattened_tables |
| 101 | + ) |
| 102 | + if not matched: |
| 103 | + raise ValueError( |
| 104 | + f"tables include a pattern {include_pattern} with no matches" |
| 105 | + ) |
| 106 | + |
| 107 | + for exclude_pattern in skip_tables: |
| 108 | + matched = any( |
| 109 | + fnmatch.fnmatch(table.name, exclude_pattern) for table in flattened_tables |
| 110 | + ) |
| 111 | + if not matched: |
| 112 | + raise ValueError( |
| 113 | + f"skip_tables include a pattern {exclude_pattern} with no matches" |
| 114 | + ) |
| 115 | + |
| 116 | + def include_func(t): |
| 117 | + return any( |
| 118 | + fnmatch.fnmatch(t.name, include_pattern) |
| 119 | + for include_pattern in include_tables |
| 120 | + ) |
| 121 | + |
| 122 | + def exclude_func(t): |
| 123 | + return any( |
| 124 | + fnmatch.fnmatch(t.name, exclude_pattern) for exclude_pattern in skip_tables |
| 125 | + ) |
| 126 | + |
| 127 | + return filter_dfs_func(tables, include_func, exclude_func, skip_dependent_tables) |
| 128 | + |
| 129 | + |
| 130 | +def filter_dfs_func(tt: List[Table], include, exclude, skip_dependent_tables: bool): |
| 131 | + filtered_tables = [] |
| 132 | + for t in tt: |
| 133 | + filtered_table = copy.deepcopy(t) |
| 134 | + filtered_table = _filter_dfs_impl( |
| 135 | + filtered_table, False, include, exclude, skip_dependent_tables |
| 136 | + ) |
| 137 | + if filtered_table is not None: |
| 138 | + filtered_tables.append(filtered_table) |
| 139 | + return filtered_tables |
| 140 | + |
| 141 | + |
| 142 | +def _filter_dfs_impl(t, parent_matched, include, exclude, skip_dependent_tables): |
| 143 | + def filter_dfs_child(r, matched, include, exclude, skip_dependent_tables): |
| 144 | + filtered_child = _filter_dfs_impl( |
| 145 | + r, matched, include, exclude, skip_dependent_tables |
| 146 | + ) |
| 147 | + if filtered_child is not None: |
| 148 | + return True, r |
| 149 | + return matched, None |
| 150 | + |
| 151 | + if exclude(t): |
| 152 | + return None |
| 153 | + |
| 154 | + matched = parent_matched and not skip_dependent_tables |
| 155 | + if include(t): |
| 156 | + matched = True |
| 157 | + |
| 158 | + filtered_relations = [] |
| 159 | + for r in t.relations: |
| 160 | + matched, filtered_child = filter_dfs_child( |
| 161 | + r, matched, include, exclude, skip_dependent_tables |
| 162 | + ) |
| 163 | + if filtered_child is not None: |
| 164 | + filtered_relations.append(filtered_child) |
| 165 | + |
| 166 | + t.relations = filtered_relations |
| 167 | + |
| 168 | + if matched: |
| 169 | + return t |
| 170 | + return None |
| 171 | + |
| 172 | + |
| 173 | +def flatten_tables(tables: List[Table]) -> List[Table]: |
| 174 | + flattened: List[Table] = [] |
93 | 175 | for table in tables:
|
94 |
| - matched = False |
95 |
| - for include_table in include_tables: |
96 |
| - if fnmatch.fnmatch(table.name, include_table): |
97 |
| - matched = True |
98 |
| - for skip_table in skip_tables: |
99 |
| - if fnmatch.fnmatch(table.name, skip_table): |
100 |
| - matched = False |
101 |
| - if matched: |
102 |
| - filtered.append(table) |
103 |
| - return filtered |
| 176 | + flattened.append(table) |
| 177 | + flattened.extend(flatten_tables(table.relations)) |
| 178 | + return flattened |
0 commit comments