Run a task when a data quality check fails¶
To respond to a data quality issue programmatically, configure an EXECUTE_TASK violation action
that runs a Snowflake task when a data metric function (DMF) detects the issue. For example, the task can
quarantine the rows that fail a check, start a remediation pipeline, or record the issue in an audit table.
Prerequisites¶
- A DMF association with an expectation or anomaly detection
- A task that performs the response you want
Grant privileges¶
To configure an EXECUTE_TASK action, the role must have the following privileges:
- The privileges required to modify the DMF association, such as MANAGE DATA QUALITY on the account or ownership of the table or view. For more information, see Access control for data quality.
- The EXECUTE TASK privilege on the account. This is the same privilege required to run EXECUTE TASK.
- The OPERATE privilege on the target task.
For example, suppose the data_steward role owns the table and configures the violation action. To let it run the
quarantine_bad_rows task, grant the following privileges:
When the action fires, the role that the DMF runs as must also be able to run the task. If it can’t, the action isn’t run and
last_trigger_status is FAILED_INSUFFICIENT_PRIVILEGE. By default, a DMF runs as the owner of the table or view. For more
information, see Required privilege on the table or view.
A triggered task runs as its owner role, not as the role that configured the violation action. This matches the behavior of EXECUTE TASK.
Set the action¶
To run a task when the check fails, set TYPE to 'EXECUTE_TASK' and set TARGET to the task:
The target task must meet the following requirements, which are validated when you set the action:
- The task exists and is in the same account as the table or view.
- The task is a standalone task or the root task of a task graph, not a child task.
TARGET can be a fully qualified name or an unqualified name. An unqualified name is resolved by using the current database and
schema of the session that runs the statement, not the schema of the table or view.
The task doesn’t need a schedule and doesn’t need to be resumed. Snowflake runs the task the same way as EXECUTE TASK.
To run more than one task, or to combine a task with another action type, see Set multiple violation actions. For information about replacing or removing the action, see Replace or remove a violation action.
How a triggered task runs¶
When the most recent evaluation of the association detects a data quality issue, Snowflake runs the target task once:
- Snowflake schedules at most one run of each target task for each violated association in each evaluation. If several associations on the same table are violated in one evaluation, each association schedules its own run.
- The task is scheduled after the DMF result is recorded.
- The task runs as its owner role and follows the standard task execution model.
To check whether the most recent evaluation scheduled a run, see Check the status of a violation action.
Find triggered task runs¶
A task run that a violation action schedules appears in the TASK_HISTORY output like any
other run. Because the action runs the task the same way as EXECUTE TASK, the run’s SCHEDULED_FROM column is EXECUTE TASK. To
find the runs of the target task, filter by the task name:
Considerations¶
If you drop the target task, the action is skipped when it fires, last_trigger_status is SKIPPED_TASK_DROPPED, and the target
field is omitted from the VIOLATION_ACTION column. Point the action at a different task or remove the action.
For limitations that apply to all violation actions, see Limitations and considerations.
Example: Quarantine rows that fail a check¶
The following example creates a monitored table and a quarantine table, creates a task that copies the rows that fail a check into
the quarantine table, and sets that task as the violation action on a NULL_COUNT DMF. The example runs as the data_steward role,
which owns the objects that it creates.
-
Create the database, schema, monitored table, and quarantine table. The quarantine table has the same columns as the monitored table:
-
Create the quarantine task. The task uses SYSTEM$DATA_METRIC_SCAN to copy the rows that failed the check into the quarantine table:
-
Grant the
data_stewardrole the EXECUTE TASK privilege on the account and the OPERATE privilege on the task: -
Set a schedule for the DMFs on the table:
-
Add the DMF, define an expectation, and set the violation action:
-
Insert rows, two of which have a NULL value in
c1. The next evaluation finds a null count greater than zero, which violates the expectation:
After the next evaluation detects the violation, confirm that the task ran:
Then confirm that the task copied the two rows with a NULL c1 value into the quarantine table:
Next steps¶
To stop data that fails a check from reaching downstream dynamic tables, see Skip downstream dynamic table refreshes when a data quality check fails.