forked from ClickHouse/dbt-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_table.sql
39 lines (31 loc) · 1.75 KB
/
remote_table.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{% materialization remote_table, adapter='clickhouse', supported_languages=['python', 'sql'] -%}
{%- set remote_config = config.require('remote_config') -%}
{%- set remote_cluster = remote_config.get('cluster') -%}
{%- set remote_schema = remote_config.get('local_db_prefix') + this.schema -%}
{%- set remote_identifier = this.identifier + remote_config.get('local_suffix') -%}
{%- set target_relation = this.incorporate(type='table') -%}
{%- set remote_relation = target_relation.incorporate(path={"identifier": remote_identifier, "schema": remote_schema}, remote_cluster=remote_cluster) -%}
{%- set existing_relation = load_cached_relation(this) -%}
{{ run_hooks(pre_hooks, inside_transaction=False) }}
{{ run_hooks(pre_hooks, inside_transaction=True) }}
{%- set column_changes = none -%}
{%- if existing_relation -%}
{%- set column_changes = adapter.check_incremental_schema_changes('ignore', existing_relation, sql) -%}
{%- endif -%}
{% call statement('main') %}
{%- if column_changes or existing_relation is none or should_full_refresh() -%}
{{ create_distributed_table(target_relation, remote_relation, sql) }}
{%- else -%}
{{ log("no-op run: distributed table exists with correct schema.", info=true) }}
select true;
{%- endif -%}
{% endcall %}
{% set should_revoke = should_revoke(target_relation, full_refresh_mode) %}
{% set grant_config = config.get('grants') %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
{% do persist_docs(target_relation, model) %}
{{ run_hooks(post_hooks, inside_transaction=True) }}
{{ adapter.commit() }}
{{ run_hooks(post_hooks, inside_transaction=False) }}
{{ return({'relations': [target_relation]}) }}
{% endmaterialization %}