Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit e458b59

Browse files
Jrmyynicor88
andauthored
chore: upgrade to dbt 1.8 (#614)
Co-authored-by: nicor88 <6278547+nicor88@users.noreply.github.com>
1 parent b3f85b9 commit e458b59

22 files changed

+182
-392
lines changed

dbt/adapters/athena/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import dbt
21
from dbt.adapters.athena.connections import AthenaConnectionManager, AthenaCredentials
32
from dbt.adapters.athena.impl import AthenaAdapter
4-
from dbt.adapters.athena.query_headers import _QueryComment
53
from dbt.adapters.base import AdapterPlugin
64
from dbt.include import athena
75

8-
Plugin = AdapterPlugin(adapter=AthenaAdapter, credentials=AthenaCredentials, include_path=athena.PACKAGE_PATH)
9-
10-
# overwrite _QueryComment to add leading "--" to query comment
11-
dbt.adapters.base.query_headers._QueryComment = _QueryComment
6+
Plugin: AdapterPlugin = AdapterPlugin(
7+
adapter=AthenaAdapter, credentials=AthenaCredentials, include_path=athena.PACKAGE_PATH
8+
)
129

1310
__all__ = [
1411
"AthenaConnectionManager",

dbt/adapters/athena/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "1.7.2"
1+
version = "1.8.0b1"

dbt/adapters/athena/column.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from dataclasses import dataclass
22
from typing import ClassVar, Dict
33

4+
from dbt_common.exceptions import DbtRuntimeError
5+
46
from dbt.adapters.athena.relation import TableType
57
from dbt.adapters.base.column import Column
6-
from dbt.exceptions import DbtRuntimeError
78

89

910
@dataclass

dbt/adapters/athena/connections.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from typing import Any, ContextManager, Dict, List, Optional, Tuple
1010

1111
import tenacity
12+
from dbt_common.exceptions import ConnectionError, DbtRuntimeError
13+
from dbt_common.utils import md5
1214
from pyathena.connection import Connection as AthenaConnection
1315
from pyathena.cursor import Cursor
1416
from pyathena.error import OperationalError, ProgrammingError
@@ -29,12 +31,15 @@
2931

3032
from dbt.adapters.athena.config import get_boto3_config
3133
from dbt.adapters.athena.constants import LOGGER
34+
from dbt.adapters.athena.query_headers import AthenaMacroQueryStringSetter
3235
from dbt.adapters.athena.session import get_boto3_session
33-
from dbt.adapters.base import Credentials
36+
from dbt.adapters.contracts.connection import (
37+
AdapterResponse,
38+
Connection,
39+
ConnectionState,
40+
Credentials,
41+
)
3442
from dbt.adapters.sql import SQLConnectionManager
35-
from dbt.contracts.connection import AdapterResponse, Connection, ConnectionState
36-
from dbt.exceptions import ConnectionError, DbtRuntimeError
37-
from dbt.utils import md5
3843

3944

4045
@dataclass
@@ -201,6 +206,9 @@ def inner() -> AthenaCursor:
201206
class AthenaConnectionManager(SQLConnectionManager):
202207
TYPE = "athena"
203208

209+
def set_query_header(self, query_header_context: Dict[str, Any]) -> None:
210+
self.query_header = AthenaMacroQueryStringSetter(self.profile, query_header_context)
211+
204212
@classmethod
205213
def data_type_code_to_name(cls, type_code: str) -> str:
206214
"""

dbt/adapters/athena/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dbt.events import AdapterLogger
1+
from dbt.adapters.events.logging import AdapterLogger
22

33
DEFAULT_THREAD_COUNT = 4
44
DEFAULT_RETRY_ATTEMPTS = 3

dbt/adapters/athena/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dbt.exceptions import CompilationError, DbtRuntimeError
1+
from dbt_common.exceptions import CompilationError, DbtRuntimeError
22

33

44
class SnapshotMigrationRequired(CompilationError):

dbt/adapters/athena/impl.py

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
from dataclasses import dataclass
88
from datetime import date, datetime
99
from functools import lru_cache
10-
from itertools import chain
1110
from textwrap import dedent
1211
from threading import Lock
13-
from typing import Any, Dict, Iterator, List, Optional, Set, Tuple, Type
12+
from typing import Any, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple, Type
1413
from urllib.parse import urlparse
1514
from uuid import uuid4
1615

1716
import agate
1817
import mmh3
1918
from botocore.exceptions import ClientError
19+
from dbt_common.clients.agate_helper import table_from_rows
20+
from dbt_common.contracts.constraints import ConstraintType
21+
from dbt_common.exceptions import DbtRuntimeError
2022
from mypy_boto3_athena import AthenaClient
2123
from mypy_boto3_athena.type_defs import DataCatalogTypeDef, GetWorkGroupOutputTypeDef
2224
from mypy_boto3_glue.type_defs import (
@@ -65,13 +67,9 @@
6567
from dbt.adapters.base import ConstraintSupport, PythonJobHelper, available
6668
from dbt.adapters.base.impl import AdapterConfig
6769
from dbt.adapters.base.relation import BaseRelation, InformationSchema
70+
from dbt.adapters.contracts.connection import AdapterResponse
71+
from dbt.adapters.contracts.relation import RelationConfig
6872
from dbt.adapters.sql import SQLAdapter
69-
from dbt.clients.agate_helper import table_from_rows
70-
from dbt.config.runtime import RuntimeConfig
71-
from dbt.contracts.connection import AdapterResponse
72-
from dbt.contracts.graph.manifest import Manifest
73-
from dbt.contracts.graph.nodes import CompiledNode, ConstraintType
74-
from dbt.exceptions import DbtRuntimeError
7573

7674
boto3_client_lock = Lock()
7775

@@ -538,29 +536,6 @@ def _s3_path_exists(self, s3_bucket: str, s3_prefix: str) -> bool:
538536
response = s3_client.list_objects_v2(Bucket=s3_bucket, Prefix=s3_prefix)
539537
return True if "Contents" in response else False
540538

541-
def _join_catalog_table_owners(self, table: agate.Table, manifest: Manifest) -> agate.Table:
542-
owners = []
543-
# Get the owner for each model from the manifest
544-
for node in manifest.nodes.values():
545-
if node.resource_type == "model":
546-
owners.append(
547-
{
548-
"table_database": node.database,
549-
"table_schema": node.schema,
550-
"table_name": node.alias,
551-
"table_owner": node.config.meta.get("owner"),
552-
}
553-
)
554-
owners_table = agate.Table.from_object(owners)
555-
556-
# Join owners with the results from catalog
557-
join_keys = ["table_database", "table_schema", "table_name"]
558-
return table.join(
559-
right_table=owners_table,
560-
left_key=join_keys,
561-
right_key=join_keys,
562-
)
563-
564539
def _get_one_table_for_catalog(self, table: TableTypeDef, database: str) -> List[Dict[str, Any]]:
565540
table_catalog = {
566541
"table_database": database,
@@ -608,13 +583,13 @@ def _get_one_table_for_non_glue_catalog(
608583
def _get_one_catalog(
609584
self,
610585
information_schema: InformationSchema,
611-
schemas: Dict[str, Optional[Set[str]]],
612-
manifest: Manifest,
586+
schemas: Set[str],
587+
used_schemas: FrozenSet[Tuple[str, str]],
613588
) -> agate.Table:
614589
"""
615590
This function is invoked by Adapter.get_catalog for each schema.
616591
"""
617-
data_catalog = self._get_data_catalog(information_schema.path.database)
592+
data_catalog = self._get_data_catalog(information_schema.database)
618593
data_catalog_type = get_catalog_type(data_catalog)
619594

620595
conn = self.connections.get_thread_connection()
@@ -630,7 +605,7 @@ def _get_one_catalog(
630605

631606
catalog = []
632607
paginator = glue_client.get_paginator("get_tables")
633-
for schema, relations in schemas.items():
608+
for schema in schemas:
634609
kwargs = {
635610
"DatabaseName": schema,
636611
"MaxResults": 100,
@@ -643,8 +618,7 @@ def _get_one_catalog(
643618

644619
for page in paginator.paginate(**kwargs):
645620
for table in page["TableList"]:
646-
if relations and table["Name"] in relations:
647-
catalog.extend(self._get_one_table_for_catalog(table, information_schema.path.database))
621+
catalog.extend(self._get_one_table_for_catalog(table, information_schema.database))
648622
table = agate.Table.from_object(catalog)
649623
else:
650624
with boto3_client_lock:
@@ -656,36 +630,28 @@ def _get_one_catalog(
656630

657631
catalog = []
658632
paginator = athena_client.get_paginator("list_table_metadata")
659-
for schema, relations in schemas.items():
633+
for schema in schemas:
660634
for page in paginator.paginate(
661-
CatalogName=information_schema.path.database,
635+
CatalogName=information_schema.database,
662636
DatabaseName=schema,
663637
MaxResults=50, # Limit supported by this operation
664638
):
665639
for table in page["TableMetadataList"]:
666-
if relations and table["Name"].lower() in relations:
667-
catalog.extend(
668-
self._get_one_table_for_non_glue_catalog(
669-
table, schema, information_schema.path.database
670-
)
671-
)
640+
catalog.extend(
641+
self._get_one_table_for_non_glue_catalog(table, schema, information_schema.database)
642+
)
672643
table = agate.Table.from_object(catalog)
673644

674-
filtered_table = self._catalog_filter_table(table, manifest)
675-
return self._join_catalog_table_owners(filtered_table, manifest)
645+
return self._catalog_filter_table(table, used_schemas)
676646

677-
def _get_catalog_schemas(self, manifest: Manifest) -> AthenaSchemaSearchMap:
647+
def _get_catalog_schemas(self, relation_configs: Iterable[RelationConfig]) -> AthenaSchemaSearchMap:
678648
"""
679649
Get the schemas from the catalog.
680650
It's called by the `get_catalog` method.
681651
"""
682652
info_schema_name_map = AthenaSchemaSearchMap()
683-
nodes: Iterator[CompiledNode] = chain(
684-
[node for node in manifest.nodes.values() if (node.is_relational and not node.is_ephemeral_model)],
685-
manifest.sources.values(),
686-
)
687-
for node in nodes:
688-
relation = self.Relation.create_from(self.config, node)
653+
for relation_config in relation_configs:
654+
relation = self.Relation.create_from(quoting=self.config, relation_config=relation_config)
689655
info_schema_name_map.add(relation)
690656
return info_schema_name_map
691657

@@ -775,9 +741,9 @@ def list_relations_without_caching(self, schema_relation: AthenaRelation) -> Lis
775741
def _get_one_catalog_by_relations(
776742
self,
777743
information_schema: InformationSchema,
778-
relations: List[BaseRelation],
779-
manifest: Manifest,
780-
) -> agate.Table:
744+
relations: List[AthenaRelation],
745+
used_schemas: FrozenSet[Tuple[str, str]],
746+
) -> "agate.Table":
781747
"""
782748
Overwrite of _get_one_catalog_by_relations for Athena, in order to use glue apis.
783749
This function is invoked by Adapter.get_catalog_by_relations.
@@ -790,12 +756,11 @@ def _get_one_catalog_by_relations(
790756
_table_definitions.extend(_table_definition)
791757
table = agate.Table.from_object(_table_definitions)
792758
# picked from _catalog_filter_table, force database + schema to be strings
793-
table_casted = table_from_rows(
759+
return table_from_rows(
794760
table.rows,
795761
table.column_names,
796762
text_only_columns=["table_database", "table_schema", "table_name"],
797763
)
798-
return self._join_catalog_table_owners(table_casted, manifest)
799764

800765
@available
801766
def swap_table(self, src_relation: AthenaRelation, target_relation: AthenaRelation) -> None:
@@ -1012,11 +977,9 @@ def persist_docs_to_glue(
1012977
# Add some of dbt model config fields as table meta
1013978
meta["unique_id"] = model.get("unique_id")
1014979
meta["materialized"] = model.get("config", {}).get("materialized")
1015-
# Get dbt runtime config to be able to get dbt project metadata
1016-
runtime_config: RuntimeConfig = self.config
1017980
# Add dbt project metadata to table meta
1018-
meta["dbt_project_name"] = runtime_config.project_name
1019-
meta["dbt_project_version"] = runtime_config.version
981+
meta["dbt_project_name"] = self.config.project_name
982+
meta["dbt_project_version"] = self.config.version
1020983
# Prepare meta values for table properties and check if update is required
1021984
for meta_key, meta_value_raw in meta.items():
1022985
if is_valid_table_parameter_key(meta_key):

dbt/adapters/athena/lakeformation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Dict, List, Optional, Sequence, Set, Union
44

5+
from dbt_common.exceptions import DbtRuntimeError
56
from mypy_boto3_lakeformation import LakeFormationClient
67
from mypy_boto3_lakeformation.type_defs import (
78
AddLFTagsToResourceResponseTypeDef,
@@ -16,8 +17,7 @@
1617
from pydantic import BaseModel
1718

1819
from dbt.adapters.athena.relation import AthenaRelation
19-
from dbt.events import AdapterLogger
20-
from dbt.exceptions import DbtRuntimeError
20+
from dbt.adapters.events.logging import AdapterLogger
2121

2222
logger = AdapterLogger("AthenaLakeFormation")
2323

dbt/adapters/athena/python_submissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from typing import Any, Dict
44

55
import botocore
6+
from dbt_common.exceptions import DbtRuntimeError
67

78
from dbt.adapters.athena.config import AthenaSparkSessionConfig
89
from dbt.adapters.athena.connections import AthenaCredentials
910
from dbt.adapters.athena.constants import LOGGER
1011
from dbt.adapters.athena.session import AthenaSparkSessionManager
1112
from dbt.adapters.base import PythonJobHelper
12-
from dbt.exceptions import DbtRuntimeError
1313

1414
SUBMISSION_LANGUAGE = "python"
1515

dbt/adapters/athena/query_headers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import dbt.adapters.base.query_headers
1+
from typing import Any, Dict
22

3+
from dbt.adapters.base.query_headers import MacroQueryStringSetter, _QueryComment
4+
from dbt.adapters.contracts.connection import AdapterRequiredConfig
35

4-
class _QueryComment(dbt.adapters.base.query_headers._QueryComment):
6+
7+
class AthenaMacroQueryStringSetter(MacroQueryStringSetter):
8+
def __init__(self, config: AdapterRequiredConfig, query_header_context: Dict[str, Any]):
9+
super().__init__(config, query_header_context)
10+
self.comment = _AthenaQueryComment(None)
11+
12+
13+
class _AthenaQueryComment(_QueryComment):
514
"""
615
Athena DDL does not always respect /* ... */ block quotations.
716
This function is the same as _QueryComment.add except that

0 commit comments

Comments
 (0)