dbt hub

dbt test: expect_table_columns_to_match_ordered_list

USE CASE

Schema

APPLIES TO

Model
Seed
Source

This page details the expect_table_columns_to_match_ordered_list test from the dbt-expectations package. This test ensures that the columns in a table or view exactly match a specified list in a given order. It is instrumental in maintaining consistency in data structure across different environments or versions of a dataset.

How it Works

The expect_table_columns_to_match_ordered_list test verifies that the structure of a database table, view, or model aligns precisely with an expected list of column names in the defined order. This is critical for ensuring that data transformations and migrations adhere to specified schemas and that the data integrity is maintained through various stages of data processing.

Steps and Conditions:

  1. Column List Definition: Define the exact order and names of columns expected in the table using the column_list property.
  2. Optional Transformation: Apply any optional transformations to the column names, like converting them to uppercase with the transform: upper configuration, to ensure the comparison aligns with external requirements or naming conventions.
  3. Verification: The test compares the current column structure of the table, model, or view against the expected list, considering any transformations specified.
  4. Outcome:
    • Pass: If the columns in the table match exactly with the ordered list provided in the test configuration, the test passes.
    • Fail: If there is a discrepancy in either the number of columns, their naming, or their order, the test fails.

Example Usage: Fintech

For a Fintech company, accurately tracking and managing financial transactions in a table called daily_transactions is crucial. Ensuring the columns are consistent across database updates or model deployments is essential for maintaining accurate reporting and compliance with regulatory standards.

Here’s an example configuration for a daily_transactions table in a Fintech environment:


models:
  - name: daily_transactions
    tests:
      - dbt_expectations.expect_table_columns_to_match_ordered_list:
          column_list: ["transaction_id", "user_id", "amount", "transaction_date", "status"]
          transform: lower

In this setup, the expect_table_columns_to_match_ordered_list test checks that the daily_transactions table includes exactly these columns: transaction_id, user_id, amount, transaction_date, status, and in this specific order, with all column names in lowercase. This ensures that every aspect of the transaction data is consistently captured and processed in accordance with the expected financial data governance standards.

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