88from typing import Any , Dict , Generator , List , NamedTuple , Optional , Union , cast
99
1010import boto3
11+ import botocore .exceptions
1112import pandas as pd
1213
1314from awswrangler import _data_types , _utils , exceptions , s3 , sts
1415from awswrangler ._config import apply_configs
1516
1617_QUERY_FINAL_STATES : List [str ] = ["FAILED" , "SUCCEEDED" , "CANCELLED" ]
17- _QUERY_WAIT_POLLING_DELAY : float = 0.2 # SECONDS
18+ _QUERY_WAIT_POLLING_DELAY : float = 0.25 # SECONDS
1819
1920_logger : logging .Logger = logging .getLogger (__name__ )
2021
@@ -91,7 +92,13 @@ def _start_query_execution(
9192
9293 client_athena : boto3 .client = _utils .client (service_name = "athena" , session = session )
9394 _logger .debug ("args: \n %s" , pprint .pformat (args ))
94- response : Dict [str , Any ] = client_athena .start_query_execution (** args )
95+ response : Dict [str , Any ] = _utils .try_it (
96+ f = client_athena .start_query_execution ,
97+ ex = botocore .exceptions .ClientError ,
98+ ex_code = "ThrottlingException" ,
99+ max_num_tries = 5 ,
100+ ** args ,
101+ )
95102 return cast (str , response ["QueryExecutionId" ])
96103
97104
@@ -608,7 +615,16 @@ def get_work_group(workgroup: str, boto3_session: Optional[boto3.Session] = None
608615
609616 """
610617 client_athena : boto3 .client = _utils .client (service_name = "athena" , session = boto3_session )
611- return cast (Dict [str , Any ], client_athena .get_work_group (WorkGroup = workgroup ))
618+ return cast (
619+ Dict [str , Any ],
620+ _utils .try_it (
621+ f = client_athena .get_work_group ,
622+ ex = botocore .exceptions .ClientError ,
623+ ex_code = "ThrottlingException" ,
624+ max_num_tries = 5 ,
625+ WorkGroup = workgroup ,
626+ ),
627+ )
612628
613629
614630def stop_query_execution (query_execution_id : str , boto3_session : Optional [boto3 .Session ] = None ) -> None :
@@ -659,20 +675,20 @@ def wait_query(query_execution_id: str, boto3_session: Optional[boto3.Session] =
659675 >>> res = wr.athena.wait_query(query_execution_id='query-execution-id')
660676
661677 """
662- client_athena : boto3 .client = _utils .client ( service_name = "athena" , session = boto3_session )
663- response : Dict [str , Any ] = client_athena . get_query_execution (QueryExecutionId = query_execution_id )
664- state : str = response ["QueryExecution" ][ " Status" ]["State" ]
678+ session : boto3 .Session = _utils .ensure_session ( session = boto3_session )
679+ response : Dict [str , Any ] = get_query_execution (query_execution_id = query_execution_id , boto3_session = session )
680+ state : str = response ["Status" ]["State" ]
665681 while state not in _QUERY_FINAL_STATES :
666682 time .sleep (_QUERY_WAIT_POLLING_DELAY )
667- response = client_athena . get_query_execution (QueryExecutionId = query_execution_id )
668- state = response ["QueryExecution" ][ " Status" ]["State" ]
683+ response = get_query_execution (query_execution_id = query_execution_id , boto3_session = session )
684+ state = response ["Status" ]["State" ]
669685 _logger .debug ("state: %s" , state )
670- _logger .debug ("StateChangeReason: %s" , response ["QueryExecution" ][ " Status" ].get ("StateChangeReason" ))
686+ _logger .debug ("StateChangeReason: %s" , response ["Status" ].get ("StateChangeReason" ))
671687 if state == "FAILED" :
672- raise exceptions .QueryFailed (response ["QueryExecution" ][ " Status" ].get ("StateChangeReason" ))
688+ raise exceptions .QueryFailed (response ["Status" ].get ("StateChangeReason" ))
673689 if state == "CANCELLED" :
674- raise exceptions .QueryCancelled (response ["QueryExecution" ][ " Status" ].get ("StateChangeReason" ))
675- return cast ( Dict [ str , Any ], response [ "QueryExecution" ])
690+ raise exceptions .QueryCancelled (response ["Status" ].get ("StateChangeReason" ))
691+ return response
676692
677693
678694def get_query_execution (query_execution_id : str , boto3_session : Optional [boto3 .Session ] = None ) -> Dict [str , Any ]:
@@ -699,5 +715,11 @@ def get_query_execution(query_execution_id: str, boto3_session: Optional[boto3.S
699715
700716 """
701717 client_athena : boto3 .client = _utils .client (service_name = "athena" , session = boto3_session )
702- response : Dict [str , Any ] = client_athena .get_query_execution (QueryExecutionId = query_execution_id )
718+ response : Dict [str , Any ] = _utils .try_it (
719+ f = client_athena .get_query_execution ,
720+ ex = botocore .exceptions .ClientError ,
721+ ex_code = "ThrottlingException" ,
722+ max_num_tries = 5 ,
723+ QueryExecutionId = query_execution_id ,
724+ )
703725 return cast (Dict [str , Any ], response ["QueryExecution" ])
0 commit comments