|
| 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 model |
| 9 | + or column has a PII tag, PII wins and this returns false. A model-level PII |
| 10 | + tag blocks show_sample_rows at every level (model, test, and column). |
| 11 | +
|
| 12 | + All tag matching is case-insensitive (tags are normalized to lowercase). |
| 13 | +#} |
| 14 | +{% macro should_show_sample_rows(flattened_test) %} |
| 15 | + {% if not elementary.get_config_var("enable_samples_on_show_sample_rows_tags") %} |
| 16 | + {% do return(false) %} |
| 17 | + {% endif %} |
| 18 | +
|
| 19 | + {% set raw_show_tags = elementary.get_config_var("show_sample_rows_tags") %} |
| 20 | + {% if raw_show_tags is string %} {% set show_tags = [raw_show_tags | lower] %} |
| 21 | + {% else %} {% set show_tags = (raw_show_tags or []) | map("lower") | list %} |
| 22 | + {% endif %} |
| 23 | +
|
| 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 | + #} |
| 28 | + {% set check_pii = elementary.get_config_var("disable_samples_on_pii_tags") %} |
| 29 | + {% if check_pii %} |
| 30 | + {% set raw_pii_tags = elementary.get_config_var("pii_tags") %} |
| 31 | + {% if raw_pii_tags is string %} {% set pii_tags = [raw_pii_tags | lower] %} |
| 32 | + {% else %} {% set pii_tags = (raw_pii_tags or []) | map("lower") | list %} |
| 33 | + {% endif %} |
| 34 | + {% else %} {% set pii_tags = [] %} |
| 35 | + {% endif %} |
| 36 | +
|
| 37 | + {# Model-level: show_sample_rows on the model applies to all its tests #} |
| 38 | + {% set raw_model_tags = elementary.insensitive_get_dict_value( |
| 39 | + flattened_test, "model_tags", [] |
| 40 | + ) %} |
| 41 | + {% if raw_model_tags is string %} {% set model_tags = [raw_model_tags | lower] %} |
| 42 | + {% else %} {% set model_tags = (raw_model_tags or []) | map("lower") | list %} |
| 43 | + {% endif %} |
| 44 | + {% 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 #} |
| 46 | + {% if check_pii and elementary.lists_intersection( |
| 47 | + model_tags, pii_tags |
| 48 | + ) | length > 0 %} |
| 49 | + {% do return(false) %} |
| 50 | + {% endif %} |
| 51 | + {% do return(true) %} |
| 52 | + {% endif %} |
| 53 | +
|
| 54 | + {# Test-level: show_sample_rows on the test definition itself #} |
| 55 | + {% set raw_test_tags = elementary.insensitive_get_dict_value( |
| 56 | + flattened_test, "tags", [] |
| 57 | + ) %} |
| 58 | + {% if raw_test_tags is string %} {% set test_tags = [raw_test_tags | lower] %} |
| 59 | + {% else %} {% set test_tags = (raw_test_tags or []) | map("lower") | list %} |
| 60 | + {% endif %} |
| 61 | + {% 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 #} |
| 63 | + {% if check_pii and elementary.lists_intersection( |
| 64 | + model_tags, pii_tags |
| 65 | + ) | length > 0 %} |
| 66 | + {% do return(false) %} |
| 67 | + {% endif %} |
| 68 | + {% do return(true) %} |
| 69 | + {% endif %} |
| 70 | +
|
| 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 | + #} |
| 75 | + {% set test_column_name = elementary.insensitive_get_dict_value( |
| 76 | + flattened_test, "test_column_name" |
| 77 | + ) %} |
| 78 | + {% if test_column_name %} |
| 79 | + {% set parent_model_unique_id = elementary.insensitive_get_dict_value( |
| 80 | + flattened_test, "parent_model_unique_id" |
| 81 | + ) %} |
| 82 | + {% set parent_model = elementary.get_node(parent_model_unique_id) %} |
| 83 | + {% if parent_model %} |
| 84 | + {% set column_nodes = parent_model.get("columns", {}) %} |
| 85 | + {% for col_name, col_node in column_nodes.items() %} |
| 86 | + {% if col_name | lower == test_column_name | lower %} |
| 87 | + {% set col_tags = elementary.get_column_tags(col_node) %} |
| 88 | + {% if elementary.lists_intersection( |
| 89 | + col_tags, show_tags |
| 90 | + ) | length > 0 %} |
| 91 | + {# PII on the column or model takes precedence over show_sample_rows #} |
| 92 | + {% if check_pii and ( |
| 93 | + elementary.lists_intersection(col_tags, pii_tags) |
| 94 | + | length |
| 95 | + > 0 |
| 96 | + or elementary.lists_intersection( |
| 97 | + model_tags, pii_tags |
| 98 | + ) |
| 99 | + | length |
| 100 | + > 0 |
| 101 | + ) %} |
| 102 | + {% do return(false) %} |
| 103 | + {% endif %} |
| 104 | + {% do return(true) %} |
| 105 | + {% endif %} |
| 106 | + {% endif %} |
| 107 | + {% endfor %} |
| 108 | + {% endif %} |
| 109 | + {% endif %} |
| 110 | +
|
| 111 | + {% do return(false) %} |
| 112 | +{% endmacro %} |
0 commit comments