Skip to content

Commit 5991fc4

Browse files
authored
Upgrade dbt-core to 1.0.1 (#52)
* Use the dbt Events Module * Upgrade dbt-core to 1.0.0 * Add a profile_template.yml file * Reorganize macros following dbt-labs/dbt-core#4154 * Use MANIFEST.in instead of package_data see: https://stackoverflow.com/questions/7522250/how-to-include-package-data-with-setuptools-distutils * Do not quote seed columns see dbt-labs/dbt-core@64fc3a3 * Upgrade to 1.0.1
1 parent 9baa80c commit 5991fc4

23 files changed

+218
-210
lines changed

Diff for: MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-include dbt/include *.sql *.yml

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# dbt-athena
22

3-
* Supports dbt version `0.21.0`
3+
* Supports dbt version `1.0.*`
44
* Supports [Seeds][seeds]
55
* Correctly detects views and their columns
66
* Support [incremental models][incremental]

Diff for: dbt/adapters/athena/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "0.21.0"
1+
version = "1.0.1"

Diff for: dbt/adapters/athena/connections.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
from dbt.contracts.connection import Connection, AdapterResponse
2121
from dbt.adapters.sql import SQLConnectionManager
2222
from dbt.exceptions import RuntimeException, FailedToConnectException
23-
from dbt.logger import GLOBAL_LOGGER as logger
23+
from dbt.events import AdapterLogger
24+
2425
import tenacity
2526
from tenacity.retry import retry_if_exception
2627
from tenacity.stop import stop_after_attempt
2728
from tenacity.wait import wait_exponential
2829

30+
logger = AdapterLogger("Athena")
31+
2932

3033
@dataclass
3134
class AthenaCredentials(Credentials):

Diff for: dbt/adapters/athena/impl.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import re
44
import boto3
55
from botocore.exceptions import ClientError
6+
from typing import Optional
67

78
from dbt.adapters.base import available
89
from dbt.adapters.sql import SQLAdapter
910
from dbt.adapters.athena import AthenaConnectionManager
1011
from dbt.adapters.athena.relation import AthenaRelation
11-
from dbt.logger import GLOBAL_LOGGER as logger
12+
from dbt.events import AdapterLogger
13+
logger = AdapterLogger("Athena")
1214

1315
class AthenaAdapter(SQLAdapter):
1416
ConnectionManager = AthenaConnectionManager
@@ -97,3 +99,8 @@ def clean_up_table(
9799
s3_bucket = s3_resource.Bucket(bucket_name)
98100
s3_bucket.objects.filter(Prefix=prefix).delete()
99101

102+
@available
103+
def quote_seed_column(
104+
self, column: str, quote_config: Optional[bool]
105+
) -> str:
106+
return super().quote_seed_column(column, False)

Diff for: dbt/include/athena/macros/adapters.sql

-125
This file was deleted.

Diff for: dbt/include/athena/macros/adapters/columns.sql

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{% macro athena__get_columns_in_relation(relation) -%}
2+
{% call statement('get_columns_in_relation', fetch_result=True) %}
3+
4+
select
5+
column_name,
6+
data_type,
7+
null as character_maximum_length,
8+
null as numeric_precision,
9+
null as numeric_scale
10+
11+
from {{ relation.information_schema('columns') }}
12+
where LOWER(table_name) = LOWER('{{ relation.identifier }}')
13+
{% if relation.schema %}
14+
and LOWER(table_schema) = LOWER('{{ relation.schema }}')
15+
{% endif %}
16+
order by ordinal_position
17+
18+
{% endcall %}
19+
20+
{% set table = load_result('get_columns_in_relation').table %}
21+
{% do return(sql_convert_columns_in_relation(table)) %}
22+
{% endmacro %}

Diff for: dbt/include/athena/macros/adapters/freshness.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% macro athena__current_timestamp() -%}
2+
-- pyathena converts time zoned timestamps to strings so lets avoid them
3+
-- now()
4+
cast(now() as timestamp)
5+
{%- endmacro %}

Diff for: dbt/include/athena/macros/catalog.sql renamed to dbt/include/athena/macros/adapters/metadata.sql

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{% macro athena__get_catalog(information_schema, schemas) -%}
32
{%- set query -%}
43
select * from (
@@ -77,3 +76,45 @@
7776
{{ return(run_query(query)) }}
7877

7978
{%- endmacro %}
79+
80+
81+
{% macro athena__list_schemas(database) -%}
82+
{% call statement('list_schemas', fetch_result=True) %}
83+
select
84+
distinct schema_name
85+
86+
from {{ information_schema_name(database) }}.schemata
87+
{% endcall %}
88+
{{ return(load_result('list_schemas').table) }}
89+
{% endmacro %}
90+
91+
92+
{% macro athena__list_relations_without_caching(schema_relation) %}
93+
{% call statement('list_relations_without_caching', fetch_result=True) -%}
94+
WITH views AS (
95+
select
96+
table_catalog as database,
97+
table_name as name,
98+
table_schema as schema
99+
from {{ schema_relation.information_schema() }}.views
100+
where LOWER(table_schema) = LOWER('{{ schema_relation.schema }}')
101+
), tables AS (
102+
select
103+
table_catalog as database,
104+
table_name as name,
105+
table_schema as schema
106+
107+
from {{ schema_relation.information_schema() }}.tables
108+
where LOWER(table_schema) = LOWER('{{ schema_relation.schema }}')
109+
110+
-- Views appear in both `tables` and `views`, so excluding them from tables
111+
EXCEPT
112+
113+
select * from views
114+
)
115+
select views.*, 'view' AS table_type FROM views
116+
UNION ALL
117+
select tables.*, 'table' AS table_type FROM tables
118+
{% endcall %}
119+
{% do return(load_result('list_relations_without_caching').table) %}
120+
{% endmacro %}

Diff for: dbt/include/athena/macros/adapters/relation.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% macro athena__drop_relation(relation) -%}
2+
{% if config.get('incremental_strategy') == 'insert_overwrite' %}
3+
{%- do adapter.clean_up_table(relation.schema, relation.table) -%}
4+
{% endif %}
5+
{% call statement('drop_relation', auto_begin=False) -%}
6+
drop {{ relation.type }} if exists {{ relation }}
7+
{%- endcall %}
8+
{% endmacro %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% macro set_table_classification(relation, default_value) -%}
2+
{%- set format = config.get('format', default=default_value) -%}
3+
4+
{% call statement('set_table_classification', auto_begin=False) -%}
5+
alter table {{ relation }} set tblproperties ('classification' = '{{ format }}')
6+
{%- endcall %}
7+
{%- endmacro %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{% macro validate_get_incremental_strategy(raw_strategy) %}
2+
{% set invalid_strategy_msg -%}
3+
Invalid incremental strategy provided: {{ raw_strategy }}
4+
Expected one of: 'append', 'insert_overwrite'
5+
{%- endset %}
6+
7+
{% if raw_strategy not in ['append', 'insert_overwrite'] %}
8+
{% do exceptions.raise_compiler_error(invalid_strategy_msg) %}
9+
{% endif %}
10+
11+
{% do return(raw_strategy) %}
12+
{% endmacro %}
13+
14+
{% macro incremental_insert(tmp_relation, target_relation, statement_name="main") %}
15+
{%- set dest_columns = adapter.get_columns_in_relation(target_relation) -%}
16+
{%- set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') -%}
17+
18+
insert into {{ target_relation }} ({{ dest_cols_csv }})
19+
(
20+
select {{ dest_cols_csv }}
21+
from {{ tmp_relation }}
22+
);
23+
{%- endmacro %}
24+
25+
{% macro delete_overlapping_partitions(target_relation, tmp_relation, partitioned_by) %}
26+
{%- set partitioned_keys = partitioned_by | tojson | replace('\"', '') | replace('[', '') | replace(']', '') -%}
27+
{% call statement('get_partitions', fetch_result=True) %}
28+
select distinct {{partitioned_keys}} from {{ tmp_relation }};
29+
{% endcall %}
30+
{%- set table = load_result('get_partitions').table -%}
31+
{%- set rows = table.rows -%}
32+
{%- set partitions = [] -%}
33+
{%- for row in rows -%}
34+
{%- set single_partition = [] -%}
35+
{%- for col in row -%}
36+
{%- set column_type = adapter.convert_type(table, loop.index0) -%}
37+
{%- if column_type == 'integer' -%}
38+
{%- set value = col|string -%}
39+
{%- elif column_type == 'string' -%}
40+
{%- set value = "'" + col + "'" -%}
41+
{%- elif column_type == 'date' -%}
42+
{%- set value = "'" + col|string + "'" -%}
43+
{%- else -%}
44+
{%- do exceptions.raise_compiler_error('Need to add support for column type ' + column_type) -%}
45+
{%- endif -%}
46+
{%- do single_partition.append(partitioned_by[loop.index0] + '=' + value) -%}
47+
{%- endfor -%}
48+
{%- set single_partition_expression = single_partition | join(' and ') -%}
49+
{%- do partitions.append('(' + single_partition_expression + ')') -%}
50+
{%- endfor -%}
51+
{%- for i in range(partitions | length) %}
52+
{%- do adapter.clean_up_partitions(target_relation.schema, target_relation.table, partitions[i]) -%}
53+
{%- endfor -%}
54+
{%- endmacro %}

Diff for: dbt/include/athena/macros/materializations/incremental.sql renamed to dbt/include/athena/macros/materializations/models/incremental/incremental.sql

-55
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,3 @@
1-
{% macro validate_get_incremental_strategy(raw_strategy) %}
2-
{% set invalid_strategy_msg -%}
3-
Invalid incremental strategy provided: {{ raw_strategy }}
4-
Expected one of: 'append', 'insert_overwrite'
5-
{%- endset %}
6-
7-
{% if raw_strategy not in ['append', 'insert_overwrite'] %}
8-
{% do exceptions.raise_compiler_error(invalid_strategy_msg) %}
9-
{% endif %}
10-
11-
{% do return(raw_strategy) %}
12-
{% endmacro %}
13-
14-
{% macro incremental_insert(tmp_relation, target_relation, statement_name="main") %}
15-
{%- set dest_columns = adapter.get_columns_in_relation(target_relation) -%}
16-
{%- set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') -%}
17-
18-
insert into {{ target_relation }} ({{ dest_cols_csv }})
19-
(
20-
select {{ dest_cols_csv }}
21-
from {{ tmp_relation }}
22-
);
23-
{%- endmacro %}
24-
25-
{% macro delete_overlapping_partitions(target_relation, tmp_relation, partitioned_by) %}
26-
{%- set partitioned_keys = partitioned_by | tojson | replace('\"', '') | replace('[', '') | replace(']', '') -%}
27-
{% call statement('get_partitions', fetch_result=True) %}
28-
select distinct {{partitioned_keys}} from {{ tmp_relation }};
29-
{% endcall %}
30-
{%- set table = load_result('get_partitions').table -%}
31-
{%- set rows = table.rows -%}
32-
{%- set partitions = [] -%}
33-
{%- for row in rows -%}
34-
{%- set single_partition = [] -%}
35-
{%- for col in row -%}
36-
{%- set column_type = adapter.convert_type(table, loop.index0) -%}
37-
{%- if column_type == 'integer' -%}
38-
{%- set value = col|string -%}
39-
{%- elif column_type == 'string' -%}
40-
{%- set value = "'" + col + "'" -%}
41-
{%- elif column_type == 'date' -%}
42-
{%- set value = "'" + col|string + "'" -%}
43-
{%- else -%}
44-
{%- do exceptions.raise_compiler_error('Need to add support for column type ' + column_type) -%}
45-
{%- endif -%}
46-
{%- do single_partition.append(partitioned_by[loop.index0] + '=' + value) -%}
47-
{%- endfor -%}
48-
{%- set single_partition_expression = single_partition | join(' and ') -%}
49-
{%- do partitions.append('(' + single_partition_expression + ')') -%}
50-
{%- endfor -%}
51-
{%- for i in range(partitions | length) %}
52-
{%- do adapter.clean_up_partitions(target_relation.schema, target_relation.table, partitions[i]) -%}
53-
{%- endfor -%}
54-
{%- endmacro %}
55-
561
{% materialization incremental, adapter='athena' -%}
572

583
{% set unique_key = config.get('unique_key') %}

0 commit comments

Comments
 (0)