Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Databricks SQL
Databricks Runtime 16.4 and above
Unity Catalog only
Creates a named policy on a securable. Policies can be row filters or column masks applied to catalogs, schemas, or tables. The policy name is scoped to the securable the policy is defined on.
To run this statement, you must have the MANAGE privilege on the target securable or be its owner.
Syntax
CREATE [ OR REPLACE ] POLICY policy_name
ON { CATALOG catalog_name | SCHEMA schema_name | TABLE table_name }
[ COMMENT description ]
{ row_filter_body | column_mask_body }
row_filter_body
ROW FILTER function_name
TO principal [, ...]
[ EXCEPT principal [, ...] ]
FOR TABLES
[ WHEN condition ]
[ MATCH COLUMNS condition [ [ AS ] alias ] [, ...] ]
[ USING COLUMNS ( function_arg [, ...] ) ]
column_mask_body
COLUMN MASK function_name
TO principal [, ...]
[ EXCEPT principal [, ...] ]
FOR TABLES
[ WHEN condition ]
[ MATCH COLUMNS condition [ [ AS ] alias ] [, ...] ]
ON COLUMN alias
[ USING COLUMNS ( function_arg [, ...] ) ]
Parameters
-
Name of the policy. The name is scoped to the securable the policy is defined on. If a policy with the same name already exists and
OR REPLACEis not specified, Azure Databricks raises POLICY_ALREADY_EXISTS. -
The name of the catalog on which the policy is defined. If the securable type is not supported for policies, Azure Databricks raises POLICY_ON_SECURABLE_TYPE_NOT_SUPPORTED.
-
The name of the schema on which the policy is defined.
-
The name of the table on which the policy is defined.
description
An optional string comment for the policy.
-
The name of the UDF used for the row filter or column mask.
principal
A user, group, or service principal name. Multiple principals can be listed after
TO. Principals listed afterEXCEPTare excluded from the policy.-
For
WHEN: a boolean expression that matches securables (for example, table-level tag conditions). ForMATCH COLUMNS: a boolean expression that matches columns (for example,hasTag('tag_name')). Conditions are evaluated by the control plane on securable metadata. The only user-facing functions supported in conditions arehasTag()andhasTagValue(). If the condition is invalid, Azure Databricks raises UC_INVALID_POLICY_CONDITION. alias
In
MATCH COLUMNS, an optional identifier for the matched column. The alias can be referenced inUSING COLUMNS(row filter) or inON COLUMNandUSING COLUMNS(column mask).function_arg
In
USING COLUMNS, each argument is either a constant expression or an alias fromMATCH COLUMNS. The arguments are passed to the policy function in order. If the options do not match the policy type, Azure Databricks raises UC_POLICY_TYPE_OPTIONS_MISMATCH.
Examples
The following example creates a column mask policy:
> CREATE FUNCTION ssn_to_last_nr (ssn STRING, nr INT) RETURNS STRING
RETURN right(ssn, nr);
> CREATE POLICY ssn_mask
ON CATALOG employees
COLUMN MASK ssn_to_last_nr
TO 'All Users' EXCEPT 'HR admins'
FOR TABLES
MATCH COLUMNS hasTag('ssn') AS ssn
ON COLUMN ssn
USING COLUMNS (4);
The following example creates a row filter policy:
> CREATE FUNCTION non_eu_region (geo_region STRING) RETURNS BOOLEAN
RETURN geo_region <> 'eu';
> CREATE POLICY hide_eu_customers
ON SCHEMA prod.customers
COMMENT 'Hide European customers from sensitive tables'
ROW FILTER non_eu_region
TO analysts
FOR TABLES
WHEN hasTagValue('sensitivity', 'high')
MATCH COLUMNS hasTag('geo_region') AS region
USING COLUMNS (region);