Skip to content

Commit 702d02f

Browse files
authored
Merge pull request #64 from ClickHouse/custom_settings
Allow adding SETTINGS section through the model
2 parents c2a6fe3 + 338ce88 commit 702d02f

File tree

8 files changed

+30
-8
lines changed

8 files changed

+30
-8
lines changed

Diff for: dbt/adapters/clickhouse/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '1.1.0.1'
1+
version = '1.1.0.2'

Diff for: dbt/adapters/clickhouse/connections.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import uuid
33
from contextlib import contextmanager
44
from dataclasses import dataclass
5-
from typing import Any, Optional, Tuple
5+
from typing import Any, Dict, Optional, Tuple
66

77
import agate
88
import clickhouse_connect
@@ -40,6 +40,7 @@ class ClickhouseCredentials(Credentials):
4040
use_default_schema: bool = (
4141
False # This is used in tests to make sure we connect always to the default database.
4242
)
43+
custom_settings: Optional[Dict[str, Any]] = None
4344

4445
@property
4546
def type(self):
@@ -103,6 +104,9 @@ def open(cls, connection):
103104
credentials = cls.get_credentials(connection.credentials)
104105

105106
try:
107+
custom_settings = (
108+
dict() if credentials.custom_settings is None else credentials.custom_settings
109+
)
106110
handle = clickhouse_connect.get_client(
107111
host=credentials.host,
108112
port=credentials.port,
@@ -116,6 +120,7 @@ def open(cls, connection):
116120
verify=credentials.verify,
117121
query_limit=0,
118122
session_id='dbt::' + str(uuid.uuid4()),
123+
**custom_settings,
119124
)
120125
connection.handle = handle
121126
connection.state = 'open'

Diff for: dbt/adapters/clickhouse/impl.py

+8
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ def run_sql_for_tests(self, sql, fetch, conn):
253253
finally:
254254
conn.state = 'close'
255255

256+
@available
257+
def get_model_settings(self, model):
258+
settings = model['config'].get('settings', dict())
259+
res = []
260+
for key in settings:
261+
res.append(f' {key}={settings[key]}')
262+
return '' if len(res) == 0 else 'SETTINGS ' + ', '.join(res) + '\n'
263+
256264

257265
def _expect_row_value(key: str, row: agate.Row):
258266
if key not in row.keys():

Diff for: dbt/include/clickhouse/macros/adapters.sql

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
as (
8787
{{ sql }}
8888
)
89+
{{ adapter.get_model_settings(model) }}
8990
{%- endmacro %}
9091

9192
{% macro clickhouse__create_view_as(relation, sql) -%}

Diff for: dbt/include/clickhouse/macros/materializations/incremental.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
{% else %}
5858
{% set old_relation = existing_relation.incorporate(path={"identifier": old_identifier}) %}
5959
-- Create a table with only updated rows.
60-
{% do run_query(create_table_as(False, tmp_relation, sql)) %}
60+
{% call statement('creat_temp_table_statement') %}
61+
{{ create_table_as(False, tmp_relation, sql) }}
62+
{% endcall %}
6163
{% if on_schema_change != 'ignore' %}
6264
-- Update schema types if necessary.
6365
{% do adapter.expand_target_column_types(
@@ -116,6 +118,7 @@
116118

117119
{% macro clickhouse__incremental_create(old_relation, target_relation) %}
118120
create table {{ target_relation }} as {{ old_relation }}
121+
119122
{%- endmacro %}
120123

121124
{% macro clickhouse__incremental_cur_insert(old_relation, tmp_relation, target_relation, unique_key=none) %}
@@ -129,6 +132,7 @@
129132
select ({{ unique_key }})
130133
from {{ tmp_relation }}
131134
)
135+
{{ adapter.get_model_settings(model) }}
132136
{%- endmacro %}
133137

134138
{% macro clickhouse__incremental_insert_from_table(tmp_relation, target_relation) %}
@@ -138,6 +142,7 @@
138142
insert into {{ target_relation }} ({{ dest_cols_csv }})
139143
select {{ dest_cols_csv }}
140144
from {{ tmp_relation }}
145+
{{ adapter.get_model_settings(model) }}
141146
{%- endmacro %}
142147

143148
{% macro clickhouse__incremental_insert(target_relation, sql) %}
@@ -146,6 +151,7 @@
146151

147152
insert into {{ target_relation }} ({{ dest_cols_csv }})
148153
{{ sql }}
154+
{{ adapter.get_model_settings(model) }}
149155
{%- endmacro %}
150156

151157
{% macro incremental_validate_on_schema_change(on_schema_change, default='ignore') %}

Diff for: dbt/include/clickhouse/macros/materializations/seed.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
{% set data_sql = adapter.get_csv_data(agate_table) %}
44

55
{% set sql -%}
6-
insert into {{ this.render() }} ({{ cols_sql }}) format CSV
7-
{{ data_sql }}
6+
insert into {{ this.render() }} ({{ cols_sql }})
7+
{{ adapter.get_model_settings(model) }}
8+
format CSV
9+
{{ data_sql }}
810
{%- endset %}
911

1012
{% do adapter.add_query(sql, bindings=agate_table, abridge_sql_log=True) %}

Diff for: dev_requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dbt-core==1.1.0
2-
clickhouse-connect>=0.1.2
2+
clickhouse-connect>=0.1.4
33
pytest==7.0.0
44
pytest-dotenv==0.5.2
55
dbt-tests-adapter==1.1

Diff for: setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def _dbt_clickhouse_version():
5353
]
5454
},
5555
install_requires=[
56-
f'dbt-core=={dbt_version}',
57-
'clickhouse-connect>=0.1.2',
56+
f'dbt-core~={dbt_version}',
57+
'clickhouse-connect>=0.1.4',
5858
],
5959
python_requires=">=3.7",
6060
platforms='any',

0 commit comments

Comments
 (0)