Data Quality Spec configuration guide

Updated at:

The Spec is the description format for Data Quality monitoring resources and Data Quality rule resources in the DataWorks Data Quality module. The Spec is written in YAML. You can use the Spec as an input parameter of OpenAPI operations to manage related resources in DataWorks Data Quality.

Basic definitions

YAML example

A simple Data Quality monitoring configuration is as follows:

datasets:
  - type: Table
    tables:
      - tb_d_spec_demo
    dataSource:
      name: odps_first
      envType: Dev
rules:
  - assertion: row_count > 0

Property description

The Spec in the preceding example defines the simplest Data Quality monitoring configuration:

  • datasets: the monitored object of Data Quality. This parameter contains three properties.

    • type: the monitored object type. Only one enumerated value is supported: Table.

    • dataSource: the data source to which the monitored object belongs. Set name and envType to identify the data source. You can call ListDataSources to obtain the data source name.

      Note

      Only some data sources are supported. For more information, see Supported data source types.

    • tables: if the monitored object is of the Table type, specify the table names.

      Note

      If the data source is bound at the database level and you need to monitor tables in a non-default schema, configure the table in the schema.table format.

  • rules: rules that data is expected to satisfy. A Data Quality monitoring configuration can contain multiple rules.

    When a Data Quality monitoring configuration runs, it scans the data of the monitored object, calculates the metric value specified by each rule in sequence, and compares the value with the expected threshold. The system determines whether a rule passes based on whether the expected threshold is satisfied.

    In most cases, the expectation for data is described by using a metric type (such as row_count), a comparison symbol (such as >), and a threshold (such as 0) in an assertion statement. A check result usually has three states.

    • pass: The check passes. The collected metric value falls within the range defined by the threshold.

    • fail: The check fails. The collected metric value falls outside the range defined by the threshold.

    • error: Another exception occurs during the check, such as a syntax error.

    Note

    In addition, table schema evolution detection rules (assertion: change for schema) are also supported. Instead of calculating a numeric metric, this rule type collects table schema metadata, compares the collected metadata with the baseline table schema, and then matches the change type against the warn and fail conditions defined in the rule to determine the check result. For more information, see Schema Check rules.

Threshold definition methods

assertion supports the following threshold definition methods:

Fixed threshold

A fixed threshold applies when you need to compare a metric value directly with a fixed value. A fixed threshold definition consists of the following parts:

  • a metric

  • an argument (optional)

  • a comparison symbol (optional)

  • a threshold (optional)

Configuration example

rules:
  # The number of data rows must be greater than 0
  - assertion: row_count > 0
  # size column: the maximum value must be less than or equal to 500
  - assertion: max(size) <= 500

The following table describes the four parts of the fixed-value threshold expression:

Expression part

row_count > 0

max(size) <= 500

a metric

row_count

max

an argument (optional)

/

(size)

a comparison symbol (optional)

>

<=

a threshold (optional)

0

500

For more supported comparison symbols, see Supported comparison symbols (comparison symbol).

Fluctuation threshold

A fluctuation threshold applies when you need to compare the current metric value with historical values of the same metric. For example, the difference between the number of users today and the number of users yesterday must be within 100, or the fluctuation range of today's revenue compared with the average revenue of the last 7 days must be within 10%.

Typically, the metric is preceded by change for to describe a fluctuation threshold. A fluctuation threshold definition consists of the following parts:

  • change (keyword)

  • an aggregate type (optional)

  • a time window

  • percent (keyword, optional)

  • for (keyword)

  • a metric

  • an argument (optional)

  • a comparison symbol (optional)

  • a threshold (optional)

The format of the combined expression is change [aggregate_type] [time_window] [percent] for metric [argument] [comparison_symbol threshold].

Configuration example

Example 1: Compare the difference against the check result of a specified time window (time_window)

Within change ... for, you can add a time window (time_window) to explicitly specify which historical metric values the current metric value is compared against:

rules:
  # The difference between the current number of data rows and the number of data rows at the check 7 days ago must be within 10000 rows
  - assertion: change 7 days ago for row_count < 10000

Example 2: Aggregate the check results of a specified time window before comparing the difference

Between change and the time window (time_window), you can add an aggregation method (aggregate type). The system uses the specified aggregation method to compute an intermediate result from the check records within the time window. The system then uses that result as the reference value for the current check:

rules:
  # The difference between the current number of data rows and the average number of data rows over the last 7 days must be within 10000 rows
  - assertion: change average last 7 days for row_count < 10000

The following two aggregation methods are supported:

  • avg: the average.

  • var: the variance.

If you do not specify an aggregation method, the system compares the current metric value with every check history record within the time window in sequence and obtains a comparison result for each record. The system then uses the most severe status as the final check status.

