Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions macros/snowflake/create_query_stats_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% macro create_query_stats_table() %}

{% if execute %}

-- Create the table if it doesn't exist
{% set create_table_sql %}

create table if not exists {{ target.database }}.{{ target.schema }}.query_operator_stats as
select
1::integer as dbt_cloud_project_id,
1::integer as dbt_cloud_run_id,
1::integer as dbt_cloud_job_id,
''::string as dbt_cloud_run_reason,
''::string as dbt_version,
''::string as invocation_id,
''::string as status,
''::string as thread_id,
1.0::float as execution_time,
''::string as unique_id,
*
from table(get_query_operator_stats(last_query_id()))
limit 0;

{% endset %}

{% do run_query(create_table_sql) %}

{% endif %}

{% endmacro %}
44 changes: 44 additions & 0 deletions macros/snowflake/get_query_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{% macro get_query_stats(results) %}

{% if execute %}

{% if results != [] %}

{% for result in results %}

{% if result.adapter_response %}

{% set query_id = result.adapter_response.query_id %}

{% do log('Uploading stats for query ' ~ query_id, true) %}

{% set insert_sql %}

insert into {{ target.database }}.{{ target.schema }}.query_operator_stats
select
{{ env_var('DBT_CLOUD_PROJECT_ID', 'null') }},
{{ env_var('DBT_CLOUD_RUN_ID', 'null') }},
{{ env_var('DBT_CLOUD_JOB_ID', 'null') }},
'{{ env_var('DBT_CLOUD_RUN_REASON', '') }}',
'{{ dbt_version }}',
'{{ invocation_id }}',
'{{ result.status }}',
'{{ result.thread_id }}',
{{ result.execution_time }},
'{{ result.unique_id }}',
*
from table(get_query_operator_stats('{{ query_id }}'));

{% endset %}

{% do run_query(insert_sql) %}

{% endif %}

{% endfor %}

{% endif %}

{% endif %}

{% endmacro %}