我目前正在编写自定义dbt
实体化,我想知道访问"当前"的最佳方式/模式是什么。从物化本身建模元数据。
的背景模型由两个文件组成:
sample_model.yaml
(含模型元数据)
version: 2
models:
- name: sample_model
description: This is a test view to test a materialization
config:
schema: temp
materialized: custom_view
columns:
- name: custom_view_column_a
description: This is a test column A in a view
- name: custom_view_column_b
description: This is a test column B in a view
sample_model.sql
(带有"actual"模型)
SELECT
1 AS custom_view_column_a,
2 AS custom_view_column_b
我的解决方案例如,在我的自定义物化(custom_view
)中,我想访问模型元数据(sample_model.yaml
)中定义的列。目前我可以使用graph
变量访问它们,如下所示:
{% set models = [] %}
{% for node in graph.nodes.values() | selectattr("resource_type", "equalto", "model") | selectattr("name", "equalto", this.identifier) %}
{% do models.append(node) %}
{% endfor %}
{% set model_metadata = models|first %}
{% set model_columns = model_metadata.get("columns") %}
可能改进这种方法工作得很好,然而它给人的感觉是有点像(ab)使用某种"全局变量"。此外,graph
可能会变得非常大,因为它存储了项目中所有模型的元数据和SQL !
是否有任何其他(本地)对象/变量,我可以从物化访问,只存储模型的元数据,它目前正在物化?
{{ model }}
给出了当前模型节点图中的数据。我认为它应该在物化中工作:
{% set model_metadata = model %}
你可能想用execute
来控制它——我不确定第一次解析是否传递模板的具体化代码:
{% set model_metadata = model if execute else {} %}