Example 3: Compare the fluctuation percentage against historical check results

Within change ... for, you can add percent to specify that the fluctuation percentage is calculated before it is compared with the threshold:

rules:
  # The fluctuation percentage of the current number of data rows compared with the number of data rows at the check 7 days ago must be within 50%
  - assertion: change 7 days ago percent for row_count < 50%
  • You can append % to the end of the threshold to improve readability.

  • Assume that the current metric value is c and the previous metric value is cl. The formula is percent = (c-cl) / cl:

    • If cl is 0 and c is also 0, the calculated percentage is 0.

    • If cl is 0 and c is not 0, the percentage cannot be calculated and the check result is error.

  • The result can be a negative number. Take note when you define the threshold. You can use the between...and... syntax described later in this topic to define the range of the fluctuation percentage.

Range threshold

You can use between...and... to define a range-type threshold.

Closed range threshold definition

A range that is defined directly by using between...and... is a closed range by default. For example, warn is triggered when the row count fluctuation compared with one day ago is not within [-1%, 1%], and fail is triggered when the fluctuation is not within [-5%, 5%]. The check passes when the value falls within the [10, 15] range.

datasets:
  - type: Table
    tables:
      - tb_d_spec_demo
    filter: dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'
    dataSource:
      name: odps_first
      envType: Dev
rules:
  - assertion: change 1 day ago percent for row_count
    warn: 
      - when not between -1% and 1%
    fail: 
      - when not between -5% and 5%

Add multi-level threshold definitions

In addition to defining the expected threshold in the assertion statement of a rule, you can omit the expected threshold from the assertion statement and set the warn or fail property of the rule to define thresholds with more levels. For example:

rules:
  - assertion: duplicate_count(phone)
    warn: when between 1 and 10
    fail: when > 10

The preceding code snippet defines two threshold levels, warn and fail:

  • phone column: when the number of duplicate rows is 0, the check passes.

  • phone column: when the number of duplicate rows is greater than 10, the check fails.

  • phone column: when the number of duplicate rows is between 1 and 10, the check result is warn.

Handling strategy when warn and fail overlap

warn and fail threshold ranges can overlap. If the metric value falls into the overlapping range, the system uses the more severe status, and the result is fail.

Use not between...and... to define the complement of a range

You can negate between...and... by adding not before it to obtain the complement of the range. For example:

rules:
  - assertion: duplicate_count(phone)
    warn: 
      - when not between -1% and 1%
    fail:
      - when not between -5% and 5%

The ranges defined by the preceding code snippet are shown in the following figure:

image

Set the unique identifier of a rule (identity)

You can specify an identity for a rule as the unique identifier of the rule within the same Data Quality monitoring configuration.

  • If you do not specify an identity when you create a rule, the system automatically assigns an id.

  • You must ensure that identity is unique within the same Data Quality monitoring configuration. Otherwise, the update may fail or another rule may be updated by mistake. Use a readable string to simplify management.

  • For rules with fluctuation thresholds (change...for...) and rules for anomaly detection (anomaly detection for ...), the check history is queried by the identity of the rule.

  • When the system receives a request to update a Data Quality monitoring configuration, it processes the request as follows:

    • The system iterates over each rule in the update request and uses the id of the rule to match and update the existing rules in the monitoring configuration.

    • The system deletes the existing rules in the monitoring configuration that are not matched by any id.

    • The system creates the remaining rules in the update request as new rules in the monitoring configuration.

The following example shows how to specify an identity:

rules:
  - assertion: row_count > 0
    name: Row count is greater than 0
    # Specify a unique identifier within the Data Quality monitoring configuration
    identity: table-not-empty

Define the business severity of a rule (severity)

You can set the severity of a rule to mark how severely the rule affects your business, which simplifies subsequent management. For example:

rules:
  - assertion: row_count > 0
    severity: High

severity supports two levels:

  • High

  • Normal (default)

Set the rule enabling status (enabled)

You can specify the enabled property of a rule to manage the enabling status of the rule. For a rule that you temporarily do not use but do not want to delete, you can set enabled to false to temporarily disable the rule. The rule is not triggered when the monitoring configuration runs.

rules:
  - assertion: row_count > 0
    # The enabling status of the rule. Default value: true
    enabled: false

Set pre-execution statements (settingConfig)

In some situations, you must run SET statements to adjust parameters before the SQL statement that calculates the metric runs. This ensures that the SQL statement runs properly or delivers better performance. You can add the settingConfig setting to a rule. For example:

rules:
  - assertion: row_count > 0
    # Set the SET statements that run before the check
    settingConfig: SET odps.sql.udf.timeout=600s; SET odps.sql.python.version=cp27;

