Sub topic 14
Inspecting objects and grants
This chapter assumes you can create catalogs, schemas, and tables with SQL. It also assumes you know how GRANT and REVOKE work and that privileges flow down the hierarchy. The examples run in a notebook or the SQL editor, attached to compute that uses Unity Catalog.
Creating objects is half the work. The other half is answering questions about them. What exists? Who owns it? Is it managed or external? Who can read it, and why? Unity Catalog gives you two ways to answer these questions. The first is SHOW and DESCRIBE commands, which are quick and interactive. The second is information_schema, a set of queryable views you can filter, join, and save.
Two ways to look
SHOW and DESCRIBE return a result set shaped for people. You can't easily filter or join their output in plain SQL. They fit exploration: "what is in this schema?"
information_schema is a schema that exists inside every catalog. Its views are ordinary relations, so you can use WHERE, JOIN, and GROUP BY on them. They fit audits and reports: "which tables in this catalog are external?"
Walking down the hierarchy with SHOW
Start at the top and move down one level per command. Each command lists only what the current user is allowed to see.
SHOW CATALOGS;
SHOW SCHEMAS IN dev;
SHOW TABLES IN dev.sales;
DESCRIBE EXTENDED dev.sales.orders;SHOW CATALOGSlists the catalogs in the metastore that you can see.SHOW SCHEMAS IN devlists the schemas in thedevcatalog.SHOW TABLES IN dev.saleslists tables and views in the schema.DESCRIBE EXTENDEDshows the columns, followed by a block of table metadata.
The metadata block of DESCRIBE EXTENDED is where you answer most questions about a table. Look for these rows:
| Row | What it tells you |
|---|---|
| Catalog, Database, Table | The three-level name of the object. |
| Type | `MANAGED` or `EXTERNAL`. |
| Location | The cloud storage path that holds the files. |
| Owner | The principal that owns the table. |
| Provider | The format, usually `delta`. |
The Type row is the fastest way to confirm whether dropping the table will also delete its data. Dropping a managed table removes the data files. Dropping an external table removes only the metadata and leaves the files at their path.
Catalogs and schemas have their own DESCRIBE forms, which show the owner, comment, and storage root where one is set:
DESCRIBE CATALOG EXTENDED dev;
DESCRIBE SCHEMA EXTENDED dev.sales;Checking where you are
If a short table name fails or resolves to the wrong table, check your current context first. The three-level name is the thing to rely on, and USE CATALOG and USE SCHEMA only change how short names resolve.
SELECT current_catalog(), current_schema();Querying information_schema
Every catalog contains an information_schema schema. Its views describe the objects in that catalog. The ones you will use most are:
| View | Contents |
|---|---|
| `schemata` | One row per schema in the catalog. |
| `tables` | One row per table or view, with its type and owner. |
| `columns` | One row per column, with data type and position. |
| `catalog_privileges` | Grants on the catalog itself. |
| `schema_privileges` | Grants on schemas in the catalog. |
| `table_privileges` | Grants on tables and views in the catalog. |
Here is the query from the earlier chapters, which lists tables and their types:
SELECT table_schema, table_name, table_type
FROM dev.information_schema.tables
WHERE table_schema = 'sales';Because the result is a table, you can ask sharper questions. This one finds every external table in the catalog, together with its owner:
SELECT table_schema, table_name, table_owner
FROM dev.information_schema.tables
WHERE table_type = 'EXTERNAL'
ORDER BY table_schema, table_name;This one lists the columns of a single table in order:
SELECT column_name, data_type, ordinal_position
FROM dev.information_schema.columns
WHERE table_schema = 'sales'
AND table_name = 'orders'
ORDER BY ordinal_position;What you can and cannot see
information_schema is filtered by your own access. A row appears only if you own the object or hold a privilege on it. An empty result therefore has two possible meanings. The object may not exist, or you may not be allowed to see it. When a query that should return rows comes back empty, ask the owner to check your grants before you assume the object is missing.
Each catalog's information_schema covers only that catalog. If you need to look across catalogs, query each one in turn. The system catalog also has an information_schema intended for metastore-wide questions, but access to it is itself governed by privileges, so don't count on being able to read it.
Inspecting grants
Privileges are the other thing you will inspect often. SHOW GRANTS lists them for one securable object:
SHOW GRANTS ON TABLE dev.sales.orders;
SHOW GRANTS ON SCHEMA dev.sales;
SHOW GRANTS ON CATALOG dev;The output has one row per principal and privilege. The columns are Principal, ActionType (the privilege, such as SELECT), ObjectType, and ObjectKey (the name of the object). On current compute, the output for an object includes privileges inherited from its parents. In that case ObjectType and ObjectKey name the parent where the privilege was actually granted. A row with ObjectType of SCHEMA in the result for a table means the grant is inherited from that schema.
You can also turn the question around and ask what one principal can do. Add the principal before ON. Backticks are needed for names with hyphens:
SHOW GRANTS `data-analysts` ON SCHEMA dev.sales;For a report you can filter and sort, use the privilege views. The inherited_from column shows where an inherited privilege was granted. It is NONE for a privilege granted directly on the object.
SELECT grantee, privilege_type, table_schema, table_name, inherited_from
FROM dev.information_schema.table_privileges
WHERE table_schema = 'sales'
ORDER BY grantee, table_name;Combine this with the inheritance rules from the previous chapters. Say you granted SELECT on the schema dev.sales to data-analysts. Every table in the schema will then show a row for data-analysts with inherited_from pointing at the schema, including tables created later.
GRANT SELECT ON SCHEMA dev.sales TO `data-analysts`;
SHOW GRANTS ON SCHEMA dev.sales;Diagnosing "why can't this user read the table?"
When a user reports a permission error, work through these checks in order:
- Run
SHOW GRANTSon the table, its schema, and its catalog. Look for the user or any group they belong to. - Confirm all three levels are covered. Reading a table needs
USE CATALOGon the catalog,USE SCHEMAon the schema, andSELECTon the table or an inheritedSELECT. - Check group membership. A grant to
data-analystsonly helps users in that group. - Check ownership with
DESCRIBE EXTENDED. Owners have full control of their objects, and the owner can change the grants. - For an external table, confirm the user is allowed to use the external location. Table privileges alone don't open the storage path.
Common mistakes
- Reading an empty
information_schemaresult as "the object doesn't exist". It may be hidden because you lack privileges. - Querying
information_schema.tableswithout the catalog prefix. Without it, you query the current catalog, which may not be the one you meant. - Checking only the table's grants. A privilege inherited from the schema or catalog doesn't appear if you only look at direct grants, so check each level, or use the
inherited_fromcolumn. - Comparing
table_typevalues with the wrong case or the wrong spelling. Print the distinct values first withSELECT DISTINCT table_type FROM dev.information_schema.tables. - Forgetting backticks around a group name with a hyphen in
SHOW GRANTS.
Exercise
A table dev.sales.orders shows no rows for the group data-analysts when you run SHOW GRANTS ON TABLE, but members of that group can query it. Which information_schema view and column would you check to find out why, and what is the likely explanation?
Show answer
Query dev.informationschema.tableprivileges (filtered to tableschema = 'sales' and tablename = 'orders') and read the inherited_from column. The group most likely holds SELECT through a grant on the schema dev.sales or on the catalog dev, which the table inherits. Members also need USE CATALOG and USE SCHEMA on the parent objects.
Exercise
Write a query that lists every external table in the catalog dev, showing the schema, the table name, and the owner.
Show answer
SELECT tableschema, tablename, tableowner FROM dev.informationschema.tables WHERE tabletype = 'EXTERNAL' ORDER BY tableschema, table_name;