Sub topic 10
Restricting data with dynamic views
This chapter assumes you can write SQL SELECT and CREATE statements. It also assumes you know how GRANT works on Unity Catalog objects and what account groups are. Here you will learn how to build a view that returns different data to different users, and where that approach breaks down.
Why a view can restrict data
A privilege such as SELECT applies to a whole table. It cannot hide one column or some of the rows. A view helps because a user can be granted SELECT on the view without any privilege on the underlying table. The view runs its query with the owner's access to the table, so the user sees only what the view returns.
A dynamic view goes one step further. Its query checks who is running it and changes the result to match. Unity Catalog provides built-in functions for this check.
| Function | Returns true when |
|---|---|
| `is_account_group_member('name')` | The current user belongs to the account-level group `name` |
| `current_user()` | Not a test. It returns the user's name, so you can compare it to a column |
Prefer is_account_group_member over is_member. The first checks account-level groups, which Unity Catalog manages. The second checks workspace-level groups, which can differ from them.
Hide a column
The view below shows the ssn column only to members of the hr group. Everyone else sees the text REDACTED.
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, using the identity of the person running the query. Two users who run the same SELECT on the view can get different results.
Hide rows
The same idea works in a WHERE clause. This view lets managers see every order and lets other users see only US orders.
CREATE VIEW main.sales.orders_scoped AS
SELECT *
FROM main.sales.orders
WHERE is_account_group_member('managers') OR region = 'US';Grant access to the view
Restricting the data is only half of the job. You must also grant 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`;The analysts get no SELECT on main.sales.employees. If they had it, they could skip the view and read ssn directly.
Common errors
- Granting the table as well as the view. This defeats the restriction. Check with
SHOW GRANTS ON TABLE main.sales.employees;. - Using a group name that does not exist.
is_account_group_memberreturns false for an unknown name, so every user is treated as a non-member. The view works but hides data from people who should see it. - Forgetting parent privileges. Users also need
USE CATALOGandUSE SCHEMA. Without them,SELECTon the view fails. - Changing the view owner carelessly. The view reads the table with its owner's access. If the owner loses access to the table, the view stops working for everyone.
Exercise
A group data-analysts has SELECT on the dynamic view main.sales.employees_redacted, USE CATALOG on main, and USE SCHEMA on main.sales. A member of hr is also in data-analysts. What does that person see in the ssn column, and why? What would change if you also granted data-analysts SELECT on the employees table?
Show answer
The person sees the real ssn values, because is_account_group_member('hr') is true for them. Other analysts see REDACTED. If data-analysts also had SELECT on main.sales.employees, every analyst could query the table directly and read the real ssn, so the redaction would no longer protect anything.