dbt hub

dbt test: expect_table_column_count_to_be_between

USE CASE

Schema

APPLIES TO

Model
Seed
Source

This documentation explains the expect_table_column_count_to_be_between test from the dbt-expectations package, which verifies that the number of columns in a specified model falls within an expected range. This test is essential for ensuring that the table's structure conforms to specific design requirements or data integrity standards.

How it Works

The expect_table_column_count_to_be_between test assesses whether the number of columns in a table, view, or source is within a predefined minimum and maximum range. This range helps in enforcing structural consistency across different environments or versions of database schemas.

Steps and Conditions:

  1. Model Selection: Choose the model, seed, or source to which the test will be applied.
  2. Set Ranges: Define the min_value and max_value for the acceptable range of column counts. If no minimum is set, the default is 0 (i.e., no columns are acceptable). If no maximum is specified, there is no upper limit.
  3. Execution: The test counts the total number of columns in the selected model and checks if this number falls within the set boundaries.
  4. Outcome:
    • Pass: The number of columns is within the specified range, indicating the table’s structure aligns with expectations.
    • Fail: The number of columns is outside the specified range, suggesting either an excess or deficit in expected columns, which may require adjustments or review.

Example Usage: E-commerce

An E-commerce platform might require strict validation on the number of columns in critical data tables to ensure that all necessary data attributes are accounted for without redundancy. Suppose there is a model called product_listings designed to have specific attributes like product_id, name, price, and stock_level.


models:
  - name: product_listings
    tests:
      - dbt_expectations.expect_table_column_count_to_be_between:
          min_value: 4
          max_value: 5

In this example, the expect_table_column_count_to_be_between test is applied to the product_listings model to ensure that the table only contains 4 or 5 columns, corresponding to the essential attributes plus any optional column that might have been added for a specific feature, like discount eligibility or seasonal categorization. This test helps in maintaining a lean and effective database without unnecessary columns that could clutter and complicate data management.

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