---
title: Column mask clause
description: Apply a function to mask a column value when rows are read with the column mask clause in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-column-mask
---

# Column `mask` clause

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 12.2 LTS and above ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Unity Catalog only

Specifies a function that is applied to a column whenever rows are fetched from the table. All subsequent queries from that column receive the result of evaluating that function over the column in place of the column's original value. This can be useful for fine-grained access control purposes where the function can inspect the identity or group memberships of the invoking user to determine whether to redact the value.

You can add column masks when you:

* Create a table using [CREATE TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-table-using.md).
* Add columns to a table with [ALTER TABLE … ADD COLUMN](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table.md).
* Altering a column with [ALTER TABLE … ALTER COLUMN](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table.md).

> [!IMPORTANT]
>
> The mask is applied as soon as each row is fetched from the data source.
> Any expressions, predicates, or ordering are applied after the masking.
> For example, joining between the masked column against a column from
> another table uses the masked values for the join comparison.

For more information on how to use column masks see [Databricks tables](https://docs.databricks.com/aws/en/tables/index.md).

## Required privileges

To assign a function that adds a column mask to a table, you must have the `EXECUTE` privilege on the function, `USE SCHEMA` on the schema, and `USE CATALOG` on the parent catalog.

If you are adding a column mask when you create a *new* table, you must have the `CREATE TABLE` privilege on the schema.

If you are adding or removing a column mask on an *existing* table, you must be the table owner, or have both the `MANAGE` and `SELECT` privileges on the table.

If the same statement also changes the table schema (for example, adding a new column with a mask), you additionally need the `MODIFY` privilege on the table.

## Syntax

```
MASK func_name [ USING COLUMNS ( other_column_name | constant_literal [, ...] ) ]
```

## Parameters

* **[func\_name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#function-name)**

  A scalar [SQL UDF](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-sql-function.md) with at least one parameter.

  The first parameter of the SQL UDF maps 1:1 with the masked column.
  The masked column's type must be castable to the SQL UDF parameter type.
  If `func_name` requires more parameters, arguments must be provided by the `USING COLUMNS` clause.

  The return type of the function must be castable to the data type of the masked column.

  > [!IMPORTANT]
  >
  > If the masked column's data type doesn't exactly match the function parameter type, the column value is implicitly cast. With [ANSI\_MODE](https://docs.databricks.com/aws/en/sql/language-manual/parameters/ansi_mode.md) disabled, values that can't be cast are silently converted to `NULL`, which can produce unexpected masking results. UDF parameter types must match the data types of the columns passed to them. See [Data type mismatch behavior](https://docs.databricks.com/aws/en/data-governance/unity-catalog/filters-and-masks/index.md#type-mismatch).

* **[other\_column\_name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#column-name)**

  Optionally specifies additional columns of the masked column's table to pass to `func_name`.
  Each `other_column_name` must be castable to the corresponding parameter of `func_name`.

  Use a column mask to selectively anonymize the value of `column_identifier` based on the user executing a query against `table_name`, the value of `column_identifier` and the optional `other_column`.

* **constant\_literal**

  Specifies a constant parameter with the type matching a function parameter. The following types are supported: `STRING`, numeric (`INTEGER`, `FLOAT,` `DOUBLE`, `DECIMAL` …), `BOOLEAN`, `INTERVAL`, `NULL`.

## Examples

You can find more examples in [Databricks tables](https://docs.databricks.com/aws/en/tables/index.md). For examples demonstrating the `USING COLUMNS` clause, see [Column mask with additional columns (`USING COLUMNS`)](https://docs.databricks.com/aws/en/data-governance/unity-catalog/filters-and-masks/manually-apply.md#using-columns-example).

```sql
-- Create a table with a masked column
> CREATE FUNCTION mask_ssn(ssn STRING) RETURN CASE WHEN is_member('HumanResourceDept') THEN ssn ELSE '***-**-****' END;
> CREATE TABLE persons(name STRING, ssn STRING MASK mask_ssn);
> INSERT INTO persons VALUES('James', '123-45-6789');

-- As a non-member of 'HumanResourceDept'
> SELECT * FROM persons;
  James  ***-**-****

-- As a member of 'HumanResourceDept'
> SELECT * FROM persons;
  James  123-45-6789

-- Create a masking function with multiple parameters. When applied first parameter will be the column that masking function applies to
> CREATE FUNCTION mask_pii_regional(value STRING, region STRING)
  RETURN IF(is_account_group_member(region || '_HumanResourceDept'), value, 'REDACTED');

-- Create a table with a masked column. Masking function first parameter will be the column that is masked.
-- The rest of the parameters should be specified in `USING COLUMNS (<columnList>)` clause
> CREATE TABLE persons(name STRING, address STRING MASK mask_pii_regional USING COLUMNS (region), region STRING);
> INSERT INTO persons('James', '160 Spear St, San Francisco', 'US')

-- As a non-member of 'US_HumanResourceDept'
> SELECT * FROM persons;
  James | REDACTED | US

-- As a member of 'US_HumanResourceDept'
> SELECT * FROM persons;
  James | 160 Spear St, San Francisco | US
```

## Limitations

* Column masks cannot be applied to columns that are referenced by generated columns. See [Generated columns and column masks](https://docs.databricks.com/aws/en/tables/features/generated-columns.md#gen-col-masks).

## Related articles

* [ROW FILTER clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-row-filter.md)
* [ALTER TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table.md)
* [CREATE TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-table-using.md)
