Skip to content

Commit 2acf1a2

Browse files
authored
Fix: Push filter down to query for get_expired_environments (#4804)
1 parent fe6f3ba commit 2acf1a2

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

sqlmesh/core/state_sync/db/environment.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,9 @@ def get_expired_environments(self, current_ts: int) -> t.List[EnvironmentSummary
172172
Returns:
173173
The list of environment summaries to remove.
174174
"""
175-
176-
environment_summaries = self.get_environments_summary()
177-
return [
178-
env_summary
179-
for env_summary in environment_summaries
180-
if env_summary.expiration_ts is not None and env_summary.expiration_ts <= current_ts
181-
]
175+
return self._fetch_environment_summaries(
176+
where=self._create_expiration_filter_expr(current_ts)
177+
)
182178

183179
def delete_expired_environments(
184180
self, current_ts: t.Optional[int] = None
@@ -225,13 +221,7 @@ def get_environments_summary(self) -> t.List[EnvironmentSummary]:
225221
Returns:
226222
A list of all environment summaries.
227223
"""
228-
return [
229-
self._environment_summmary_from_row(row)
230-
for row in fetchall(
231-
self.engine_adapter,
232-
self._environments_query(required_fields=list(EnvironmentSummary.all_fields())),
233-
)
234-
]
224+
return self._fetch_environment_summaries()
235225

236226
def get_environment(
237227
self, environment: str, lock_for_update: bool = False
@@ -327,6 +317,20 @@ def _create_expiration_filter_expr(self, current_ts: int) -> exp.Expression:
327317
expression=exp.Literal.number(current_ts),
328318
)
329319

320+
def _fetch_environment_summaries(
321+
self, where: t.Optional[str | exp.Expression] = None
322+
) -> t.List[EnvironmentSummary]:
323+
return [
324+
self._environment_summmary_from_row(row)
325+
for row in fetchall(
326+
self.engine_adapter,
327+
self._environments_query(
328+
where=where,
329+
required_fields=list(EnvironmentSummary.all_fields()),
330+
),
331+
)
332+
]
333+
330334

331335
def _environment_to_df(environment: Environment) -> pd.DataFrame:
332336
import pandas as pd

sqlmesh/core/state_sync/db/facade.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@
6969
T = t.TypeVar("T")
7070

7171

72-
if t.TYPE_CHECKING:
73-
pass
74-
75-
7672
class EngineAdapterStateSync(StateSync):
7773
"""Manages state of nodes and snapshot with an existing engine adapter.
7874

0 commit comments

Comments
 (0)