This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dbt-adapter): dbt-athena-community support (#741)
* tests(dbt-fal): add athena profile * feat: add dbt-athena-community support dbt-athena from PyPI doesn't support dbt 1.3, but dbt-athena-community does https://github.com/dbt-athena/dbt-athena dbt-athena-community does not support dbt 1.4, so this branch doesn't need to be merged to main. Instead, we will release a patch to dbt-fal 1.3 in order to add support to dbt-athena-community * PR comments * refactor: move some hacks location (#742) --------- Co-authored-by: Matteo Ferrando <[email protected]>
- Loading branch information
Showing
7 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
config: | ||
send_anonymous_usage_stats: False | ||
|
||
fal_test: | ||
target: staging | ||
outputs: | ||
staging: | ||
type: fal | ||
db_profile: db | ||
db: | ||
type: athena | ||
s3_staging_dir: "{{ env_var('ATHENA_S3_STAGING_DIR') }}" | ||
region_name: us-east-1 | ||
database: "{{ env_var('ATHENA_DATABASE') }}" | ||
schema: "{{ env_var('ATHENA_SCHEMA') }}" | ||
num_retries: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
adapter/src/dbt/adapters/fal_experimental/support/athena.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from typing import Any | ||
import six | ||
from dbt.adapters.base.relation import BaseRelation | ||
from dbt.contracts.connection import AdapterResponse | ||
import sqlalchemy | ||
import pandas as pd | ||
from dbt.adapters.base import BaseAdapter | ||
from urllib.parse import quote_plus | ||
|
||
|
||
def create_engine(adapter: BaseAdapter) -> Any: | ||
creds = adapter.config.credentials._db_creds | ||
conn_str = ("awsathena+rest://:@athena.{region_name}.amazonaws.com:443/" | ||
"{schema_name}?s3_staging_dir={s3_staging_dir}" | ||
"&location={location}&compression=snappy") | ||
return sqlalchemy.create_engine(conn_str.format( | ||
region_name=creds.region_name, | ||
schema_name=creds.schema, | ||
s3_staging_dir=quote_plus(creds.s3_staging_dir), | ||
location=quote_plus(creds.s3_staging_dir))) | ||
|
||
|
||
def drop_relation_if_it_exists(adapter: BaseAdapter, relation: BaseRelation) -> None: | ||
if adapter.get_relation( | ||
database=relation.database, | ||
schema=relation.schema, | ||
identifier=relation.identifier, | ||
): | ||
adapter.drop_relation(relation) | ||
|
||
|
||
def write_df_to_relation(adapter, dataframe, relation, if_exists) -> AdapterResponse: | ||
|
||
assert adapter.type() == "athena" | ||
|
||
# This is a quirk of dbt-athena-community, where they set | ||
# relation.schema = relation.identifier | ||
temp_relation = relation.replace_path( | ||
schema=relation.database, | ||
database=adapter.config.credentials._db_creds.database, | ||
# athena complanes when table location has x.__y | ||
identifier=f"dbt_fal_temp_{relation.schema}" | ||
) | ||
|
||
relation = temp_relation.replace_path(identifier=relation.schema) | ||
|
||
drop_relation_if_it_exists(adapter, temp_relation) | ||
|
||
alchemy_engine = create_engine(adapter) | ||
|
||
rows_affected = dataframe.to_sql( | ||
con=alchemy_engine, | ||
name=temp_relation.identifier, | ||
schema=temp_relation.schema, | ||
if_exists=if_exists, | ||
index=False, | ||
) | ||
|
||
adapter.cache.add(temp_relation) | ||
|
||
drop_relation_if_it_exists(adapter, relation) | ||
|
||
|
||
# athena doesn't let us rename relations, so we do it by hand | ||
stmt = f"create table {relation} as select * from {temp_relation} with data" | ||
adapter.execute(six.text_type(stmt).strip()) | ||
adapter.cache.add(relation) | ||
adapter.drop_relation(temp_relation) | ||
|
||
adapter.commit_if_has_connection() | ||
return AdapterResponse("OK", rows_affected=rows_affected) | ||
|
||
def read_relation_as_df(adapter: BaseAdapter, relation: BaseRelation) -> pd.DataFrame: | ||
alchemy_engine = create_engine(adapter) | ||
|
||
# This is dbt-athena-community quirk, table_name=relation.schema | ||
|
||
return pd.read_sql_table( | ||
con=alchemy_engine, | ||
table_name=relation.schema, | ||
schema=relation.database, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters