Skip to content

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

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 15 commits into from
Jan 9, 2025

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;"]
)

@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from f57f88e to f012e96 Compare December 18, 2024 18:46
@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
@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from 14d1f49 to 46bb623 Compare January 2, 2025 19:52
@themisvaltinos themisvaltinos requested a review from a team January 3, 2025 08:22
@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from 46bb623 to d225eeb Compare January 7, 2025 08:14
@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from d225eeb to a2b8955 Compare January 7, 2025 21:47
@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from 7d1e30d to 6343569 Compare January 8, 2025 16:41
@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from 3e4492c to a4d4de6 Compare January 9, 2025 11:41
@@ -853,7 +859,18 @@ def _promote_snapshot(
view_name=view_name,
model=snapshot.model,
environment=environment_naming_info.name,
deployability_index=deployability_index,
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need to pass deployability_index here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh right we don't will remove this

default_catalog: t.Optional[str] = None,
dialect: t.Optional[str] = None,
) -> t.Dict[str, str]:
return {
Copy link
Member

Choose a reason for hiding this comment

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

[Nit] I guess we can return empty dict here if the environment is prod

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried it, but it didn't work since it led to the model names been mapped to the physical table names. This is because in resolve tables without this dict or an empty dict: _to_table_mapping is used to create the mapping:

**self._to_table_mapping(snapshots.values(), deployability_index),

Copy link
Member

@izeigerman izeigerman left a comment

Choose a reason for hiding this comment

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

This looks great, thank you!

@themisvaltinos themisvaltinos force-pushed the themis/vlayer_post_statements branch from a4d4de6 to ac4b4f5 Compare January 9, 2025 20:08
@themisvaltinos themisvaltinos merged commit 8f8abd5 into main Jan 9, 2025
21 checks passed
@themisvaltinos themisvaltinos deleted the themis/vlayer_post_statements branch January 9, 2025 21:37
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