Feat(dbt): Add support for dbt custom materializations#5435
Feat(dbt): Add support for dbt custom materializations#5435themisvaltinos merged 7 commits intomainfrom
Conversation
| class DbtCustomKind(_ModelKind): | ||
| name: t.Literal[ModelKindName.DBT_CUSTOM] = ModelKindName.DBT_CUSTOM | ||
| materialization: str | ||
| adapter: str = "default" |
There was a problem hiding this comment.
it's to allow users to define different materialisation tied to different adapters and for dispatch then to appropriately match the correct one used
{% materialization my_materialization_name, default %}
-- cross-adapter materialization... assume Redshift is not supported
{% endmaterialization %}
{% materialization my_materialization_name, adapter='redshift' %}
-- override the materialization for Redshift
{% endmaterialization %}
test for this example: test_adapter_specific_materialization_override
sqlmesh/core/model/kind.py
Outdated
| @property | ||
| def metadata_hash_values(self) -> t.List[t.Optional[str]]: | ||
| return [ | ||
| *super().metadata_hash_values, |
There was a problem hiding this comment.
What is the point of this override?
There was a problem hiding this comment.
thank you good catch. I had originally the definition there and then realised it should be in the data hash and moved it but forgot to remove the metadata_hash_values entirely since it's not needed will do
sqlmesh/core/snapshot/evaluator.py
Outdated
| adapter.session(snapshot.model.render_session_properties(**render_statements_kwargs)), | ||
| ): | ||
| adapter.execute(model.render_pre_statements(**render_statements_kwargs)) | ||
| if not snapshot.is_dbt_custom: |
There was a problem hiding this comment.
Does the custom materialization strategy calls the pre-hooks?
There was a problem hiding this comment.
yes internally using run_hooks so it's up to the user when they should call them or if the specific materialisation doesn't call them at all: https://docs.getdbt.com/guides/create-new-materializations?step=2#run-pre-hooks
There was a problem hiding this comment.
ok, can we instead extend the materialization strategy with:
def run_pre_statements(...)
def run_post_statements(...)
and in the default implementation we do
adapter.execute(model.render_pre_statements(**render_statements_kwargs))
while for DbtCustomMaterializationStrategy it's a noop
There was a problem hiding this comment.
yes that seems better, revised it with run methods instead in the materialisation strategy
sqlmesh/core/snapshot/evaluator.py
Outdated
| ) -> None: | ||
| from sqlmesh.dbt.builtin import create_builtin_globals | ||
|
|
||
| jinja_macros = getattr(model, "jinja_macros", JinjaMacroRegistry()) |
There was a problem hiding this comment.
why do we need to use getattr instead of accessing the attribute directly?
There was a problem hiding this comment.
yes good point I forgot all our model classes have jinja_macros
sqlmesh/core/snapshot/evaluator.py
Outdated
| "execution_dt": kwargs.get("execution_time"), | ||
| } | ||
|
|
||
| context = create_builtin_globals( |
There was a problem hiding this comment.
Why do we manually construct the context instead of relying on JinjaMacros to do it for us?
There was a problem hiding this comment.
so that I didn't use its private method, but I revised it to use jinjamacros instead if this is what you meant. if you meant to do it through build_environment that got quite complicated and it updates internally the macros that were needed which lead to failures. if you mean why this entire logic is needed in the first place it is because we need to inject runtime values specific to the materialization, like the actual sql of the query being materialized, the pre/post hooks, the physical table name parts if it’s a vde etc.
7d2829b to
6cf1de3
Compare
sqlmesh/core/snapshot/evaluator.py
Outdated
| raise SQLMeshError(f"Custom materialization '{name}' not present in the Python environment") | ||
|
|
||
|
|
||
| class DbtCustomMaterialization(MaterializableStrategy): |
There was a problem hiding this comment.
Let's follow the existing naming convention and add Strategy at the end of the name
75a4b8e to
2aab482
Compare
2aab482 to
7de8247
Compare
This update adds support for dbt custom materializations which allows users to define their own strategies via jinja macros.
Docs: https://docs.getdbt.com/guides/create-new-materializations?step=2