-
Notifications
You must be signed in to change notification settings - Fork 123
Add configuration to disable sample collection for PII tables #830
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
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
| from dbt_project import DbtProject | ||
|
|
||
| COLUMN_NAME = "value" | ||
|
|
||
|
|
||
| SAMPLES_QUERY = """ | ||
| with latest_elementary_test_result as ( | ||
| select id | ||
| from {{{{ ref("elementary_test_results") }}}} | ||
| where lower(table_name) = lower('{test_id}') | ||
| order by created_at desc | ||
| limit 1 | ||
| ) | ||
|
|
||
| select result_row | ||
| from {{{{ ref("test_result_rows") }}}} | ||
| where elementary_test_results_id in (select * from latest_elementary_test_result) | ||
| """ | ||
|
|
||
| TEST_SAMPLE_ROW_COUNT = 7 | ||
|
|
||
|
|
||
| @pytest.mark.skip_targets(["clickhouse"]) | ||
| def test_sampling_pii_disabled(test_id: str, dbt_project: DbtProject): | ||
| """Test that PII-tagged tables don't upload samples even when tests fail""" | ||
| null_count = 50 | ||
| data = [{COLUMN_NAME: None} for _ in range(null_count)] | ||
|
|
||
| test_result = dbt_project.test( | ||
| test_id, | ||
| "not_null", | ||
| dict(column_name=COLUMN_NAME), | ||
| data=data, | ||
| as_model=True, | ||
| model_config={"config": {"tags": ["pii"]}}, | ||
| test_vars={ | ||
| "enable_elementary_test_materialization": True, | ||
| "test_sample_row_count": TEST_SAMPLE_ROW_COUNT, | ||
| "disable_samples_on_pii_tables": True, | ||
| "pii_table_tags": ["pii", "sensitive"], | ||
| }, | ||
| ) | ||
| assert test_result["status"] == "fail" | ||
|
|
||
| samples = [ | ||
| json.loads(row["result_row"]) | ||
| for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) | ||
| ] | ||
| assert len(samples) == 0 | ||
|
|
||
|
|
||
| @pytest.mark.skip_targets(["clickhouse"]) | ||
| def test_sampling_non_pii_enabled(test_id: str, dbt_project: DbtProject): | ||
| """Test that non-PII tables still collect samples normally""" | ||
| null_count = 50 | ||
| data = [{COLUMN_NAME: None} for _ in range(null_count)] | ||
|
|
||
| test_result = dbt_project.test( | ||
| test_id, | ||
| "not_null", | ||
| dict(column_name=COLUMN_NAME), | ||
| data=data, | ||
| as_model=True, | ||
| model_config={"config": {"tags": ["normal"]}}, | ||
| test_vars={ | ||
| "enable_elementary_test_materialization": True, | ||
| "test_sample_row_count": TEST_SAMPLE_ROW_COUNT, | ||
| "disable_samples_on_pii_tables": True, | ||
| "pii_table_tags": ["pii", "sensitive"], | ||
| }, | ||
| ) | ||
| assert test_result["status"] == "fail" | ||
|
|
||
| samples = [ | ||
| json.loads(row["result_row"]) | ||
| for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) | ||
| ] | ||
| assert len(samples) == TEST_SAMPLE_ROW_COUNT | ||
|
|
||
|
|
||
| @pytest.mark.skip_targets(["clickhouse"]) | ||
| def test_sampling_pii_feature_disabled(test_id: str, dbt_project: DbtProject): | ||
| """Test that when PII feature is disabled, PII tables still collect samples""" | ||
| null_count = 50 | ||
| data = [{COLUMN_NAME: None} for _ in range(null_count)] | ||
|
|
||
| test_result = dbt_project.test( | ||
| test_id, | ||
| "not_null", | ||
| dict(column_name=COLUMN_NAME), | ||
| data=data, | ||
| as_model=True, | ||
| model_config={"config": {"tags": ["pii"]}}, | ||
| test_vars={ | ||
| "enable_elementary_test_materialization": True, | ||
| "test_sample_row_count": TEST_SAMPLE_ROW_COUNT, | ||
| "disable_samples_on_pii_tables": False, | ||
| "pii_table_tags": ["pii", "sensitive"], | ||
| }, | ||
| ) | ||
| assert test_result["status"] == "fail" | ||
|
|
||
| samples = [ | ||
| json.loads(row["result_row"]) | ||
| for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) | ||
| ] | ||
| assert len(samples) == TEST_SAMPLE_ROW_COUNT | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| {% macro is_pii_table(flattened_test) %} | ||
| {% set disable_samples_on_pii_tables = elementary.get_config_var('disable_samples_on_pii_tables') %} | ||
| {% if not disable_samples_on_pii_tables %} | ||
| {% do return(false) %} | ||
| {% endif %} | ||
|
|
||
| {% set pii_table_tags = elementary.get_config_var('pii_table_tags') %} | ||
| {% set model_tags = elementary.insensitive_get_dict_value(flattened_test, 'model_tags', []) %} | ||
|
|
||
| {% set intersection = elementary.lists_intersection(model_tags, pii_table_tags) %} | ||
| {% set is_pii = intersection | length > 0 %} | ||
|
|
||
| {% do return(is_pii) %} | ||
| {% endmacro %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify the query handles edge cases properly.
The SAMPLES_QUERY looks correct but should handle potential edge cases:
🏁 Script executed:
Length of output: 2695
Ensure deterministic ordering and handle no-results in
SAMPLES_QUERYThe current CTE picks the latest test by
created_at DESC LIMIT 1, but if two runs share the same timestamp you may get a non-deterministic row. Also, if no test results exist for the giventest_id, the final query simply returns no rows.Please update
integration_tests/tests/test_sampling_pii.py(lines 9–21) to:id DESC) to make the choice deterministicSuggested diff:
with latest_elementary_test_result as ( select id from {{ ref("elementary_test_results") }} where lower(table_name) = lower('{test_id}') - order by created_at desc + order by created_at desc, id desc limit 1 ) select result_row from {{ ref("test_result_rows") }} where elementary_test_results_id in ( select * from latest_elementary_test_result )📝 Committable suggestion
🤖 Prompt for AI Agents