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

[feature-wip](dbt) exchange table temp to target table atomically #21931

Merged
merged 3 commits into from
Jul 19, 2023
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
34 changes: 29 additions & 5 deletions extension/dbt-doris/dbt/include/doris/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,31 @@
{% endcall %}
{% endif %}

{%- endmacro %}
{%- endmacro %}


{% macro exchange_relation(relation1, relation2, is_drop_r1=false) -%}

{% if relation2.is_view %}
{% set from_results = run_query('show create view ' + relation1.render() ) %}
{% set to_results = run_query('show create view ' + relation2.render() ) %}
{% call statement('exchange_view_relation') %}
alter view {{ relation1 }} as {{ to_results[0]['Create View'].split('AS',1)[1] }}
{% endcall %}
{% if is_drop_r1 %}
{% do doris__drop_relation(relation2) %}
{% else %}
{% call statement('exchange_view_relation') %}
alter view {{ relation2 }} as {{ from_results[0]['Create View'].split('AS',1)[1] }}
{% endcall %}
{% endif %}
{% else %}
{% call statement('exchange_relation') %}
ALTER TABLE {{ relation1 }} REPLACE WITH TABLE `{{ relation2.table }}` PROPERTIES('swap' = '{{not is_drop_r1}}');
{% endcall %}
{% endif %}

{%- endmacro %}

{% macro doris__timestimp_id() -%}
{{ return( (modules.datetime.datetime.now() ~ "").replace('-','').replace(':','').replace('.','').replace(' ','') ) }}
Expand All @@ -166,16 +190,16 @@
WITH LABEL dbt_doris_label_{{doris__timestimp_id()}}
{% else %}
WITH LABEL dbt_doris_label_{{ lable_suffix_id }}
{% endif %}
{% endif %}
{%- endmacro %}

{% macro doris__get_or_create_relation(database, schema, identifier, type) %}
{%- set target_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}

{% if target_relation %}
{% do return([true, target_relation]) %}
{% endif %}

{%- set new_relation = api.Relation.create(
database=none,
schema=schema,
Expand All @@ -186,5 +210,5 @@
{% endmacro %}

{% macro catalog_source(catalog,database,table) -%}
`{{catalog}}`.`{{database}}`.`{{table}}`
`{{catalog}}`.`{{database}}`.`{{table}}`
{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@
{#-- append or no unique key --#}


{% if unique_key is none or strategy == 'append' %}

{% if not unique_key or strategy == 'append' %}
{#-- create table first --#}
{% if existing_relation is none %}
{% set build_sql = doris__create_table_as(False, target_relation, sql) %}
{% elif existing_relation.is_view or full_refresh_mode %}
{#-- backup data before drop old table #}
{#-- backup table is new table ,exchange table backup and old table #}
{% set backup_identifier = existing_relation.identifier ~ "__dbt_backup" %}
{% set backup_relation = existing_relation.incorporate(path={"identifier": backup_identifier}) %}
{% do adapter.drop_relation(backup_relation) %} {#-- likes 'drop table if exists ... ' --#}
{% do adapter.rename_relation(target_relation, backup_relation) %}
{% set build_sql = doris__create_table_as(False, target_relation, sql) %}
{% do to_drop.append(backup_relation) %}
{% set run_sql = doris__create_table_as(False, backup_relation, sql) %}
{% call statement("run_sql") %}
{{ run_sql }}
{% endcall %}
{% do exchange_relation(target_relation, backup_relation, True) %}
{% set build_sql = "show frontends" %}
{#-- append data --#}
{% else %}
{% do to_drop.append(tmp_relation) %}
{% do run_query(create_table_as(True, tmp_relation, sql)) %}
{% set build_sql = tmp_insert(tmp_relation, target_relation, unique_key=none) %}
{% endif %}
Expand All @@ -52,13 +57,16 @@
{% set build_sql = doris__create_unique_table_as(False, target_relation, sql) %}
{#-- insert data refresh --#}
{% elif existing_relation.is_view or full_refresh_mode %}
{#-- backup data before drop old table #}
{#-- backup table is new table ,exchange table backup and old table #}
{% set backup_identifier = existing_relation.identifier ~ "__dbt_backup" %}
{% set backup_relation = existing_relation.incorporate(path={"identifier": backup_identifier}) %}
{% do adapter.drop_relation(backup_relation) %} {#-- likes 'drop table if exists ... ' --#}
{% do adapter.rename_relation(target_relation, backup_relation) %}
{% set build_sql = doris__create_unique_table_as(False, target_relation, sql) %}
{% do to_drop.append(backup_relation) %}
{% set run_sql = doris__create_unique_table_as(False, backup_relation, sql) %}
{% call statement("run_sql") %}
{{ run_sql }}
{% endcall %}
{% do exchange_relation(target_relation, backup_relation, True) %}
{% set build_sql = "show frontends" %}
{#-- append data --#}
{% else %}
{#-- check doris unique table --#}
Expand Down