Create and manage rule templates

Rule libraries in Knowledge Catalog (formerly Dataplex Universal Catalog) let you create and manage custom GoogleSQL rule templates so you can reuse data quality rules across multiple tables. Centralizing these definitions helps you update business logic in one place instead of duplicating SQL across individual scans.

For conceptual background, SQL parameter syntax, and aspect schemas, see About reusing data quality rules. After you create templates, you can apply them to catalog entries and data quality scans.

Limitations

The following limitations apply when creating rule libraries and custom rule templates:

  • You can't change a rule library's location after you create it.
  • Each custom rule template supports one SQL query in sqlCollection.
  • A data quality scan or catalog entry can reference a custom rule template only if the template is in the same region or in the global location.

Before you begin

This document is for data stewards and data engineers who author and maintain standardized validation logic. Follow these tasks in order:

  1. Browse system rule templates to check whether a Google-managed template already meets your requirements.
  2. If you need custom SQL logic, then create a rule library to store your organization's templates, create custom rule templates inside it, and verify the saved templates.
  3. Apply your templates to scans or catalog entries to start validating BigQuery tables.

Before you start, review the following concepts and complete the setup requirements:

Enable the Dataplex API, if it is not already enabled.

Roles required to enable APIs

To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

Enable the API

Required roles

To get the permissions that you need to create, view, list, update, and delete rule libraries and custom rule templates, ask your administrator to grant you the following IAM roles on the project or entry group that stores your rule library:

For more information about granting roles, see Manage access to projects, folders, and organizations.

These predefined roles contain the permissions required to create, view, list, update, and delete rule libraries and custom rule templates. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to create, view, list, update, and delete rule libraries and custom rule templates:

  • Create and manage rule libraries:
    • dataplex.entryGroups.create
    • dataplex.entryGroups.get
    • dataplex.entryGroups.list
    • dataplex.entryGroups.update
    • dataplex.entryGroups.delete
  • Create, update, and delete custom rule templates:
    • dataplex.entries.create
    • dataplex.entries.update
    • dataplex.entries.delete
    • dataplex.entryGroups.useDataQualityRuleTemplateEntry
    • dataplex.entryGroups.useDataQualityRuleTemplateAspect
  • View and list rule templates:
    • dataplex.entries.get
    • dataplex.entries.getData
    • dataplex.entries.list

You might also be able to get these permissions with custom roles or other predefined roles.

The Dataplex Catalog Editor (roles/dataplex.catalogEditor) role also includes all permissions needed to create, view, list, update, and delete custom rule templates. To browse Rule libraries > Custom in the Google Cloud console, you also need the dataplex.entryGroups.list permission on the project, which isn't included in roles/dataplex.entryOwner. You can grant dataplex.entryGroups.list through Dataplex Catalog Viewer (roles/dataplex.catalogViewer), Dataplex Entry Group Owner (roles/dataplex.entryGroupOwner), or Dataplex Catalog Editor (roles/dataplex.catalogEditor). For read-only access to view and list custom rule templates without write or delete permissions, grant a custom role with only the dataplex.entries.get, dataplex.entries.getData, and dataplex.entries.list permissions.

Browse system rule templates

Before authoring custom SQL templates, check the Google-managed system rule templates in the global rule-library entry group to see whether a predefined template meets your validation requirements. For descriptions of every predefined template, see System rule templates list.

Select one of the following options:

Console

  1. In the Google Cloud console, go to the Data profiling & quality page.

    Go to Data profiling & quality

  2. Click Rule libraries > System.

  3. To view all available system rule templates, click rule-library.

  4. Optional: To inspect a template's SQL logic, input parameters, and capabilities, click the template name.

REST

To list available system rule templates, send a GET request to the projects.locations.entryGroups.entries.list method:

curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  "https://dataplex.googleapis.com/v1/projects/dataplex-templates/locations/global/entryGroups/rule-library/entries"

The response lists the system rule template entries in the global rule-library entry group, including each template's name, entrySource.displayName, and entrySource.description.

Create a rule library

If no system template covers your validation logic, then create a rule library entry group to organize and store your organization's custom rule templates. Choose the same region as your target scans—or select global to share templates across regions—because you can't change the location after you create the library. For rule library ID rules, see Resource naming conventions.

Select one of the following options:

