Sub topic 14
Inspecting objects and grants
This chapter assumes you can run SQL in a Databricks notebook or the SQL editor, and that you have already created the dev catalog, the dev.sales schema, and the dev.sales.orders table from earlier chapters. You will learn how to list objects at each level, read a table's details, and check who has access to what.
Listing objects with SHOW
The SHOW commands follow the hierarchy: catalogs, then schemas, then tables. Each one lists only the objects you are allowed to see.
SHOW CATALOGS;
SHOW SCHEMAS IN dev;
SHOW TABLES IN dev.sales;Use these commands to find your way around. If a catalog you expect is missing, you probably lack a privilege on it. The catalog may still exist.
Reading table details with DESCRIBE
DESCRIBE EXTENDED returns the columns of a table, followed by detailed metadata.
DESCRIBE EXTENDED dev.sales.orders;Look for these fields in the detail section:
- Type:
MANAGEDorEXTERNAL. This tells you who controls the data files. - Location: the storage path of the data files.
- Owner: the principal that owns the table.
- Provider: the table format, such as Delta.
For dev.sales.orders, which you created without a LOCATION clause, the type is MANAGED. For dev.sales.raw_orders, which has an explicit path, the type is EXTERNAL.
Querying information_schema
Every catalog has an information_schema schema. It holds views that describe the objects in that catalog, so you can filter and join the results with ordinary SQL.
SELECT table_schema, table_name, table_type
FROM dev.information_schema.tables
WHERE table_schema = 'sales';SHOW is quick for browsing. information_schema is better when you need filters, sorting, or a result you can feed into another query. Each catalog's information_schema covers only that catalog. Query dev.information_schema to see dev, and a different catalog's copy to see that catalog.
Inspecting grants
SHOW GRANTS lists the privileges set on one object.
SHOW GRANTS ON SCHEMA dev.sales;
SHOW GRANTS ON TABLE dev.sales.orders;Each row shows a principal, an object type, an object, and the privilege. You can also list the grants held by one principal:
SHOW GRANTS `data-analysts` ON SCHEMA dev.sales;Grants are shown where they were set. If you grant SELECT on the schema, SHOW GRANTS ON TABLE dev.sales.orders does not list that privilege, even though the group can read the table through inheritance. To find out why a user can or cannot read a table, check the table, its schema, and its catalog in turn.
A worked check
Suppose an analyst in data-analysts reports that SELECT * FROM dev.sales.orders fails. Work down the hierarchy:
SHOW GRANTS ON CATALOG dev;
SHOW GRANTS ON SCHEMA dev.sales;
SHOW GRANTS ON TABLE dev.sales.orders;You need USE CATALOG on the catalog, USE SCHEMA on the schema, and SELECT on the table or one of its parents. If any of the three is missing, the query fails. Add the missing privilege with GRANT, then run SHOW GRANTS again to confirm.
Common errors
- Reading only the table's grants. Inherited privileges come from the schema and catalog, so check all three levels.
- Using the wrong catalog's
information_schema.dev.information_schemadoes not describe objects in other catalogs. - Assuming a missing row means the object does not exist. It may only mean you cannot see it.
- Forgetting the current catalog. Unqualified names resolve against the current catalog and schema, so confirm them before you read results.
Exercise
A user in data-analysts gets a permission error on dev.sales.orders. SHOW GRANTS ON TABLE dev.sales.orders returns no rows for the group. Does that prove the group has no access path to the table? What do you check next?
Show answer
No. The group may hold SELECT through inheritance from the schema or catalog, and those grants appear only on the object where they were set. Run SHOW GRANTS ON SCHEMA dev.sales and SHOW GRANTS ON CATALOG dev. Confirm the group has USE CATALOG on dev, USE SCHEMA on dev.sales, and SELECT on the table or on a parent. The error means one of these is missing.
Exercise
Write a query that lists the names of all external tables in the sales schema of the dev catalog.
Show answer
SELECT tablename FROM dev.informationschema.tables WHERE tableschema = 'sales' AND tabletype = 'EXTERNAL';