Skip to content

Commit 4cbdc5b

Browse files
authored
Dbt 1 3 (#109)
* Clean up some datatypes, add datatypes tests * Update array macros, add array and concurrency tests * Minor tweaks * Update test actions * Force case sensitive relation searches
1 parent b0f33ae commit 4cbdc5b

File tree

22 files changed

+407
-135
lines changed

22 files changed

+407
-135
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on: # yamllint disable-line rule:truthy
77
branches-ignore:
88
- '*_test'
99
- '*_dev'
10+
- '*_cloud'
1011

1112
jobs:
1213
lint:

.github/workflows/test_cloud.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
uses: actions/checkout@v3
2323

2424
- name: Setup Python 3.10
25-
uses: actions/setup-python@v2
25+
uses: actions/setup-python@v4
2626
with:
2727
python-version: '3.10'
2828

.github/workflows/test_matrix.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
clickhouse/clickhouse-server:${{ matrix.clickhouse-version }}
4747

4848
- name: Setup Python ${{ matrix.python-version }}
49-
uses: actions/setup-python@v2
49+
uses: actions/setup-python@v4
5050
with:
5151
python-version: ${{ matrix.python-version }}
5252

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
### Release [1.3.0], 2022-10-30
2+
#### Improvement
3+
- Support dbt [1.3.0] https://github.com/ClickHouse/dbt-clickhouse/issues/105
4+
- Adds additional dbt 1.3.0 core tests
5+
- Adds array utility macros ported from dbt-utils
6+
- Does NOT add support for Python models (not implemented)
7+
- Does NOT utilize default/standard incremental materialization macros (standard strategies do not work in ClickHouse)
8+
9+
#### Bug Fix
10+
- Require exact match for relations. ClickHouse databases and tables are all case sensitive, so all searches are now case sensitive. Closes https://github.com/ClickHouse/dbt-clickhouse/issues/100 and https://github.com/ClickHouse/dbt-clickhouse/issues/110
11+
12+
</br></br>
13+
114
### Release [1.2.1], 2022-09-19
215
#### Improvements
316
- Support dbt 1.2.1 https://github.com/ClickHouse/dbt-clickhouse/issues/79
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '1.2.2'
1+
version = '1.3.0'

dbt/adapters/clickhouse/column.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class ClickHouseColumn(Column):
1313
TYPE_LABELS = {
1414
'STRING': 'String',
1515
'TIMESTAMP': 'DateTime',
16-
'FLOAT': 'Float64',
17-
'INTEGER': 'Int64',
16+
'FLOAT': 'Float32',
17+
'INTEGER': 'Int32',
1818
}
1919
is_nullable: bool = False
2020
_brackets_regex = re.compile(r'^(Nullable|LowCardinality)\((.*)\)$')
@@ -109,7 +109,7 @@ def string_type(cls, size: int) -> str:
109109

110110
@classmethod
111111
def numeric_type(cls, dtype: str, precision: Any, scale: Any) -> str:
112-
return dtype
112+
return f'Decimal({precision}, {scale})'
113113

114114
def literal(self, value):
115115
return f'to{self.dtype}({value})'

dbt/adapters/clickhouse/impl.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import io
33
from concurrent.futures import Future
44
from dataclasses import dataclass
5-
from typing import Callable, List, Optional, Set, Union
5+
from typing import Callable, Dict, List, Optional, Set, Union
66

77
import agate
88
import dbt.exceptions
@@ -14,7 +14,7 @@
1414
from dbt.contracts.graph.manifest import Manifest
1515
from dbt.contracts.relation import RelationType
1616
from dbt.events import AdapterLogger
17-
from dbt.utils import executor
17+
from dbt.utils import executor, filter_null_values
1818

1919
from dbt.adapters.clickhouse.column import ClickHouseColumn
2020
from dbt.adapters.clickhouse.connections import ClickHouseConnectionManager
@@ -49,6 +49,7 @@ def convert_text_type(cls, agate_table: agate.Table, col_idx: int) -> str:
4949
@classmethod
5050
def convert_number_type(cls, agate_table: agate.Table, col_idx: int) -> str:
5151
decimals = agate_table.aggregate(agate.MaxPrecision(col_idx))
52+
# We match these type to the Column.TYPE_LABELS for consistency
5253
return 'Float32' if decimals else 'Int32'
5354

5455
@classmethod
@@ -112,6 +113,14 @@ def drop_schema(self, relation: BaseRelation) -> None:
112113
if conn:
113114
conn.handle.database_dropped(relation.schema)
114115

116+
def _make_match_kwargs(self, database: str, schema: str, identifier: str) -> Dict[str, str]:
117+
return filter_null_values(
118+
{
119+
"database": database,
120+
"identifier": identifier,
121+
}
122+
)
123+
115124
def list_relations_without_caching(
116125
self, schema_relation: ClickHouseRelation
117126
) -> List[ClickHouseRelation]:
@@ -170,7 +179,6 @@ def parse_clickhouse_columns(
170179

171180
def get_columns_in_relation(self, relation: ClickHouseRelation) -> List[ClickHouseColumn]:
172181
rows: List[agate.Row] = super().get_columns_in_relation(relation)
173-
174182
return self.parse_clickhouse_columns(relation, rows)
175183

176184
def get_catalog(self, manifest):
@@ -220,6 +228,7 @@ def get_rows_different_sql(
220228
relation_a: ClickHouseRelation,
221229
relation_b: ClickHouseRelation,
222230
column_names: Optional[List[str]] = None,
231+
except_operator: str = None,
223232
) -> str:
224233
names: List[str]
225234
if column_names is None:

dbt/adapters/clickhouse/relation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
from typing import Optional
23

34
import dbt.exceptions
45
from dbt.adapters.base.relation import BaseRelation, Policy
@@ -38,3 +39,15 @@ def render(self):
3839
'include, but only one can be set'
3940
)
4041
return super().render()
42+
43+
def matches(
44+
self,
45+
database: Optional[str] = None,
46+
schema: Optional[str] = None,
47+
identifier: Optional[str] = None,
48+
):
49+
if schema:
50+
raise dbt.exceptions.RuntimeException(
51+
f'Passed unexpected schema value {schema} to Relation.matches'
52+
)
53+
return self.database == database and self.identifier == identifier
Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,3 @@
1-
{% macro engine_clause(label) %}
2-
{%- set engine = config.get('engine', validator=validation.any[basestring]) -%}
3-
{%- if engine is not none %}
4-
{{ label }} = {{ engine }}
5-
{%- else %}
6-
{{ label }} = MergeTree()
7-
{%- endif %}
8-
{%- endmacro -%}
9-
10-
{% macro partition_cols(label) %}
11-
{%- set cols = config.get('partition_by', validator=validation.any[list, basestring]) -%}
12-
{%- if cols is not none %}
13-
{%- if cols is string -%}
14-
{%- set cols = [cols] -%}
15-
{%- endif -%}
16-
{{ label }} (
17-
{%- for item in cols -%}
18-
{{ item }}
19-
{%- if not loop.last -%},{%- endif -%}
20-
{%- endfor -%}
21-
)
22-
{%- endif %}
23-
{%- endmacro -%}
24-
25-
{% macro primary_key_clause(label) %}
26-
{%- set primary_key = config.get('primary_key', validator=validation.any[basestring]) -%}
27-
28-
{%- if primary_key is not none %}
29-
{{ label }} {{ primary_key }}
30-
{%- endif %}
31-
{%- endmacro -%}
32-
33-
{% macro order_cols(label) %}
34-
{%- set cols = config.get('order_by', validator=validation.any[list, basestring]) -%}
35-
{%- set engine = config.get('engine', validator=validation.any[basestring]) -%}
36-
{%- set supported = [
37-
'HDFS',
38-
'MaterializedPostgreSQL',
39-
'S3',
40-
'EmbeddedRocksDB',
41-
'Hive'
42-
] -%}
43-
44-
{%- if engine is none or 'MergeTree' in engine or engine in supported %}
45-
{%- if cols is not none %}
46-
{%- if cols is string -%}
47-
{%- set cols = [cols] -%}
48-
{%- endif -%}
49-
{{ label }} (
50-
{%- for item in cols -%}
51-
{{ item }}
52-
{%- if not loop.last -%},{%- endif -%}
53-
{%- endfor -%}
54-
)
55-
{%- else %}
56-
{{ label }} (tuple())
57-
{%- endif %}
58-
{%- endif %}
59-
{%- endmacro -%}
60-
61-
{% macro on_cluster_clause(label) %}
62-
{% set on_cluster = adapter.get_clickhouse_cluster_name() %}
63-
{%- if on_cluster is not none %}
64-
{{ label }} {{ on_cluster }}
65-
{%- endif %}
66-
{%- endmacro -%}
67-
68-
{% macro clickhouse__create_table_as(temporary, relation, sql) -%}
69-
{% set create_table = create_table_or_empty(temporary, relation, sql) %}
70-
{% if adapter.is_before_version('22.7.1') -%}
71-
{{ create_table }}
72-
{%- else %}
73-
{% call statement('create_table_empty') %}
74-
{{ create_table }}
75-
{% endcall %}
76-
{{ clickhouse__insert_into(relation.include(database=False), sql) }}
77-
{%- endif %}
78-
{%- endmacro %}
79-
80-
{% macro create_table_or_empty(temporary, relation, sql) -%}
81-
{%- set sql_header = config.get('sql_header', none) -%}
82-
83-
{{ sql_header if sql_header is not none }}
84-
85-
{% if temporary -%}
86-
create temporary table {{ relation.name }}
87-
engine Memory
88-
{{ order_cols(label="order by") }}
89-
{{ partition_cols(label="partition by") }}
90-
{{ adapter.get_model_settings(model) }}
91-
{%- else %}
92-
create table {{ relation.include(database=False) }}
93-
{{ on_cluster_clause(label="on cluster") }}
94-
{{ engine_clause(label="engine") }}
95-
{{ order_cols(label="order by") }}
96-
{{ primary_key_clause(label="primary key") }}
97-
{{ partition_cols(label="partition by") }}
98-
{{ adapter.get_model_settings(model) }}
99-
{% if not adapter.is_before_version('22.7.1') -%}
100-
empty
101-
{%- endif %}
102-
{%- endif %}
103-
as (
104-
{{ sql }}
105-
)
106-
{%- endmacro %}
107-
1081
{% macro clickhouse__create_view_as(relation, sql) -%}
1092
{%- set sql_header = config.get('sql_header', none) -%}
1103

@@ -221,16 +114,9 @@
221114
{% endcall %}
222115
{% endmacro %}
223116

224-
{% macro clickhouse__insert_into(target_relation, sql) %}
225-
{%- set dest_columns = adapter.get_columns_in_relation(target_relation) -%}
226-
{%- set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') -%}
227-
228-
insert into {{ target_relation }} ({{ dest_cols_csv }})
229-
{{ sql }}
230-
{%- endmacro %}
231-
232117
{% macro exchange_tables_atomic(old_relation, target_relation) %}
233118
{%- call statement('exchange_tables_atomic') -%}
234119
EXCHANGE TABLES {{ old_relation }} AND {{ target_relation }}
235120
{% endcall %}
236121
{% endmacro %}
122+

dbt/include/clickhouse/macros/materializations/incremental.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
-- First create a temporary table with all of the new data
5151
{% set new_data_relation = existing_relation.incorporate(path={"identifier": model['name'] + '__dbt_new_data'}) %}
52+
{{ drop_relation_if_exists(new_data_relation) }}
5253
{% call statement('create_new_data_temp') %}
5354
{{ get_create_table_as_sql(False, new_data_relation, sql) }}
5455
{% endcall %}

0 commit comments

Comments
 (0)