Console

  1. In the Google Cloud console, go to the Data profiling & quality page.

    Go to Data profiling & quality

  2. Click Rule libraries > Custom, and then click Create.

  3. In the Create rule library pane, complete the following fields:

    1. Optional: In the Display name field, enter a display name.
    2. In the Rule library ID field, enter a unique ID.
    3. Optional: In the Description field, enter a description.
    4. In the Location menu, select a region or global. You can't change this location after creation.
    5. Optional: To group related resources, add key-value labels.
  4. To create the library, click Save.

  5. Verify that the rule library appears in the Custom list.

REST

To create a rule library, send a POST request to the projects.locations.entryGroups.create method with the required goog-dataplex-entry-group-type: rule_library label:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://dataplex.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/entryGroups?entryGroupId=RULE_LIBRARY_ID" \
  -d '{
    "displayName": "DISPLAY_NAME",
    "description": "DESCRIPTION",
    "labels": {
      "goog-dataplex-entry-group-type": "rule_library"
    }
  }'

Replace the following:

  • PROJECT_ID: the Google Cloud project ID (for example, test-project).
  • LOCATION: the region or global location for the rule library (for example, us-central1).
  • RULE_LIBRARY_ID: a unique ID for the rule library (for example, finance-rule-library).
  • DISPLAY_NAME: an optional human-readable name for the rule library.
  • DESCRIPTION: an optional description of the rule library.

Terraform

To create a rule library, use the google_dataplex_entry_group resource in the Terraform Registry:

resource "google_dataplex_entry_group" "rule_library" {
  project        = "PROJECT_ID"
  location       = "LOCATION"
  entry_group_id = "RULE_LIBRARY_ID"
  display_name   = "DISPLAY_NAME"
  description    = "DESCRIPTION"

  labels = {
    "goog-dataplex-entry-group-type" = "rule_library"
  }
}

Replace the following:

  • PROJECT_ID: the Google Cloud project ID (for example, test-project).
  • LOCATION: the region or global location for the rule library (for example, us-central1).
  • RULE_LIBRARY_ID: a unique ID for the rule library (for example, finance-rule-library).
  • DISPLAY_NAME: an optional human-readable name for the rule library.
  • DESCRIPTION: an optional description of the rule library.

Create a custom rule template

After creating a rule library, add custom rule templates inside it to define parameterized GoogleSQL checks that teams can reuse across scans and catalog entries. For query guidelines and system parameters, see SQL query syntax for rule templates. For ID requirements, dimensions, and aspect fields, see Resource naming conventions, Data quality dimensions, and Rule template aspect fields.

Select one of the following options:

Console

  1. In the Google Cloud console, go to the Data profiling & quality page.

    Go to Data profiling & quality

  2. Click Rule libraries > Custom.

  3. Click the rule library where you want to add a template, and then click Create.

  4. In the Create rule template pane, enter the template metadata:

    1. Optional: In the Template name field, enter a name for the template.
    2. In the Template ID field, enter a unique ID.
    3. Optional: In the Description field, enter a description.
    4. In the Dimension menu, select a data quality dimension.
  5. In the SQL query field, enter a query that returns invalid rows without a trailing semicolon. For example, the following query checks whether column values fall between two bounds:

    SELECT *
    FROM ${data()}
    WHERE
      NOT ((${column()} >= ${param(min_value)} AND ${column()} <= ${param(max_value)}) IS TRUE)
    
  6. Optional: To let referencing rules configure pass-ratio thresholds, select Support threshold.

  7. Optional: To let referencing rules ignore NULL values, first select Support threshold, and then select Support ignore null.

  8. In the Input parameters section, click Add input parameter, and then enter a name, description, and optional default value for each parameter in your query (for example, min_value and max_value).

  9. To create the template, click Save.

REST

To create a custom rule template, send a POST request to the projects.locations.entryGroups.entries.create method with the data-quality-rule-template entry type and aspect:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://dataplex.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/entryGroups/RULE_LIBRARY_ID/entries?entryId=TEMPLATE_ID" \
  -d '{
    "entryType": "projects/dataplex-types/locations/global/entryTypes/data-quality-rule-template",
    "entrySource": {
      "displayName": "DISPLAY_NAME",
      "description": "DESCRIPTION"
    },
    "aspects": {
      "dataplex-types.global.data-quality-rule-template": {
        "data": {
          "dimension": "VALIDITY",
          "sqlCollection": [
            {
              "query": "SELECT t.* FROM ${data()} AS t LEFT JOIN `${param(reference_table)}` AS s ON t.${column()} = s.`${param(reference_column)}` WHERE s.`${param(reference_column)}` IS NULL"
            }
          ],
          "inputParameters": {
            "reference_table": {
              "description": "Reference table in PROJECT_ID.DATASET_ID.TABLE_ID format."
            },
            "reference_column": {
              "description": "Primary key column in the reference table."
            }
          },
          "capabilities": ["THRESHOLD"]
        }
      }
    }
  }'

