Glossary · Governance and security
What is data masking?
Data masking hides sensitive values while keeping data usable: readers see a null, hash, partial or default value instead of the original. BigQuery dynamic data masking applies a masking rule at query time based on who runs the query, using policy tags or a data policy set directly on a column.
How dynamic data masking works in BigQuery
Masking builds on column-level access control. The classic setup:
- Create a taxonomy with policy tags, for example
pii.emailandpii.phone. - Create data policies on a tag. Each maps a masking rule to principals, who get the BigQuery Masked Reader role (
roles/bigquerydatapolicy.maskedReader). - Attach the policy tags to table columns.
- Grant Data Catalog Fine-Grained Reader (
roles/datacatalog.categoryFineGrainedReader) to people who need raw values.
Fine-Grained Readers see the original, Masked Readers see the masked value, and everyone else gets permission denied on that column. Alternatively, a data policy can be set directly on a column without managing taxonomies, or through data governance tags (in Preview).
Masking rules
- Nullify: returns
NULL. - Default masking value: a fixed placeholder that depends on the column's data type.
- Hash (SHA-256): deterministic, so joins on the masked column still work.
- Email mask: replaces the user name and keeps the domain.
- First four / last four characters: keep part of the value and replace the rest.
- Date year mask: truncates a date to the first day of its year.
- Random hash and custom masking routines: a SQL UDF with
data_governance_type = 'DATA_MASKING'.
A custom routine that hashes with a secret salt, harder to reverse than a plain hash:
CREATE OR REPLACE FUNCTION `my-project.governance.mask_email`(email STRING)
RETURNS STRING
OPTIONS (data_governance_type = 'DATA_MASKING')
AS (
CAST(SHA256(CONCAT(email, 'replace-with-a-secret-salt')) AS STRING FORMAT 'HEX')
);
Masking routines may use only a documented list of functions and can't reference tables, other UDFs or subqueries; once created as a masking routine, a function can't be turned back into a regular one.
Choosing a rule
Nullify and default values leak nothing but break joins and counts of distinct values. Hashes keep joins working but plain SHA-256 is reversible by guessing likely inputs, such as known emails, which is why Google rates salted hashes and AEAD encryption in custom routines higher. Partial masks (last four characters, email domain, year) leak some information by design; use them where that context is the point, such as support agents matching the last digits of a phone number.
Common pitfalls
- Edition. Dynamic and custom data masking are available with Enterprise and Enterprise Plus reservations and on-demand pricing, not with Standard edition reservations.
- Granting roles too high. Masked Reader granted at project level applies to every data policy under it; Google recommends granting it on the data policy.
- Row-level security interplay. Masking applies on top of row filters, but queries on tables with subquery row access policies need Fine-Grained Reader on the columns the policy references.
- Masked is not deleted. Raw values still exist for PII retention and deletion purposes; masking controls who sees them, not how long you keep them.
FAQ
What is the difference between static and dynamic data masking?
Static masking writes masked values into a copy of the data, so the original is gone from that copy. Dynamic masking stores the original and masks it at query time depending on who asks, so authorized users still see real values.
What happens if a user has neither Masked Reader nor Fine-Grained Reader?
Their query fails with a permission error on the protected column. They can still query the table's other columns, for example with SELECT * EXCEPT on the masked column.
Which masking rule wins if a user is in several groups?
BigQuery applies the first rule in its hierarchy: custom masking routine, random hash, hash (SHA-256), email mask, last four characters, first four characters, date year mask, default masking value, and nullify last.
