Skip to content

Commit 266d0a3

Browse files
display which tables have changes
1 parent 0f1c229 commit 266d0a3

2 files changed

Lines changed: 114 additions & 35 deletions

File tree

sqlmesh/core/console.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ def show_model_difference_summary(
217217
) -> None:
218218
"""Displays a summary of differences for the given models."""
219219

220+
@abc.abstractmethod
221+
def show_impacted_tables_diff(
222+
self,
223+
table_diffs: t.List[TableDiff],
224+
show_sample: bool = True,
225+
skip_grain_check: bool = False,
226+
temp_schema: t.Optional[str] = None,
227+
) -> None:
228+
"""Display the table diff between all mismatched tables."""
229+
230+
@abc.abstractmethod
231+
def show_table_diff(
232+
self,
233+
table_diff: TableDiff,
234+
show_sample: bool = True,
235+
skip_grain_check: bool = False,
236+
temp_schema: t.Optional[str] = None,
237+
) -> None:
238+
"""Display the table diff between two tables."""
239+
220240
@abc.abstractmethod
221241
def show_table_diff_summary(self, table_diff: TableDiff) -> None:
222242
"""Display information about the tables being diffed and how they are being joined"""
@@ -648,6 +668,24 @@ def loading_start(self, message: t.Optional[str] = None) -> uuid.UUID:
648668
def loading_stop(self, id: uuid.UUID) -> None:
649669
pass
650670

671+
def show_impacted_tables_diff(
672+
self,
673+
table_diffs: t.List[TableDiff],
674+
show_sample: bool = True,
675+
skip_grain_check: bool = False,
676+
temp_schema: t.Optional[str] = None,
677+
) -> None:
678+
pass
679+
680+
def show_table_diff(
681+
self,
682+
table_diff: TableDiff,
683+
show_sample: bool = True,
684+
skip_grain_check: bool = False,
685+
temp_schema: t.Optional[str] = None,
686+
) -> None:
687+
pass
688+
651689
def show_table_diff_summary(self, table_diff: TableDiff) -> None:
652690
pass
653691

