-
Notifications
You must be signed in to change notification settings - Fork 263
Fix!: exclude Semicolon expressions from model state #4257
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
68 changes: 68 additions & 0 deletions
68
sqlmesh/migrations/v0082_warn_if_incorrectly_duplicated_statements.py
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
This script's goal is to warn users if there are two adjacent expressions in a SQL | ||
model that are equivalent. | ||
|
||
Context: | ||
|
||
We used to include `Semicolon` expressions in the model's state, which led to a bug | ||
where the expression preceding the semicolon would be duplicated in pre_statements | ||
or post_statements. For example, the query in the model below would be incorrectly | ||
included in its post_statements list: | ||
|
||
``` | ||
MODEL ( | ||
name test | ||
); | ||
|
||
SELECT 1 AS c; | ||
|
||
-- foo | ||
``` | ||
|
||
We now don't include `Semicolon` expressions in the model's state, which fixes this | ||
issue, but unfortunately migrating existing snapshots is not possible because we do | ||
not have a signal in state to detect whether an expression was incorrectly duplicated. | ||
|
||
If a SQL model suffered from this issue, then there would be two adjacent equivalent | ||
expressions in it, so we use that as a heuristic to warn the user accordingly. | ||
""" | ||
|
||
import json | ||
|
||
from sqlglot import exp | ||
|
||
from sqlmesh.core.console import get_console | ||
|
||
|
||
def migrate(state_sync, **kwargs): # type: ignore | ||
engine_adapter = state_sync.engine_adapter | ||
schema = state_sync.schema | ||
snapshots_table = "_snapshots" | ||
if schema: | ||
snapshots_table = f"{schema}.{snapshots_table}" | ||
|
||
warning = ( | ||
"SQLMesh detected that it may not be able to fully migrate the state database. This should not impact " | ||
"the migration process, but may result in unexpected changes being reported by the next `sqlmesh plan` " | ||
"command. Please run `sqlmesh diff prod` after the migration has completed, before making any new " | ||
"changes. If any unexpected changes are reported, consider running a forward-only plan to apply these " | ||
"changes and avoid unnecessary backfills: sqlmesh plan prod --forward-only. " | ||
"See https://sqlmesh.readthedocs.io/en/stable/concepts/plans/#forward-only-plans for more details.\n" | ||
) | ||
|
||
for (snapshot,) in engine_adapter.fetchall( | ||
exp.select("snapshot").from_(snapshots_table), quote_identifiers=True | ||
): | ||
parsed_snapshot = json.loads(snapshot) | ||
node = parsed_snapshot["node"] | ||
|
||
if node.get("source_type") == "sql": | ||
expressions = [ | ||
*node.get("pre_statements", []), | ||
node["query"], | ||
*node.get("post_statements", []), | ||
] | ||
for e1, e2 in zip(expressions, expressions[1:]): | ||
if e1 == e2: | ||
get_console().log_warning(warning) | ||
return |
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
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.