【问题标题】:change table name dynamically using a macro generate_alias_name in DBT在 DBT 中使用宏 generate_alias_name 动态更改表名
【发布时间】:2023-02-18 01:47:31
【问题描述】:

我的表名为 rw_ghi_abc。 我想从表名中删除 rw_,为此我使用宏 generate_alias_name 作为:

{% macro generate_alias_name(re,custom_alias_name=none, node=none) -%}
    {%- if custom_alias_name is none -%}
        {{ re.search('g\w+',node.name) }}
    {%- else -%}
        {{ custom_alias_name | trim }}
    {%- endif -%}
{%- endmacro %}

我正在使用 re.search 以便我可以从名称中跳过 rw_,在 dbt 运行后它会给出如下错误:

Database Error in model rw_ghi_abc(models/RAW/abc/rw_ghi_abc.sql)
  001003 (42000): SQL compilation error:
  syntax error line 1 at position 77 unexpected '='.
  syntax error line 1 at position 102 unexpected '='.
  syntax error line 1 at position 113 unexpected '='.
  syntax error line 1 at position 131 unexpected ''RAW''.
  syntax error line 1 at position 140 unexpected ''abc''.
  syntax error line 1 at position 152 unexpected ''rw_ghi_abc''.
  syntax error line 1 at position 184 unexpected '='.
  syntax error line 1 at position 234 unexpected '='.
  syntax error line 1 at position 631 unexpected '='.
  syntax error line 1 at position 672 unexpected '='.
  syntax error line 1 at position 698 unexpected '='.
  syntax error line 1 at position 771 unexpected '='.
  syntax error line 1 at position 831 unexpected '='.
  syntax error line 1 at position 884 unexpected '='.
  syntax error line 1 at position 920 unexpected '='.
  syntax error line 1 at position 953 unexpected '='.
  syntax error line 1 at position 984 unexpected '='.
  syntax error line 1 at position 1,017 unexpected '='.
  syntax error line 1 at position 1,085 unexpected ','.
  compiled Code at target/run/data/models/RAW/abc/rw_ghi_abc.sql

有人可以帮我弄这个吗。

【问题讨论】:

  • 您可能想查看已编译的 SQL 并从那里获取它
  • 您可以发布 rw_ghi_abc.sql 的原始代码和编译代码吗?

标签: dbt


【解决方案1】:

如果型号名称始终以“rw_”开头,您可以使用如下内容:

{% macro generate_alias_name(custom_alias_name=none, node=none) -%}

{%- if custom_alias_name is none -%}
    {%- if node.name.startswith('rw_') -%}
        {{ node.name[node.name.find('rw_') + 3 : node.name|length] }}
    {%- else -%}
        {{ node.name }}
    {%- endif -%}
{%- else -%}
    {{ custom_alias_name | trim }}
{%- endif -%}

{%- 结束宏 %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-19
    • 2023-03-14
    • 2017-01-31
    • 2021-11-01
    • 2021-09-07
    • 2021-12-26
    • 2011-09-15
    • 2022-08-03
    相关资源
    最近更新 更多