Set the failed row collection switch (collectFailedRows)

You can turn on the failed row collection switch at the rule level. When a rule check does not pass (the warn or fail status), the system automatically filters out the data that caused the failure. The data is saved to another table in the same database as the monitored object table. Turn on the failed row collection switch as follows:

rules:
  - assertion: duplicate_count(phone) = 0
    collectFailedRows: true

Set collectFailedRows to true to enable failed row collection. For custom SQL rules, you must also specify failedRowsQuery to explicitly configure the filter statement for failed row collection:

rules:
  - assertion: id_null_count = 0
    id_null_count:
      expression: id IS NULL
    collectFailedRows: true
    failedRowsQuery: SELECT * FROM tb_d_spec_demo WHERE dt = '$[yyyymmdd-1]' AND id IS NULL

Apart from custom SQL rules, only the following metrics support failed row collection. Other metrics do not support collectFailedRows:

  • missing_count

  • missing_percent

  • duplicate_count

  • duplicate_percent

  • distinct_count

  • distinct_percent

Set rule names and descriptions

You can set a name and a description for each rule to simplify management.

For example, you can set a name for the entire monitoring configuration and a separate name for each rule. These names are used in subsequent check results and on pages, which simplifies management.

datasets:
  - type: Table
    tables:
      - tb_d_spec_demo
    filter: dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'
    dataSource:
      name: odps_first
      envType: Dev
rules:
  - assertion: row_count > 0
    # The rule name and description
    name: Row count is greater than 0
    description: The output data must not be empty

Set data filters (filter)

Set a data filter for a rule

If only a subset of data is required for metric statistics when a rule is checked, for example, only the rows in which the id column is not NULL, you can add a filter setting to the rule. For example:

rules:
  - assertion: row_count > 0
    filter: id IS NOT NULL

Set a data filter for a Data Quality monitoring configuration

In Scan.Dataset, you can add a filter setting. In this case, the filter takes effect during the check of every rule in the Data Quality monitoring configuration.

datasets:
  - type: Table
    tables:
      - tb_d_spec_demo
    filter: dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'
    dataSource:
      name: odps_first
rules:
  - assertion: row_count > 0

In the preceding code snippet, the dataset contains a filter setting. When each rule is checked, the system first uses the filter to filter the data and then runs the subsequent check.

Note

In the definition of the example filter, the time expression $[yyyymmdd-1] is used. The system uses the triggerTime parameter of the monitoring trigger, applies the time offset, and then substitutes the result into the filter. For more information about the parameter reference methods that the system supports, see Configure data filtering.

Appendix

Supported comparison symbols (comparison symbol)

  • >

  • >=

  • <

  • <=

  • =

  • !=

  • between ... and ...

Supported data source types

The following data source types are supported:

  • maxcompute

  • hologres

  • emr

  • mysql

  • analyticdb_for_mysql

  • analyticdb_for_postgresql

  • cdh

  • starrocks

  • lindorm

  • sqlserver

  • selectdb

Supported built-in metric types

  • avg

  • row_count

  • sum

  • min

  • max

  • distinct_count

  • distinct_percent

  • table_size

  • missing_count

  • missing_percent

  • duplicate_percent

  • duplicate_count

  • group_by

  • invalid_count

  • invalid_distinct_count

  • change for schema

    Note

    To detect changes to the table schema, including column deletion, column addition, column order changes, and column type changes, use a table schema evolution detection rule. For more information, see Schema Check rules.

Time window (time_window) definition methods

DataWorks Data Quality supports multiple methods for defining a time window. The basic formats are as follows:

  • n time units ago: n (minute[s]|hour[s]|day[s]|week[s]|month[s]) ago, for example, n months ago, n days ago, or n hours ago.

    • One day ago: 1 day ago

    • Seven days ago: 7 days ago

    • One month ago: 1 month ago

    • Eight hours ago: 8 hours ago

    • Fifteen minutes ago: 15 minutes ago

  • The last n time units: last n (minute[s]|hour[s]|day[s]|week[s]|month[s]) , for example, last 15 minutes, last 7 days, or last 1 month.

    • The last 15 minutes: last 15 minutes

    • The last 24 hours: last 24 hours

      Note

      The system collects a data point at each one-hour interval, from 24 hours before the current time up to the current time.

    • The last 7 days: last 7 days

    • The last 1 month: last 1 month

  • The same time on a specific day of a month or a specific day of a week: 1/2/3/.../-3/-2/-1 of (current|last|n) (months|weeks) (ago)

    • The same time on the first day of the current month: 1 of current month

    • The same time on the last day of the previous month: -1 of last month

    • The same time on Tuesday three weeks ago: 2 of 3 weeks ago