Skip to content

Commit 6a6ebbc

Browse files
display which tables have changes
1 parent 6a2ada2 commit 6a6ebbc

2 files changed

Lines changed: 115 additions & 35 deletions

File tree

sqlmesh/core/console.py

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