dbt render macro 简单说明

dbt render macro 官方文档并没有说明,但是在一些三方adapter 中发现有使用到简单说明下

参考使用

{% materialization external, adapter="duckdb", supported_languages=['sql', 'python'] %}
  {%- set location = render(config.get('location', default=external_location(this, config))) -%})
  {%- set rendered_options = render_write_options(config) -%}
.....

内部处理

  • context 定义
@contextmember
def render(self, string: str) -> str:
    return get_rendered(string, self._ctx, self.model)
  • get_rendered 处理

可以看到内部实际上是一个jinja2 的处理

def get_rendered(
    string: str,
    ctx: Dict[str, Any],
    node=None,
    capture_macros: bool = False,
    native: bool = False,
) -> str:
    # performance optimization: if there are no jinja control characters in the
    # string, we can just return the input. Fall back to jinja if the type is
    # not a string or if native rendering is enabled (so '1' -> 1, etc...)
    # If this is desirable in the native env as well, we could handle the
    # native=True case by passing the input string to ast.literal_eval, like
    # the native renderer does.
    if not native and isinstance(string, str) and _HAS_RENDER_CHARS_PAT.search(string) is None:
        return string
    template = get_template(
        string,
        ctx,
        node,
        capture_macros=capture_macros,
        native=native,
    )
    return render_template(template, ctx, node)

说明

render macro 官方没有说明,但是从功能上看似乎是一个很强大的macro,可以实现macro 再调用处理,如下

{% set  location = "{{this}}" %}
{{log("render    info " ~ render(location),info=True) }}
select * from {{myref('pg','sensor')}}

参考资料

https://github.com/duckdb/dbt-duckdb

posted on 2024-05-14 08:00  荣锋亮  阅读(11)  评论(0编辑  收藏  举报

导航