Referential Integrity

Checks that the values of a field in one table exist in a field of another table.

The Referential Integrity monitor checks that the values of a field in one table exist in another table, which can come from a different data source.

When to Use

  • Foreign keys: Every value of a foreign key must exist in the reference table.
  • Mandatory relationships: Every row of a parent table must have at least one matching row in a child table. Select the child table as the left table and use the right join check.
  • One-to-one relationships: Every parent row has a child row, and every child row belongs to an existing parent row (outer join check).

When the accepted values are a short, fixed list rather than another table, use Value List Validation.

How It Works

Sifflet joins the two tables on the selected fields and counts the rows without a match, depending on the comparison mode:

Comparison modeCountsExample
Left join check (default)Rows of the left table whose value does not exist in the right tableEvery foreign key value exists in the reference table.
Right join checkRows of the right table whose value does not exist in the left tableEvery parent row has a child row (with the child table as the left table).
Outer join checkRows of either table whose value does not exist in the other tableParent and child rows match each other.

The default static threshold fails when the count is greater than 0, that is, as soon as one row has no match.

Example

You want every order to belong to an existing customer. Create a Referential Integrity monitor with the following settings:

  • Datasets: orders (left table), then customers (right table).
  • Left join key column and Right join key column: customer_id in both tables.
  • Comparison mode: Left join check (default).
  • Monitoring mode: Incremental scan, with created_at of orders as the time window column and a daily time-based data aggregation.

On June 7, the nightly synchronization of customers fails, so the customers who signed up that day are missing from customers. Their 340 orders of June 7 have no matching customer. The data point of June 7 is 340, greater than 0, so the monitor fails.

Configure the Monitor

Referential Integrity Settings

SettingDescriptionDefault
DatasetsThe left table first, then the right table. Select the table that receives new rows first, and the reference table second.None
Left join key column and Right join key columnThe field of each table to compare.None
Comparison modeLeft join check, Right join check, or Outer join check (see How It Works).Left join check

Common Settings

  • Threshold: Static (default), relative, or dynamic, on the count of unmatched rows (there is no percentage value mode). See Thresholds.
  • Scan mode: Incremental or full dataset. Time settings only apply to the left table, and are only available with the Left join check comparison mode. In incremental scan mode, the monitor only checks the new rows of the left table against the whole right table, which reduces the cost of each run. Without time settings, the monitor scans both tables entirely at each run. See Scan Modes.
  • Where clause: Each table has its own where clause.
  • Group by: Only available on the left table, with the Left join check comparison mode.
  • Joins: Not available: the monitor already compares the two tables.

Monitor as Code

The monitor kind is ReferentialIntegrity. The first dataset is the left table, and the second dataset is the right table. The following YAML creates the monitor of the example on Snowflake:

datasets:
  - uri: snowflake://<account>/<DATABASE>.SALES.ORDERS
  - uri: snowflake://<account>/<DATABASE>.SALES.CUSTOMERS
parameters:
  kind: ReferentialIntegrity
  left:
    field: customer_id
    timeWindow:
      field: created_at
      firstRun: P365D
      frequency: P1D
  right:
    field: customer_id
ParameterRequiredDescription
matchTypeNoLeftOnRight (default, Left join check), RightOnLeft (Right join check), or Full (Outer join check).
left.fieldYesThe field of the left table.
left.whereStatementNoSQL condition (without WHERE) to filter the left table, for example my_field <> 'cancelled'.
left.timeWindowNoTime settings for incremental checks on the left table. Only with matchType: LeftOnRight.
left.groupByNoGroup by on the left table. Only with matchType: LeftOnRight.
left.partitionNoPartition settings of the left table.
right.fieldYesThe field of the right table.
right.whereStatementNoSQL condition (without WHERE) to filter the right table.

Related Templates


Did this page help you?