Sub topic 11
Row Filters and Column Masks
This chapter assumes you know how to grant privileges with GRANT and REVOKE, and that you have seen dynamic views. Here you will restrict data inside a table with a row filter and a column mask.
Why filter inside the table
A dynamic view puts the access logic in a separate object. Users who can query the base table bypass it. Row filters and column masks attach the logic to the table itself, so every query path sees the same rules.
- A row filter is a SQL function that returns a boolean for each row. Rows where it returns
falseorNULLare removed from the results. - A column mask is a SQL function that returns a replacement value for a column. The replacement is used instead of the stored value.
Both functions typically call is_account_group_member(), which checks whether the user running the query belongs to an account-level group.
Applying a row filter
Create a function that takes the filtered column as input and returns a boolean. Then attach it to the table and name the column or columns to pass in.
CREATE FUNCTION main.sales.us_only(region STRING)
RETURNS BOOLEAN
RETURN is_account_group_member('admins') OR region = 'US';
ALTER TABLE main.sales.orders SET ROW FILTER main.sales.us_only ON (region);Members of admins see every row. Everyone else sees only rows where region is 'US'. The filter runs on every read of main.sales.orders, whether the query goes through a notebook, a SQL warehouse, or a view built on the table.
Applying a column mask
A mask function takes the column value and returns a value of the same type. The mask is attached to a single column.
CREATE FUNCTION main.sales.mask_email(email STRING)
RETURNS STRING
RETURN CASE WHEN is_account_group_member('pii_readers') THEN email ELSE '***' END;
ALTER TABLE main.sales.customers ALTER COLUMN email SET MASK main.sales.mask_email;Users in pii_readers see real addresses. Everyone else sees ***.
Choosing between the approaches
| Approach | Where logic lives | Bypassed by direct table access? |
|---|---|---|
| Dynamic view | A separate view | Yes, if users can read the base table |
| Row filter | Function attached to the table | No |
| Column mask | Function attached to a column | No |
Use dynamic views when you need a different shape of data for a specific audience. Use filters and masks when the rule must hold for every consumer of the table.
Common errors
- Wrong group function. Use
is_account_group_member()for account-level groups. A workspace-local group check can give different results across workspaces. - Function permissions. The function lives in a schema, so it needs to be reachable. The table owner sets the policy, but verify that the function is in a schema the owner can use.
- Type mismatch. A mask must return the same data type as the column. Returning
'***'for anINTcolumn fails. - Unsupported compute. Filters and masks are enforced only on compute that supports them. Check the compute access mode for your cluster, as covered in a later chapter.
- Assuming the owner is exempt. Ownership does not skip the policy. If you want the owner to see all rows, put the owner's group in the function.
Removing policies
You can detach a policy without dropping the function.
ALTER TABLE main.sales.orders DROP ROW FILTER;
ALTER TABLE main.sales.customers ALTER COLUMN email DROP MASK;Exercise
A table has a row filter that keeps only rows where region = 'US' for non-admins. A row has region set to NULL. Will a non-admin user see it, and why?
Show answer
No. For a non-admin, the filter evaluates FALSE OR (NULL = 'US'), which is NULL. Rows are kept only when the function returns true, so the row is removed.
Exercise
Why might you prefer a column mask over a dynamic view to hide ssn values in a table that many teams query directly?
Show answer
A mask is attached to the column, so it applies on every query path to the table. A dynamic view only protects users who query the view, and users with SELECT on the base table can still read the real values.