
    dhi                    v    S SK Jr  S SKrS SKrS SKrS SKJrJrJrJ	r	J
r
Jr  S SKJr  S SKJr   " S S\5      rg)    )annotationsN)AnyDictIteratorListOptionalTuple)Document)
BaseLoaderc                      \ rS rSrSr  S         SS jjrSS jrSS jrSS jrSS jr	    SS	 jr
SS
 jrSrg)AthenaLoader   a%  Load documents from `AWS Athena`.

Each document represents one row of the result.
- By default, all columns are written into the `page_content` of the document
and none into the `metadata` of the document.
- If `metadata_columns` are provided then these columns are written
into the `metadata` of the document while the rest of the columns
are written into the `page_content` of the document.

To authenticate, the AWS client uses this method to automatically load credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

If a specific credential profile should be used, you must pass
the name of the profile from the ~/.aws/credentials file that is to be used.

Make sure the credentials / roles used have the required policies to
access the Amazon Textract service.
Nc                V   Xl         X l        X0l        Ub  UO/ U l         SSKn Ub  UR                  US9OUR                  5       nUR                  S5      U l
        UR                  S5      U l        g! [
         a    [        S5      ef = f! [         a  n[        S5      UeSnAff = f)a/  Initialize Athena document loader.

Args:
    query: The query to run in Athena.
    database: Athena database.
    s3_output_uri: Athena output path.
    profile_name: Optional. AWS credential profile, if profiles are being used.
    metadata_columns: Optional. Columns written to Document `metadata`.
Nr   zRCould not import boto3 python package. Please install it with `pip install boto3`.)profile_namezCould not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid.athenas3)querydatabases3_output_urimetadata_columnsboto3ImportErrorSession	Exception
ValueErrorclientathena_client	s3_client)	selfr   r   r   r   r   r   sessiones	            c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/athena.py__init__AthenaLoader.__init__!   s    " 
 *4D4P 0VX		  + <8]]_  %^^H5 -)  	> 	  	* 		s"   A4 "B 4B

B(B##B(c                   U R                   R                  U R                  SU R                  0SU R                  0S9nUS   n U R                   R                  US9nUS   S   S   nUS	:X  a  OLUS
:X  a  US   S   nUS   nSU 3n[        U5      eUS:X  a  [        S5      e[        R                  " S5        Mw  U R                  U5      n[        R                  " UR                  SS95      $ )NDatabaseOutputLocation)QueryStringQueryExecutionContextResultConfigurationQueryExecutionId)r+   QueryExecutionStatusState	SUCCEEDEDFAILEDStateChangeReasonzQuery Failed: 	CANCELLEDz Query was cancelled by the user.   records)orient)r   start_query_executionr   r   r   get_query_executionr   timesleep_get_result_setjsonloadsto_json)r   responsequery_execution_idstateresp_statusstate_change_reasonerr
result_sets           r"   _execute_queryAthenaLoader._execute_queryO   s   %%;;

#-t}}"=!143E3E F < 

 &&89))==!3 > H -.x8AE#("&'78B&12E&F#&':&;<n$+% BCCJJqM   ))*<=
zz*,,I,>??    c                ^    U(       a%  UR                  U5      (       a  US [        U5      *  $ U$ N)endswithlenr   input_stringsuffixs      r"   _remove_suffixAthenaLoader._remove_suffixi   s.    l++F333v;,//rG   c                \    U(       a$  UR                  U5      (       a  U[        U5      S  $ U$ rI   )
startswithrK   rL   s      r"   _remove_prefixAthenaLoader._remove_prefixn   s,    l--f55F..rG   c                    SS K nU R                  nU R                  U R	                  US5      S5      R                  S5      nUS   nSR                  USS  U/-   5      S-   nU R                  R                  XVS9nUR                  [        R                  " US   R                  5       5      S	S
9nU$ ! [         a    [        S5      ef = f)Nr   zTCould not import pandas python package. Please install it with `pip install pandas`./zs3://r3   z.csv)BucketKeyBodyutf8)encoding)pandasr   r   rS   rO   splitjoinr   
get_objectread_csvioBytesIOread)	r   r?   pd
output_uritokensbucketkeyobjdfs	            r"   r:   AthenaLoader._get_result_sets   s    	 ''
$$
C0'

%* 	 hhvabz%7$889FBnn''v'?[[CK$4$4$67&[I	  	? 	s   B3 3C	c                    / n/ n[        US   R                  5       5      nU H6  nXPR                  ;   a  UR                  U5        M%  UR                  U5        M8     X#4$ )Nr   )listkeysr   append)r   query_resultcontent_columnsr   all_columnsrh   s         r"   _get_columnsAthenaLoader._get_columns   sc     <?//12C+++ '',&&s+	  00rG   c              #  >  ^	#    U R                  5       nU R                  U5      u  m	nU Hh  nSR                  U	4S jUR                  5        5       5      nUR                  5        VVs0 sH  u  pVXR;   d  M  Uc  M  XV_M     nnn[	        XGS9nUv   Mj     g s  snnf 7f)N
c              3  D   >#    U H  u  pUT;   d  M  U S U 3v   M     g7f)z: N ).0kvrq   s      r"   	<genexpr>)AthenaLoader.lazy_load.<locals>.<genexpr>   s)      %(3qO7K1#Rss     )page_contentmetadata)rE   rs   r^   itemsr
   )
r   rp   r   rowr~   rz   r{   r   docrq   s
            @r"   	lazy_loadAthenaLoader.lazy_load   s     **,,0,=,=l,K))C99 %(+		% L "%!,0E!   HCI  s   A&B)
B7B<BB)r   r   r   r   r   r   )NN)
r   strr   r   r   r   r   zOptional[str]r   zOptional[List[str]])returnList[Dict[str, Any]])rM   r   rN   r   r   r   )r?   r   r   r   )rp   r   r   zTuple[List[str], List[str]])r   zIterator[Document])__name__
__module____qualname____firstlineno____doc__r#   rE   rO   rS   r:   rs   r   __static_attributes__rx   rG   r"   r   r      st    0 '+04,.,. ,. 	,.
 $,. .,.\@4

(101	$1rG   r   )
__future__r   ra   r;   r8   typingr   r   r   r   r   r	   langchain_core.documentsr
   )langchain_community.document_loaders.baser   r   rx   rG   r"   <module>r      s+    " 	   = = - @S: SrG   