Skip to content

feat: take a contracted dictionary's structure from its YAML columns - #767

Open
bim-ba wants to merge 3 commits into
ClickHouse:mainfrom
bim-ba:feat/dictionary-fields-from-columns
Open

bim-ba wants to merge 3 commits into
ClickHouse:mainfrom
bim-ba:feat/dictionary-fields-from-columns

Conversation

@bim-ba

@bim-ba bim-ba commented Oct 26, 2026 •

Copy link
Copy Markdown

Summary

Closes #765. Builds on #766 (column comments on dictionaries): until that merges, this diff also shows its commit.

A dictionary model declares its structure twice: in the fields config, and in its YAML columns, where the same names and types are documented and tested. Nothing compares the two, so they drift apart without an error. This PR lets the YAML be the single source when the model has an enforced contract, as it already is for a table:

  • With contract: {enforced: true} and no fields, the dictionary structure is the model's YAML columns and their data_type, in their order. fields works as before and takes precedence when set.
  • The contract is enforced for real: whenever the contract is enforced and the dictionary reads the model's query (the default clickhouse source without table), with or without fields, get_assert_columns_equivalent(sql) compares that query with the YAML columns, as in the table, view and incremental materializations, and fails on a missing, extra or differently typed column. A dictionary that reads a table or an http source has no query to compare, so nothing is checked there.
  • Behaviour change: a dictionary that already has an enforced contract (for example from a project-wide +contract) was never checked; it is now, so a YAML that does not list every query column with its exact type fails.
  • A YAML column without data_type fails with dbt's ColumnTypeMissingError (exceptions.column_type_missing). Without the check the DDL would get name None.
  • With neither fields nor a contract, the model fails with a compile error that names both options, instead of 'NoneType' object is not iterable.
{{ config(materialized='dictionary', primary_key='id', layout='HASHED()', lifetime='MIN 0 MAX 300') }}

select id, name from {{ source('raw', 'people') }}
models:
  - name: people_dict
    config:
      contract:
        enforced: true
    columns:
      - name: id
        data_type: UInt64
      - name: name
        data_type: String

The dbt Core 2.0 ClickHouse adapter (#660; dictionary parity #706) would need the same behaviour.

Tested: pytest tests -k "dictionary or persist_docs or PersistDocs" against a single-node ClickHouse in Docker (25.8, 26.3 and 26.10.2.103), HTTP driver: 17 passed, 1 skipped (TestHttpDictionary needs DBT_CH_TEST_INCLUDE_S3); the new tests fail on the parent commit, and the table-source test fails when the table/http guard is removed. make lint passes. Not run: the Distributed* and replicated-database tests, which need a cluster.

Checklist

  • Unit and integration tests covering the common scenarios were added. Unit tests (macro harness): the structure taken from the YAML in order, the error without a contract, ColumnTypeMissingError for a column without data_type. Integration tests: a contracted dictionary built from its YAML columns; a query whose type differs from the YAML failing the contract; a table-source dictionary that is not compared with the placeholder query; a model with neither fields nor a contract failing with the new message. The seven new tests fail on the parent commit and pass here.
  • A changelog entry was added following Updating the Changelog (user-facing, concise, PR link)
  • For significant changes, documentation in docs was updated with further explanations or tutorials

Note

Medium Risk
Enforced-contract dictionaries backed by model SQL are newly validated against the query; previously unchecked projects may fail on the next run. Behavior change is intentional governance, not runtime data risk.

Overview
Dictionary models with an enforced contract can now omit the fields config: structure is derived from YAML columns and their data_type (in order), matching table behavior. Setting fields still wins when present.

When the contract is enforced and the source is the model SQL (default ClickHouse source, not table or http), the adapter runs get_assert_columns_equivalent on the query vs YAML—so existing contracted dictionaries may start failing if columns drift. Without fields or a contract, compile fails with a clear message instead of a NoneType iteration error; YAML columns missing data_type raise column_type_missing.

persist_docs on dictionary materializations now passes for_columns=false, so relation docs apply but column comments are skipped (ClickHouse rejects column comments on dictionaries).

Reviewed by Cursor Bugbot for commit c25ff73. Bugbot is set up for automated code reviews on this repo. Configure here.

ClickHouse rejects a column comment on a dictionary (Code 48,
NOT_IMPLEMENTED; seen on 25.8, 26.3 and 26.10), and the adapter sends
every comment in one ALTER, so persist_docs with columns: true failed
the whole dictionary model. The dictionary materialization now persists
only the relation's comment.
A dictionary model has to list its structure in the `fields` config,
next to the YAML columns that document the same names and types, and
the two lists drift apart. With an enforced contract and no `fields`,
the structure is now the model's YAML columns and their data_type, in
their order, as a table's DDL is. An enforced contract is now also
checked for a dictionary that reads the model's query, with or without
`fields`, so a contracted dictionary whose YAML columns do not match
its query fails, as a table does. A YAML column without data_type fails
with dbt's own error; a model with neither `fields` nor a contract
fails with a message that says so instead of "'NoneType' object is not
iterable".
@CLAassistant

CLAassistant commented Oct 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Flipmonster Flipmonster reopened this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Take a dictionary model's structure from its YAML columns

3 participants