diff --git a/docs/img/unused_staging_models.png b/docs/img/unused_staging_models.png new file mode 100644 index 00000000..5ff612af Binary files /dev/null and b/docs/img/unused_staging_models.png differ diff --git a/docs/rules.md b/docs/rules.md index a6f814ed..96687369 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -19,6 +19,7 @@ hide: |Modeling |[Root Models](../rules/modeling/#root-models) |`fct_root_models`| |Modeling |[Staging Models Dependent on Downstream Models](../rules/modeling/#staging-models-dependent-on-downstream-models) |`fct_staging_dependent_on_marts_or_intermediate`| |Modeling |[Unused Sources](../rules/modeling/#unused-sources) |`fct_unused_sources`| +|Modeling |[Unused Staging Models](../rules/modeling/#unused-staging-models) |`fct_unused_staging_models`| |Modeling |[Models with Too Many Joins](../rules/modeling/#models-with-too-many-joins) |`fct_too_many_joins`| |Testing |[Missing Primary Key Tests](../rules/testing/#missing-primary-key-tests) |`fct_missing_primary_key_tests`| |Testing |[Missing Source Freshness](../rules/testing/#missing-source-freshness) |`fct_sources_without_freshness`| diff --git a/docs/rules/modeling.md b/docs/rules/modeling.md index b00f0610..5e76b97a 100644 --- a/docs/rules/modeling.md +++ b/docs/rules/modeling.md @@ -430,6 +430,26 @@ or any other nested information. ![A refactored DAG showing three sources which are each being referenced by an accompanying staging model](https://user-images.githubusercontent.com/30663534/159603703-6e94b00b-07d1-4f47-89df-8e5685d9fcf0.png){ width=500 } +--- +## Unused Staging Models + +`fct_unused_staging_models` ([source](https://github.com/dbt-labs/dbt-project-evaluator/tree/main/models/marts/dag/fct_unused_staging_models.sql)) shows each staging model with 0 children. + +**Example** + +`model.stg_auditboard__teams` isn't being referenced. + +![A DAG showing three staging_models which are each being referenced by an accompanying staging model, and one source that isn't being referenced at all](../img/unused_staging_models.png){ width=500 } + +**Reason to Flag** + +This represents a staging models that was created but never used in downstream models. This simply +represents the buildup of cruft in the project that doesn’t need to be there. + +**How to Remediate** + +Remove the unused staging model and their sources from the project. + --- ## Models with Too Many Joins diff --git a/integration_tests/seeds/dag/dag_seeds.yml b/integration_tests/seeds/dag/dag_seeds.yml index b963404a..6e6861d3 100644 --- a/integration_tests/seeds/dag/dag_seeds.yml +++ b/integration_tests/seeds/dag/dag_seeds.yml @@ -42,6 +42,12 @@ seeds: name: equality_fct_unused_sources compare_model: ref('fct_unused_sources') + - name: test_fct_unused_staging_models + data_tests: + - dbt_utils.equality: + name: equality_fct_unused_staging_models + compare_model: ref('fct_unused_staging_models') + - name: test_fct_source_fanout data_tests: - dbt_utils.equality: diff --git a/integration_tests/seeds/dag/test_fct_unused_staging_models.csv b/integration_tests/seeds/dag/test_fct_unused_staging_models.csv new file mode 100644 index 00000000..de3a8302 --- /dev/null +++ b/integration_tests/seeds/dag/test_fct_unused_staging_models.csv @@ -0,0 +1,3 @@ +parent +model.stage_model_4 +model.stage_model_5 \ No newline at end of file diff --git a/models/marts/dag/dag.yml b/models/marts/dag/dag.yml index 5ce89973..478ecb77 100644 --- a/models/marts/dag/dag.yml +++ b/models/marts/dag/dag.yml @@ -48,6 +48,10 @@ models: description: "This table shows each source with 0 children." data_tests: - is_empty + - name: fct_unused_staging_models + description: "This table shows each staging model with 0 children." + data_tests: + - is_empty - name: fct_exposure_parents_materializations description: "This table shows each direct parent of an exposure that is not materialized as a table or incremental." data_tests: diff --git a/models/marts/dag/fct_unused_staging_models.sql b/models/marts/dag/fct_unused_staging_models.sql new file mode 100644 index 00000000..f520ec76 --- /dev/null +++ b/models/marts/dag/fct_unused_staging_models.sql @@ -0,0 +1,23 @@ +-- this model finds cases where a source has no children + +with source_relationships as ( + select + * + from {{ ref('int_all_dag_relationships') }} + where parent_resource_type = 'model' + and parent_id like '%stg_%' + and not parent_is_excluded + and not child_is_excluded +), + +final as ( + select + parent + from source_relationships + group by 1 + having max(distance) = 0 +) + +select * from final + +{{ filter_exceptions() }} \ No newline at end of file