Schema checks

Use a SodaCL schema check to validate column presence, absence, or position in a table, or the type of data column contains.

Use a schema check to validate the presence, absence or position of columns in a dataset, or to validate the type of data column contains.

checks for dim_product:
  - schema:
      name: Confirm that required columns are present
      warn:
        when required column missing: [weight_unit_measure_code, product_subcategory_key, made_up_column]
      fail:
        when required column missing:
          - product_key 
          - product_alternate_key
  - schema:
      warn:
        when forbidden column present: [credit_card]
        when wrong column type:
          standard_cost: money
      fail:
        when forbidden column present: [pii*]
        when wrong column type:
          reorder_point: smallint
  - schema:
      name: Columns out of order
      warn:
        when wrong column index:
          style: 1
      fail:
        when wrong column index:
          model_name: 22
  - schema:
      name: Any schema changes
      warn: 
        when schema changes: any

Define schema checks

✖️ Requires Soda Core Scientific (included in a Soda Agent) ✔️ Supported in Soda Core ✔️ Supported in Soda Library + Soda Cloud ✔️ Supported in Soda Cloud Agreements + Soda Agent ✔️ Available as a no-code check with a self-hosted Soda Agent connected to any Soda-supported data source, except Spark, and Dask and Pandas OR with a Soda-hosted Agent connected to a BigQuery, Databricks SQL, MS SQL Server, MySQL, PostgreSQL, Redshift, or Snowflake data source

In the context of SodaCL check types, schema checks are unique. Schema checks always employ alert configurations – specifying warn and/or fail alert conditions – with validation keys. Refer to Add alert configurations for exhaustive alert configuration details.

The validation key:value pairs in schema checks set the conditions for a warn or a fail check result. See a List of validation keys below.

For example, the following check uses the when required column missing validation key to validate that specific columns are present in a dataset; if any of columns in the list are absent, the check result is fail.

In the example above, the value for the validation key is in a nested list format, but you can use an inline list of comma-separated values inside square brackets instead. The following example yields identical check results to the example above.

You can define a schema check with both warn and fail alert conditions, each with multiple validation keys. Refer to Configure multiple alerts for details. Be aware, however, that a single schema check only ever produces a single check result. See Expect one check result below for details.

The following example is a single check; Soda executes each of its validations during a scan. Note that unlike the nested list of column names in the example above, the nested key:value pairs that form the value for these validation keys are indented, but do not use a -.

Add a schema_name parameter to a schema check to address a situation in which you need to explicitly identify or override a dataset's schema in the data source.

Define schema evolution checks

✖️ Requires Soda Core Scientific (included in a Soda Agent) ✖️ Supported in Soda Core ✔️ Supported in Soda Library + Soda Cloud ✔️ Supported in Soda Cloud Agreements + Soda Agent ✔️ Available as a no-code check with a self-hosted Soda Agent connected to any Soda-supported data source, except Spark, and Dask and Pandas ✔️ Available as a no-code check with a Soda-hosted Agent connected to a BigQuery, Databricks SQL, MS SQL Server, MySQL, PostgreSQL, Redshift, or Snowflake data source

Rather than specifying exact parameters for column changes, you can use the when schema changes validation key to warn or fail when indistinct changes occur in a dataset.

Soda Cloud must have at least two measurements to yield a check result. In other words, the first time you run a scan to execute a schema evolution check, Soda returns no results because it has nothing against which to compare; the second scan that executes the check yields a check result.

The output in Soda Cloud displays the output of all the alert states during the scan.

Optional check configurations

Supported
Configuration
Documentation

Define a name for a schema check; see example.

Add an identity to a check.

Define alert configurations to specify warn and fail alert conditions; see example.

Apply an in-check filter to return results for a specific portion of the data in your dataset.

-

Use quotes when identifying dataset or column names; see example. Note that the type of quotes you use must match that which your data source uses. For example, BigQuery uses a backtick (`) as a quotation mark.

Use wildcard characters ( % or * ) in values in the check; see example.

See note in example below.

Use for each to apply schema checks to multiple datasets in one scan; see example.

Apply a dataset filter to partition data during a scan; see example.

Example with check name

Example with alert configuration

Example with quotes

Example with wildcards

You can use * or % as wildcard characters in a list of column names. If the column name begins with a wildcard character, add single quotes as per the example below.

Example with for each

Example with dataset filter

List of validation keys

Validation key
Values

when required column missing

one or more column names in an inline list of comma-separated values, or a nested list

when forbidden column present

one or more column names in an inline list of comma-separated values, or a nested list

when wrong column type

nested key:value pair to identify column:expected_data_type

when wrong column index

nested key:value pair to identify column:expected_position_in_dataset_index

when schema changes

any as an inline value column add as a nested list item column delete as a nested list item column index change as a nested list item column type change as a nested list item

Expect one check result

Be aware that a check that contains one or more alert configurations only ever yields a single check result; one check yields one check result. If your check triggers both a warn and a fail, the check result only displays the more severe, failed check result.

Using the following example, Soda Library, during a scan, discovers that the data in the dataset triggers both alerts, but the check result at the bottom is Oops! 1 failures. Nonetheless, the results in the Scan summary section of the CLI output still display both the warn and fail alerts as having been triggered.

Example: Detect PII

To address a common use case, you may wish to use a schema check in combination with a for each configuration and wildcard characters to automatically detect columns that contain personally identifiable information (PII) in your datasets, as in the following example.

Go further

Need help? Join the Soda community on Slack.

Last updated

Was this helpful?