Sub topic 10
Restricting data with dynamic views
This chapter assumes you can write SELECT and CREATE VIEW statements. It also assumes you know how to grant privileges on catalogs, schemas and tables, and how groups work in Unity Catalog. Dynamic views are the oldest way to restrict data inside a table, and you will meet them in many existing workspaces. Row filters and column masks, covered in the next chapter, are usually a better choice for new work.
What a dynamic view is
A dynamic view is an ordinary Unity Catalog view whose query asks who is running it. The query calls an identity function and returns different values or rows depending on the answer. Two users can run SELECT * FROM the same view and see different results.
A view is also a securable object. A user who has SELECT on a view can read it without having any privilege on the tables it reads from. The view owner is the one who needs access to the base tables. This lets you keep the full table private and expose a restricted version through the view.
Identity functions
Dynamic views rely on functions that report the caller's identity.
| Function | Returns | Use it for |
|---|---|---|
| `is_account_group_member('group')` | `true` if the caller belongs to the named account-level group | Group-based checks. This is the one to use. |
| `current_user()` | The caller's user name, usually an email address | Per-user lookups, such as a mapping table |
| `is_member('group')` | `true` if the caller belongs to the named workspace-local group | Legacy code only |
Prefer is_account_group_member. Unity Catalog permissions are defined on account-level groups, and is_member checks workspace-local groups. It can return false for a user who is in the account group you meant. The view then looks correct but hides data from the wrong people, or worse, shows data to the wrong people if you invert the logic.
Column-level restriction
The most common pattern replaces a sensitive column with a placeholder for everyone outside an approved group.
CREATE VIEW main.sales.employees_redacted AS
SELECT
id,
name,
CASE WHEN is_account_group_member('hr') THEN ssn ELSE 'REDACTED' END AS ssn
FROM main.sales.employees;The CASE expression is evaluated for each row at query time, using the identity of the user running the query. Give the new column the same name as the original (AS ssn) so downstream queries do not need to change.
You can also mask partially instead of hiding the whole value.
CREATE OR REPLACE VIEW main.sales.employees_partial AS
SELECT
id,
name,
CASE
WHEN is_account_group_member('hr') THEN ssn
ELSE concat('***-**-', substr(ssn, -4))
END AS ssn
FROM main.sales.employees;Make sure the masked branch returns the same data type as the real branch. Both branches here are STRING. If the real column were an integer and the masked branch a string, the CASE would coerce or fail in surprising ways.
Row-level restriction
To restrict rows, put the identity check in the WHERE clause.
CREATE OR REPLACE VIEW main.sales.orders_by_region AS
SELECT *
FROM main.sales.orders
WHERE is_account_group_member('admins') OR region = 'US';Members of admins see every row. Everyone else sees only rows where region is US.
Using a mapping table
Hard-coding group names and regions in view definitions does not scale. If every region needs its own group, you end up editing views whenever a team changes. A mapping table moves that data out of the SQL.
CREATE TABLE IF NOT EXISTS main.sales.region_access (
user_email STRING,
region STRING
);
CREATE OR REPLACE VIEW main.sales.orders_scoped AS
SELECT o.*
FROM main.sales.orders AS o
WHERE is_account_group_member('admins')
OR EXISTS (
SELECT 1
FROM main.sales.region_access AS a
WHERE a.user_email = current_user()
AND a.region = o.region
);Access changes now become INSERT and DELETE statements on region_access. Protect that table. Anyone who can write to it can grant themselves rows, so keep it out of reach of the people the view restricts.
Granting access correctly
A dynamic view only protects data if users cannot reach the base table another way. The grants must give access to the view and withhold it from the table.
GRANT USE CATALOG ON CATALOG main TO `data-analysts`;
GRANT USE SCHEMA ON SCHEMA main.sales TO `data-analysts`;
GRANT SELECT ON VIEW main.sales.employees_redacted TO `data-analysts`;Notice there is no SELECT on main.sales.employees. Two rules decide whether this works.
- The view owner needs
SELECTon the base table, plusUSE CATALOGandUSE SCHEMAon its parents. The view runs with the owner's access to the sources. If the owner loses that access, the view fails for every reader. - The reader needs
USE CATALOGandUSE SCHEMAon the view's own catalog and schema, andSELECTon the view.
Check the result with SHOW GRANTS.
SHOW GRANTS ON VIEW main.sales.employees_redacted;
SHOW GRANTS ON TABLE main.sales.employees;The second command should not list the restricted group. Remember that privileges inherit downward. A SELECT granted on the schema main.sales covers the base table as well as the view, and it defeats the whole design. When you use dynamic views, put the base tables and the views in different schemas, and grant at the schema level only on the schema that holds the views.
CREATE SCHEMA IF NOT EXISTS main.sales_restricted;
-- Create the dynamic views in main.sales_restricted, then:
GRANT USE SCHEMA, SELECT ON SCHEMA main.sales_restricted TO `data-analysts`;Here main.sales holds the private base tables, and main.sales_restricted holds the views that analysts can read. Granting SELECT on the second schema is safe because it contains nothing sensitive.
Common errors and gotchas
- Using
is_memberinstead ofis_account_group_member. The group checks happen at different scopes, and results can differ. Use the account-level function. - Mistyped group names. If you misspell the group in
is_account_group_member('hr'), the function returnsfalseand no error appears. Everyone sees the redacted value, including people who should not. Test the view as a member and as a non-member of the group. - Leaving a side door open. A direct
SELECTon the base table, or a schema-level grant that covers it, lets users skip the view. RunSHOW GRANTSon the base table after every change. - Changing the view owner or the owner's access. If the owner's
SELECTon the base table is revoked, the view breaks. Transfer ownership of views to a group, as you do for tables, so that access does not depend on one person. - Depending on
SELECT *behavior. A view defined withSELECT *fixes its column list when you create it. Columns you add to the table later do not appear in the view, and a new sensitive column stays hidden until you change the view. This is safe in the restrictive direction, but it surprises people who expect new columns to appear. - Masking only one path. If the same sensitive column reaches users through several views, each view needs the check. A change in one place does not carry over to the others.
- Compute support. Unity Catalog enforces dynamic views only on compute that supports them. Check the compute access mode requirements for your runtime version before you rely on a view for protection. A later chapter covers compute access modes.
Dynamic views compared with row filters and column masks
| Aspect | Dynamic view | Row filter or column mask |
|---|---|---|
| Where the rule lives | In a separate view | On the table itself |
| Access to the base table | Must be blocked separately | The rule applies to every query on the table |
| Number of objects to manage | One view per restricted variant | One function per rule |
| Fits when | You want a differently shaped result, such as fewer columns or joined data | You want the same table to behave differently by user |
Dynamic views remain useful when you want to expose a different shape of data, such as an aggregated or joined result, rather than just hide values. When the goal is only to hide values in a table, a row filter or column mask closes the side door by design.
Exercise
Analysts in the group data-analysts can still read the SSN values from main.sales.employees_redacted, even though the view masks them for non-HR users. SHOW GRANTS ON VIEW main.sales.employees_redacted looks correct. What is the most likely cause, and how do you check it?
Show answer
The analysts probably have SELECT on the base table main.sales.employees. The access may come from a direct grant, or inherited from a SELECT granted on the schema main.sales or the catalog. They can query the table directly and skip the view. Run SHOW GRANTS ON TABLE main.sales.employees and check the schema and catalog grants. Then move the base table to a private schema and keep the views in a separate schema that carries the grant.
Exercise
Write a dynamic view main.sales.orders_us_only over main.sales.orders that shows all rows to the group admins and only rows with region = 'US' to everyone else. Which function should you use for the group check?
Show answer
Use is_account_group_member:
CREATE OR REPLACE VIEW main.sales.ordersusonly AS
SELECT * FROM main.sales.orders
WHERE isaccountgroupmember('admins') OR region = 'US';
`isaccountgroupmember checks account-level groups, which are the groups Unity Catalog permissions use. is_member` checks workspace-local groups and can give a different answer.
Exercise
A view reader reports "permission denied" errors, though they have SELECT on the view and USE CATALOG and USE SCHEMA on its parents. Everyone else could read it yesterday. Name one likely cause related to the view owner.
Show answer
The view owner may have lost SELECT on the base table, or USE CATALOG or USE SCHEMA on its parents. The view runs with the owner's access to its sources, so a revoked privilege breaks the view for all readers. Restore the owner's privileges, or transfer ownership to a group that holds them.