Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 427
Feat: Check freshness for mixed external & sqlmesh models#5499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
403fb6a
Feat: Check freshness for mixed external & sqlmesh models
VaggelisD 0acaf61
Refactor parent signals
VaggelisD a764f98
Refactor parent_intervals to use snapshot_batches
VaggelisD 8f13577
Add new test for ready intervals
VaggelisD aa9fda4
Add new test for external models
VaggelisD 126a902
Reinstate config
VaggelisD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,6 +2,7 @@ | ||
| import typing as t | ||
| from sqlmesh.utils import UniqueKeyDict, registry_decorator | ||
| from sqlmesh.utils.errors import MissingSourceError | ||
| if t.TYPE_CHECKING: | ||
| from sqlmesh.core.context import ExecutionContext | ||
| @@ -42,7 +43,16 @@ class signal(registry_decorator): | ||
| @signal() | ||
| def freshness(batch: DatetimeRanges, snapshot: Snapshot, context: ExecutionContext) -> bool: | ||
| def freshness( | ||
| batch: DatetimeRanges, | ||
| snapshot: Snapshot, | ||
| context: ExecutionContext, | ||
| ) -> bool: | ||
| """ | ||
| Implements model freshness as a signal, i.e it considers this model to be fresh if: | ||
| - Any upstream SQLMesh model has available intervals to compute i.e is fresh | ||
| - Any upstream external model has been altered since the last time the model was evaluated | ||
| """ | ||
| adapter = context.engine_adapter | ||
| if context.is_restatement or not adapter.SUPPORTS_METADATA_TABLE_LAST_MODIFIED_TS: | ||
| return True | ||
| @@ -54,24 +64,35 @@ def freshness(batch: DatetimeRanges, snapshot: Snapshot, context: ExecutionConte | ||
| if deployability_index.is_deployable(snapshot) | ||
| else snapshot.dev_last_altered_ts | ||
| ) | ||
| if not last_altered_ts: | ||
| return True | ||
| parent_snapshots = {context.snapshots[p.name] for p in snapshot.parents} | ||
| if len(parent_snapshots) != len(snapshot.node.depends_on) or not all( | ||
| p.is_external for p in parent_snapshots | ||
| ): | ||
| # The mismatch can happen if e.g an external model is not registered in the project | ||
| upstream_parent_snapshots = {p for p in parent_snapshots if not p.is_external} | ||
| external_parents = snapshot.node.depends_on - {p.name for p in upstream_parent_snapshots} | ||
VaggelisD marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if context.parent_intervals: | ||
izeigerman marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # At least one upstream sqlmesh model has intervals to compute (i.e is fresh), | ||
| # so the current model is considered fresh too | ||
| return True | ||
| # Finding new data means that the upstream depedencies have been altered | ||
| # since the last time the model was evaluated | ||
| upstream_dep_has_new_data = any( | ||
| upstream_last_altered_ts > last_altered_ts | ||
| for upstream_last_altered_ts in adapter.get_table_last_modified_ts( | ||
| [p.name for p in parent_snapshots] | ||
| if external_parents: | ||
| external_last_altered_timestamps = adapter.get_table_last_modified_ts( | ||
| list(external_parents) | ||
| ) | ||
| if len(external_last_altered_timestamps) != len(external_parents): | ||
| raise MissingSourceError( | ||
| f"Expected {len(external_parents)} sources to be present, but got {len(external_last_altered_timestamps)}." | ||
| ) | ||
| # Finding new data means that the upstream depedencies have been altered | ||
| # since the last time the model was evaluated | ||
| return any( | ||
| external_last_altered_ts > last_altered_ts | ||
| for external_last_altered_ts in external_last_altered_timestamps | ||
| ) | ||
| ) | ||
| # Returning true is a no-op, returning False nullifies the batch so the model will not be evaluated. | ||
| return upstream_dep_has_new_data | ||
| return False | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.