dbt get_materialized_view_configuration_changes 简单说明

dbt 的get_materialized_view_configuration_changes 实际上是属于 on_configuration_change 相关的处理,目前一些实现主要是处理
物化视图的,以下说明下内部实现

内部处理

  • 默认实现

dbt-adapter 中,实际为空

{% macro get_materialized_view_configuration_changes(existing_relation, new_config) %}
    /* {#
    It's recommended that configuration changes be formatted as follows:
    {"<change_category>": [{"action": "<name>", "context": ...}]}
 
    For example:
    {
        "indexes": [
            {"action": "drop", "context": "index_abc"},
            {"action": "create", "context": {"columns": ["column_1", "column_2"], "type": "hash", "unique": True}},
        ],
    }
 
    Either way, `get_materialized_view_configuration_changes` needs to align with `get_alter_materialized_view_as_sql`.
    #} */
    {{- log('Determining configuration changes on: ' ~ existing_relation) -}}
    {%- do return(adapter.dispatch('get_materialized_view_configuration_changes', 'dbt')(existing_relation, new_config)) -%}
{% endmacro %}
 
 
{% macro default__get_materialized_view_configuration_changes(existing_relation, new_config) %}
    {{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }}
{% endmacro %}
  • pg 的实现
{% macro postgres__get_materialized_view_configuration_changes(existing_relation, new_config) %}
    {% set _existing_materialized_view = postgres__describe_materialized_view(existing_relation) %}
    # get_materialized_view_config_change_collection 实际是adapter 自己的实现,这个我以前简单说明过
    {% set _configuration_changes = existing_relation.get_materialized_view_config_change_collection(_existing_materialized_view, new_config.model) %}
    {% do return(_configuration_changes) %}
{% endmacro %}

pg adapter relation 处理(get_materialized_view_config_change_collection 方法),此处返回的格式应该遵循上边的说明

def get_materialized_view_config_change_collection(
    self, relation_results: RelationResults, relation_config: RelationConfig
) -> Optional[PostgresMaterializedViewConfigChangeCollection]:
    config_change_collection = PostgresMaterializedViewConfigChangeCollection()
 
    existing_materialized_view = PostgresMaterializedViewConfig.from_relation_results(
        relation_results
    )
    new_materialized_view = PostgresMaterializedViewConfig.from_config(relation_config)
 
    config_change_collection.indexes = self._get_index_config_changes(
        existing_materialized_view.indexes, new_materialized_view.indexes
    )
 
    # we return `None` instead of an empty `PostgresMaterializedViewConfigChangeCollection` object
    # so that it's easier and more extensible to check in the materialization:
    # `core/../materializations/materialized_view.sql` :
    #     {% if configuration_changes is none %}
    if config_change_collection.has_changes:
        return config_change_collection
    return None

说明

dbt get_materialized_view_configuration_changes 的实现实际上是由具体的adapter 处理的,pg 是基于了自己扩展的relation

参考资料

https://docs.getdbt.com/reference/resource-configs/on_configuration_change

posted on 2024-05-23 07:02  荣锋亮  阅读(12)  评论(0编辑  收藏  举报

导航