Trigger actions when a data quality check fails

A violation action automatically responds when a data metric function (DMF) detects a data quality issue. The action runs as part of the DMF evaluation, so you don’t need a separate task that polls the results table before you can respond to an issue.

A data quality issue is detected when an expectation on the association is violated or anomaly detection flags a result.

Violation action types

The following action types are supported:

TYPETARGETBehaviorFor more information
EXECUTE_TASKA taskRuns the target task once, the same as EXECUTE TASK.Run a task when a data quality check fails
SKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESHNoneSkips refreshes of the dynamic tables that depend on the monitored object until the check passes.Skip downstream dynamic table refreshes when a data quality check fails

Use EXECUTE_TASK to respond to an issue programmatically; for example, to quarantine rows that fail a check, start a remediation pipeline, or record the issue in an audit table. Use SKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESH to stop data that fails a check from propagating through a dynamic table pipeline.

Set a violation action

A violation action is configured on the association between a table or view and a DMF, the same granularity as an expectation. Two associations on the same table carry independent actions.

To set an action, specify the ON_VIOLATION_ACTION property with a property block that identifies the action TYPE and, if the type requires one, its TARGET. You can set the property on an existing association:

ALTER TABLE t1
  MODIFY DATA METRIC FUNCTION snowflake.core.null_count ON (c1)
    SET ON_VIOLATION_ACTION = (TYPE = 'EXECUTE_TASK', TARGET = my_db.my_schema.quarantine_bad_rows);

You can also set the property in the same statement that adds the DMF and its expectation:

ALTER TABLE t1
  ADD DATA METRIC FUNCTION snowflake.core.null_count ON (c1)
    EXPECTATION expect_no_nulls (VALUE = 0)
    ON_VIOLATION_ACTION = (TYPE = 'EXECUTE_TASK', TARGET = my_db.my_schema.quarantine_bad_rows);

An action fires only when the association detects a data quality issue. If the association has no expectation and doesn’t use anomaly detection, the action never fires.

Set multiple violation actions

To respond to an issue in more than one way, specify a parenthesized list of property blocks. For example, the following statement runs a task and also skips downstream dynamic table refreshes when the check fails:

ALTER TABLE t1
  MODIFY DATA METRIC FUNCTION snowflake.core.null_count ON (c1)
    SET ON_VIOLATION_ACTION = (
      (TYPE = 'EXECUTE_TASK', TARGET = my_db.my_schema.quarantine_bad_rows),
      (TYPE = 'SKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESH')
    );

An association can have up to eight violation actions. Each action must be unique: you can’t specify the same action type with the same target twice, but you can specify the same type with different targets; for example, two EXECUTE_TASK actions that run different tasks. When the association detects a data quality issue, every action runs.

For the privileges required by each action type, see Run a task when a data quality check fails and Skip downstream dynamic table refreshes when a data quality check fails.

Replace or remove a violation action

To replace the actions on an association, set a new ON_VIOLATION_ACTION. The new value replaces all of the existing actions; to keep an existing action, include it in the new value:

ALTER TABLE t1
  MODIFY DATA METRIC FUNCTION snowflake.core.null_count ON (c1)
    SET ON_VIOLATION_ACTION = (TYPE = 'SKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESH');

To remove all of the actions, set the property to an empty property block. The DMF and its expectations remain in place:

ALTER TABLE t1
  MODIFY DATA METRIC FUNCTION snowflake.core.null_count ON (c1)
    SET ON_VIOLATION_ACTION = ();

Check the status of a violation action

The DATA_METRIC_FUNCTION_REFERENCES function returns a VIOLATION_ACTION column for each association. The column contains a JSON array with one element for each action configured on the association, in the order that you specified them. Each element describes the action and the outcome of the most recent evaluation that observed it. The column is NULL when no action is configured.

For example, an association with a task action and a dynamic table action returns a value like the following:

[
  {"type": "EXECUTE_TASK", "target": "MY_DB.MY_SCHEMA.QUARANTINE_BAD_ROWS", "last_trigger_status": "SCHEDULED"},
  {"type": "SKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESH", "last_trigger_status": "DOWNSTREAM_DYNAMIC_TABLE_REFRESH_SKIPPED"}
]

To return one row for each action, use FLATTEN:

SELECT
    r.metric_name,
    r.ref_entity_name,
    a.value:type::string                AS action_type,
    a.value:target::string              AS action_target,
    a.value:last_trigger_status::string AS last_trigger_status
  FROM TABLE(INFORMATION_SCHEMA.DATA_METRIC_FUNCTION_REFERENCES(
      REF_ENTITY_NAME => 'my_db.my_schema.t1',
      REF_ENTITY_DOMAIN => 'TABLE')) AS r,
    LATERAL FLATTEN(INPUT => r.violation_action) AS a;

Each element of the array contains the following fields:

FieldDescription
typeThe action type, such as EXECUTE_TASK or SKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESH.
targetThe fully qualified name of the target object. Omitted when the action type takes no target, or when the target no longer exists.
last_trigger_statusThe outcome of the most recent evaluation that observed the action. Omitted until the first evaluation.

The last_trigger_status field takes one of the following values:

StatusAction typesMeaning
NOT_TRIGGEREDAllThe most recent evaluation detected no data quality issue. This is the expected value for healthy data.
SCHEDULEDEXECUTE_TASKA data quality issue was detected and the task run was scheduled. The run is visible in TASK_HISTORY.
SKIPPED_TASK_DROPPEDEXECUTE_TASKA data quality issue was detected, but the target task no longer exists, so no run was scheduled.
FAILED_INSUFFICIENT_PRIVILEGEEXECUTE_TASKA data quality issue was detected, but the role that the DMF runs as can’t run the target task.
FAILEDEXECUTE_TASKA data quality issue was detected, but scheduling the task run failed for another reason.
DOWNSTREAM_DYNAMIC_TABLE_REFRESH_SKIPPEDSKIP_DOWNSTREAM_DYNAMIC_TABLE_REFRESHThe most recent evaluation detected a data quality issue, so downstream dynamic table refreshes are skipped.

The status reflects only the most recent evaluation and is overwritten by each evaluation. It isn’t a history of past actions. To see the outcome of each action, use TASK_HISTORY for a task, or dynamic table refresh history for skipped refreshes.

Limitations and considerations

  • Each DMF association supports up to eight violation actions.
  • Every action on an association runs when any expectation on the association is violated. You can’t route different expectations to different actions.
  • A failure to run an action doesn’t fail the DMF evaluation. The failure is recorded in that action’s last_trigger_status.
  • When you clone a table or view, its violation actions are copied to the clone.
  • When a database or replication group is replicated, its violation actions are replicated. An EXECUTE_TASK action on the secondary runs the replicated copy of the target task. If the target task isn’t replicated, the action’s target field is omitted on the secondary. The last_trigger_status field isn’t replicated; it reflects evaluations on the secondary only.