|
1 |
| -{% macro athena__create_table_as(temporary, relation, sql) -%} |
| 1 | +{% macro athena__create_table_as(temporary, relation, sql, skip_partitioning=False) -%} |
2 | 2 | {%- set materialized = config.get('materialized', default='table') -%}
|
3 | 3 | {%- set external_location = config.get('external_location', default=none) -%}
|
4 |
| - {%- set partitioned_by = config.get('partitioned_by', default=none) -%} |
| 4 | + {%- set partitioned_by = config.get('partitioned_by', default=none) if not skip_partitioning else none -%} |
5 | 5 | {%- set bucketed_by = config.get('bucketed_by', default=none) -%}
|
6 | 6 | {%- set bucket_count = config.get('bucket_count', default=none) -%}
|
7 | 7 | {%- set field_delimiter = config.get('field_delimiter', default=none) -%}
|
|
90 | 90 |
|
91 | 91 | {% macro create_table_as_with_partitions(temporary, relation, sql) -%}
|
92 | 92 |
|
93 |
| - {% set partitions_batches = get_partition_batches(sql) %} |
| 93 | + {%- set tmp_relation = api.Relation.create( |
| 94 | + identifier=relation.identifier ~ '__tmp_not_partitioned', |
| 95 | + schema=relation.schema, |
| 96 | + database=relation.database, |
| 97 | + s3_path_table_part=relation.identifier ~ '__tmp_not_partitioned' , |
| 98 | + type='table' |
| 99 | + ) |
| 100 | + -%} |
| 101 | + |
| 102 | + {%- if tmp_relation is not none -%} |
| 103 | + {%- do drop_relation(tmp_relation) -%} |
| 104 | + {%- endif -%} |
| 105 | + |
| 106 | + {%- do log('CREATE NON-PARTIONED STAGING TABLE: ' ~ tmp_relation) -%} |
| 107 | + {%- do run_query(create_table_as(temporary, tmp_relation, sql, True)) -%} |
| 108 | + |
| 109 | + {% set partitions_batches = get_partition_batches(sql=tmp_relation, as_subquery=False) %} |
94 | 110 | {% do log('BATCHES TO PROCESS: ' ~ partitions_batches | length) %}
|
95 | 111 |
|
96 |
| - {%- do log('CREATE EMPTY TABLE: ' ~ relation) -%} |
97 |
| - {%- set create_empty_table_query -%} |
98 |
| - {{ create_table_as(temporary, relation, sql) }} |
99 |
| - limit 0 |
100 |
| - {%- endset -%} |
101 |
| - {%- do run_query(create_empty_table_query) -%} |
102 |
| - {%- set dest_columns = adapter.get_columns_in_relation(relation) -%} |
| 112 | + {%- set dest_columns = adapter.get_columns_in_relation(tmp_relation) -%} |
103 | 113 | {%- set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') -%}
|
104 | 114 |
|
105 | 115 | {%- for batch in partitions_batches -%}
|
106 | 116 | {%- do log('BATCH PROCESSING: ' ~ loop.index ~ ' OF ' ~ partitions_batches | length) -%}
|
107 | 117 |
|
108 |
| - {%- set insert_batch_partitions -%} |
109 |
| - insert into {{ relation }} ({{ dest_cols_csv }}) |
110 |
| - select {{ dest_cols_csv }} |
111 |
| - from ({{ sql }}) |
112 |
| - where {{ batch }} |
113 |
| - {%- endset -%} |
| 118 | + {%- if loop.index == 1 -%} |
| 119 | + {%- set create_target_relation_sql -%} |
| 120 | + select {{ dest_cols_csv }} |
| 121 | + from {{ tmp_relation }} |
| 122 | + where {{ batch }} |
| 123 | + {%- endset -%} |
| 124 | + {%- do run_query(create_table_as(temporary, relation, create_target_relation_sql)) -%} |
| 125 | + {%- else -%} |
| 126 | + {%- set insert_batch_partitions_sql -%} |
| 127 | + insert into {{ relation }} ({{ dest_cols_csv }}) |
| 128 | + select {{ dest_cols_csv }} |
| 129 | + from {{ tmp_relation }} |
| 130 | + where {{ batch }} |
| 131 | + {%- endset -%} |
| 132 | + |
| 133 | + {%- do run_query(insert_batch_partitions_sql) -%} |
| 134 | + {%- endif -%} |
| 135 | + |
114 | 136 |
|
115 |
| - {%- do run_query(insert_batch_partitions) -%} |
116 | 137 | {%- endfor -%}
|
117 | 138 |
|
| 139 | + {%- do drop_relation(tmp_relation) -%} |
| 140 | + |
118 | 141 | select 'SUCCESSFULLY CREATED TABLE {{ relation }}'
|
119 | 142 |
|
120 | 143 | {%- endmacro %}
|
121 | 144 |
|
122 | 145 | {% macro safe_create_table_as(temporary, relation, sql) -%}
|
123 |
| - {%- set query_result = adapter.run_query_with_partitions_limit_catching(create_table_as(temporary, relation, sql)) -%} |
124 |
| - {%- do log('QUERY RESULT: ' ~ query_result) -%} |
125 |
| - {%- if query_result == 'TOO_MANY_OPEN_PARTITIONS' -%} |
126 |
| - {%- do create_table_as_with_partitions(temporary, relation, sql) -%} |
127 |
| - {%- set query_result = relation ~ ' with many partitions created' -%} |
| 146 | + {%- if temporary -%} |
| 147 | + {%- do run_query(create_table_as(temporary, relation, sql, True)) -%} |
| 148 | + {%- set query_result = relation ~ ' as temporary relation without partitioning created' -%} |
| 149 | + {%- else -%} |
| 150 | + {%- set query_result = adapter.run_query_with_partitions_limit_catching(create_table_as(temporary, relation, sql)) -%} |
| 151 | + {%- do log('QUERY RESULT: ' ~ query_result) -%} |
| 152 | + {%- if query_result == 'TOO_MANY_OPEN_PARTITIONS' -%} |
| 153 | + {%- do create_table_as_with_partitions(temporary, relation, sql) -%} |
| 154 | + {%- set query_result = relation ~ ' with many partitions created' -%} |
| 155 | + {%- endif -%} |
128 | 156 | {%- endif -%}
|
| 157 | + |
129 | 158 | {{ return(query_result) }}
|
130 | 159 | {%- endmacro %}
|
0 commit comments