Skip to content

Commit 77e5034

Browse files
committed
docs(code): add explanatory comments to show_sample_rows and PII sampling macros
1 parent bddae5d commit 77e5034

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

macros/edr/materializations/test/test.sql

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,19 @@
7575
{% set disable_test_samples = flattened_test["meta"]["disable_test_samples"] %}
7676
{% endif %}
7777

78+
{#
79+
Sampling control precedence (highest to lowest):
80+
1. disable_test_samples meta flag — explicit per-test kill switch, always wins.
81+
2. show_sample_rows tag (model/test/column) — opt-in when
82+
enable_samples_on_show_sample_rows_tags is true. If the tag is present,
83+
skip all further checks and keep the sample_limit.
84+
3. enable_samples_on_show_sample_rows_tags — hide-by-default mode: if the
85+
feature is on but no show_sample_rows tag was found, disable samples.
86+
4. PII tag detection (model/test/column) — hide when disable_samples_on_pii_tags
87+
is true and a PII tag is detected at any level.
88+
#}
7889
{% if disable_test_samples %} {% set sample_limit = 0 %}
79-
{% elif elementary.should_show_sample_rows(flattened_test) %} {# show_sample_rows tag overrides default-hide #}
90+
{% elif elementary.should_show_sample_rows(flattened_test) %}
8091
{% elif elementary.get_config_var("enable_samples_on_show_sample_rows_tags") %}
8192
{% set sample_limit = 0 %}
8293
{% elif elementary.is_pii_table(flattened_test) %} {% set sample_limit = 0 %}

macros/edr/system/system_utils/get_pii_columns_from_parent_model.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
{% set column_nodes = parent_model.get("columns") %}
3939
{% if not column_nodes %} {% do return(pii_columns) %} {% endif %}
4040

41+
{#
42+
A column tagged show_sample_rows (without pii) should still appear in samples
43+
even when disable_samples_on_pii_tags is active — it is intentionally opted in.
44+
We only skip it from the PII columns list if it does NOT also carry a PII tag,
45+
since PII always takes precedence over show_sample_rows.
46+
#}
4147
{% set enable_show_tags = elementary.get_config_var(
4248
"enable_samples_on_show_sample_rows_tags"
4349
) %}
@@ -49,7 +55,7 @@
4955
{% for column_node in column_nodes.values() %}
5056
{% set all_column_tags_lower = elementary.get_column_tags(column_node) %}
5157

52-
{# PII takes precedence: only skip if show_sample_rows is set and pii is not #}
58+
{# Skip column from PII list only if show_sample_rows is set and pii is not #}
5359
{% set has_show_tag = (
5460
enable_show_tags
5561
and elementary.lists_intersection(

macros/edr/system/system_utils/is_pii_test.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
{#
2+
Complements is_pii_table (model-level) and should_disable_sampling_for_pii
3+
(column-level) by adding test-level PII tag support. A test tagged with a PII
4+
tag will have its samples disabled, consistent with the other two levels.
5+
#}
16
{% macro is_pii_test(flattened_test) %}
27
{% if not elementary.get_config_var("disable_samples_on_pii_tags") %}
38
{% do return(false) %}

macros/edr/system/system_utils/should_show_sample_rows.sql

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
{#
2+
Inverse of PII protection: when enable_samples_on_show_sample_rows_tags is true,
3+
samples are hidden by default and only shown when the show_sample_rows tag is present.
4+
5+
Checks three levels in order: model → test → column (test's target column only).
6+
Returns true if any level has a matching show_sample_rows tag.
7+
8+
PII precedence: if disable_samples_on_pii_tags is also enabled and the same
9+
model/column has a PII tag, PII wins and this returns false. This handles the
10+
edge case where both features are active simultaneously.
11+
12+
All tag matching is case-insensitive (tags are normalized to lowercase).
13+
#}
114
{% macro should_show_sample_rows(flattened_test) %}
215
{% if not elementary.get_config_var("enable_samples_on_show_sample_rows_tags") %}
316
{% do return(false) %}
@@ -8,7 +21,10 @@
821
{% else %} {% set show_tags = (raw_show_tags or []) | map("lower") | list %}
922
{% endif %}
1023
11-
{# Resolve PII tags once — only relevant when PII hiding is also enabled #}
24+
{#
25+
Resolve PII tags once upfront. We use `is string` (not `is iterable`) because
26+
strings are iterable in Jinja — iterating a string gives individual characters.
27+
#}
1228
{% set check_pii = elementary.get_config_var("disable_samples_on_pii_tags") %}
1329
{% if check_pii %}
1430
{% set raw_pii_tags = elementary.get_config_var("pii_tags") %}
@@ -18,14 +34,15 @@
1834
{% else %} {% set pii_tags = [] %}
1935
{% endif %}
2036
21-
{# Model-level check #}
37+
{# Model-level: show_sample_rows on the model applies to all its tests #}
2238
{% set raw_model_tags = elementary.insensitive_get_dict_value(
2339
flattened_test, "model_tags", []
2440
) %}
2541
{% if raw_model_tags is string %} {% set model_tags = [raw_model_tags | lower] %}
2642
{% else %} {% set model_tags = (raw_model_tags or []) | map("lower") | list %}
2743
{% endif %}
2844
{% if elementary.lists_intersection(model_tags, show_tags) | length > 0 %}
45+
{# PII on the model takes precedence over show_sample_rows on the same model #}
2946
{% if check_pii and elementary.lists_intersection(
3047
model_tags, pii_tags
3148
) | length > 0 %}
@@ -34,14 +51,15 @@
3451
{% do return(true) %}
3552
{% endif %}
3653
37-
{# Test-level check #}
54+
{# Test-level: show_sample_rows on the test definition itself #}
3855
{% set raw_test_tags = elementary.insensitive_get_dict_value(
3956
flattened_test, "tags", []
4057
) %}
4158
{% if raw_test_tags is string %} {% set test_tags = [raw_test_tags | lower] %}
4259
{% else %} {% set test_tags = (raw_test_tags or []) | map("lower") | list %}
4360
{% endif %}
4461
{% if elementary.lists_intersection(test_tags, show_tags) | length > 0 %}
62+
{# If the model itself is PII-tagged, respect that even for test-level overrides #}
4563
{% if check_pii and elementary.lists_intersection(
4664
model_tags, pii_tags
4765
) | length > 0 %}
@@ -50,7 +68,10 @@
5068
{% do return(true) %}
5169
{% endif %}
5270
53-
{# Column-level check: only the test's target column #}
71+
{#
72+
Column-level: only checks the specific column the test targets (test_column_name),
73+
not all columns on the model. This avoids showing samples for unrelated columns.
74+
#}
5475
{% set test_column_name = elementary.insensitive_get_dict_value(
5576
flattened_test, "test_column_name"
5677
) %}
@@ -67,6 +88,7 @@
6788
{% if elementary.lists_intersection(
6889
col_tags, show_tags
6990
) | length > 0 %}
91+
{# PII on the column takes precedence over show_sample_rows on the same column #}
7092
{% if check_pii and elementary.lists_intersection(
7193
col_tags, pii_tags
7294
) | length > 0 %}

0 commit comments

Comments
 (0)