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

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:

GRANT MANAGE DATA QUALITY ON ACCOUNT TO ROLE data_steward;
GRANT EXECUTE TASK ON ACCOUNT TO ROLE data_steward;
GRANT OPERATE ON TASK my_db.my_schema.quarantine_bad_rows TO ROLE data_steward;

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:

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);

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:

SELECT name, scheduled_from, state, query_id, scheduled_time, completed_time
  FROM TABLE(INFORMATION_SCHEMA.TASK_HISTORY())
  WHERE name = 'QUARANTINE_BAD_ROWS'
  ORDER BY scheduled_time DESC;

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.

  1. Create the database, schema, monitored table, and quarantine table. The quarantine table has the same columns as the monitored table:

    CREATE OR REPLACE DATABASE my_db;
    CREATE OR REPLACE SCHEMA my_db.my_schema;
    USE SCHEMA my_db.my_schema;
    
    CREATE OR REPLACE TABLE t1 (id NUMBER, c1 STRING);
    CREATE OR REPLACE TABLE t1_quarantine (id NUMBER, c1 STRING);
    
  2. Create the quarantine task. The task uses SYSTEM$DATA_METRIC_SCAN to copy the rows that failed the check into the quarantine table:

    CREATE OR REPLACE TASK my_db.my_schema.quarantine_bad_rows
      WAREHOUSE = my_wh
      AS
        INSERT INTO t1_quarantine
          SELECT * FROM TABLE(SYSTEM$DATA_METRIC_SCAN(
            REF_ENTITY_NAME => 'my_db.my_schema.t1',
            METRIC_NAME => 'SNOWFLAKE.CORE.NULL_COUNT',
            ARGUMENT_NAME => 'c1'));
    
  3. Grant the data_steward role the EXECUTE TASK privilege on the account and the OPERATE privilege on the task:

    GRANT EXECUTE TASK ON ACCOUNT TO ROLE data_steward;
    GRANT OPERATE ON TASK my_db.my_schema.quarantine_bad_rows TO ROLE data_steward;
    
  4. Set a schedule for the DMFs on the table:

    ALTER TABLE t1 SET DATA_METRIC_SCHEDULE = '5 MINUTE';
    
  5. Add the DMF, define an expectation, and set the violation action:

    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);
    
  6. 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:

    INSERT INTO t1 VALUES (1, 'a'), (2, NULL), (3, NULL), (4, 'b');
    

After the next evaluation detects the violation, confirm that the task ran:

SELECT name, scheduled_from, state, scheduled_time
  FROM TABLE(INFORMATION_SCHEMA.TASK_HISTORY())
  WHERE name = 'QUARANTINE_BAD_ROWS'
  ORDER BY scheduled_time DESC;

Then confirm that the task copied the two rows with a NULL c1 value into the quarantine table:

SELECT * FROM t1_quarantine;

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.