Skip to content

Commit

Permalink
Create stg_dbt__test_executions.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-atheon authored Apr 22, 2021
1 parent da09991 commit 9d73802
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions models/staging/stg_dbt__test_executions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
with base as (

select *
from {{ ref('stg_dbt__artifacts') }}

),

run_results as (

select *
from base
where artifact_type = 'run_results.json'

),

dbt_run as (

select *
from run_results
where data:args:which = 'test'

),

fields as (

select
data:metadata:invocation_id::string as command_invocation_id,
generated_at as artifact_generated_at,
coalesce(data:args:full_refresh, 'false')::boolean as was_full_refresh,
result.value:unique_id::string as node_id,
split(result.value:thread_id::string, '-')[1]::integer as thread_id,
result.value:status::string as status,
result.value:message::string as message,

-- The first item in the timing array is the model-level `compile`
result.value:timing[0]:started_at::timestamp_ntz as compile_started_at,

-- The second item in the timing array is `execute`.
result.value:timing[1]:completed_at::timestamp_ntz as query_completed_at,

-- Confusingly, this does not match the delta of the above two timestamps.
-- should we calculate it instead?
coalesce(result.value:execution_time::float, 0) as total_node_runtime,

result.value:adapter_response:rows_affected::int as rows_affected
from dbt_run,
lateral flatten(input => data:results) as result

),

surrogate_key as (

select
{{ dbt_utils.surrogate_key(['command_invocation_id', 'node_id']) }} as model_execution_id,
command_invocation_id,
artifact_generated_at,
was_full_refresh,
node_id,
thread_id,
status,
message,
compile_started_at,
query_completed_at,
total_node_runtime,
rows_affected
from fields

)

select * from surrogate_key

0 comments on commit 9d73802

Please sign in to comment.