|
| 1 | +{% macro fetch_column_masks(relation) -%} |
| 2 | + {% if relation.is_hive_metastore() %} |
| 3 | + {{ exceptions.raise_compiler_error("Column masks are not supported for Hive Metastore") }} |
| 4 | + {%- endif %} |
| 5 | + {% call statement('list_column_masks', fetch_result=True) -%} |
| 6 | + {{ fetch_column_masks_sql(relation) }} |
| 7 | + {% endcall %} |
| 8 | + {% do return(load_result('list_column_masks').table) %} |
| 9 | +{%- endmacro -%} |
| 10 | + |
| 11 | +{% macro fetch_column_masks_sql(relation) -%} |
| 12 | + SELECT |
| 13 | + column_name, |
| 14 | + mask_name |
| 15 | + FROM `{{ relation.database|lower }}`.information_schema.column_masks |
| 16 | + WHERE table_catalog = '{{ relation.database|lower }}' |
| 17 | + AND table_schema = '{{ relation.schema|lower }}' |
| 18 | + AND table_name = '{{ relation.identifier|lower }}'; |
| 19 | +{%- endmacro -%} |
| 20 | + |
| 21 | +{% macro apply_column_masks_from_model_columns(relation) -%} |
| 22 | + {% if relation.is_hive_metastore() %} |
| 23 | + {{ exceptions.raise_compiler_error("Column masks are not supported for Hive Metastore") }} |
| 24 | + {%- endif %} |
| 25 | + {{ log("Applying column masks from model to relation " ~ relation) }} |
| 26 | + {% set columns = model.get('columns', {}) %} |
| 27 | + {% for column_name, column_def in columns.items() %} |
| 28 | + {% if column_def is mapping and column_def.get('column_mask') %} |
| 29 | + {%- call statement('main') -%} |
| 30 | + {{ alter_set_column_mask(relation, column_name, column_def.column_mask) }} |
| 31 | + {%- endcall -%} |
| 32 | + {% endif %} |
| 33 | + {% endfor %} |
| 34 | +{%- endmacro -%} |
| 35 | + |
| 36 | +{% macro apply_column_masks(relation, column_masks) -%} |
| 37 | + {% if relation.is_hive_metastore() %} |
| 38 | + {{ exceptions.raise_compiler_error("Column masks are not supported for Hive Metastore") }} |
| 39 | + {%- endif %} |
| 40 | + {{ log("Applying column masks to relation " ~ relation) }} |
| 41 | + {%- if column_masks.unset_column_mask %} |
| 42 | + {%- for column in column_masks.unset_column_mask -%} |
| 43 | + {%- call statement('main') -%} |
| 44 | + {{ alter_drop_column_mask(relation, column) }} |
| 45 | + {%- endcall -%} |
| 46 | + {%- endfor -%} |
| 47 | + {%- endif %} |
| 48 | + {%- if column_masks.set_column_mask %} |
| 49 | + {%- for column, mask in column_masks.set_column_mask.items() -%} |
| 50 | + {%- call statement('main') -%} |
| 51 | + {{ alter_set_column_mask(relation, column, mask) }} |
| 52 | + {%- endcall -%} |
| 53 | + {%- endfor -%} |
| 54 | + {%- endif %} |
| 55 | +{%- endmacro -%} |
| 56 | + |
| 57 | +{% macro alter_drop_column_mask(relation, column) -%} |
| 58 | + ALTER TABLE {{ relation.render() }} |
| 59 | + ALTER COLUMN {{ column }} |
| 60 | + DROP MASK; |
| 61 | +{%- endmacro -%} |
| 62 | + |
| 63 | +{% macro alter_set_column_mask(relation, column, mask) -%} |
| 64 | + ALTER TABLE {{ relation.render() }} |
| 65 | + ALTER COLUMN {{ column }} |
| 66 | + SET MASK {{ mask }}; |
| 67 | +{%- endmacro -%} |
| 68 | + |
| 69 | + |
0 commit comments