Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 9656 Raise an error when check_cols has duplicated values #9698

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240228-192518.yaml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions core/dbt/artifacts/resources/v1/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def final_validate(self):
f"Invalid value for 'check_cols': {self.check_cols}. "
"Expected 'all' or a list of strings."
)
# Validate if there are duplicate column names in check_cols.
if isinstance(self.check_cols, list):
if len(self.check_cols) != len(set(self.check_cols)):
raise ValidationError(
f"Duplicate column names in 'check_cols':" f" {self.check_cols}."
)
elif self.strategy == "timestamp":
if not self.updated_at:
raise ValidationError(
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/contracts/graph/test_nodes_parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,16 @@ def assert_snapshot_config_fails_validation(dct):
obj.final_validate()


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)
cfg = SnapshotConfig.from_dict(duplicate_cols)
cfg.final_validate()


def test_invalid_check_value(basic_check_snapshot_config_dict):
invalid_check_type = basic_check_snapshot_config_dict
invalid_check_type["check_cols"] = "some"
Expand Down