What you will be able to do
- Write a boolean UDF and attach it to a table as a row filter so different groups see different rows
- Write a masking UDF and attach it to a column so only privileged groups see the real value
- Recognise where filters and masks cannot be attached, and which privileges and compute they need
- Avoid the type-mismatch and drop-order mistakes that silently break a filter
- Decide when to move from per-table filters and masks to ABAC policies, and keep filter UDFs fast
Key concept
Row filter and column mask — Two kinds of SQL UDF that Unity Catalog binds to a table. A row filter returns a boolean for each row and hides rows where it returns FALSE. A column mask takes a column's value and returns either that value or a redacted version. Both run every time someone queries the table, so the result can depend on who is asking.
1.Restricting rows and values inside a single table
A SELECT grant works at the level of the whole table: a user either has it or doesn't. Real data rarely fits that. A sales rep should see their own region's orders and nobody else's, and an analyst may need the employees table without being able to read salaries or social security numbers. Row filters and column masks solve this inside the table itself, so everyone queries the same object and gets a different answer depending on who they are.
Both controls are SQL user-defined functions registered in Unity Catalog and bound to a table. A row filter is checked against each row when the query runs, and any row where it returns FALSE is left out of the results. Users don't get an error and don't see a placeholder. The row just isn't there. A column mask receives a column's value and returns either the original or a substitute such as 'REDACTED'. The row still appears, but the sensitive value is replaced.
| Aspect | Row filter | Column mask |
|---|---|---|
| What the UDF returns | BOOLEAN; rows returning FALSE are excluded | The original value or a masked version, castable to the column's type |
| How many per object | One per table | One per column |
| Attach with | ALTER TABLE ... SET ROW FILTER fn ON (cols) | ALTER TABLE ... ALTER COLUMN col SET MASK fn |
| Remove with | ALTER TABLE ... DROP ROW FILTER | Column mask removal in Catalog Explorer or ALTER TABLE |
| Typical use | Restrict users to a region, department or account | Redact SSNs, emails, addresses |
These per-table controls are one of three related tools. Dynamic views wrap one or more base tables in a separate view that filters or masks the data, usually checked with is_account_group_member(). You grant users access to the view instead of the table. That suits sharing a curated or joined slice with people who shouldn't touch the base tables. ABAC policies apply rules across many tables based on tags. A row filter or mask, by contrast, lives on the table itself and is managed with ALTER TABLE by the table owner or a user with MANAGE.
Checkpoint 1 of 9· Check yourself
A team wants analysts outside the US group to stop seeing non-US orders in the sales table. They write a UDF that returns the region for US group members and 'REDACTED' for everyone else, then attach it with ALTER TABLE sales ALTER COLUMN region SET MASK. What does a non-US analyst get from SELECT * FROM sales?
A column mask only swaps a column's value for the original or a substitute, so every row still appears. Leaving rows out takes a BOOLEAN UDF attached with SET ROW FILTER, which drops rows where it returns FALSE.
“The mask is a SQL UDF that takes the column value as input and returns the original value or a masked version.”Source: docs.databricks.com
Sources1
2.Row filters: a boolean UDF decides which rows a user sees
A row filter takes zero or more parameters, and each one is bound to a column of the table (or to a constant). The function body is any expression that returns a boolean. Group-based access usually comes from IS_ACCOUNT_GROUP_MEMBER('group'), which checks the person running the query. The example below lets members of admin see every row, while everyone else only sees rows where region = 'US'.
CREATE FUNCTION us_filter(region STRING)
RETURN IF(IS_ACCOUNT_GROUP_MEMBER('admin'), true, region='US');CREATE TABLE sales (region STRING, id INT);
ALTER TABLE sales SET ROW FILTER us_filter ON (region);The ON (...) list maps table columns to the function's parameters in order. Use ON () for a function with no parameters, such as one that checks SESSION_USER() against a list of allowed users. You can also attach the filter when you create the table with CREATE TABLE sales (...) WITH ROW FILTER us_filter ON (region). ALTER TABLE sales DROP ROW FILTER removes it, and from then on every query returns every row. To change the logic without detaching anything, run CREATE OR REPLACE FUNCTION on the same function.
A table can have only one row filter. If reps should be limited by region and contractors by account, both rules go into one function. For rules more complex than a hard-coded list of groups, such as org hierarchies or entitlements copied from a source system, the filter can check a mapping table with EXISTS (SELECT 1 FROM ... WHERE ...) against the current user or their groups.
Checkpoint 2 of 9· Fill the gap
This filter is meant to show members of the admin group every row in sales, while everyone else sees only the rows where region is 'US'. Which value completes it?
CREATE FUNCTION us_filter(region STRING)
RETURN IF(IS_ACCOUNT_GROUP_MEMBER('admin'), ? , region='US');A row filter keeps every row where the function returns TRUE and drops the rows where it returns FALSE. Returning true for admin group members keeps all rows for them. Everyone else falls through to region='US', so they see only US rows.
Source: docs.databricks.comCheckpoint 3 of 9· Exam question
A data engineer must ensure that sales representatives in the orders table only see rows for the region stored in their Unity Catalog group membership, while managers see all regions. What is the correct way to implement this with a per-table row filter?
Correct answer: A — Create a BOOLEAN UDF checking is_account_group_member() against the row's region, then apply it with ALTER TABLE orders SET ROW FILTER region_filter ON (region)
- A. This is the documented pattern for Unity Catalog row filters: the UDF must return
BOOLEAN, evaluatingis_account_group_member()for the caller against the row's region, andSET ROW FILTERattaches it to the table so rows failing the check are excluded. - B. A dynamic view can approximate row-level filtering, but it requires managing a separate view object and re-granting privileges, which is not the native row-filter mechanism the exam scope calls for and adds ongoing maintenance overhead.
- C. Returning the region as a
STRINGand applyingSET MASKinvokes the column-masking mechanism, which transforms a column's displayed value; it does not filter out entire rows, so unauthorized rows would still be returned with their region value unmasked. - D. Unity Catalog
CHECKconstraints validate data on write and cannot reference the querying user's identity or group membership, so they cannot implement per-user row visibility at query time.
Sources2
3.Column masks: a value UDF decides what a column shows
A column mask is a function whose first parameter is always the column being masked and whose return value replaces that column's value in query results. The return type must match the column's type or be castable to it, and each column can have one mask. Inside the function you can use any built-in function, including session_user() and is_account_group_member(). So the usual pattern is: return the real value for a privileged group, and a redacted value for everyone else.
Masks can also depend on other columns. USING COLUMNS passes extra arguments after the masked column, either other columns from the same table or constant values. The function below shows an address only to members of a group named after that row's country, such as US_address_viewers.
CREATE FUNCTION mask_address_by_country(address STRING, country STRING, group_suffix STRING DEFAULT '_address_viewers')
RETURN IF(
is_account_group_member(country || group_suffix),
address,
'REDACTED'
);ALTER TABLE customers
ALTER COLUMN address
SET MASK mask_address_by_country USING COLUMNS (country, '_address_viewers');A member of US_address_viewers who runs SELECT * FROM customers sees all three customers. Alice's US address appears in full, and Bob's and Charlie's show REDACTED. The mask hid values without dropping a single row, and that is the difference from a filter. The same pattern applies to an ssn column: return ssn for hr_admin members and a redacted string for everyone else. Users keep querying the original table rather than a separate hashed copy.
Two details come up in practice. For a STRUCT column, the mask should rebuild the struct with named_struct() and return the same struct type, replacing only the sensitive field. Otherwise later INSERT, MERGE and UPDATE statements can hit schema mismatches. Python logic needs a SQL wrapper: you create the Python UDF, call it from a SQL UDF, and attach the SQL UDF. Attaching the Python UDF directly fails with [ROUTINE_NOT_FOUND].
Checkpoint 4 of 9· Check yourself
Suppose someone who belongs to UK_address_viewers and to no other viewer group runs SELECT * FROM customers. The customers table has mask_address_by_country attached with USING COLUMNS (country, '_address_viewers'). What do they get back?
The mask joins each row's country to the suffix and checks membership in that group. For this user, only the UK row passes the check, so only Bob's address shows in full. A mask replaces values but never removes rows, so all three rows still appear.
“Only members of the resulting country-group pair can view addresses for that country.”Source: docs.databricks.com
Checkpoint 5 of 9· Exam question
An engineer needs to mask the ssn column in the employees table so that only members of the hr_admin group see the full value, while everyone else sees the last four digits redacted with asterisks. Which approach correctly implements this?
Correct answer: A — Define a UDF returning ssn unmasked for hr_admin members, otherwise a redacted string, applied with ALTER COLUMN ssn SET MASK ssn_mask
- A. This correctly uses a column mask: the UDF returns a value of the same type as
ssn, conditionally revealing or redacting it based on group membership, andSET MASKattaches that transformation to the column for every query against the table. - B.
SET ROW FILTERexpects aBOOLEAN-returning UDF and removes whole rows rather than transforming a column's value, so it cannot selectively redact digits within thessncolumn while keeping the row visible. - C. Unity Catalog privileges are granted at the table level, not per column, so
GRANT/DENYcannot expose a column to one group while hiding it from another on the same table. - D. A separate hashed materialized view produces an irreversible value for everyone, so
hr_adminusers could no longer retrieve the real SSN, and it requires users to remember to query a different object entirely.
4.Who can attach them, and where they cannot go
Attaching a filter or mask takes privileges on both the function and the table. You need EXECUTE on the function, plus USE SCHEMA and USE CATALOG on its parents. To add or remove one on an existing table, you must own the table or hold both MANAGE and SELECT on it. Setting one in CREATE TABLE requires CREATE TABLE on the schema. A statement that also changes the schema, such as adding a masked column, additionally needs MODIFY.
Filters and masks belong on tables. Running SET ROW FILTER against a view fails, so the fix is to attach the filter to the underlying base table. Delta Sharing has a similar limit. A recipient can apply filters and masks to shared tables and foreign tables, but not to shared streaming tables or materialized views. A provider cannot share a table that has table-level filters or masks at all.
Reading a protected table also depends on the compute. A SQL warehouse works, as does standard access mode on Databricks Runtime 12.2 LTS or above, or dedicated access mode on 15.4 LTS or above. On dedicated compute, the filtering runs on serverless compute, so the workspace needs serverless enabled. Runtimes below 12.2 LTS fail securely: queries return no data rather than unfiltered data.
Filters and masks survive REPLACE TABLE. The row filter is always kept, and each mask is kept if the new table still has a column with that name. That protects against accidentally losing an access rule. The downside: if a kept filter references a column that was removed or changed, later queries can fail until you update or drop the filter with ALTER TABLE.
Checkpoint 6 of 9· Check yourself
A data engineer who does not own the sales table holds MANAGE on it. They also have EXECUTE on us_filter, plus USE SCHEMA and USE CATALOG on its parents. Their ALTER TABLE sales SET ROW FILTER us_filter ON (region) is rejected. Which missing privilege explains this?
To add or remove a filter or mask on an existing table, you must own the table or hold both MANAGE and SELECT on it. MODIFY is needed only when the same statement also changes the schema. CREATE TABLE is needed only when you set the filter while creating a new table.
“you must be the table owner, or have both the MANAGE and SELECT privileges on the table.”Source: docs.databricks.com
Checkpoint 7 of 9· Exam question
A data engineer runs ALTER TABLE finance.transactions_view SET ROW FILTER region_filter ON (region) against a view built on top of the transactions table, expecting it to restrict rows by region for downstream analysts. What will happen?
Correct answer: A — The statement fails because row filters can only attach to tables, not views; the engineer must instead attach the filter directly to transactions
- A. Unity Catalog does not support attaching row filters or column masks to views; these controls are applied to base tables, so the engineer must run
ALTER TABLEagainsttransactionsitself to restrict rows for anyone querying it or views built on it. - B. Since the operation cannot succeed on a view at all, there is no propagation behavior to downstream objects, and views do not silently inherit a filter attempted on themselves.
- C. The restriction against filtering views is not scoped to a particular access path such as Delta Sharing; the
ALTER TABLE ... SET ROW FILTERstatement itself is rejected regardless of how the view would later be queried. - D. Attaching a row filter does not require an
EXECUTEgrant on the target object beforehand; the actual blocker is that the target ofSET ROW FILTERis a view rather than a table.
5.Two mistakes that break a filter without an error
A parameter type that doesn't match its column can open the table to everyone. Databricks implicitly casts each column to the UDF parameter's type. With ANSI mode off, values that can't be cast become NULL with no error. In the example below, the filter means to show only rows with a NULL department. But 'exec' and 'engineering' can't be cast to INT, so they arrive as NULL too, and the query returns every row.
-- Bug: parameter type is INT, but the column is STRING
CREATE FUNCTION salary_filter(dept INT) RETURNS BOOLEAN
RETURN dept IS NULL;
ALTER TABLE employees SET ROW FILTER salary_filter ON (department);There are two fixes, and they work together. Declare the parameter as STRING so it matches the column. Then set spark.sql.ansi.enabled = true, so any future failed cast raises an error you'll notice instead of quietly turning into NULL. Masks fail the same way, ending up hiding the wrong values.
Order matters when deleting a filter. Run ALTER TABLE ... DROP ROW FILTER first, then DROP FUNCTION. If you drop the function first, the table is left pointing at a function that no longer exists, and nobody can read it until someone runs ALTER TABLE <table_name> DROP ROW FILTER to clear the reference.
Checkpoint 8 of 9· Fill the gap
With ANSI mode off, the employees table's department column is a STRING that holds values such as 'exec', 'engineering' and NULL. Which parameter type makes this row filter return only the rows where department is actually NULL?
CREATE FUNCTION salary_filter(dept ? ) RETURNS BOOLEAN
RETURN dept IS NULL;The parameter type has to match the column type, which is STRING. If you declare INT, BIGINT or BOOLEAN, 'exec' and 'engineering' can't be cast. With ANSI mode off they silently become NULL, so the filter returns every row and raises no error.
Source: docs.databricks.com6.Scaling up: when to move to ABAC, and how to keep UDFs fast
Per-table filters stop scaling once the same rule has to cover dozens of tables. Each table needs its own ALTER TABLE, each new table is unprotected until someone remembers, and the table owner can remove the rule. Unity Catalog ABAC policies (CREATE POLICY) attach a filter or mask at catalog or schema level and apply it automatically to every table whose governed tags match. Move to ABAC when you need the same rule across many tables, separation between policy authors and table owners, or automatic coverage of newly tagged tables. Keep per-table filters for logic that really belongs to one table.
Whichever form you use, the engine puts security ahead of speed. When it has to choose between an optimization and the risk of leaking filtered or masked values, it always picks the secure option. Poorly written UDFs therefore cost query time. These habits keep that cost low:
- Keep functions simple, and prefer a
CASEexpression to a mapping-table lookup or subquery when either would work. - Mask only truly sensitive columns, and reuse one masking function where you can, because each distinct mask is evaluated during queries.
- Pass fewer UDF arguments. Columns passed as arguments can't be optimized away, even when the query never selects them.
- Avoid stacking many
ANDconditions in a filter. Only one filter resolves per user and table, so combined filters tend to grow. - Use error-free expressions such as
try_divide. Anything that can throw an error blocks pushdown, since the error itself could reveal a value. - Prefer SQL to Python UDFs. If you must use Python, mark the function
DETERMINISTICwhere it applies.
Checkpoint 9 of 9· Match them up
Match each habit for keeping row filter and column mask UDFs fast to the reason it helps query performance.
Tap a term, then the definition that fits it.
The engine always chooses security over speed when the two conflict. So each habit removes something that would otherwise stop it optimizing: errors that could leak values, argument columns it has to keep, extra masks it has to evaluate, and complex lookups.
“Databricks cannot optimize away column references that come from UDF arguments, even if those columns are not used in the query.”Source: docs.databricks.com
Sources1
Sources
(2)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Rows where the function returns FALSE are excluded from query results.”
↩︎ Restricting rows and values inside a single table“expose a curated, transformed, or joined version of your data to users who don't have access to the underlying tables”
↩︎ Restricting rows and values inside a single table“The return type must match or be castable to the column's data type. Each column can have one mask.”
↩︎ Column masks: a value UDF decides what a column shows“You cannot apply row-level security or column masks to a view.”
↩︎ Who can attach them, and where they cannot go“OpenSharing recipients can apply row filters and column masks only to shared tables and foreign tables, not to streaming tables or materialized views.”
↩︎ Who can attach them, and where they cannot go“With ANSI mode disabled (spark.sql.ansi.enabled = false), uncastable values are silently converted to NULL, no error is raised”
↩︎ Two mistakes that break a filter without an error“ABAC scales across catalogs and schemas and can be defined by higher-level admins, so table owners can't override or remove them.”
↩︎ Scaling up: when to move to ABAC, and how to keep UDFs fast“it always makes the secure choice, which can affect query performance.”
↩︎ Scaling up: when to move to ABAC, and how to keep UDFs fast“Databricks cannot optimize away column references that come from UDF arguments, even if those columns are not used in the query.”
↩︎ Scaling up: when to move to ABAC, and how to keep UDFs fast“Use deterministic expressions that never throw errors, such as try_divide.”
↩︎ Scaling up: when to move to ABAC, and how to keep UDFs fast“Row filters and column masks are Unity Catalog access controls that restrict the rows and column values a user can see at query time.”
↩︎ Key concept“Rows where the function returns FALSE are excluded from query results.”
↩︎ Exam trap 1“You cannot apply row-level security or column masks to a view.”
↩︎ Exam trap 2“OpenSharing recipients can apply row filters and column masks only to shared tables and foreign tables, not to streaming tables or materialized views.”
↩︎ Exam trap 3“The mask is a SQL UDF that takes the column value as input and returns the original value or a masked version.”
↩︎ Checkpoint - 2.https://docs.databricks.com/aws/en/data-governance/unity-catalog/filters-and-masks/manually-applyOfficial docs
“Each table can have only one row filter.”
↩︎ Row filters: a boolean UDF decides which rows a user sees“All filters run with definer's rights except for functions that check user context”
↩︎ Row filters: a boolean UDF decides which rows a user sees“The first parameter of the masking function always maps to the masked column itself.”
↩︎ Column masks: a value UDF decides what a column shows“You must apply the SQL wrapper function (email_mask_sql) as the column mask, not the Python UDF directly.”
↩︎ Column masks: a value UDF decides what a column shows“you must be the table owner, or have both the MANAGE and SELECT privileges on the table.”
↩︎ Who can attach them, and where they cannot go“you must have the EXECUTE privilege on the function, USE SCHEMA on the schema, and USE CATALOG on the parent catalog.”
↩︎ Who can attach them, and where they cannot go“You cannot read row filters or column masks using dedicated compute on Databricks Runtime 15.3 or below.”
↩︎ Who can attach them, and where they cannot go“If you run REPLACE TABLE, any existing row filter is retained regardless of schema changes.”
↩︎ Who can attach them, and where they cannot go“You must perform the ALTER TABLE ... DROP ROW FILTER command before you drop the function.”
↩︎ Two mistakes that break a filter without an error“You must perform the ALTER TABLE ... DROP ROW FILTER command before you drop the function.”
↩︎ Exam trap 4“Only members of the resulting country-group pair can view addresses for that country.”
↩︎ Checkpoint