
    b                     h   % S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	r	SSK
r
SSKrSSKrSSKrSSKJrJrJrJr  SSKrSSKJr  SSKrSSKrSS	KJr  SSKrSS
KJr  SSKrSSKrSSKrSSKJ r   SSK!J"r"  SSK!J#r$  SSK!J%r%  SSK&J'r'  SSK&J(r(  SSK#J)r)  SSK#J*r*  SSK#J+r+   SSK,J-r.  Sr/ SSK1r1Sr2\3" 5       r4\\ Rj                  \Rl                  4   r7\)Rp                  r8\\Rr                  4   r9\\:S'   \\Rr                  S4   r;\\:S'    " S S5      r<g! \0 a    Sr/ Nuf = f! \0 a    Sr2 N|f = f)zBigqueryClient class.    )absolute_import)division)print_functionN)client)CallableListOptionalUnion)flags)	discovery)	TypeAlias)main_credential_loader)bigquery_http)utils)wait_printer)discovery_document_cache)discovery_document_loader)bq_api_utils)bq_error)
bq_logging)credentialsTFHttpz#google_auth_httplib2.AuthorizedHttpAuthorizedHttpc                   >  ^  \ rS rSrSr " S S\\R                  5      rSS\	SSS\
R                  \R                  " \R                  " 5       5      SSSSSS	.S
\S\S\\   S\\   S\\\S4   S\S\\   S\S\\/ \
R*                  4      S\R,                  S\\   S\\   S\S\\   S\4U 4S jjjr/ SQr/ SQrS\4S jr  S7S\S\S\\   S \\   S\4
S! jjrS\S"\ S\4S# jr!S\S$\\   S\S\\\\\4      4S% jr"S\S"\S$\S\\\\\4      4S& jr#     S8S\S$\\   S'\\   S\\   S\\   S \\   S\$RJ                  4S( jjr&\'S\$RJ                  4S) j5       r(S\$RJ                  4S* jr)S\$RJ                  4S+ jr*S\$RJ                  4S, jr+S\$RJ                  4S- jr,S\$RJ                  4S. jr- S9S/\\   S\$RJ                  4S0 jjr. S9S1\\   S\$RJ                  4S2 jjr/ S9S3\\   S\$RJ                  4S4 jjr0 S9S\\\4   S\S'\\   S\\   4S5 jjr1S6r2U =r3$ ):BigqueryClientM   z:Class encapsulating interaction with the BigQuery service.c                        \ rS rSrSrSrSrSrg)BigqueryClient.JobCreationModeP   zEnum of job creation mode.JOB_CREATION_REQUIREDJOB_CREATION_OPTIONAL N)__name__
__module____qualname____firstlineno____doc__r    r!   __static_attributes__r"       &platform/bq/clients/bigquery_client.pyJobCreationModer   P   s    $33r)   r+    NTF)
project_id
dataset_iddiscovery_documentjob_propertytracesyncwait_printer_factoryjob_id_generatormax_rows_per_requestquota_project_iduse_google_authr   enable_resumable_uploadsapiapi_versionr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r   r8   c                &  > [         TU ]  5         Xl        X l        X0l        X@l        XPl        X`l        Xpl        Xl	        Xl
        Xl        Xl        Xl        Xl        Xl        Xl        UR#                  5        H  u  nn[%        U UU5        M     SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        S[8        0nUR#                  5        H&  u  nn[;        U U5      (       a  M  [%        U UU5        M(     g)a  Initializes BigqueryClient.

Required keywords:
  api: the api to connect to, for example "bigquery".
  api_version: the version of the api to connect to, for example "v2".

Optional keywords:
  project_id: a default project id to use. While not required for
    initialization, a project_id is required when calling any
    method that creates a job on the server. Methods that have
    this requirement pass through **kwds, and will raise
    bq_error.BigqueryClientConfigurationError if no project_id can be
    found.
  dataset_id: a default dataset id to use.
  discovery_document: the discovery document to use. If None, one
    will be retrieved from the discovery api. If not specified,
    the built-in discovery document will be used.
  job_property: a list of "key=value" strings defining properties
    to apply to all job operations.
  trace: a tracing header to include in all bigquery api requests.
  sync: boolean, when inserting jobs, whether to wait for them to
    complete before returning from the insert request.
  wait_printer_factory: a function that returns a WaitPrinter.
    This will be called for each job that we wait on. See WaitJob().

Raises:
  ValueError: if keywords are missing or incorrectly specified.
Niam_policy_discovery_document)super__init__r9   r:   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r   r8   itemssetattr
_apiclient_routines_apiclient_row_access_policies_apiclient_op_transfer_client_op_reservation_client_op_bi_reservation_client_models_apiclient_op_connection_service_client_iam_policy_apiclient_DEFAULThasattr)selfr9   r:   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r   r8   kwdskeyvaluedefault_flag_valuesflagnamedefault	__class__s                         r*   r>   BigqueryClient.__init__V   s   h 
GH" O O0$JI 4, 4,*"$<!jjl
UdC #DO#D*.D'#D"&D%)D"!D)-D&!%D' 1668'T8$$h( 9r)   )
updateTimeschedulerunTimescheduleTimeparamsendTimedataSourceIddestinationDatasetIdstate	startTimename)rV   rZ   r^   returnc                    [         R                  n[        R                  R                  (       a  [        R                  R
                  (       a   [        [        R                  R
                  5      n[         R                  " S[        R                  R                  U[        R                  R                  =(       d    S[        R                  R                  =(       d    SS9n[         R                  " U[        R                  R                  =(       d    [        R                  " 5       [        R                  R                   S9n[#        US5      (       a  [%        UR&                  5      S1-
  Ul        [        R                  R(                  (       ax  [*        R,                  " 5       u  oPl        [*        R,                  " 5       u  oPl        [2        R4                  " U[2        R6                  " 5       U R.                  U R0                  5        U$ ! [         a8  n[        SR                  [        R                  R
                  5      5      UeSnAff = f)z!Returns the httplib2 Http to use.z Invalid value for proxy_port: {}N   )
proxy_type
proxy_host
proxy_port
proxy_user
proxy_pass)
proxy_infoca_certs"disable_ssl_certificate_validationredirect_codesi4  )httplib2proxy_info_from_environmentr   FLAGSproxy_addressre   int
ValueErrorformat	ProxyInfoproxy_usernameproxy_passwordr   ca_certificates_filecertifiwheredisable_ssl_validationrK   setrk   mtlstempfilemkstemp
_cert_file	_key_filer   add_mtls_credsget_client_options)rL   rh   portehttp_s         r*   GetHttpBigqueryClient.GetHttp   s   
 55J{{  U[[%;%;5;;))*
 %%[[..[[//74[[//74j ==11DW]]_+0;;+M+MD t%&& 3 34u<d{{#++-a"**,a
	,,. K7  .55ekk6L6LM
	s   #H 
I3IIservicedomain_rootlabelsc                     SnU(       d5  [         R                  " U=(       d    [         R                  " U[        S9US9nU$ )z7Returns the url to the discovery document for bigquery.Nr   inputted_flagsr:   )r   get_discovery_url_from_root_urlget_tpc_root_url_from_flagsbq_flags)rL   r   r:   r   r   discovery_urls         r*   GetDiscoveryUrlBigqueryClient.GetDiscoveryUrl   sD     M"BB
 55h "m r)   r   c                    U R                   (       aa  [        (       d  [        R                  " S5        [	        U[
        R                  5      (       d   [        R                  " S[        U5      5        O[        R                  " S5        [        (       aI  [	        U[
        R                  5      (       a*  [        (       d  [        S5      e[        R                  " XS9$ [        US5      (       a  UR                  U5      $ [        S5      e)zEReturns an http client that is authorized with the given credentials.z8System is set to use `google.auth`, but it did not load.zYThe system is using `google.auth` but the parsed credentials are of an incorrect type: %sz'System is set to not use `google.auth`.zCredentials from google.auth specified, but google-api-python-client is unable to use these credentials unless google-auth-httplib2 is installed. Please install google-auth-httplib2.)r   	authorizez0Unsupported credential type: {type(credentials)})r7   _HAS_GOOGLE_AUTHloggingerror
isinstancegoogle_credentialsCredentialstypedebug_HAS_GOOGLE_AUTH_HTTPLIB2rq   google_auth_httplib2r   rK   r   	TypeError)rL   r   r   s      r*   GetAuthorizedHttp BigqueryClient.GetAuthorizedHttp   s     F	
 %7%C%CDD(	
 mm=> J'33  '&$
 	
 "00HH {K((""4((HIIr)   r   c                    SnU R                   [        :w  a&  U R                   n[        R                  " SUU5        U$ Ub  [        R                  " SU5        U$ [        R
                  R                  (       a  [        R                  " SU5        U$  [        R                  " UU R                  US9nU(       a  [        R                  " SU5        U$ ! [         a#  n[        R                  " SUU5         SnAU$ SnAff = f)a#  Loads the local discovery document for the given service.

Args:
  service: The BigQuery service being used.
  discovery_url: The URL to load the discovery doc from.
  api_version: The API version for the targeted discovery doc.

Returns:
  discovery_document The loaded discovery document.
NzTSkipping local "%s" discovery document load since discovery_document has a value: %szOSkipping the local "%s" discovery document load since discovery_url has a valuez^Skipping local "%s" discovery document load since the bigquery_discovery_api_key flag was used)r   r9   r:   z(The "%s" discovery doc is already loadedz:Failed to load the "%s" discovery doc from local files: %s)r/   rJ   r   infor   BIGQUERY_DISCOVERY_API_KEY_FLAGpresentr   %load_local_discovery_doc_from_servicer9   FileNotFoundErrorwarning)rL   r   r   r:   r/   r   s         r*   _LoadDiscoveryDocumentLocal*BigqueryClient._LoadDiscoveryDocumentLocal%  s   " (*22ll

	   ll
: 1 
	1	1	9	9ll6
. !
%KKHH' 	 
,,A7
K   
H	
 	

 
s   >C 
C?C::C?c                    SnSnSn[        U S5      (       a  U R                  (       a  SS0OSnXe:  a  Uc  US:  a  [        R                  " SU-  5        US-  n [        R
                  " S	UU5        U(       a  UR                  X7S
9u  pOUR                  U5      u  pUR                  S5      n[        UR                  S5      5      S:  aF  SUR                  S5      < SU< 3n	[        R                  " SX5        [        R                  " U	5      e Xe:  a  Uc  M  U$ ! [        R                  [        R                   R"                  [$        R&                  4 a  n
[        U
S5      (       aG  Xe:X  a=  Sn[        U
S5      (       a  U
R(                  n[        R                  " SU
< SU< 35      e Sn
A
NXe:X  a-  [        R                  " S[*        R,                  " 5       < 35      e Sn
A
NSn
A
f[.         a*  n
Xe:X  a  [        R                  " SU
< 35      e Sn
A
GN
Sn
A
f[        R                   R0                   a(  n
[        R                  " S[3        U
5      < 35      eSn
A
ff = f)aq  Loads the discovery document from the provided URL.

Args:
  service: The BigQuery service being used.
  http: Http object to be used to execute request.
  discovery_url: The URL to load the discovery doc from.

Returns:
  discovery_document The loaded discovery document.

Raises:
  bq_error.BigqueryClientError: If the request to load the discovery
  document fails.
Nrb   r   use_uber_mintz#X-ESF-Use-Cloud-UberMint-If-Enabled1      z*Requesting "%s" discovery document from %s)headerszutf-8statusi  zGot z response from discovery url: z%s:
%scontentr,   z0Cannot contact server. Please try again.
Error: z

Content: z4Cannot contact server. Please try again.
Traceback: zInvalid API name or version: )rK   r   timesleepr   r   requestdecoderp   getr   r   BigqueryCommunicationErrorrl   HttpLib2Errorgoogleapiclienterrors	HttpErrorhttp_client_libHTTPExceptionr   	traceback
format_excIOErrorUnknownApiNameOrVersionstr)rL   r   r   r   r/   max_retries
iterationsr   response_metadatamsgr   r   s               r*   _LoadDiscoveryDocumentUrl(BigqueryClient._LoadDiscoveryDocumentUrla  sZ   *  KJ 4))d.@.@ 
/4 
 
"'9'A	a

1j=!Aoj4
8	

 26,, 3? 3
/
/ 37,,}2M
/
/66w? $$X./36##H-# --	3
;33C8
8 7% 
"'9'At A 
 
 

 
 
*
*

'
'  1i  &Gq)$$		g55#$g/ 	 ' &55''),  '
  $33GHJ  % ##;; 
 1114Q9
 	

s8   B9D" "=I)AG.72G..I);H  !I)#I$$I)discovery_root_urlc           
         [         R                  " SUU5        Uc  U R                  n[        R                  " U R
                  [        R                  " U R                  U R                  U R                  U R                  S9S9n[        R                  R                  U5      nU R                  (       a,  [        U R                  S5      (       a  SU R                  l        U R!                  5       n	U R#                  U R                  U	5      n
SnUc  U R%                  UUUS9nUSL nUc  Uc  U R'                  UUUUS9nU(       d  U R)                  UU
US9nU R+                  UUUS	9n[,        R.                  " S
U[0        R2                  R4                  S9   [6        R8                  " UU
UUS9nU$ ! [:         a    [         R<                  " SUU5        e f = f)zABuild and return BigQuery Dynamic client from discovery document.z8BuildApiClient discovery_url: %s, discovery_root_url: %sN)r6   r-   r7   r   )r1   r6   _quota_project_id)r   r   r:   )r   r:   r   r   )r   r   r   )r/   r   r   r/   )file_prefixr   apilog)r   modelrequestBuilderz3Error building from the "%s" discovery document: %s)r   r   r:   r   BigqueryModelr1   bq_utils'GetEffectiveQuotaProjectIDForHTTPHeaderr6   r-   r7   r   BigqueryHttpFactoryrK   r   r   r   r   r   r   OverrideEndpointr   #SaveStringToLogDirectoryIfAvailabler   APILOGrO   r   build_from_document	Exceptionr   )rL   r   r   r   r:   r   r   bigquery_modelbq_request_builderhttp_clientr   r/   discovery_url_not_provided"discovery_document_to_build_clientbuilt_clients                  r*   BuildApiClientBigqueryClient.BuildApiClient  s    LLB
 $$k #00jj!II!22 00((	
N '33;; (8(8:M N N+/d(,,.K!!$"2"2K@D!;;%! <  "/$!6!m&;**!!	 + m 99% :  *.)>)>-- *? *& 22(2$$ 22
,+	l   mm
?


 s   F/ /$Gc                     U R                   (       a"  [        R                  " S5        U R                   $ U R                  [        R
                  S9U l         U R                   $ )z>Returns a singleton ApiClient built for the BigQuery core API.z$Using the cached BigQuery API clientr   )rA   r   r   r   ServiceBIGQUERYrL   s    r*   	apiclientBigqueryClient.apiclient*  sK     ll9: ?? ++G4D4D+Edo??r)   c                 x    U R                   c"  U R                  [        R                  S9U l         U R                   $ )z'Returns the apiclient attached to self.r   )rG   r   r   r   r   s    r*   GetModelsApiClient!BigqueryClient.GetModelsApiClient3  s6    %#227;K;K2Ld!!!r)   c                 x    U R                   c"  U R                  [        R                  S9U l         U R                   $ z&Return the apiclient attached to self.r   )rB   r   r   r   r   s    r*   GetRoutinesApiClient#BigqueryClient.GetRoutinesApiClient9  s6    '!%!4!4W=M=M!4!Nd###r)   c                 x    U R                   c"  U R                  [        R                  S9U l         U R                   $ r   )rC   r   r   r   r   s    r*   GetRowAccessPoliciesApiClient,BigqueryClient.GetRowAccessPoliciesApiClient?  s>    **2,0,?,?"" -@ -d) ...r)   c                 x    U R                   c"  U R                  [        R                  S9U l         U R                   $ r   )rI   r   r   BQ_IAMr   s    r*   GetIAMPolicyApiClient$BigqueryClient.GetIAMPolicyApiClientG  s;    !!)#'#6#6.. $7 $d  %%%r)   c                 h    SnU(       a  U R                  U[        R                  S9$ U R                  $ )z4Return the apiclient that supports insert operation.N)r   r   )r   r   r   r   )rL   r   s     r*   GetInsertApiClient!BigqueryClient.GetInsertApiClientO  s9    M  %w/?/? !   >>r)   transferserver_addressc                 V   [         R                  " SU5        U R                  (       a"  [         R                  " S5        U R                  $ U=(       d'    [        R                  " [
        R                  [        S9nU R                  US[
        R                  S9U l        U R                  $ )z9Return the apiclient that supports Transfer v1 operation.z1GetTransferV1ApiClient transferserver_address: %sz$Using the cached Transfer API clientr   v1)r   r:   r   )	r   r   rD   r   r   r   DTSr   r   )rL   r   paths      r*   GetTransferV1ApiClient%BigqueryClient.GetTransferV1ApiClientX  s     LL;
 ll9: ### $ |'O'O++h(d "&!4!4++ "5 "d
 ###r)   reservationserver_addressc                 4   U R                   (       a"  [        R                  " S5        U R                   $ U=(       d'    [        R                  " [
        R                  [        S9nSnSnU R                  [
        R                  UUUUS9U l         U R                   $ )z:Return the apiclient that supports reservation operations.z(Using the cached Reservations API clientr   r   N)r   r   r   r:   r   )	rE   r   r   r   r   r   RESERVATIONSr   r   )rL   r  r  reservation_versionr   s        r*   GetReservationApiClient&BigqueryClient.GetReservationApiClientn  s     ""ll=>$ &&& $ 55**%  !f$($7$7&&!) %8 %d! &&&r)   connection_service_addressc                    U R                   (       a"  [        R                  " S5        U R                   $ U=(       d'    [        R                  " [
        R                  [        S9n[        R                  " USS9n[        R                  " U[        R                  R                  [        S9nU R                  UU[
        R                  SS9U l         U R                   $ )z:Return the apiclient that supports connections operations.z'Using the cached Connections API clientr   r   r   )r   universe_domainr   )r   r   r   r:   )rH   r   r   r   r   r   CONNECTIONSr   r   add_api_key_to_discovery_urlUNIVERSE_DOMAINrO   r   )rL   r
  r  r   s       r*   GetConnectionV1ApiClient'BigqueryClient.GetConnectionV1ApiClient  s     ))ll<=. ---) % 55))%  #BB
Dm #??%"2288!m
 ,0+>+>%!%%	 ,? ,d( ---r)   c                    Uc  U$ [         R                  " U5      n[        R                  " SUS   US   US   5        SnUS   nU(       a  [         R                  " U[
        S9US'   [        R                  R                  US   US   5      US'   [        R                  " SUS   US   US   5        [        R                  " U5      $ )	as  Override rootUrl for regional endpoints.

Args:
  discovery_document: BigQuery discovery document.
  service: The BigQuery service being used.
  discovery_root_url: The root URL to use for the discovery document.

Returns:
  discovery_document updated discovery document.

Raises:
  bq_error.BigqueryClientError: if location is not set and
    use_regional_endpoints is.
zgDiscovery doc routing values being considered for updates: rootUrl: (%s), basePath: (%s), baseUrl: (%s)rootUrlbasePathbaseUrlTr   servicePathzWDiscovery doc routing values post updates: rootUrl: (%s), basePath: (%s), baseUrl: (%s))r   parse_discovery_docr   r   r   r   urllibparseurljoinjsondumps)rL   r/   r   r   is_prodoriginal_root_urls         r*   r   BigqueryClient.OverrideEndpoint  s    ( !%99:LMLL	/9%:&9% G*95&2&N&N('# %+LL$8$89%'9-'H%y! LL	9%:&9% ::())r)   )rA   r~   rI   r   rG   rF   rH   rE   rD   rB   rC   r9   r:   r   r.   r/   r8   r4   r0   r5   r-   r6   r2   r1   r7   r3   )NN)NNNNN)N)4r#   r$   r%   r&   r'   r   enumEnumr+   rJ   r   TransitionWaitPrinterbq_client_utilsJobIdGeneratorIncrementingJobIdGeneratorRandomr	   r
   bytesobjectboolr   WaitPrinterJobIdGeneratorrp   'LegacyAndGoogleAuthCredentialsUnionTyper>   #columns_to_include_for_transfer_run&columns_excluded_for_make_transfer_runr   r   r   r   r   r   r   r   r   Resourcer   propertyr   r   r   r   r   r   r  r  r  r   r(   __classcell__)rS   s   @r*   r   r   M   s   B4TYY 4 #%"$7?! 
,
,9H9c9c

.
.
0: -1(,#GK'++U) U) 	U)
 3-U) 3-U)  vt 34U) U) c]U) U) %
2|///
0U) (66U)" %SM#U)$ !%U)& 'U)( CD)U)* !%+U) U)n)%  ,P($$T $("  C=	
 sm &&J:&J &J 	&JZ99 c]9 	9
 c5&()*9xYY Y 	Y
 c5&()*Y| &**.#'#'"ll c]l #3-	l
 C=l C=l sml l\ ++  ")"4"4 "$I$6$6 $/Y-?-? /&Y%7%7 &)"4"4  59$$,SM$$. 8<''/}''4 9=.(0..D +/	8*U
+8* 8* #3-	8*
 }8* 8*r)   r   )=r'   
__future__r   r   r   r   r   r   r   r  r   r|   r   r   typingr   r   r	   r
   r  abslr   rw   r   r   rl   typing_extensionsr   r   r   credential_loaderauthr   clientsr   r   r#  r   discovery_documentsr   r   r   r   r   google.authr   r   r   ImportErrorr   r   r'  rJ   GoogleAuthCredentialsUnionTypeCredentialsFromFlagsUnionTyper+  r   r   __annotations__r   r   r"   r)   r*   <module>r>     s3    &  %  *      2 2     %  '    ' ! ,   8 9   ;
$" 8*/99335+ '
 

MMNi  "MM)+	 P
* P
*A    $#$s$   %D .D& D#"D#&D10D1