Replace the following:

  • PROJECT_ID: the Google Cloud project ID (for example, test-project).
  • LOCATION: the location of the rule library (for example, us-central1).
  • RULE_LIBRARY_ID: the ID of the rule library entry group.
  • TEMPLATE_ID: a unique ID for the rule template (for example, foreign-key-check).
  • DISPLAY_NAME: a human-readable display name for the template.
  • DESCRIPTION: a description of what the template validates.

Terraform

To create a custom rule template, use the google_dataplex_entry resource in the Terraform Registry. Escape ${...} parameters as $${...} so Terraform doesn't interpolate them as HCL variables, and use 655216118709 (the project number for dataplex-types) in entry_type and aspect_key:

resource "google_dataplex_entry" "rule_template" {
  project        = "PROJECT_NUMBER"
  location       = "LOCATION"
  entry_group_id = "RULE_LIBRARY_ID"
  entry_id       = "TEMPLATE_ID"

  entry_type = "projects/655216118709/locations/global/entryTypes/data-quality-rule-template"

  entry_source {
    display_name = "DISPLAY_NAME"
    description  = "DESCRIPTION"
  }

  aspects {
    aspect_key = "655216118709.global.data-quality-rule-template"
    aspect {
      data = jsonencode({
        dimension = "VALIDITY"
        sqlCollection = [
          {
            query = "SELECT t.* FROM $${data()} AS t LEFT JOIN `$${param(reference_table)}` AS s ON t.$${column()} = s.`$${param(reference_column)}` WHERE s.`$${param(reference_column)}` IS NULL"
          }
        ]
        inputParameters = {
          reference_table = {
            description = "Reference table in PROJECT_ID.DATASET_ID.TABLE_ID format."
          }
          reference_column = {
            description = "Primary key column in the reference table."
          }
        }
        capabilities = ["THRESHOLD"]
      })
    }
  }
}

Replace the following:

  • PROJECT_NUMBER: the Google Cloud project number.
  • LOCATION: the location of the rule library (for example, us-central1).
  • RULE_LIBRARY_ID: the ID of the rule library entry group.
  • TEMPLATE_ID: a unique ID for the rule template (for example, foreign-key-check).
  • DISPLAY_NAME: a human-readable display name for the template.
  • DESCRIPTION: a description of what the template validates.

View and list custom rule templates

Before teams reference your custom rule templates in scans or catalog entries, inspect the templates to verify that your SQL logic, parameters, and capabilities are saved as expected.

Select one of the following options:

Console

  1. In the Google Cloud console, go to the Data profiling & quality page.

    Go to Data profiling & quality

  2. Click Rule libraries > Custom.

  3. Click the rule library that contains your templates.

  4. In the Rule templates list, review the listed template names and descriptions.

  5. To view the dimension, SQL query, input parameters, and capabilities, click a template name.

REST

To inspect a specific custom rule template including its aspect data, send a GET request to the projects.locations.entryGroups.entries.get method with view=ALL. To list all templates in a library, use projects.locations.entryGroups.entries.list.

curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  "https://dataplex.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/entryGroups/RULE_LIBRARY_ID/entries/TEMPLATE_ID?view=ALL"

Replace the following:

  • PROJECT_ID: the Google Cloud project ID.
  • LOCATION: the location of the rule library.
  • RULE_LIBRARY_ID: the ID of the rule library entry group.
  • TEMPLATE_ID: the ID of the rule template to inspect.

Verify the following fields in the response:

Field What to check
name Full resource name used in templateReference.name when binding rules.
aspects.*.data.dimension Default data quality dimension for rules referencing this template.
aspects.*.data.sqlCollection Active parameterized SQL query evaluated during scan runs.
aspects.*.data.capabilities Enabled rule-level capabilities (THRESHOLD or IGNORE_NULL).

Update a custom rule template

When your business validation requirements change, update the custom rule template once in your rule library. Every scan referencing that template uses the revised SQL logic, parameters, or capabilities on its next run. If you rename or add required input parameters without a defaultValue, then existing rules referencing the template fail until you update their parameter values.

