Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Dictionaries should be created on cluster when cluster is set #307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Unreleased
#### Bug Fix
- ClickHouse dictionaries are now correctly created "on cluster" when a cluster is defined.

### Release [1.7.6], 2024-04-12
#### Bug Fix
- A bug in (experimental) Distributed Table model creation could lead to errors when there was a change in the model definition (see, e.g.,
Expand Down
5 changes: 4 additions & 1 deletion dbt/adapters/clickhouse/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def get_on_cluster(
cls: Type[Self], cluster: str = '', materialized: str = '', engine: str = ''
) -> bool:
if cluster.strip():
return 'view' == materialized or 'distributed' in materialized or 'Replicated' in engine
return materialized in ('view', 'dictionary') or \
'distributed' in materialized or \
'Replicated' in engine

else:
return False

Expand Down
15 changes: 8 additions & 7 deletions dbt/include/clickhouse/macros/materializations/dictionary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
{%- set backup_relation_type = 'dictionary' if existing_relation is none else existing_relation.type -%}
{%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%}
{%- set existing_backup_relation = load_cached_relation(backup_relation) -%}
{%- set cluster_clause = on_cluster_clause(target_relation) -%}

{%- set grant_config = config.get('grants') -%}

{{ run_hooks(pre_hooks, inside_transaction=False) }}

{{ drop_dictionary_if_exists(existing_backup_relation) }}
{{ drop_dictionary_if_exists(existing_intermediate_relation) }}
{{ drop_dictionary_if_exists(existing_backup_relation, cluster_clause) }}
{{ drop_dictionary_if_exists(existing_intermediate_relation, cluster_clause) }}


{{ run_hooks(pre_hooks, inside_transaction=True) }}

{# create our new dictionary #}
{% call statement('main') -%}
{{ clickhouse__get_create_dictionary_as_sql(intermediate_relation, sql) }}
{{ clickhouse__get_create_dictionary_as_sql(intermediate_relation, cluster_clause, sql) }}
{%- endcall %}

{# cleanup #}
Expand Down Expand Up @@ -50,11 +51,11 @@
{%- endmaterialization -%}


{% macro clickhouse__get_create_dictionary_as_sql(relation, sql) %}
{% macro clickhouse__get_create_dictionary_as_sql(relation, cluster_clause, sql) %}
{%- set fields = config.get('fields') -%}
{%- set source_type = config.get('source_type') -%}

CREATE DICTIONARY {{ relation }} {{ on_cluster_clause(relation) }}
CREATE DICTIONARY {{ relation }} {{ cluster_clause }}
(
{%- for (name, data_type) in fields -%}
{{ name }} {{ data_type }}{%- if not loop.last -%},{%- endif -%}
Expand Down Expand Up @@ -100,13 +101,13 @@
{% endmacro %}


{% macro drop_dictionary_if_exists(relation) %}
{% macro drop_dictionary_if_exists(relation, cluster_clause) %}
{% if relation.type != 'dictionary' %}
{{ log(relation ~ ' is not a dictionary; defaulting to drop_relation_if_exists') }}
{{ drop_relation_if_exists(relation) }}
{% else %}
{% call statement('drop_dictionary_if_exists') %}
drop dictionary if exists {{ relation }}
drop dictionary if exists {{ relation }} {{ cluster_clause }}
{% endcall %}
{% endif %}
{% endmacro %}
Loading