Sub topic 11
Row filters and column masks
This chapter covers how to restrict the rows and columns a user sees in a table by attaching SQL functions to the table. It assumes you can already grant and revoke privileges, know how groups work in Unity Catalog, and have seen dynamic views. Those are the earlier chapters on grants, principals and views.
Why filters and masks exist
Table-level privileges are all or nothing. A user with SELECT on a table reads every row and every column. Often that is too much. A regional analyst should see only their region's orders. A support agent should see customer records but not full email addresses.
Unity Catalog offers two ways to restrict data inside a table:
- Dynamic views wrap a table in a view that applies the logic. Users query the view, not the table.
- Row filters and column masks attach the logic to the table itself. Users keep querying the same table name, and the restriction applies automatically.
A row filter is a SQL function that returns a boolean for each row. Rows where it returns true are visible, and the rest are hidden. A column mask is a SQL function that takes the column's value and returns a replacement value, so the user sees the replacement instead of the stored value.
How they work
Both are ordinary SQL user-defined functions (UDFs) stored in a catalog and schema. You create the function, then attach it to a table with ALTER TABLE. When someone queries the table, Unity Catalog evaluates the function for each row and applies the result before returning data.
The user still needs the normal privileges first. Filters and masks narrow what a user can see. They never grant access.
Row filters
A row filter function takes one or more columns as parameters and returns BOOLEAN. The next example shows only US rows to most users and all rows to members of admins.
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);Notes on this example:
- The function's parameter list defines which table columns it receives.
ON (region)maps the table'sregioncolumn to the function'sregionparameter, in order. is_account_group_member('admins')checks the account-level group membership of the user running the query. Use this function rather thanis_member(), which checks workspace-local groups and can behave differently when a table is accessed from another workspace.- A row filter must be deterministic and must return a boolean. A
NULLresult hides the row, the same asfalse.
To remove a row filter:
ALTER TABLE main.sales.orders DROP ROW FILTER;A table can have only one row filter. To change the logic, drop the filter and set a new one, or redefine the function with CREATE OR REPLACE FUNCTION.
A filter driven by a mapping table
Hard-coding groups in the function gets unwieldy. A common pattern is to look up allowed values in a mapping table. The function below shows a row only if the current user has an entry for that region.
CREATE TABLE main.sales.region_access (
user_email STRING,
region STRING
);
CREATE FUNCTION main.sales.by_region(region STRING)
RETURNS BOOLEAN
RETURN
is_account_group_member('admins')
OR EXISTS (
SELECT 1
FROM main.sales.region_access ra
WHERE ra.user_email = current_user()
AND ra.region = region
);Treat the mapping table as sensitive. Anyone who can write to it can change who sees what. Grant MODIFY on it only to the team that administers access. Also check your compute and runtime documentation for limits on functions that read other tables, because support varies by runtime version.
Column masks
A column mask function takes the column's value as its first parameter and returns a value of the same type. It can take extra columns from the same row as additional parameters, which lets the decision depend on other data.
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;Members of pii_readers see the real email. Everyone else sees ***. The stored data does not change. Only the query result does.
To remove a mask:
ALTER TABLE main.sales.customers ALTER COLUMN email DROP MASK;Partial masking
A mask does not have to hide everything. This one keeps the domain, which is often enough for analysis.
CREATE FUNCTION main.sales.mask_email_partial(email STRING)
RETURNS STRING
RETURN CASE
WHEN is_account_group_member('pii_readers') THEN email
ELSE concat('***@', substring_index(email, '@', -1))
END;Masks that use other columns
Pass extra columns with USING COLUMNS. This mask reveals the value only for rows in the caller's region.
CREATE FUNCTION main.sales.mask_by_region(phone STRING, region STRING)
RETURNS STRING
RETURN CASE
WHEN is_account_group_member('admins') OR region = 'US' THEN phone
ELSE '***'
END;
ALTER TABLE main.sales.customers
ALTER COLUMN phone SET MASK main.sales.mask_by_region USING COLUMNS (region);Choosing between the three tools
| Need | Best fit | Reason |
|---|---|---|
| Hide some rows from some groups | Row filter | Applies on the table itself, so no user can bypass it by choosing a different object |
| Hide or alter one column's values | Column mask | Same table name, same schema, only values change |
| Expose a reshaped or joined result | Dynamic view | The view can select, rename and join, which a filter cannot do |
| Different logic per consumer, with no change to the table | Dynamic view | Each view can encode its own rules |
Row filters and masks are generally the better default for a table that many people query. The rule lives in one place. With dynamic views, the base table must be locked down and users must be pointed at the right view, and a missed grant on the base table defeats the control.
Managing and inspecting
- Privileges to create: you need
CREATE FUNCTIONon the schema to create the function. To attach it, you need to own the table or haveMANAGEon it, plusEXECUTEon the function. - Who needs
EXECUTE: querying users do not needEXECUTEon the filter or mask function. The function runs with the permissions needed to evaluate the policy, so users cannot call it to learn about hidden data unless you also grant them access to it. - Where to look:
DESCRIBE EXTENDED main.sales.ordersshows table details, including whether a row filter is set. The Catalog Explorer table page also shows attached filters and masks. - Auditing: changes to filters and masks appear in
system.access.auditlike other Unity Catalog actions.
Edge cases and gotchas
- Compute matters. Filters and masks are enforced only on compute that supports them, such as SQL warehouses and standard or serverless compute. Compute that cannot enforce them either fails the query or is blocked from reading the table. Check the supported runtime versions for your workspace before relying on them.
- Performance. The function runs for every row. Keep it simple, avoid subqueries on large tables, and prefer group checks to per-row lookups where possible.
- Table operations. Some operations are restricted on tables with filters or masks. Examples are certain clones, shares and streaming reads. Test the workflows that matter to you.
- Owners and admins. Table ownership does not bypass a filter. If the function says an owner sees fewer rows, the owner sees fewer rows. Include an admin group in the function if administrators need full access.
- Type mismatches. A mask must return the same type as the column. Masking a numeric column with the string
'***'fails. UseNULLor a placeholder number such as0, or a different approach, for numeric columns. - Dropping the function. You cannot drop a function that is in use by a table without first removing the filter or mask. Always detach before dropping.
- Derived data leaks. Data copied into another table by a user who sees all rows does not carry the policy. Limit who can see unfiltered data, and control where they can write it.
A complete example
This sequence protects an orders table by region and a customers table by email.
-- 1. Create the functions
CREATE OR REPLACE FUNCTION main.sales.us_only(region STRING)
RETURNS BOOLEAN
RETURN is_account_group_member('admins') OR region = 'US';
CREATE OR REPLACE FUNCTION main.sales.mask_email(email STRING)
RETURNS STRING
RETURN CASE WHEN is_account_group_member('pii_readers') THEN email ELSE '***' END;
-- 2. Attach them
ALTER TABLE main.sales.orders SET ROW FILTER main.sales.us_only ON (region);
ALTER TABLE main.sales.customers ALTER COLUMN email SET MASK main.sales.mask_email;
-- 3. Grant normal read access
GRANT SELECT ON TABLE main.sales.orders TO `data-analysts`;
GRANT SELECT ON TABLE main.sales.customers TO `data-analysts`;
-- 4. Test as a member of data-analysts
SELECT region, count(*) FROM main.sales.orders GROUP BY region;
SELECT email FROM main.sales.customers LIMIT 5;A member of data-analysts who is not in admins or pii_readers sees only US in the first query and *** in the second. Test with a real account in each group, not just as yourself, because your own admin rights can hide mistakes.
Summary
Row filters hide rows and column masks replace values, both through SQL functions attached to a table. They apply automatically to every query, rely on is_account_group_member for group checks, and narrow access without granting any. Keep functions simple, include an admin path, and test with real accounts from each group.
Exercise
A table main.hr.staff has a salary DOUBLE column. You want members of payroll to see real salaries and everyone else to see NULL. Write the function and the statement that attaches it. Why not return the string '***'?
Show answer
CREATE FUNCTION main.hr.masksalary(salary DOUBLE) RETURNS DOUBLE RETURN CASE WHEN isaccountgroupmember('payroll') THEN salary ELSE NULL END; then ALTER TABLE main.hr.staff ALTER COLUMN salary SET MASK main.hr.mask_salary; A mask must return the same type as the column, and a string cannot be returned for a DOUBLE column, so the query would fail.
Exercise
An analyst with SELECT on main.sales.orders still sees all regions after you set the row filter us_only. The analyst is a member of the admins group. Is the filter broken? What would you change if admins should also see only US rows?
Show answer
The filter is working as written, because the function returns true for members of admins. To limit admins too, remove is_account_group_member('admins') OR from the function so it returns region = 'US' only, and keep a separate break-glass group if someone must see everything.
Exercise
Why is a row filter safer than a dynamic view when many teams query the same base table?
Show answer
The filter is attached to the table, so every query on it is restricted no matter how it is reached. With a dynamic view, users must be given the view and kept away from the base table, and one wrong grant on the base table bypasses the rule.