22import os
33
44import pytest
5- from dbt .tests .util import run_dbt
5+ from dbt .tests .util import run_dbt , relation_from_name
6+
7+ NUM_CLUSTER_NODES = 3
68
79ref_models__table_comment_sql = """
810{{
1820
1921"""
2022
23+ ref_models__replicated_table_comment_sql = """
24+ {{
25+ config(
26+ materialized = "table",
27+ persist_docs = {"relation": true, "columns": true},
28+ engine="ReplicatedMergeTree('/clickhouse/tables/{uuid}/one-shard', '{replica}' )"
29+ )
30+ }}
31+
32+ select
33+ 'foo' as first_name,
34+ 'bar' as second_name
35+
36+ """
37+
2138ref_models__view_comment_sql = """
2239{{
2340 config(
4360 description: "XXX first description"
4461 - name: second_name
4562 description: "XXX second description"
63+ - name: replicated_table_comment
64+ description: "YYY table"
65+ columns:
66+ - name: first_name
67+ description: "XXX first description"
68+ - name: second_name
69+ description: "XXX second description"
4670 - name: view_comment
4771 description: "YYY view"
4872 columns:
@@ -59,12 +83,13 @@ def models(self):
5983 return {
6084 "schema.yml" : ref_models__schema_yml ,
6185 "table_comment.sql" : ref_models__table_comment_sql ,
86+ "replicated_table_comment.sql" : ref_models__replicated_table_comment_sql ,
6287 "view_comment.sql" : ref_models__view_comment_sql ,
6388 }
6489
6590 @pytest .mark .parametrize (
6691 'model_name' ,
67- [' table_comment' , ' view_comment' ],
92+ [" table_comment" , "replicated_table_comment" , " view_comment" ],
6893 )
6994 def test_comment (self , project , model_name ):
7095 if os .environ .get ('DBT_CH_TEST_CLOUD' , '' ).lower () in ('1' , 'true' , 'yes' ):
@@ -81,3 +106,99 @@ def test_comment(self, project, model_name):
81106 assert column_comment .startswith ("XXX" )
82107
83108 assert column_node ['metadata' ]['comment' ].startswith ("YYY" )
109+
110+ # Ensure comment is propoagated to all replicas on cluster
111+ #cluster = project.test_config['cluster']
112+
113+ # local_relation = relation_from_name(project.adapter, model_name)
114+ # if cluster:
115+ # ensure_column_comments_consistent_across_replicas(project, cluster, local_relation)
116+ # ensure_table_comment_on_cluster(project, cluster, local_relation)
117+
118+
119+
120+ # def ensure_table_comment_on_cluster(project, cluster, local_relation):
121+ # """Ensure all replicas have same comment for given relation"""
122+ # # Returns 'ok' if exactly one distinct comment exists across all replicas for this table; otherwise 'mismatch'.
123+ # sql = f"""
124+ # SELECT
125+ # if(COUNT(DISTINCT comment) = 1, 'ok', 'mismatch') AS status
126+ # FROM clusterAllReplicas('{cluster}', system.tables)
127+ # WHERE database = currentDatabase()
128+ # AND name = '{local_relation.identifier}'
129+ # """
130+ # result = project.run_sql(sql, fetch="one")
131+ # assert result[0] == "ok"
132+
133+ # sql = f"""
134+ # SELECT
135+ # hostname(),
136+ # comment
137+ # FROM clusterAllReplicas('{cluster}', system.tables)
138+ # WHERE `table` = '{local_relation.identifier}'
139+ # """
140+
141+ # result = project.run_sql(sql, fetch="all")
142+
143+ # for _table_row in result:
144+ # assert _table_row[-1].startswith("YYY")
145+
146+ # def ensure_column_comments_consistent_across_replicas(project, cluster, local_relation):
147+ # # This query groups by column name and checks that each has exactly one distinct comment across replicas.
148+ # check_sql = f"""
149+ # SELECT
150+ # name AS column_name,
151+ # COUNT(DISTINCT comment) AS distinct_comment_count,
152+ # groupArray((hostName(), comment)) AS per_replica_comments
153+ # FROM clusterAllReplicas('{cluster}', system.columns)
154+ # WHERE database = currentDatabase()
155+ # AND table = '{local_relation.identifier}'
156+ # GROUP BY column_name
157+ # """
158+ # rows = project.run_sql(check_sql, fetch="all")
159+
160+ # mismatches = [r for r in rows if r[1] != 1]
161+ # if mismatches:
162+ # print("Column comment mismatches:", mismatches)
163+
164+ # assert not mismatches
165+
166+ # sql = f"""
167+ # SELECT
168+ # name,
169+ # groupArray(hostname()) as hosts,
170+ # groupUniqArray(comment) as comments,
171+ # length(comments) as num_comments
172+ # FROM clusterAllReplicas('{cluster}', system.columns)
173+ # WHERE table = '{local_relation.identifier}'
174+ # GROUP BY name
175+ # """
176+
177+ # result = project.run_sql(sql, fetch="all")
178+
179+ # print("WOW"*100)
180+ # print("\n\n")
181+ # print(result)
182+ # print("WOW"*100)
183+
184+ # for _col in result:
185+ # assert _col[-1] == 1
186+
187+ # assert result == []
188+
189+
190+ # sql = f"""
191+ # SELECT
192+ # name,
193+ # count(hostname())
194+ # FROM clusterAllReplicas('{cluster}', system.columns)
195+ # WHERE table = '{local_relation.identifier}'
196+ # GROUP BY name
197+ # """
198+ # result = project.run_sql(sql, fetch="all")
199+ # assert result[-1] == NUM_CLUSTER_NODES
200+
201+ # assert result == []
202+
203+
204+
0 commit comments