dbt hub

dbt test: expect_column_values_to_be_of_type

USE CASE

Data type
Schema

APPLIES TO

Column

This page introduces the expect_column_values_to_be_of_type test from the dbt-expectations package, which verifies that the data type of values in a specified column matches the expected data type. This test is vital for ensuring that data adheres to the schema requirements and for preventing errors in data processing that can arise from type mismatches.

How it Works

The expect_column_values_to_be_of_type test checks that every entry within a selected column adheres to a designated data type. Ensuring the correct type within a column is crucial for the integrity and reliability of downstream processes such as calculations, reporting, and data integration.

Steps and Conditions:

  1. Column and Data Type Specification: The specific column to be tested is identified, and the expected data type is defined, such as integer, text, boolean, etc.
  2. Data Type Validation: Each entry in the targeted column is checked to confirm that it matches the specified data type.
  3. Outcome:
    • Pass: If all values in the column conform to the designated data type, the test passes, validating the column’s data type adherence.
    • Fail: If any value in the column does not match the expected data type, the test fails, indicating a potential issue with data integrity or processing errors that need resolution.

Example Usage: Fintech

For a Fintech company managing financial transactions, maintaining the correct data type for each column in their databases is crucial to ensuring accurate data processing. Columns with monetary values, dates, or unique identifiers must have their data types strictly enforced to prevent calculation errors, misreporting, or system crashes.

Consider a scenario where a transactions table includes a transaction_date column that is expected to hold values only of the date type because dates are often involved in calculating durations or determining the sequencing of transactions.


models:
  - name: transactions
    columns:
      - name: transaction_date
        tests:
          - dbt_expectations.expect_column_values_to_be_of_type:
              column_type: date

In this example, applying the expect_column_values_to_be_of_type test ensures that the transaction_date column contains only date entries, preventing errors from operations such as sorting transactions chronologically or generating monthly financial reports.

The only data observability platform built into your dbt code

  • Get monitors on your production tables out-of-the-box with zero configuration
  • Add tests to your code in bulk with a simple UI
  • Track test results over time
  • Set owners and create meaningful alerts
  • Triage incidents faster using our end-to-end column-level lineage graph