|
1 |
| -with base as ( |
2 |
| - select * |
3 |
| - from {{ ref('stg_dbt__models') }} |
4 |
| -), |
5 |
| - |
6 |
| -model_executions as ( |
7 |
| - select * |
8 |
| - from {{ ref('stg_dbt__model_executions') }} |
9 |
| -), |
10 |
| - |
11 |
| -latest_models as ( |
12 |
| - /* Retrieves the models present in the most recent run */ |
13 |
| - select * |
14 |
| - from base |
15 |
| - where run_started_at = (select max(run_started_at) from base) |
16 |
| -), |
17 |
| - |
18 |
| -latest_models_runs as ( |
19 |
| - /* Retreives all successful run information for the models present in the most |
20 |
| - recent run and ranks them based on query completion time */ |
21 |
| - select |
22 |
| - model_executions.node_id |
23 |
| - , model_executions.was_full_refresh |
24 |
| - , model_executions.query_completed_at |
25 |
| - , model_executions.total_node_runtime |
26 |
| - , model_executions.rows_affected |
27 |
| - {% if target.type == 'bigquery' %} |
28 |
| - , model_executions.bytes_processed |
29 |
| - {% endif %} |
30 |
| - /* Row number by refresh and node ID */ |
31 |
| - , row_number() over ( |
32 |
| - partition by latest_models.node_id, model_executions.was_full_refresh |
33 |
| - order by model_executions.query_completed_at desc /* most recent ranked first */ |
34 |
| - ) as run_idx |
35 |
| - /* Row number by node ID */ |
36 |
| - , row_number() over ( |
37 |
| - partition by latest_models.node_id |
38 |
| - order by model_executions.query_completed_at desc /* most recent ranked first */ |
39 |
| - ) as run_idx_id_only |
40 |
| - from model_executions |
41 |
| - inner join latest_models on model_executions.node_id = latest_models.node_id |
42 |
| - where model_executions.status = 'success' |
43 |
| -), |
44 |
| - |
45 |
| -latest_model_stats as ( |
46 |
| - select |
47 |
| - node_id |
48 |
| - , max(case when was_full_refresh then query_completed_at end) as last_full_refresh_run_completed_at |
49 |
| - , max(case when was_full_refresh then total_node_runtime end) as last_full_refresh_run_total_runtime |
50 |
| - , max(case when was_full_refresh then rows_affected end) as last_full_refresh_run_rows_affected |
51 |
| - {% if target.type == 'bigquery' %} |
52 |
| - , max(case when was_full_refresh then bytes_processed end) as last_full_refresh_run_bytes_processed |
53 |
| - {% endif %} |
54 |
| - , max(case when run_idx_id_only = 1 then query_completed_at end) as last_run_completed_at |
55 |
| - , max(case when run_idx_id_only = 1 then total_node_runtime end) as last_run_total_runtime |
56 |
| - , max(case when run_idx_id_only = 1 then rows_affected end) as last_run_rows_affected |
57 |
| - {% if target.type == 'bigquery' %} |
58 |
| - , max(case when run_idx_id_only = 1 then bytes_processed end) as last_run_bytes_processed |
59 |
| - {% endif %} |
60 |
| - , max(case when not was_full_refresh then query_completed_at end) as last_incremental_run_completed_at |
61 |
| - , max(case when not was_full_refresh then total_node_runtime end) as last_incremental_run_total_runtime |
62 |
| - , max(case when not was_full_refresh then rows_affected end) as last_incremental_run_rows_affected |
63 |
| - {% if target.type == 'bigquery' %} |
64 |
| - , max(case when not was_full_refresh then bytes_processed end) as last_incremental_run_bytes_processed |
65 |
| - {% endif %} |
66 |
| - from latest_models_runs |
67 |
| - where run_idx = 1 |
68 |
| - group by 1 |
69 |
| -), |
70 |
| - |
71 |
| -final as ( |
72 |
| - select |
73 |
| - latest_models.* |
74 |
| - , latest_model_stats.last_full_refresh_run_completed_at |
75 |
| - , latest_model_stats.last_full_refresh_run_total_runtime |
76 |
| - , latest_model_stats.last_full_refresh_run_rows_affected |
77 |
| - {% if target.type == 'bigquery' %} |
78 |
| - , latest_model_stats.last_full_refresh_run_bytes_processed |
79 |
| - {% endif %} |
80 |
| - , latest_model_stats.last_run_completed_at |
81 |
| - , latest_model_stats.last_run_total_runtime |
82 |
| - , latest_model_stats.last_run_rows_affected |
83 |
| - {% if target.type == 'bigquery' %} |
84 |
| - , latest_model_stats.last_run_bytes_processed |
85 |
| - {% endif %} |
86 |
| - , latest_model_stats.last_incremental_run_completed_at |
87 |
| - , latest_model_stats.last_incremental_run_total_runtime |
88 |
| - , latest_model_stats.last_incremental_run_rows_affected |
89 |
| - {% if target.type == 'bigquery' %} |
90 |
| - , latest_model_stats.last_incremental_run_bytes_processed |
91 |
| - {% endif %} |
92 |
| - from latest_models |
93 |
| - left join latest_model_stats |
94 |
| - on latest_models.node_id = latest_model_stats.node_id |
95 |
| -) |
| 1 | +with |
| 2 | + base as ( |
| 3 | + |
| 4 | + select * from {{ ref('stg_dbt__models') }} |
| 5 | + |
| 6 | + ) |
| 7 | + |
| 8 | + , model_executions as ( |
| 9 | + |
| 10 | + select * from {{ ref('stg_dbt__model_executions') }} |
| 11 | + |
| 12 | + ) |
| 13 | + |
| 14 | + , latest_models as ( |
| 15 | + |
| 16 | + /* Retrieves the models present in the most recent run */ |
| 17 | + select * |
| 18 | + from base |
| 19 | + where run_started_at = (select max(run_started_at) from base) |
| 20 | + |
| 21 | + ) |
| 22 | + |
| 23 | + , latest_models_runs as ( |
| 24 | + |
| 25 | + /* Retreives all successful run information for the models present in the most |
| 26 | + recent run and ranks them based on query completion time */ |
| 27 | + select |
| 28 | + model_executions.node_id |
| 29 | + , model_executions.was_full_refresh |
| 30 | + , model_executions.query_completed_at |
| 31 | + , model_executions.total_node_runtime |
| 32 | + , model_executions.rows_affected |
| 33 | + {% if target.type == 'bigquery' %} |
| 34 | + , model_executions.bytes_processed |
| 35 | + {% endif %} |
| 36 | + /* Row number by refresh and node ID */ |
| 37 | + , row_number() over ( |
| 38 | + partition by latest_models.node_id, model_executions.was_full_refresh |
| 39 | + order by model_executions.query_completed_at desc /* most recent ranked first */ |
| 40 | + ) as run_idx |
| 41 | + /* Row number by node ID */ |
| 42 | + , row_number() over ( |
| 43 | + partition by latest_models.node_id |
| 44 | + order by model_executions.query_completed_at desc /* most recent ranked first */ |
| 45 | + ) as run_idx_id_only |
| 46 | + from model_executions |
| 47 | + inner join latest_models on model_executions.node_id = latest_models.node_id |
| 48 | + where model_executions.status = 'success' |
| 49 | + |
| 50 | + ) |
| 51 | + |
| 52 | + , latest_model_stats as ( |
| 53 | + |
| 54 | + select |
| 55 | + node_id |
| 56 | + , max(case when was_full_refresh then query_completed_at end) as last_full_refresh_run_completed_at |
| 57 | + , max(case when was_full_refresh then total_node_runtime end) as last_full_refresh_run_total_runtime |
| 58 | + , max(case when was_full_refresh then rows_affected end) as last_full_refresh_run_rows_affected |
| 59 | + {% if target.type == 'bigquery' %} |
| 60 | + , max(case when was_full_refresh then bytes_processed end) as last_full_refresh_run_bytes_processed |
| 61 | + {% endif %} |
| 62 | + , max(case when run_idx_id_only = 1 then query_completed_at end) as last_run_completed_at |
| 63 | + , max(case when run_idx_id_only = 1 then total_node_runtime end) as last_run_total_runtime |
| 64 | + , max(case when run_idx_id_only = 1 then rows_affected end) as last_run_rows_affected |
| 65 | + {% if target.type == 'bigquery' %} |
| 66 | + , max(case when run_idx_id_only = 1 then bytes_processed end) as last_run_bytes_processed |
| 67 | + {% endif %} |
| 68 | + , max(case when not was_full_refresh then query_completed_at end) as last_incremental_run_completed_at |
| 69 | + , max(case when not was_full_refresh then total_node_runtime end) as last_incremental_run_total_runtime |
| 70 | + , max(case when not was_full_refresh then rows_affected end) as last_incremental_run_rows_affected |
| 71 | + {% if target.type == 'bigquery' %} |
| 72 | + , max(case when not was_full_refresh then bytes_processed end) as last_incremental_run_bytes_processed |
| 73 | + {% endif %} |
| 74 | + from latest_models_runs |
| 75 | + where run_idx = 1 |
| 76 | + group by 1 |
| 77 | + |
| 78 | + ) |
| 79 | + |
| 80 | + , final as ( |
| 81 | + |
| 82 | + select |
| 83 | + latest_models.* |
| 84 | + , latest_model_stats.last_full_refresh_run_completed_at |
| 85 | + , latest_model_stats.last_full_refresh_run_total_runtime |
| 86 | + , latest_model_stats.last_full_refresh_run_rows_affected |
| 87 | + {% if target.type == 'bigquery' %} |
| 88 | + , latest_model_stats.last_full_refresh_run_bytes_processed |
| 89 | + {% endif %} |
| 90 | + , latest_model_stats.last_run_completed_at |
| 91 | + , latest_model_stats.last_run_total_runtime |
| 92 | + , latest_model_stats.last_run_rows_affected |
| 93 | + {% if target.type == 'bigquery' %} |
| 94 | + , latest_model_stats.last_run_bytes_processed |
| 95 | + {% endif %} |
| 96 | + , latest_model_stats.last_incremental_run_completed_at |
| 97 | + , latest_model_stats.last_incremental_run_total_runtime |
| 98 | + , latest_model_stats.last_incremental_run_rows_affected |
| 99 | + {% if target.type == 'bigquery' %} |
| 100 | + , latest_model_stats.last_incremental_run_bytes_processed |
| 101 | + {% endif %} |
| 102 | + from latest_models |
| 103 | + left join latest_model_stats |
| 104 | + on latest_models.node_id = latest_model_stats.node_id |
| 105 | + |
| 106 | + ) |
96 | 107 |
|
97 | 108 | select * from final
|
0 commit comments