Skip to content

Commit f23c8de

Browse files
committed
Implement TTL column definition
1 parent 8897ecb commit f23c8de

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

dbt/adapters/clickhouse/impl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,13 @@ def get_credentials(self, connection_overrides) -> Dict:
541541
return credentials
542542

543543
@classmethod
544-
def render_raw_columns_constraints(cls, raw_columns: Dict[str, Dict[str, Any]]) -> List:
544+
def render_raw_columns_constraints(cls, raw_columns: Dict[str, Dict[str, Any]]) -> List[str]:
545545
rendered_columns = []
546546
for v in raw_columns.values():
547547
codec = f"CODEC({_codec})" if (_codec := v.get('codec')) else ""
548+
ttl = f"TTL {_ttl}" if (_ttl := v.get('ttl')) else ""
548549
rendered_columns.append(
549-
f"{quote_identifier(v['name'])} {v['data_type']} {codec}".rstrip()
550+
f"{quote_identifier(v['name'])} {v['data_type']} {codec} {ttl}".rstrip()
550551
)
551552
if v.get("constraints"):
552553
warn_or_error(ConstraintNotSupported(constraint='column', adapter='clickhouse'))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
column_ttl_model_yml = """
2+
version: 2
3+
models:
4+
- name: column_ttl_model
5+
config:
6+
contract:
7+
enforced: true
8+
columns:
9+
- name: datetime_col
10+
data_type: DateTime
11+
- name: id
12+
data_type: UInt32
13+
ttl: toStartOfDay(datetime_col) + INTERVAL 1 DAY
14+
"""
15+
16+
17+
column_ttl_model_sql = """
18+
{{
19+
config(
20+
materialized = "table"
21+
)
22+
}}
23+
24+
select
25+
now() as datetime_col,
26+
1::UInt32 as id
27+
"""
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
from dbt.tests.util import get_manifest, run_dbt
3+
from fixtures_column_ttl import (
4+
column_ttl_model_yml,
5+
column_ttl_model_sql
6+
)
7+
8+
9+
class TestTtlColumn:
10+
@pytest.fixture(scope="class")
11+
def models(self):
12+
return {
13+
"column_ttl_model.yml": column_ttl_model_yml,
14+
"column_ttl_model.sql": column_ttl_model_sql
15+
}
16+
17+
def test_ttl_column(self, project):
18+
run_dbt(["run", "-s", "column_ttl_model"], expect_pass=True)
19+
manifest = get_manifest(project.project_root)
20+
model_id = "model.test.column_ttl_model"
21+
id_column_config = manifest.nodes[model_id].columns.get("id")
22+
assert id_column_config._extra.get("ttl") == "toStartOfDay(datetime_col) + INTERVAL 1 DAY"
23+
24+

0 commit comments

Comments
 (0)