Select one of the following options:

Console

  1. In the Google Cloud console, go to the Data profiling & quality page.

    Go to Data profiling & quality

  2. Click Rule libraries > Custom.

  3. Click the rule library that contains the template that you want to update.

  4. In the Rule templates list, click the template name.

  5. On the rule template details page, click Edit.

  6. Update the template fields, and then click Save.

  7. Verify that the updated SQL query and parameters appear on the template details page.

REST

To update a custom rule template, send a PATCH request to the projects.locations.entryGroups.entries.patch method with updateMask=aspects:

curl -X PATCH \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://dataplex.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/entryGroups/RULE_LIBRARY_ID/entries/TEMPLATE_ID?updateMask=aspects" \
  -d '{
    "aspects": {
      "dataplex-types.global.data-quality-rule-template": {
        "data": {
          "dimension": "VALIDITY",
          "sqlCollection": [
            {
              "query": "SELECT * FROM ${data()} WHERE ${column()} IS NULL"
            }
          ],
          "capabilities": ["THRESHOLD"]
        }
      }
    }
  }'

Replace the following:

  • PROJECT_ID: the Google Cloud project ID.
  • LOCATION: the location of the rule library.
  • RULE_LIBRARY_ID: the ID of the rule library entry group.
  • TEMPLATE_ID: the ID of the rule template to update.

Terraform

To update a custom rule template, modify the aspects block of the google_dataplex_entry resource and run terraform apply:

resource "google_dataplex_entry" "rule_template" {
  project        = "PROJECT_NUMBER"
  location       = "LOCATION"
  entry_group_id = "RULE_LIBRARY_ID"
  entry_id       = "TEMPLATE_ID"

  entry_type = "projects/655216118709/locations/global/entryTypes/data-quality-rule-template"

  aspects {
    aspect_key = "655216118709.global.data-quality-rule-template"
    aspect {
      data = jsonencode({
        dimension = "VALIDITY"
        sqlCollection = [
          {
            query = "SELECT * FROM $${data()} WHERE $${column()} IS NULL"
          }
        ]
        capabilities = ["THRESHOLD"]
      })
    }
  }
}

Replace the following:

  • PROJECT_NUMBER: the Google Cloud project number.
  • LOCATION: the location of the rule library.
  • RULE_LIBRARY_ID: the ID of the rule library entry group.
  • TEMPLATE_ID: the ID of the rule template to update.

Delete a custom rule template

When you retire a custom validation check that active scans or catalog entries no longer need, delete the template from your rule library. Removing retired templates prevents other teams from referencing obsolete logic.

Select one of the following options:

Console

  1. In the Google Cloud console, go to the Data profiling & quality page.

    Go to Data profiling & quality

  2. Click Rule libraries > Custom.

  3. Click the rule library that contains the template that you want to delete.

  4. In the Rule templates list, click the template name.

  5. Click Delete, and then click Delete again to confirm.

  6. Verify that the template no longer appears in the Rule templates list.

REST

To delete a custom rule template, send a DELETE request to the projects.locations.entryGroups.entries.delete method:

curl -X DELETE \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  "https://dataplex.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/entryGroups/RULE_LIBRARY_ID/entries/TEMPLATE_ID"

Replace the following:

  • PROJECT_ID: the Google Cloud project ID.
  • LOCATION: the location of the rule library.
  • RULE_LIBRARY_ID: the ID of the rule library entry group.
  • TEMPLATE_ID: the ID of the rule template to delete.

Troubleshoot

If you encounter errors while creating or updating rule templates, then check the following common causes:

  • SQL syntax validation fails: remove any trailing semicolon (;) from the query string, and verify that parameter names match case exactly between ${param(PARAMETER_NAME)} and inputParameters.
  • IGNORE_NULL capability rejected: confirm that capabilities also includes THRESHOLD, because IGNORE_NULL requires THRESHOLD and applies only to column-level rules.
  • Permission denied when saving a template: verify that you have the Dataplex Catalog Editor (roles/dataplex.catalogEditor) or Dataplex Entry and EntryLink Owner (roles/dataplex.entryOwner) role on the entry group. If you use a custom role, then verify that it includes the dataplex.entryGroups.useDataQualityRuleTemplateEntry and dataplex.entryGroups.useDataQualityRuleTemplateAspect permissions.

What's next