我想在雪花上放多个桌子。我没有将多个命令添加到我的DBT作业中,而只需传递表名称,而是在数据库的Information_Schema下使用了表视图,并找到了一种从那里构造我的Drop语句的方法。
SELECT
LISTAGG(CONCAT('"DROP TABLE IF EXISTS ', CONCAT(CONCAT_WS('.', TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), '"')), ', ')
FROM "{{ var('db_name') }}"."INFORMATION_SCHEMA"."TABLES"
WHERE table_owner = 'TRANSFORM_ROLE'
AND table_schema = '{{ var("schema_name") }}'
AND table_name LIKE '%TEST%TABLE%'
然后,我将上述语句插入我的DBT模型中的呼叫语句:
{%- call statement('generate_drop_table_statement', fetch_result=True) -%}
SELECT
LISTAGG(CONCAT('"DROP TABLE IF EXISTS ', CONCAT(CONCAT_WS('.', TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), '"')), ', ')
FROM "{{ var('db_name') }}"."INFORMATION_SCHEMA"."TABLES"
WHERE table_owner = 'TRANSFORM_ROLE'
AND table_schema = '{{ var("schema_name") }}'
AND table_name LIKE '%TEST%TABLE%'
{%- endcall -%}
然后加载结果:
{%- set drop_statements = load_result('generate_drop_table_statement')['data'][0][0] -%}
此后,我想将drop_statments传递给我的配置中的post_hook。
{{ config(
alias='DROP_STATEMENTS_TABLE',
materialized='table',
post_hook=['{{ drop_statements }}', "DROP TABLE IF EXISTS {{ var('db_name') }}.{{ var('schema_name') }}.FIRST_MODEL"]
) }}
问题是,生成的查询不起作用,而另一个drop语句与drop_statement变量并驾齐驱。我不确定我是否会错误地调用包含我的drop语句的变量,或者根本不可能。
整个代码:
{%- call statement('generate_drop_table_statement', fetch_result=True) -%}
SELECT
LISTAGG(CONCAT('"DROP TABLE IF EXISTS ', CONCAT(CONCAT_WS('.', TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), '"')), ', ')
FROM "{{ var('db_name') }}"."INFORMATION_SCHEMA"."TABLES"
WHERE table_owner = 'TRANSFORM_ROLE'
AND table_schema = '{{ var("schema_name") }}'
AND table_name LIKE '%TEST%TABLE%'
{%- endcall -%}
{%- set drop_statements = load_result('generate_drop_table_statement')['data'][0][0] -%}
{{ log(drop_statements, info=True) }}
{{ config(
alias='DROP_STATEMENTS_TABLE',
materialized='table',
post_hook=['{{ drop_statements }}', "DROP TABLE IF EXISTS {{ var('db_name') }}.{{ var('schema_name') }}.FIRST_MODEL"]
) }}
with statements as (
SELECT
'{{ drop_statements }}'
)
SELECT *
FROM statements
感谢您的帮助。提前致谢。
更新:
我阅读a 尚未得到支持。显然,前后挂钩只接受字符串。
I want to drop multiple tables in snowflake. Instead of adding multiple commands to my dbt job and just pass the tables names, I used the TABLES view under the INFORMATION_SCHEMA of my database and found a way to construct my drop statements from there.
SELECT
LISTAGG(CONCAT('"DROP TABLE IF EXISTS ', CONCAT(CONCAT_WS('.', TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), '"')), ', ')
FROM "{{ var('db_name') }}"."INFORMATION_SCHEMA"."TABLES"
WHERE table_owner = 'TRANSFORM_ROLE'
AND table_schema = '{{ var("schema_name") }}'
AND table_name LIKE '%TEST%TABLE%'
Then, I inserted the statement above to my call statement in my dbt model:
{%- call statement('generate_drop_table_statement', fetch_result=True) -%}
SELECT
LISTAGG(CONCAT('"DROP TABLE IF EXISTS ', CONCAT(CONCAT_WS('.', TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), '"')), ', ')
FROM "{{ var('db_name') }}"."INFORMATION_SCHEMA"."TABLES"
WHERE table_owner = 'TRANSFORM_ROLE'
AND table_schema = '{{ var("schema_name") }}'
AND table_name LIKE '%TEST%TABLE%'
{%- endcall -%}
Then load the result:
{%- set drop_statements = load_result('generate_drop_table_statement')['data'][0][0] -%}
After this, I want to pass drop_statments to my post_hook in my config.
{{ config(
alias='DROP_STATEMENTS_TABLE',
materialized='table',
post_hook=['{{ drop_statements }}', "DROP TABLE IF EXISTS {{ var('db_name') }}.{{ var('schema_name') }}.FIRST_MODEL"]
) }}
The problem is, the query that was generated doesn't work while the other drop statement alongside the drop_statements variable does. I'm not sure if I'm calling the variable containing my drop statement incorrectly or this is not possible at all.
Whole code:
{%- call statement('generate_drop_table_statement', fetch_result=True) -%}
SELECT
LISTAGG(CONCAT('"DROP TABLE IF EXISTS ', CONCAT(CONCAT_WS('.', TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), '"')), ', ')
FROM "{{ var('db_name') }}"."INFORMATION_SCHEMA"."TABLES"
WHERE table_owner = 'TRANSFORM_ROLE'
AND table_schema = '{{ var("schema_name") }}'
AND table_name LIKE '%TEST%TABLE%'
{%- endcall -%}
{%- set drop_statements = load_result('generate_drop_table_statement')['data'][0][0] -%}
{{ log(drop_statements, info=True) }}
{{ config(
alias='DROP_STATEMENTS_TABLE',
materialized='table',
post_hook=['{{ drop_statements }}', "DROP TABLE IF EXISTS {{ var('db_name') }}.{{ var('schema_name') }}.FIRST_MODEL"]
) }}
with statements as (
SELECT
'{{ drop_statements }}'
)
SELECT *
FROM statements
Appreciate your help. Thanks, in advance.
Update:
I read a post/question published in November 2021 that this is not yet supported. Apparently, pre and post hooks only accept strings.
发布评论
评论(2)
我认为问题是您已经在Jinja块中时使用了卷发(
{{}}
)。尝试删除这些,看看它是否有效!I think the issue is that you’ve used curlies (
{{}}
) when you’re already inside a jinja block. Try removing those and see if it works!您可以这样通过:
You can pass like this: