77from dataclasses import dataclass
88from datetime import date , datetime
99from functools import lru_cache
10- from itertools import chain
1110from textwrap import dedent
1211from 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
1413from urllib .parse import urlparse
1514from uuid import uuid4
1615
1716import agate
1817import mmh3
1918from 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
2022from mypy_boto3_athena import AthenaClient
2123from mypy_boto3_athena .type_defs import DataCatalogTypeDef , GetWorkGroupOutputTypeDef
2224from mypy_boto3_glue .type_defs import (
6567from dbt .adapters .base import ConstraintSupport , PythonJobHelper , available
6668from dbt .adapters .base .impl import AdapterConfig
6769from dbt .adapters .base .relation import BaseRelation , InformationSchema
70+ from dbt .adapters .contracts .connection import AdapterResponse
71+ from dbt .adapters .contracts .relation import RelationConfig
6872from 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
7674boto3_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 ):
0 commit comments