From 5c3da0fda44347c10c53a078aa6aee3e434545e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Mu=C3=B1oz?= Date: Thu, 12 Nov 2026 18:44:51 +0100 Subject: [PATCH 1/5] persist_docs` now warns about columns documented in the schema yml that do not exist in the relation --- CHANGELOG.md | 1 + .../clickhouse/macros/persist_docs.sql | 1 + pyproject.toml | 2 +- .../adapter/persist_docs/fixtures.py | 16 ------ .../adapter/persist_docs/test_persist_docs.py | 54 +++++++------------ tests/unit/test_persist_docs_macro.py | 45 ++++++++++++++++ 6 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 tests/unit/test_persist_docs_macro.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5144ae65e..70cdde533 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Improvements * `dbt-core` is now capped at `<2.0`, and `dbt-common` and `agate` are declared as direct dependencies, matching dbt-labs adapters ([#776](https://github.com/ClickHouse/dbt-clickhouse/pull/776)). +* `persist_docs` now warns about columns documented in the schema yml that do not exist in the relation ([#XXX](https://github.com/ClickHouse/dbt-clickhouse/pull/XXX)). #### Repository maintenance * The shard-local table name of distributed materializations is now resolved in one place, `clickhouse_local_relation`, instead of being rebuilt inline in each materialization and incremental strategy ([#753](https://github.com/ClickHouse/dbt-clickhouse/pull/753)). diff --git a/dbt/include/clickhouse/macros/persist_docs.sql b/dbt/include/clickhouse/macros/persist_docs.sql index 53885825e..0d4904ddd 100644 --- a/dbt/include/clickhouse/macros/persist_docs.sql +++ b/dbt/include/clickhouse/macros/persist_docs.sql @@ -20,6 +20,7 @@ {%- if for_columns and config.persist_column_docs() and model.columns -%} {% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %} + {% do validate_doc_columns(relation, model.columns, existing_columns) %} {% for column_name in model.columns if (column_name in existing_columns) %} {%- set comment = model.columns[column_name]['description'] -%} {%- if comment %} diff --git a/pyproject.toml b/pyproject.toml index 1c838de59..eaef258fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ classifiers = [ ] requires-python = ">=3.10" dependencies = [ - "dbt-adapters>=1.22.0,<1.25.0", # This version should be dbt-adapters<2.0, but keeping it fixed for now to avoid unexpected issues. We need to frequently update it. + "dbt-adapters>=1.22.10,<1.25.0", # This version should be dbt-adapters<2.0, but keeping it fixed for now to avoid unexpected issues. We need to frequently update it. # installed via dbt-adapters but used directly "dbt-common>=1.36,<2.0", "agate>=1.0,<2.0", diff --git a/tests/integration/adapter/persist_docs/fixtures.py b/tests/integration/adapter/persist_docs/fixtures.py index f7179bb1a..23d120a19 100644 --- a/tests/integration/adapter/persist_docs/fixtures.py +++ b/tests/integration/adapter/persist_docs/fixtures.py @@ -26,11 +26,6 @@ """ -_MODELS__MISSING_COLUMN = """ -{{ config(materialized='table') }} -select 1 as id, 'Ed' as name -""" - _MODELS__MODEL_USING_QUOTE_UTIL = """ select 1 as {{ adapter.quote("2id") }} """ @@ -46,17 +41,6 @@ quote: true """ -_PROPERITES__SCHEMA_MISSING_COL = """ -version: 2 -models: - - name: missing_column - columns: - - name: id - description: "test id column description" - - name: column_that_does_not_exist - description: "comment that cannot be created" -""" - _PROPERTIES__SCHEMA_YML = """ version: 2 diff --git a/tests/integration/adapter/persist_docs/test_persist_docs.py b/tests/integration/adapter/persist_docs/test_persist_docs.py index 58c1ae78c..51c966788 100644 --- a/tests/integration/adapter/persist_docs/test_persist_docs.py +++ b/tests/integration/adapter/persist_docs/test_persist_docs.py @@ -2,15 +2,19 @@ import os import pytest +from dbt.tests.adapter.persist_docs.test_persist_docs import ( + BasePersistDocsAllColumnsMissing, + BasePersistDocsColumnMissing, + BasePersistDocsQuotedColumnCaseSensitive, + BasePersistDocsQuotedDescriptionNotAppliedOnMismatch, +) from dbt.tests.util import run_dbt from fixtures import ( _DOCS__MY_FUN_DOCS, - _MODELS__MISSING_COLUMN, _MODELS__MODEL_USING_QUOTE_UTIL, _MODELS__NO_DOCS_MODEL, _MODELS__TABLE, _MODELS__VIEW, - _PROPERITES__SCHEMA_MISSING_COL, _PROPERTIES__QUOTE_MODEL, _PROPERTIES__SCHEMA_YML, _SEEDS__SEED, @@ -119,38 +123,6 @@ def test_has_comments_pg_like(self): self._assert_has_view_comments(no_docs_node, False, False) -class BasePersistDocsColumnMissing(BasePersistDocsBase): - @pytest.fixture(scope="class") - def project_config_update(self): - return { - "models": { - "test": { - "+persist_docs": { - "columns": True, - }, - } - } - } - - @pytest.fixture(scope="class") - def models(self): - return {"missing_column.sql": _MODELS__MISSING_COLUMN} - - @pytest.fixture(scope="class") - def properties(self): - return {"schema.yml": _PROPERITES__SCHEMA_MISSING_COL} - - def test_missing_column(self, project): - run_dbt(["docs", "generate"]) - with open("target/catalog.json") as fp: - catalog_data = json.load(fp) - assert "nodes" in catalog_data - - table_node = catalog_data["nodes"]["model.test.missing_column"] - table_id_comment = table_node["columns"]["id"]["comment"] - assert table_id_comment.startswith("test id column description") - - class BasePersistDocsCommentOnQuotedColumn: """Covers edge case where column with comment must be quoted. We set this using the `quote:` tag in the property file.""" @@ -213,5 +185,19 @@ class TestPersistDocsColumnMissing(BasePersistDocsColumnMissing): pass +class TestPersistDocsAllColumnsMissing(BasePersistDocsAllColumnsMissing): + pass + + +class TestPersistDocsQuotedColumnCaseSensitive(BasePersistDocsQuotedColumnCaseSensitive): + pass + + +class TestPersistDocsQuotedDescriptionNotAppliedOnMismatch( + BasePersistDocsQuotedDescriptionNotAppliedOnMismatch +): + pass + + class TestPersistDocsCommentOnQuotedColumn(BasePersistDocsCommentOnQuotedColumn): pass diff --git a/tests/unit/test_persist_docs_macro.py b/tests/unit/test_persist_docs_macro.py new file mode 100644 index 000000000..0db00f827 --- /dev/null +++ b/tests/unit/test_persist_docs_macro.py @@ -0,0 +1,45 @@ +from types import SimpleNamespace + +from tests.unit.macro_harness import SandboxSafeMock + + +def test_persist_docs_validates_documented_columns_and_dollar_quotes_comments(macros): + adapter = SandboxSafeMock() + adapter.get_clickhouse_cluster_name.return_value = None + adapter.get_columns_in_relation.return_value = [SimpleNamespace(name='id')] + config = SandboxSafeMock() + config.persist_relation_docs.return_value = True + config.persist_column_docs.return_value = True + validate_doc_columns = SandboxSafeMock() + run_query = SandboxSafeMock() + relation = '`my_db`.`my_table`' + model = { + 'description': "Table description; with 'quotes'", + 'columns': { + 'id': {'description': 'id description; semicolon'}, + 'column_that_does_not_exist': {'description': 'documented but absent'}, + }, + } + + macros.call( + 'clickhouse__persist_docs', + relation, + model, + True, + True, + context={ + 'adapter': adapter, + 'config': config, + 'validate_doc_columns': validate_doc_columns, + 'run_query': run_query, + }, + ) + + validate_doc_columns.assert_called_once_with(relation, model['columns'], ['id']) + run_query.assert_called_once() + sql = ' '.join(run_query.call_args.args[0].split()) + assert sql == ( + 'alter table `my_db`.`my_table` modify comment ' + "$dbt_comment_literal_block$Table description; with 'quotes'$dbt_comment_literal_block$, " + 'comment column `id` $dbt_comment_literal_block$id description; semicolon$dbt_comment_literal_block$' + ) From ab8c4d3e3b6af3decccb7a54edfa483fbed67069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Mu=C3=B1oz?= Date: Thu, 12 Nov 2026 19:03:30 +0100 Subject: [PATCH 2/5] Use just default tests for Persist docs as now they all work without changes --- .../adapter/persist_docs/fixtures.py | 121 ------------ .../adapter/persist_docs/test_persist_docs.py | 173 +----------------- 2 files changed, 2 insertions(+), 292 deletions(-) delete mode 100644 tests/integration/adapter/persist_docs/fixtures.py diff --git a/tests/integration/adapter/persist_docs/fixtures.py b/tests/integration/adapter/persist_docs/fixtures.py deleted file mode 100644 index 23d120a19..000000000 --- a/tests/integration/adapter/persist_docs/fixtures.py +++ /dev/null @@ -1,121 +0,0 @@ -_MODELS__VIEW = """ -{{ config(materialized='view') }} -select 2 as id, 'Bob' as name -""" - -_MODELS__NO_DOCS_MODEL = """ -select 1 as id, 'Alice' as name -""" - -_DOCS__MY_FUN_DOCS = """ -{% docs my_fun_doc %} -name Column description "with double quotes" -and with 'single quotes' as welll as other; -'''abc123''' -reserved -- characters --- -/* comment */ -Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - -{% enddocs %} -""" - -_MODELS__TABLE = """ -{{ config(materialized='table') }} -select 1 as id, 'Joe' as name -""" - - -_MODELS__MODEL_USING_QUOTE_UTIL = """ -select 1 as {{ adapter.quote("2id") }} -""" - -_PROPERTIES__QUOTE_MODEL = """ -version: 2 -models: - - name: quote_model - description: "model to test column quotes and comments" - columns: - - name: 2id - description: "XXX My description" - quote: true -""" - -_PROPERTIES__SCHEMA_YML = """ -version: 2 - -models: - - name: table_model - description: | - Table model description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - columns: - - name: id - description: | - id Column description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - - name: name - description: | - Some stuff here and then a call to - {{ doc('my_fun_doc')}} - - name: view_model - description: | - View model description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - columns: - - name: id - description: | - id Column description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - -seeds: - - name: seed - description: | - Seed model description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - columns: - - name: id - description: | - id Column description "with double quotes" - and with 'single quotes' as welll as other; - '''abc123''' - reserved -- characters - -- - /* comment */ - Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting - - name: name - description: | - Some stuff here and then a call to - {{ doc('my_fun_doc')}} -""" - - -_SEEDS__SEED = """id,name -1,Alice -2,Bob -""" diff --git a/tests/integration/adapter/persist_docs/test_persist_docs.py b/tests/integration/adapter/persist_docs/test_persist_docs.py index 51c966788..4f192e6b9 100644 --- a/tests/integration/adapter/persist_docs/test_persist_docs.py +++ b/tests/integration/adapter/persist_docs/test_persist_docs.py @@ -1,180 +1,11 @@ -import json -import os - -import pytest from dbt.tests.adapter.persist_docs.test_persist_docs import ( + BasePersistDocs, BasePersistDocsAllColumnsMissing, BasePersistDocsColumnMissing, + BasePersistDocsCommentOnQuotedColumn, BasePersistDocsQuotedColumnCaseSensitive, BasePersistDocsQuotedDescriptionNotAppliedOnMismatch, ) -from dbt.tests.util import run_dbt -from fixtures import ( - _DOCS__MY_FUN_DOCS, - _MODELS__MODEL_USING_QUOTE_UTIL, - _MODELS__NO_DOCS_MODEL, - _MODELS__TABLE, - _MODELS__VIEW, - _PROPERTIES__QUOTE_MODEL, - _PROPERTIES__SCHEMA_YML, - _SEEDS__SEED, -) - - -class BasePersistDocsBase: - @pytest.fixture(scope="class", autouse=True) - def setUp(self, project): - run_dbt(["seed"]) - run_dbt() - - @pytest.fixture(scope="class") - def seeds(self): - return {"seed.csv": _SEEDS__SEED} - - @pytest.fixture(scope="class") - def models(self): - return { - "no_docs_model.sql": _MODELS__NO_DOCS_MODEL, - "table_model.sql": _MODELS__TABLE, - "view_model.sql": _MODELS__VIEW, - } - - @pytest.fixture(scope="class") - def properties(self): - return { - "my_fun_docs.md": _DOCS__MY_FUN_DOCS, - "schema.yml": _PROPERTIES__SCHEMA_YML, - } - - def _assert_common_comments(self, *comments): - for comment in comments: - assert '"with double quotes"' in comment - assert """'''abc123'''""" in comment - assert "\n" in comment - assert "Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting" in comment - assert "/* comment */" in comment - if os.name == "nt": - assert "--\r\n" in comment or "--\n" in comment - else: - assert "--\n" in comment - - def _assert_has_table_comments(self, table_node): - table_comment = table_node["metadata"]["comment"] - assert table_comment.startswith("Table model description") - - table_id_comment = table_node["columns"]["id"]["comment"] - assert table_id_comment.startswith("id Column description") - - table_name_comment = table_node["columns"]["name"]["comment"] - assert table_name_comment.startswith("Some stuff here and then a call to") - - self._assert_common_comments(table_comment, table_id_comment, table_name_comment) - - def _assert_has_view_comments( - self, view_node, has_node_comments=True, has_column_comments=True - ): - view_comment = view_node["metadata"]["comment"] - if has_node_comments: - assert view_comment.startswith("View model description") - self._assert_common_comments(view_comment) - else: - assert view_comment is None - - view_id_comment = view_node["columns"]["id"]["comment"] - if has_column_comments: - assert view_id_comment.startswith("id Column description") - self._assert_common_comments(view_id_comment) - else: - assert view_id_comment is None - - view_name_comment = view_node["columns"]["name"]["comment"] - assert view_name_comment is None - - -class BasePersistDocs(BasePersistDocsBase): - @pytest.fixture(scope="class") - def project_config_update(self): - return { - "models": { - "test": { - "+persist_docs": { - "relation": True, - "columns": True, - }, - } - } - } - - def test_has_comments_pg_like(self): - if os.environ.get('DBT_CH_TEST_CLOUD', '').lower() in ('1', 'true', 'yes'): - pytest.skip('Not running comment test for cloud') - run_dbt(["docs", "generate"]) - with open("target/catalog.json") as fp: - catalog_data = json.load(fp) - assert "nodes" in catalog_data - assert len(catalog_data["nodes"]) == 4 - table_node = catalog_data["nodes"]["model.test.table_model"] - self._assert_has_table_comments(table_node) - - view_node = catalog_data["nodes"]["model.test.view_model"] - self._assert_has_view_comments(view_node) - - no_docs_node = catalog_data["nodes"]["model.test.no_docs_model"] - self._assert_has_view_comments(no_docs_node, False, False) - - -class BasePersistDocsCommentOnQuotedColumn: - """Covers edge case where column with comment must be quoted. - We set this using the `quote:` tag in the property file.""" - - @pytest.fixture(scope="class") - def models(self): - return {"quote_model.sql": _MODELS__MODEL_USING_QUOTE_UTIL} - - @pytest.fixture(scope="class") - def properties(self): - return {"properties.yml": _PROPERTIES__QUOTE_MODEL} - - @pytest.fixture(scope="class") - def project_config_update(self): - return { - "models": { - "test": { - "materialized": "table", - "+persist_docs": { - "relation": True, - "columns": True, - }, - } - } - } - - @pytest.fixture(scope="class") - def run_has_comments(self, project): - if os.environ.get('DBT_CH_TEST_CLOUD', '').lower() in ('1', 'true', 'yes'): - pytest.skip('Not running comment test for cloud') - - def fixt(): - run_dbt() - run_dbt(["docs", "generate"]) - with open("target/catalog.json") as fp: - catalog_data = json.load(fp) - assert "nodes" in catalog_data - assert len(catalog_data["nodes"]) == 1 - column_node = catalog_data["nodes"]["model.test.quote_model"] - column_comment = column_node["columns"]["2id"]["comment"] - assert column_comment.startswith("XXX") - - return fixt - - def test_quoted_column_comments(self, run_has_comments): - run_has_comments() - - -""" -Code from tests is a copy from dbt-core/tests/adapter/dbt/tests/adapter/persist_docs -It can be removed after upgrading to 1.5.0 -""" class TestPersistDocs(BasePersistDocs): From 0a49759dc4262465aa70078f657728d115277166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Mu=C3=B1oz?= Date: Thu, 12 Nov 2026 19:06:06 +0100 Subject: [PATCH 3/5] unit tests not needed anymore --- tests/unit/test_persist_docs_macro.py | 45 --------------------------- 1 file changed, 45 deletions(-) delete mode 100644 tests/unit/test_persist_docs_macro.py diff --git a/tests/unit/test_persist_docs_macro.py b/tests/unit/test_persist_docs_macro.py deleted file mode 100644 index 0db00f827..000000000 --- a/tests/unit/test_persist_docs_macro.py +++ /dev/null @@ -1,45 +0,0 @@ -from types import SimpleNamespace - -from tests.unit.macro_harness import SandboxSafeMock - - -def test_persist_docs_validates_documented_columns_and_dollar_quotes_comments(macros): - adapter = SandboxSafeMock() - adapter.get_clickhouse_cluster_name.return_value = None - adapter.get_columns_in_relation.return_value = [SimpleNamespace(name='id')] - config = SandboxSafeMock() - config.persist_relation_docs.return_value = True - config.persist_column_docs.return_value = True - validate_doc_columns = SandboxSafeMock() - run_query = SandboxSafeMock() - relation = '`my_db`.`my_table`' - model = { - 'description': "Table description; with 'quotes'", - 'columns': { - 'id': {'description': 'id description; semicolon'}, - 'column_that_does_not_exist': {'description': 'documented but absent'}, - }, - } - - macros.call( - 'clickhouse__persist_docs', - relation, - model, - True, - True, - context={ - 'adapter': adapter, - 'config': config, - 'validate_doc_columns': validate_doc_columns, - 'run_query': run_query, - }, - ) - - validate_doc_columns.assert_called_once_with(relation, model['columns'], ['id']) - run_query.assert_called_once() - sql = ' '.join(run_query.call_args.args[0].split()) - assert sql == ( - 'alter table `my_db`.`my_table` modify comment ' - "$dbt_comment_literal_block$Table description; with 'quotes'$dbt_comment_literal_block$, " - 'comment column `id` $dbt_comment_literal_block$id description; semicolon$dbt_comment_literal_block$' - ) From 1b1445f37ca5a9cf2e9de2fd52ecc11b0c4fe019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Mu=C3=B1oz?= Date: Thu, 12 Nov 2026 19:07:00 +0100 Subject: [PATCH 4/5] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70cdde533..3d2918d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Improvements * `dbt-core` is now capped at `<2.0`, and `dbt-common` and `agate` are declared as direct dependencies, matching dbt-labs adapters ([#776](https://github.com/ClickHouse/dbt-clickhouse/pull/776)). -* `persist_docs` now warns about columns documented in the schema yml that do not exist in the relation ([#XXX](https://github.com/ClickHouse/dbt-clickhouse/pull/XXX)). +* `persist_docs` now warns about columns documented in the schema yml that do not exist in the relation ([#798](https://github.com/ClickHouse/dbt-clickhouse/pull/798)). #### Repository maintenance * The shard-local table name of distributed materializations is now resolved in one place, `clickhouse_local_relation`, instead of being rebuilt inline in each materialization and incremental strategy ([#753](https://github.com/ClickHouse/dbt-clickhouse/pull/753)). From 2a50004403a5a539c4c0047a9ad79df6328ccecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Mu=C3=B1oz?= Date: Thu, 12 Nov 2026 19:36:49 +0100 Subject: [PATCH 5/5] Increase minimum version of dbt-tests-adapter --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index eaef258fd..7512992c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,7 @@ Slack = "https://clickhouse.com/slack" [dependency-groups] dev = [ "dbt-core==1.12.*", - "dbt-tests-adapter>=1.10,<2.0", + "dbt-tests-adapter>=1.20,<2.0", "pytest>=7.2.0", "pytest-dotenv==0.5.2", "pytest-timeout==2.4.0",