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 #656 #658

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% macro create_table_as(temporary, relation, compiled_code, language='sql', skip_partitioning=false) -%}
{{ adapter.dispatch('create_table_as', 'athena')(temporary, relation, compiled_code, language, skip_partitioning) }}
{% macro create_table_as(temporary, relation, compiled_code, language='sql', skip_partitioning=false, with_no_data=false) -%}
{{ adapter.dispatch('create_table_as', 'athena')(temporary, relation, compiled_code, language, skip_partitioning, with_no_data) }}
{%- endmacro %}


{% macro athena__create_table_as(temporary, relation, compiled_code, language='sql', skip_partitioning=false) -%}
{% macro athena__create_table_as(temporary, relation, compiled_code, language='sql', skip_partitioning=false, with_no_data=false) -%}
{%- set materialized = config.get('materialized', default='table') -%}
{%- set external_location = config.get('external_location', default=none) -%}
{%- do log("Skip partitioning: " ~ skip_partitioning) -%}
Expand Down Expand Up @@ -142,7 +142,15 @@
{% endif %}
)
as
{{ compiled_code }}
SELECT *
FROM (
{{ compiled_code }}
)

{% if with_no_data %}
WITH NO DATA;
{% endif %}

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

Expand Down Expand Up @@ -170,27 +178,24 @@
{%- set dest_columns = adapter.get_columns_in_relation(tmp_relation) -%}
{%- set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') -%}

{%- set create_target_relation_sql -%}
select {{ dest_cols_csv }}
from {{ tmp_relation }}
where {{ batch }}
{%- endset -%}
{%- do run_query(create_table_as(temporary, relation, create_target_relation_sql, language, with_no_data=true)) -%}

{%- for batch in partitions_batches -%}
{%- do log('BATCH PROCESSING: ' ~ loop.index ~ ' OF ' ~ partitions_batches | length) -%}

{%- if loop.index == 1 -%}
{%- set create_target_relation_sql -%}
select {{ dest_cols_csv }}
from {{ tmp_relation }}
where {{ batch }}
{%- endset -%}
{%- do run_query(create_table_as(temporary, relation, create_target_relation_sql, language)) -%}
{%- else -%}
{%- set insert_batch_partitions_sql -%}
insert into {{ relation }} ({{ dest_cols_csv }})
select {{ dest_cols_csv }}
from {{ tmp_relation }}
where {{ batch }}
{%- endset -%}

{%- do run_query(insert_batch_partitions_sql) -%}
{%- endif -%}
{%- set insert_batch_partitions_sql -%}
insert into {{ relation }} ({{ dest_cols_csv }})
select {{ dest_cols_csv }}
from {{ tmp_relation }}
where {{ batch }}
{%- endset -%}

{%- do run_query(insert_batch_partitions_sql) -%}

{%- endfor -%}

Expand Down
Loading