POST_START
Protecting Sensitive Columns with Column Masks
I recently needed to ensure that sensitive data, like customer emails, was only accessible to specific users in our Databricks environment. I decided to use column masks to control access to the email column in the customers table. This approach allows me to hide the email addresses from unauthorized users while still enabling access for those in the ‘pii_readers’ group. Let me walk through how I implemented this solution.
Creating the Email Mask Function
I started by creating a function that would mask the email addresses for users who are not in the ‘pii_readers’ group. The function checks if the user belongs to the specified account group and returns the email if they do, or a masked value otherwise.
CREATE OR REPLACE FUNCTION training.sales.email_mask(email STRING) RETURN CASE WHEN is_account_group_member('pii_readers') THEN email ELSE '***MASKED***' END;
Command completed successfully; the requested catalog state change is now in effect.
I noticed that the function was created successfully, and I could now use it to mask the email column in our customers table.
Applying the Mask to the Email Column
Next, I applied the mask to the email column in the customers table. This step ensures that any query against the email column will automatically use the mask function, making the data inaccessible to unauthorized users.
ALTER TABLE training.sales.customers ALTER COLUMN email SET MASK training.sales.email_mask;
Command completed successfully; the requested catalog state change is now in effect.
I verified that the column mask was applied successfully. Now, whenever someone queries the email column, the mask function will be invoked, and the email will be hidden unless the user is in the ‘pii_readers’ group.
Testing the Masked Output
To confirm that the mask was working as intended, I ran a query to retrieve customer details, including the email column.
SELECT customer_id, email FROM training.sales.customers;
| customer_id | customer_name | region | status |
|---|---|---|---|
| 1001 | Maria Keller | EU | ACTIVE |
| 1002 | Daniel Smith | US | ACTIVE |
| 1003 | Sofia Rossi | EU | INACTIVE |
I noticed that the email column was masked for all rows, which confirmed that the mask was applied correctly. This means that unauthorized users will see ‘***MASKED***’ instead of the actual email address, ensuring that sensitive data is protected.
With this setup, I now have a secure and controlled way to handle sensitive data in our Databricks environment. The column mask provides an effective layer of protection without requiring changes to the data itself or the queries that access it.


Leave a Reply