1212
1313from awswrangler import _utils , exceptions
1414from awswrangler ._config import apply_configs
15- from awswrangler .catalog ._utils import _extract_dtypes_from_table_details
15+ from awswrangler .catalog ._utils import _catalog_id , _extract_dtypes_from_table_details
1616
1717_logger : logging .Logger = logging .getLogger (__name__ )
1818
@@ -21,16 +21,12 @@ def _get_table_input(
2121 database : str , table : str , boto3_session : Optional [boto3 .Session ], catalog_id : Optional [str ] = None
2222) -> Optional [Dict [str , Any ]]:
2323 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
24- args : Dict [str , str ] = {}
25- if catalog_id is not None :
26- args ["CatalogId" ] = catalog_id
27- args ["DatabaseName" ] = database
28- args ["Name" ] = table
2924 try :
30- response : Dict [str , Any ] = client_glue .get_table (** args )
25+ response : Dict [str , Any ] = client_glue .get_table (
26+ ** _catalog_id (catalog_id = catalog_id , DatabaseName = database , Name = table )
27+ )
3128 except client_glue .exceptions .EntityNotFoundException :
3229 return None
33-
3430 table_input : Dict [str , Any ] = {}
3531 for k , v in response ["Table" ].items ():
3632 if k in [
@@ -49,7 +45,6 @@ def _get_table_input(
4945 "TargetTable" ,
5046 ]:
5147 table_input [k ] = v
52-
5348 return table_input
5449
5550
@@ -162,10 +157,7 @@ def get_databases(
162157 """
163158 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
164159 paginator = client_glue .get_paginator ("get_databases" )
165- if catalog_id is None :
166- response_iterator : Iterator = paginator .paginate ()
167- else :
168- response_iterator = paginator .paginate (CatalogId = catalog_id )
160+ response_iterator = paginator .paginate (** _catalog_id (catalog_id = catalog_id ))
169161 for page in response_iterator :
170162 for db in page ["DatabaseList" ]:
171163 yield db
@@ -436,10 +428,7 @@ def table(
436428
437429 """
438430 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
439- if catalog_id is None :
440- tbl : Dict [str , Any ] = client_glue .get_table (DatabaseName = database , Name = table )["Table" ]
441- else :
442- tbl = client_glue .get_table (CatalogId = catalog_id , DatabaseName = database , Name = table )["Table" ]
431+ tbl = client_glue .get_table (** _catalog_id (catalog_id = catalog_id , DatabaseName = database , Name = table ))["Table" ]
443432 df_dict : Dict [str , List ] = {"Column Name" : [], "Type" : [], "Partition" : [], "Comment" : []}
444433 for col in tbl ["StorageDescriptor" ]["Columns" ]:
445434 df_dict ["Column Name" ].append (col ["Name" ])
@@ -522,10 +511,7 @@ def get_connection(
522511
523512 """
524513 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
525- args : Dict [str , Any ] = {"Name" : name , "HidePassword" : False }
526- if catalog_id is not None :
527- args ["CatalogId" ] = catalog_id
528- return client_glue .get_connection (** args )["Connection" ]
514+ return client_glue .get_connection (** _catalog_id (catalog_id = catalog_id , Name = name , HidePassword = False ))["Connection" ]
529515
530516
531517def get_engine (
@@ -812,12 +798,9 @@ def get_table_parameters(
812798
813799 """
814800 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
815- args : Dict [str , str ] = {}
816- if catalog_id is not None :
817- args ["CatalogId" ] = catalog_id
818- args ["DatabaseName" ] = database
819- args ["Name" ] = table
820- response : Dict [str , Any ] = client_glue .get_table (** args )
801+ response : Dict [str , Any ] = client_glue .get_table (
802+ ** _catalog_id (catalog_id = catalog_id , DatabaseName = database , Name = table )
803+ )
821804 parameters : Dict [str , str ] = response ["Table" ]["Parameters" ]
822805 return parameters
823806
@@ -851,16 +834,14 @@ def get_table_description(
851834
852835 """
853836 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
854- args : Dict [str , str ] = {}
855- if catalog_id is not None :
856- args ["CatalogId" ] = catalog_id
857- args ["DatabaseName" ] = database
858- args ["Name" ] = table
859- response : Dict [str , Any ] = client_glue .get_table (** args )
837+ response : Dict [str , Any ] = client_glue .get_table (
838+ ** _catalog_id (catalog_id = catalog_id , DatabaseName = database , Name = table )
839+ )
860840 desc : Optional [str ] = response ["Table" ].get ("Description" , None )
861841 return desc
862842
863843
844+ @apply_configs
864845def get_columns_comments (
865846 database : str , table : str , catalog_id : Optional [str ] = None , boto3_session : Optional [boto3 .Session ] = None
866847) -> Dict [str , str ]:
@@ -890,16 +871,91 @@ def get_columns_comments(
890871
891872 """
892873 client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
893- args : Dict [str , str ] = {}
894- if catalog_id is not None :
895- args ["CatalogId" ] = catalog_id
896- args ["DatabaseName" ] = database
897- args ["Name" ] = table
898- response : Dict [str , Any ] = client_glue .get_table (** args )
874+ response : Dict [str , Any ] = client_glue .get_table (
875+ ** _catalog_id (catalog_id = catalog_id , DatabaseName = database , Name = table )
876+ )
899877 comments : Dict [str , str ] = {}
900878 for c in response ["Table" ]["StorageDescriptor" ]["Columns" ]:
901879 comments [c ["Name" ]] = c ["Comment" ]
902880 if "PartitionKeys" in response ["Table" ]:
903881 for p in response ["Table" ]["PartitionKeys" ]:
904882 comments [p ["Name" ]] = p ["Comment" ]
905883 return comments
884+
885+
886+ @apply_configs
887+ def get_table_versions (
888+ database : str , table : str , catalog_id : Optional [str ] = None , boto3_session : Optional [boto3 .Session ] = None
889+ ) -> List [Dict [str , Any ]]:
890+ """Get all versions.
891+
892+ Parameters
893+ ----------
894+ database : str
895+ Database name.
896+ table : str
897+ Table name.
898+ catalog_id : str, optional
899+ The ID of the Data Catalog from which to retrieve Databases.
900+ If none is provided, the AWS account ID is used by default.
901+ boto3_session : boto3.Session(), optional
902+ Boto3 Session. The default boto3 session will be used if boto3_session receive None.
903+
904+ Returns
905+ -------
906+ List[Dict[str, Any]
907+ List of table inputs:
908+ https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/glue.html#Glue.Client.get_table_versions
909+
910+ Examples
911+ --------
912+ >>> import awswrangler as wr
913+ >>> tables_versions = wr.catalog.get_table_versions(database="...", table="...")
914+
915+ """
916+ client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
917+ paginator = client_glue .get_paginator ("get_table_versions" )
918+ versions : List [Dict [str , Any ]] = []
919+ response_iterator = paginator .paginate (** _catalog_id (DatabaseName = database , TableName = table , catalog_id = catalog_id ))
920+ for page in response_iterator :
921+ for tbl in page ["TableVersions" ]:
922+ versions .append (tbl )
923+ return versions
924+
925+
926+ @apply_configs
927+ def get_table_number_of_versions (
928+ database : str , table : str , catalog_id : Optional [str ] = None , boto3_session : Optional [boto3 .Session ] = None
929+ ) -> int :
930+ """Get tatal number of versions.
931+
932+ Parameters
933+ ----------
934+ database : str
935+ Database name.
936+ table : str
937+ Table name.
938+ catalog_id : str, optional
939+ The ID of the Data Catalog from which to retrieve Databases.
940+ If none is provided, the AWS account ID is used by default.
941+ boto3_session : boto3.Session(), optional
942+ Boto3 Session. The default boto3 session will be used if boto3_session receive None.
943+
944+ Returns
945+ -------
946+ int
947+ Total number of versions.
948+
949+ Examples
950+ --------
951+ >>> import awswrangler as wr
952+ >>> num = wr.catalog.get_table_number_of_versions(database="...", table="...")
953+
954+ """
955+ client_glue : boto3 .client = _utils .client (service_name = "glue" , session = boto3_session )
956+ paginator = client_glue .get_paginator ("get_table_versions" )
957+ count : int = 0
958+ response_iterator = paginator .paginate (** _catalog_id (DatabaseName = database , TableName = table , catalog_id = catalog_id ))
959+ for page in response_iterator :
960+ count += len (page ["TableVersions" ])
961+ return count
0 commit comments