@@ -2113,6 +2151,76 @@ def show_row_diff(
21132151
self.console.print(f"\n[b][green]{target_name} ONLY[/green] sample rows:[/b]")
21142152
self.console.print(row_diff.t_sample.to_string(index=False), end="\n\n")
21152153

2154+
def show_table_diff(
2155+
self,
2156+
table_diff: TableDiff,
2157+
show_sample: bool = True,
2158+
skip_grain_check: bool = False,
2159+
temp_schema: t.Optional[str] = None,
2160+
) -> None:
2161+
"""Display the table diff between two tables.
2162+
2163+
Args:
2164+
table_diff: The TableDiff object containing schema and summary differences
2165+
show_sample: Show the sample dataframe in the console. Requires show=True.
2166+
skip_grain_check: Skip check for rows that contain null or duplicate grains.
2167+
temp_schema: The schema to use for temporary tables.
2168+
"""
2169+
2170+
self.show_table_diff_summary(table_diff)
2171+
self.show_schema_diff(table_diff.schema_diff())
2172+
self.show_row_diff(
2173+
table_diff.row_diff(temp_schema=temp_schema, skip_grain_check=skip_grain_check),
2174+
show_sample=show_sample,
2175+
skip_grain_check=skip_grain_check,
2176+
)
2177+
2178+
def show_impacted_tables_diff(
2179+
self,
2180+
table_diffs: t.List[TableDiff],
2181+
show_sample: bool = True,
2182+
skip_grain_check: bool = False,
2183+
temp_schema: t.Optional[str] = None,
2184+
) -> None:
2185+
"""
2186+
Display the table diff between all mismatched tables.
2187+
"""
2188+
mismatched_tables = []
2189+
fully_matched = []
2190+
for table_diff in table_diffs:
2191+
if (
2192+
table_diff.row_diff(
2193+
temp_schema=temp_schema, skip_grain_check=skip_grain_check
2194+
).full_match_pct
2195+
== 100
2196+
):
2197+
fully_matched.append(table_diff)
2198+
else:
2199+
mismatched_tables.append(table_diff)
2200+
2201+
if fully_matched:
2202+
m_tree = Tree("\n[b]Identical Tables")
2203+
for m in fully_matched:
2204+
m_tree.add(
2205+
f"[{self.TABLE_DIFF_SOURCE_BLUE}]{m.source}[/{self.TABLE_DIFF_SOURCE_BLUE}] - [{self.TABLE_DIFF_TARGET_GREEN}]{m.target}[/{self.TABLE_DIFF_TARGET_GREEN}]"
2206+
)
2207+
self._print(m_tree)
2208+
2209+
if mismatched_tables:
2210+
m_tree = Tree("\n[b]Mismatched Tables")
2211+
for m in mismatched_tables:
2212+
m_tree.add(
2213+
f"[{self.TABLE_DIFF_SOURCE_BLUE}]{m.source}[/{self.TABLE_DIFF_SOURCE_BLUE}] - [{self.TABLE_DIFF_TARGET_GREEN}]{m.target}[/{self.TABLE_DIFF_TARGET_GREEN}]"
2214+
)
2215+
self._print(m_tree)
2216+
for diff in mismatched_tables:
2217+
self.show_table_diff(
2218+
table_diff=diff,
2219+
show_sample=show_sample,
2220+
skip_grain_check=skip_grain_check,
2221+
temp_schema=temp_schema,
2222+
)
2223+
21162224
def print_environments(self, environments_summary: t.Dict[str, int]) -> None:
21172225
"""Prints all environment names along with expiry datetime."""
21182226
output = [

sqlmesh/core/context.py

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ def table_diff(
16591659
decimals=decimals,
16601660
)
16611661
if show:
1662-
self._show_table_diff(
1662+
self.console.show_table_diff(
16631663
table_diff=table_diff,
16641664
show_sample=show_sample,
16651665
skip_grain_check=skip_grain_check,
@@ -1668,31 +1668,6 @@ def table_diff(
16681668

16691669
return table_diff
16701670

1671-
def _show_table_diff(
1672-
self,
1673-
table_diff: TableDiff,
1674-
show_sample: bool = True,
1675-
skip_grain_check: bool = False,
1676-
temp_schema: t.Optional[str] = None,
1677-
) -> None:
1678-
"""Display the table diff between two tables.
1679-
1680-
Args:
1681-
table_diff: The TableDiff object containing schema and summary differences
1682-
show_sample: Show the sample dataframe in the console. Requires show=True.
1683-
skip_grain_check: Skip check for rows that contain null or duplicate grains.
1684-
temp_schema: The schema to use for temporary tables.
1685-
1686-
"""
1687-
1688-
self.console.show_table_diff_summary(table_diff)
1689-
self.console.show_schema_diff(table_diff.schema_diff())
1690-
self.console.show_row_diff(
1691-
table_diff.row_diff(temp_schema=temp_schema, skip_grain_check=skip_grain_check),
1692-
show_sample=show_sample,
1693-
skip_grain_check=skip_grain_check,
1694-
)
1695-
16961671
def _concurrent_table_diff(
16971672
self,
16981673
modified_snapshot_ids: t.Set[SnapshotId],
@@ -1797,7 +1772,7 @@ def table_diff_impacted_models(
17971772
for _, (current_snapshot, _) in context_diff.modified_snapshots.items()
17981773
}
17991774
if modified_snapshot_ids:
1800-
results = self._concurrent_table_diff(
1775+
table_diffs = self._concurrent_table_diff(
18011776
modified_snapshot_ids=modified_snapshot_ids,
18021777
source=source,
18031778
target=target,
@@ -1813,14 +1788,10 @@ def table_diff_impacted_models(
18131788
temp_schema=temp_schema,
18141789
)
18151790
if show:
1816-
for result in results:
1817-
self._show_table_diff(
1818-
table_diff=result,
1819-
show_sample=show_sample,
1820-
skip_grain_check=skip_grain_check,
1821-
temp_schema=temp_schema,
1822-
)
1823-
return results
1791+
self.console.show_impacted_tables_diff(
1792+
table_diffs, show_sample, skip_grain_check, temp_schema
1793+
)
1794+
return table_diffs
18241795
return []
18251796

18261797
@python_api_analytics

0 commit comments

Comments
 (0)