From 8082fa768c39bc0534185ca5b703f7f3842e3e89 Mon Sep 17 00:00:00 2001 From: ariosramirez Date: Wed, 28 Feb 2024 00:00:38 -0300 Subject: [PATCH 1/3] added duplicate column names in check_cols validation --- core/dbt/artifacts/resources/v1/snapshot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/dbt/artifacts/resources/v1/snapshot.py b/core/dbt/artifacts/resources/v1/snapshot.py index 3eceb9bb1d2..2b32eebe332 100644 --- a/core/dbt/artifacts/resources/v1/snapshot.py +++ b/core/dbt/artifacts/resources/v1/snapshot.py @@ -52,6 +52,10 @@ def validate(cls, data): if data.get("materialized") and data.get("materialized") != "snapshot": raise ValidationError("A snapshot must have a materialized value of 'snapshot'") + # Validate if there are duplicate column names in check_cols. + if len(data.get("check_cols")) != len(set(data.get("check_cols"))): + raise ValidationError(f"Duplicate column names in 'check_cols': {data['check_cols']}.") + # Called by "calculate_node_config_dict" in ContextConfigGenerator def finalize_and_validate(self): data = self.to_dict(omit_none=True) From 5343684ff38608010fe97e7ba2431951b7c95340 Mon Sep 17 00:00:00 2001 From: ariosramirez Date: Wed, 28 Feb 2024 00:02:25 -0300 Subject: [PATCH 2/3] Added test to validate if an error is raised when check_cols has duplicated columns --- tests/unit/test_contracts_graph_parsed.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/unit/test_contracts_graph_parsed.py b/tests/unit/test_contracts_graph_parsed.py index 0e9a1c523f7..667c74fa920 100644 --- a/tests/unit/test_contracts_graph_parsed.py +++ b/tests/unit/test_contracts_graph_parsed.py @@ -1474,6 +1474,14 @@ def test_missing_snapshot_configs(basic_check_snapshot_config_dict): SnapshotConfig.validate(wrong_fields) +def test_duplicate_check_cols(basic_check_snapshot_config_dict): + duplicate_cols = basic_check_snapshot_config_dict + # Introducing duplicate column names + duplicate_cols["check_cols"] = ["col1", "col2", "col2"] + with pytest.raises(ValidationError, match=r"Duplicate column names in 'check_cols'"): + SnapshotConfig.validate(duplicate_cols) + + def test_invalid_check_value(basic_check_snapshot_config_dict): invalid_check_type = basic_check_snapshot_config_dict invalid_check_type["check_cols"] = "some" From 67d5cb48d5643d7a5be5f45209b750f5e89b5f57 Mon Sep 17 00:00:00 2001 From: ariosramirez Date: Wed, 28 Feb 2024 19:27:59 -0300 Subject: [PATCH 3/3] added .changes/unreleased/Features-20240228-192518.yaml file --- .changes/unreleased/Features-20240228-192518.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Features-20240228-192518.yaml diff --git a/.changes/unreleased/Features-20240228-192518.yaml b/.changes/unreleased/Features-20240228-192518.yaml new file mode 100644 index 00000000000..5f52a703952 --- /dev/null +++ b/.changes/unreleased/Features-20240228-192518.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Validation to detect duplicate column names in the check_cols configuration of a snapshot. +time: 2024-02-28T19:25:18.00447-03:00 +custom: + Author: ariosramirez + Issue: "9656"