Sub topic 13
Views, Volumes, and Functions
This chapter assumes you can create catalogs, schemas, and tables, and that you know how GRANT works on them. Tables are not the only objects that live in a schema. Views, volumes, and functions sit at the same level, use the same three-level naming, and are governed by the same privilege model.
Where these objects live
A schema holds several kinds of securable objects. Each one is addressed as catalog.schema.object, and each one can be granted to a principal.
Views
A view is a saved query. It stores no data of its own. Each time you read it, the query runs against the underlying tables.
CREATE VIEW dev.sales.customer_totals AS
SELECT customer_id, SUM(amount) AS total_amount
FROM dev.sales.orders
GROUP BY customer_id;Views are useful for hiding columns, filtering rows, and giving analysts a stable shape over changing tables. A view is also a way to share part of a table. A user needs SELECT on the view, plus USE CATALOG and USE SCHEMA on its parents. The view runs with its owner's access to the underlying tables. This means a user can read the view without having SELECT on dev.sales.orders itself, as long as the view's owner has it.
GRANT SELECT ON VIEW dev.sales.customer_totals TO `data-analysts`;Volumes
Tables hold structured rows. A volume governs files: CSVs, images, PDFs, model artifacts, or anything else in cloud storage that is not a table. A volume is a logical storage area inside a schema. Like tables, volumes are either managed or external.
-- Managed volume: Unity Catalog chooses the storage location
CREATE VOLUME IF NOT EXISTS dev.sales.landing;
-- External volume: you supply a path covered by an external location
CREATE EXTERNAL VOLUME IF NOT EXISTS dev.sales.partner_files
LOCATION 's3://example-bucket/partner-files';Files in a volume are reached through a path built from the three names:
LIST '/Volumes/dev/sales/landing/';Access uses two volume privileges, READ VOLUME and WRITE VOLUME.
GRANT READ VOLUME ON VOLUME dev.sales.landing TO `data-analysts`;The external volume follows the same rule as an external table. The path must be covered by an external location that you are allowed to use.
Functions
A user-defined function (UDF) is reusable logic saved in a schema. A SQL function is the simplest kind.
CREATE FUNCTION dev.sales.to_cents(amount DECIMAL(10,2))
RETURNS BIGINT
RETURN CAST(amount * 100 AS BIGINT);
SELECT order_id, dev.sales.to_cents(amount) AS amount_cents
FROM dev.sales.orders;To let someone call it, grant EXECUTE:
GRANT EXECUTE ON FUNCTION dev.sales.to_cents TO `data-analysts`;Privileges at a glance
| Object | Common privilege | What it allows |
|---|---|---|
| Table | `SELECT`, `MODIFY` | Read rows, change rows |
| View | `SELECT` | Read the query result |
| Volume | `READ VOLUME`, `WRITE VOLUME` | Read or write files |
| Function | `EXECUTE` | Call the function |
Inheritance works as it does for tables. A schema-level grant of SELECT, READ VOLUME, or EXECUTE applies to the matching objects in that schema, including ones created later.
Common errors
- Granting on the view but not the parents. The user still needs
USE CATALOGandUSE SCHEMA. - Assuming a view exposes only what the reader can see. It exposes what the owner can see, so check the view's definition before sharing it.
- Creating an external volume on a path that no external location covers. The statement fails.
- Calling a function without
EXECUTE, even if the user can read the schema.
Exercise
An analyst group has USE CATALOG on dev and USE SCHEMA on dev.sales. They have no privileges on the table dev.sales.orders. You grant SELECT on the view dev.sales.customer_totals, which is owned by a user who can read dev.sales.orders. Can the analysts query the view? Can they query the table?
Show answer
They can query the view, because it runs with its owner's access to the underlying table and they hold SELECT on the view plus USE CATALOG and USE SCHEMA. They cannot query dev.sales.orders directly, because they have no SELECT on it.