Skip to content
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

Feat!: Add support for virtual statements to be executed post update #3524

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

themisvaltinos
Copy link
Contributor

This update introduces statements that are executed after a virtual update. These can be used for example to grant privileges on views of the virtual layer.

These expressions should be defined within a ON_VIRTUAL_UPDATE_BEGIN; ...; ON_VIRTUAL_UPDATE_END; block. For example:

MODEL (
  name db.customers 
);

SELECT 
  item_id
FROM 
  db.seed_model;

ON_VIRTUAL_UPDATE_BEGIN; 

GRANT SELECT ON VIEW @this_model TO ROLE role_name;

INJA_STATEMENT_BEGIN;     
GRANT REFERENCES, SELECT ON FUTURE VIEWS IN DATABASE {{ db }} TO ROLE owner_name;  
JINJA_END;  

ON_VIRTUAL_UPDATE_END;

Jinja expressions are also supported within this block by properly nesting the Jinja block as shown in the example above.

In these statements table resolution occurs at the virtual level, meaning that qualified view names (including @this_model) are used instead of the physical table names. For example, when running the plan in an environment named dev, the db.customers as well as this_model would resolve to db__dev.customers rather than the physical table.

For python models these statements are defined in the @model decorator:

@model( 
  "db.customers",  
  on_virtual_update=["GRANT SELECT ON VIEW @this_model TO ROLE role_name;"]
)

sqlmesh/core/dialect.py Outdated Show resolved Hide resolved
sqlmesh/core/dialect.py Outdated Show resolved Hide resolved
sqlmesh/core/model/definition.py Outdated Show resolved Hide resolved
sqlmesh/core/plan/evaluator.py Outdated Show resolved Hide resolved
sqlmesh/core/snapshot/evaluator.py Outdated Show resolved Hide resolved
tests/core/test_model.py Outdated Show resolved Hide resolved
tests/core/test_model.py Outdated Show resolved Hide resolved
@@ -6266,3 +6266,174 @@ def assert_metadata_only():
model = load_sql_based_model(expressions, signal_definitions=signal.get_registry())
model.signals.clear()
assert_metadata_only()


def test_model_on_virtual_update(make_snapshot: t.Callable):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add an end-to-end test for this?

@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from f57f88e to f012e96 Compare December 18, 2024 18:46
sqlmesh/core/dialect.py Outdated Show resolved Hide resolved
sqlmesh/core/dialect.py Outdated Show resolved Hide resolved
sqlmesh/core/dialect.py Outdated Show resolved Hide resolved
sqlmesh/core/model/definition.py Outdated Show resolved Hide resolved
Comment on lines 1014 to 1015
snapshot_deps = {snapshots[p_sid].name: snapshots[p_sid] for p_sid in snapshot.parents}
snapshot_deps[snapshot.name] = snapshot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we wanna make a helper for this, there are a few places where we do the same thing in the codebase:

cc @izeigerman

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, good catch lol. Yeah, we can add one in the snapshot/definition.py

@izeigerman
Copy link
Member

Can we please make sure this feature is documented before we merge.

@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from 10006db to 14d1f49 Compare December 23, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants