
                            S r SSKJr  SSKJ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
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Jr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\R:                  5      rS\S\\\   \\   \\   4   4S jr S\S\\\   \\   \\   4   4S jr!S\S\\\   \\   \\   4   4S jr"S\S\\#   S\\\   \\   \\   4   4S jr$S\S\\\   \\   \\   4   4S jr%S\S\\\   \\   \\   \\   4   4S jr&S\S\\\   \\   \\   \\   4   4S jr'S\S\\\   \\   \\   4   4S jr(S\S\\\   \\   \\   4   4S jr)S\S\\\   \\   \\   4   4S jr*S\S\\\   \\   \\   4   4S jr+S\4S jr,S r-S  r. " S! S"\R^                  5      r0 " S# S$\05      r1 " S% S&\05      r2 " S' S(\05      r3 " S) S*\05      r4S+ r5S\S\\\4   4S, jr6S-\S.\S\\\4   4S/ jr7S\S\\\\4   4S0 jr8 SVS1\" S2S3\\   4/5      S\S\Rr                  Rt                  4S4 jjr; SVS1\" S2S3\\   4S-\\   4/5      S\S\Rr                  Rx                  4S5 jjr=  SWS1\" S2S3\\   4S-\\   4/5      S\S6\S\Rr                  R|                  4S7 jjr?  SWS1\" S2S3\\   4S-\\   4/5      S8\S9\S\Rr                  R                  4S: jjrA SVS1\" S2S3\\   4S-\\   4/5      S\S\Rr                  R                  4S; jjrC SVS1\" S2S3\\   4S-\\   4/5      S\S\Rr                  R                  4S< jjrES\S\\\4   4S= jrF SVS1\" S2S3\\   4S-\\   4/5      S\S\\Rr                  R|                  \Rr                  Rx                  \Rr                  Rt                  4   4S> jjrG  SXS1\" S2S3\\   4/5      S\S?\\   S\Rr                  R                  4S@ jjrI    SYS1\" S2S3\\   4SA\\   4/5      S\\   S?\\   SB\\   SC\#S\Rr                  R                  4SD jjrK SZS1\" S2S3\\   4/5      S?\\   S\Rr                  R                  4SE jjrM     S[S1\" S2S3\\   4/5      S\\   S\\   S?\\   SF\\   S\\#   S\Rr                  R                  4SG jjrO     S[S1\" S2S3\\   4SA\\   4/5      S\\   S\\   S?\\   SB\\   SH\\   S\Rr                  R                  4SI jjrQ     S\S1\" S2S3\\   4/5      S\\   S\\   S?\\   SJ\\   SK\#S\Rr                  R                  4SL jjrS    S]S1\" S2S3\\   4SA\\   4/5      S\\   S\\   S?\\   SM\\   S\Rr                  R                  4SN jjrUSO rVSP rWSQ rXSR\S\\   4SS jrYS1\" S2S3\\   4/5      ST\Rr                  Rt                  S\Rr                  Rt                  4SU jrZg)^z#Bigquery Client library for Python.    )absolute_import)division)print_functionN)DictList
NamedTupleOptionalTupleUnion)flags)bq_error)bq_id_utils)bq_processor_utils   c                   $    \ rS rSrSrSrSrSrSrg)
UpdateMode   zEnum for update modes.UPDATE_METADATA
UPDATE_ACLUPDATE_FULL N)	__name__
__module____qualname____firstlineno____doc__r   r   r   __static_attributes__r       platform/bq/clients/utils.pyr   r      s    %/*+r   r   
identifierreturnc                 `   SnSnSn[         R                  " SUUUS.-  [         R                  5      n[         R                  " X@5      nU(       a_  UR	                  5       R                  SS5      UR	                  5       R                  SS5      UR	                  5       R                  S	S5      4$ g
)a  Parses a job identifier string into its components.

Args:
  identifier: String specifying the job identifier in the format
    "project_id:job_id", "project_id:location.job_id", or "job_id".

Returns:
  A tuple of three elements: containing project_id, location,
  job_id. If an element is not found, it is represented by
  None. If no elements are found, the tuple contains three None
  values.
z[\w:\-.]*[\w:\-]+z[a-zA-Z\-0-9]+z[\w\-]+zz
    ^((?P<project_id>%(PROJECT_ID)s)
    :)?
    ((?P<location>%(LOCATION)s)
    \.)?
    (?P<job_id>%(JOB_ID)s)
    $
  )
PROJECT_IDLOCATIONJOB_ID
project_idNlocationjob_id)NNN)recompileXsearch	groupdictget)r    project_id_patternlocation_patternjob_id_patternpatternmatchs         r   _ParseJobIdentifierr4   %   s     ,&.JJ +&"	 	dd'" ))G
(%
lD1j$/h- 
 
r   c                    [         R                  " S[         R                  5      n[         R                  " X5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nX4U4$ )a  Parses the reservation identifier string into its components.

Args:
  identifier: String specifying the reservation identifier in the format
    "project_id:reservation_id", "project_id:location.reservation_id", or
    "reservation_id".

Returns:
  A tuple of three elements: containing project_id, location, and
  reservation_id. If an element is not found, it is represented by None.

Raises:
  bq_error.BigqueryError: if the identifier could not be parsed.
zh
  ^((?P<project_id>[\w:\-.]*[\w:\-]+):)?
  ((?P<location>[\w\-]+)\.)?
  (?P<reservation_id>[\w\-]*)$
  z*Could not parse reservation identifier: %sr&   Nr'   reservation_idr)   r*   r+   r,   r   BigqueryErrorr-   r.   )r    r2   r3   r&   r'   r6   s         r   _ParseReservationIdentifierr9   S   s    $ JJ
 	dd' ))G
(%	

 
 4zA   $$\48*__"":t4(??$(()94@.
	//r   pathc                   ^ [         R                  " S[         R                  5      n[         R                  " X5      mT(       d  [        R
                  " SU -  5      eU4S jnU" S5      nU" S5      nU" S5      =(       d    U" S5      nX4U4$ )a  Parses the reservation path string into its components.

Args:
  path: String specifying the reservation path in the format
    projects/<project_id>/locations/<location>/reservations/<reservation_id>
    or projects/<project_id>/locations/<location>/biReservation

Returns:
  A tuple of three elements: containing project_id, location and
  reservation_id. If an element is not found, it is represented by None.

Raises:
  bq_error.BigqueryError: if the path could not be parsed.
z^projects/(?P<project_id>[\w:\-.]*[\w:\-]+)?/locations/(?P<location>[\w\-]+)?/(reservations/(?P<reservation_id>[\w\-/]+)|(?P<bi_id>biReservation))$z$Could not parse reservation path: %sc                 D   > TR                  5       R                  U S 5      $ Nr-   r.   keyr3   s    r   <lambda>&ParseReservationPath.<locals>.<lambda>       eoo'++C6r   r&   r'   r6   bi_idr)   r*   r+   r,   r   r8   )r:   r2   groupr&   r'   r6   r3   s         @r   ParseReservationPathrG   z   s    $ JJ 	dd
' ))G
"%	

 
 !G$!N
OO
6%\"*:()*<eGn.
	//r   allow_commasc                    SnU(       a&  [         R                  " S[         R                  5      nO%[         R                  " S[         R                  5      n[         R                  " X 5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nXEU4$ )aE  Parses the capacity commitment identifier string into its components.

Args:
  identifier: String specifying the capacity commitment identifier in the
    format "project_id:capacity_commitment_id",
    "project_id:location.capacity_commitment_id", or "capacity_commitment_id".
  allow_commas: whether to allow commas in the capacity commitment id.

Returns:
  A tuple of three elements: containing project_id, location
  and capacity_commitment_id. If an element is not found, it is represented by
  None.

Raises:
  bq_error.BigqueryError: if the identifier could not be parsed.
Nzy
    ^((?P<project_id>[\w:\-.]*[\w:\-]+):)?
    ((?P<location>[\w\-]+)\.)?
    (?P<capacity_commitment_id>[\w|,-]*)$
    zx
    ^((?P<project_id>[\w:\-.]*[\w:\-]+):)?
    ((?P<location>[\w\-]+)\.)?
    (?P<capacity_commitment_id>[\w|-]*)$
    z2Could not parse capacity commitment identifier: %sr&   r'   capacity_commitment_idr7   )r    rH   r2   r3   r&   r'   rJ   s          r   "_ParseCapacityCommitmentIdentifierrK      s    & 'jj	
 	G jj	
 	G ))G
(%	

 
 <zI   $$\48*__"":t4( ??,001I4P
 6	77r   c                    [         R                  " S[         R                  5      n[         R                  " X5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nX4U4$ )a  Parses the capacity commitment path string into its components.

Args:
  path: String specifying the capacity commitment path in the format
    projects/<project_id>/locations/<location>/capacityCommitments/<capacity_commitment_id>

Returns:
  A tuple of three elements: containing project_id, location,
  and capacity_commitment_id. If an element is not found, it is represented by
  None.

Raises:
  bq_error.BigqueryError: if the path could not be parsed.
z
  ^projects\/(?P<project_id>[\w:\-.]*[\w:\-]+)?
  \/locations\/(?P<location>[\w\-]+)?
  \/capacityCommitments\/(?P<capacity_commitment_id>[\w|-]+)$
  z,Could not parse capacity commitment path: %sr&   Nr'   rJ   r7   )r:   r2   r3   r&   r'   rJ   s         r   ParseCapacityCommitmentPathrM      s    " JJ
 	dd' ))G
"%	

 
 6=   $$\48*__"":t4( ??,001I4P
 6	77r   c                    [         R                  " S[         R                  5      n[         R                  " X5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nX4XV4$ )a  Parses the reservation assignment identifier string into its components.

Args:
  identifier: String specifying the reservation assignment identifier in the
    format "project_id:reservation_id.assignment_id",
    "project_id:location.reservation_id.assignment_id", or
    "reservation_id.assignment_id".

Returns:
  A tuple of three elements: containing project_id, location, and
  reservation_assignment_id. If an element is not found, it is represented by
  None.

Raises:
  bq_error.BigqueryError: if the identifier could not be parsed.
z
  ^((?P<project_id>[\w:\-.]*[\w:\-]+):)?
  ((?P<location>[\w\-]+)\.)?
  (?P<reservation_id>[\w\-\/]+)\.
  (?P<reservation_assignment_id>[\w\-_]+)$
  z5Could not parse reservation assignment identifier: %sr&   Nr'   r6   reservation_assignment_idr7   )r    r2   r3   r&   r'   r6   rO   s          r   %_ParseReservationAssignmentIdentifierrP      s    ( JJ 	dd' ))G
(%	

 
 ?*L   $$\48*__"":t4(??$(()94@.#oo/33!4 	JJr   c                    [         R                  " S[         R                  5      n[         R                  " X5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nX4XV4$ )a%  Parses the reservation assignment path string into its components.

Args:
  path: String specifying the reservation assignment path in the format
    projects/<project_id>/locations/<location>/
    reservations/<reservation_id>/assignments/<assignment_id> The
    reservation_id must be that of a top level reservation.

Returns:
  A tuple of three elements: containing project_id, location and
  reservation_assignment_id. If an element is not found, it is represented by
  None.

Raises:
  bq_error.BigqueryError: if the path could not be parsed.
z
  ^projects\/(?P<project_id>[\w:\-.]*[\w:\-]+)?
  \/locations\/(?P<location>[\w\-]+)?
  \/reservations\/(?P<reservation_id>[\w\-]+)
  \/assignments\/(?P<reservation_assignment_id>[\w\-_]+)$
  z/Could not parse reservation assignment path: %sr&   Nr'   r6   rO   r7   )r:   r2   r3   r&   r'   r6   rO   s          r   ParseReservationAssignmentPathrR   )  s    ( JJ 	dd' ))G
"%	

 
 9D@   $$\48*__"":t4(??$(()94@.#oo/33!4 	JJr   c                    [         R                  " S[         R                  5      n[         R                  " X5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nX4U4$ )a  Parses the reservation group identifier string into its components.

Args:
  identifier: String specifying the reservation group identifier in the format
    "project_id:reservation_group_id",
    "project_id:location.reservation_group_id", or "reservation_group_id".

Returns:
  A tuple of three elements: containing project_id, location, and
  reservation_group_id. If an element is not found, it is represented by None.

Raises:
  bq_error.BigqueryError: if the identifier could not be parsed.
zn
  ^((?P<project_id>[\w:\-.]*[\w:\-]+):)?
  ((?P<location>[\w\-]+)\.)?
  (?P<reservation_group_id>[\w\-]*)$
  z0Could not parse reservation group identifier: %sr&   Nr'   reservation_group_idr7   )r    r2   r3   r&   r'   rT   s         r    _ParseReservationGroupIdentifierrU   V  s    $ JJ
 	dd' ))G
(%	

 
 :ZG   $$\48*__"":t4(*../EtL
 4	55r   c                    ^ [         R                  " S[         R                  5      n[         R                  " X5      mT(       d  [        R
                  " SU -  5      eU4S jnU" S5      nU" S5      nU" S5      nX4U4$ )a  Parses the reservation group path string into its components.

Args:
  path: String specifying the reservation group path in the format
    projects/<project_id>/locations/<location>/reservationGroups/<reservation_group_id>

Returns:
  A tuple of three elements: containing project_id, location and
  reservation_group_id. If an element is not found, it is represented by None.

Raises:
  bq_error.BigqueryError: if the path could not be parsed.
z
  ^projects\/(?P<project_id>[\w:\-.]*[\w:\-]+)?
  \/locations\/(?P<location>[\w\-]+)?
  \/reservationGroups\/(?P<reservation_group_id>[\w|-]+)$
  z*Could not parse reservation group path: %sc                 D   > TR                  5       R                  U S 5      $ r=   r>   r?   s    r   rA   +ParseReservationGroupPath.<locals>.<lambda>  rC   r   r&   r'   rT   rE   )r:   r2   rF   r&   r'   rT   r3   s         @r   ParseReservationGroupPathrY   }  s    " JJ
 	dd' ))G
"%	

 
 4t;  7%\"*:(56
 4	55r   c                 $   U (       d  [         R                  " S5      eU R                  S5      n[        U5      nUS:  a  [         R                  " SU -  5      eXS-
     nUS:  a  XS-
     OSnUS:  a  SR	                  USUS-
   5      OSnXTU4$ )a  Parses the connection identifier string into its components.

Args:
  identifier: String specifying the connection identifier in the format
    "connection_id", "location.connection_id",
    "project_id.location.connection_id"

Returns:
  A tuple of four elements: containing project_id, location, connection_id
  If an element is not found, it is represented by None.

Raises:
  bq_error.BigqueryError: if the identifier could not be parsed.
zEmpty connection identifier.   z)Could not parse connection identifier: %s      N)r   r8   splitlenjoin)r    tokens
num_tokensconnection_idr'   r&   s         r   _ParseConnectionIdentifierre     s    $ 


 
 !>
??C &6{*!^

 
 3j@  a(-'1A~VN#4(5?!^sxx/a01*
	..r   c                    [         R                  " S[         R                  5      n[         R                  " X5      nU(       d  [        R
                  " SU -  5      eUR                  5       R                  SS5      nUR                  5       R                  SS5      nUR                  5       R                  SS5      nX4U4$ )a  Parses the connection path string into its components.

Args:
  path: String specifying the connection path in the format
    projects/<project_id>/locations/<location>/connections/<connection_id>

Returns:
  A tuple of three elements: containing project_id, location and
  connection_id. If an element is not found, it is represented by None.

Raises:
  bq_error.BigqueryError: if the path could not be parsed.
z
  ^projects\/(?P<project_id>[\w:\-.]*[\w:\-]+)?
  \/locations\/(?P<location>[\w\-]+)?
  \/connections\/(?P<connection_id>[\w\-\/]+)$
  z#Could not parse connection path: %sr&   Nr'   rd   r7   )r:   r2   r3   r&   r'   rd   s         r   ParseConnectionPathrg     s    " JJ
 	dd' ))G
"%	

 
 !F!M
NN $$\48*__"":t4(//#''>-
	..r   table_constraintsc                 T   U (       d  [         R                  " S5      e[        R                  R	                  U 5      (       a.  [        U 5       n [        R                  " U5      n SSS5        U$ [        R                  " SU 5      b  [         R                  " SU < S35      e [        R                  " U 5      nU$ ! [         a!  n[         R                  " SU < S35      UeSnAff = f! , (       d  f       W$ = f! [         a!  n[         R                  " SU < S35      UeSnAff = f)	az  Create table constraints json object from string or a file name.

Args:
  table_constraints: Either a json string that presents a table_constraints
    proto or name of a file that contains the json string.

Returns:
  The table_constraints (as a json object).

Raises:
  bq_error.BigqueryTableConstraintsError: If load the table constraints
    from the string or file failed.
z!table_constraints cannot be emptyz0Error decoding JSON table constraints from file r[   Nz^[./~\\]z"Error reading table constraints: "+" looks like a filename, but was not found.z2Error decoding JSON table constraints from string )r   BigqueryTableConstraintsErrorosr:   existsopenjsonload
ValueErrorr)   r,   loads)rh   floaded_jsones       r   ReadTableConstrantsrv     s    


0
0+  WW^^%&&		 Aiil 
! YY{-.: 
0
0 1	4 **./K 
)  44 #
 	 
!	   
 

0
0	 sB   C*B<$C< <
C'C""C''C**
C9<
D'D""D'c           	         U R                   R                  SS5      R                  S5      (       a;  [        R                  " U R
                  R                  S5      5      n[        U5        gSn[        R                  R                  (       a  Sn[        R                  " SU< SU R                   R                  SS	5      < S
U R
                  < 35      e)z*Raises a BigQueryError given an HttpError.zcontent-type zapplication/jsonutf-8z6 The specified regional endpoint may not be supported.z'Could not connect with BigQuery server.z
Http response status: statusz(unexpected)z
Http response content:
N)respr.   
startswithro   rr   contentdecode
RaiseErrorr   FLAGSuse_regional_endpointsr   BigqueryCommunicationError)ru   r}   error_detailss      r   RaiseErrorFromHttpErrorr     s    VVZZ#../ABBjj))'23Gw M{{))Nm

-
- !&&**X~>			K r   c                 6    [         R                  " SU < 35      e)z-Raises a BigQueryError given a non-HttpError.z/Could not connect with BigQuery server due to: )r   r   )ru   s    r   RaiseErrorFromNonHttpErrorr   )  s    ++=>@	 r   c                   B    \ rS rSrSrS r\R                  S 5       rSr	g)JobIdGeneratori0  z!Base class for job id generators.c                     g r=   r   )selfs    r   __init__JobIdGenerator.__init__3  s    r   c                     g)z0Generates a job_id to use for job_configuration.Nr   r   job_configurations     r   GenerateJobIdGenerator.Generate6  s    r   r   N)
r   r   r   r   r   r   abcabstractmethodr   r   r   r   r   r   r   0  s$    )	 ; ;r   r   c                       \ rS rSrSrS rSrg)JobIdGeneratorNonei;  zGJob id generator that returns None, letting the server pick the job id.c                     g r=   r   r   s     r   r   JobIdGeneratorNone.Generate>  s    r   r   Nr   r   r   r   r   r   r   r   r   r   r   r   ;  s
    Or   r   c                       \ rS rSrSrS rSrg)JobIdGeneratorRandomiB  z"Generates random job id_fallbacks.c                     S[         R                  " 5       R                  S[        R                  5      [        [        R                  " 5       S-  5      4-  $ )Nzbqjob_r%08x_%016xr   i  )randomSystemRandomrandintsysmaxsizeinttimer   s     r   r   JobIdGeneratorRandom.GenerateE  sD    %%a5DIIK$"  r   r   Nr   r   r   r   r   r   B  s
    *r   r   c                   *    \ rS rSrSrS rS rS rSrg)JobIdGeneratorFingerprintiL  z5Generates job ids that uniquely match the job config.c                 d    [        U[        5      (       a  U$ [        U5      R                  S5      $ )Nry   )
isinstancebytesstrencode)r   objs     r   _HashableRepr'JobIdGeneratorFingerprint._HashableReprO  s'    #ujs8??7##r   c                 ,   [        UR                  5       5      nUR                  5         U H  nUR                  U R	                  U5      5        X   n[        U[        5      (       a*  [        R                  " SU5        U R                  XR5        Mf  [        U[         5      (       a3  [        R                  " SU5        U H  nU R                  Xb5        M     M  [        R                  " SXE5        UR                  U R	                  U5      5        M     g)z!Computes the sha1 hash of a dict.zHashing: %s...zHashing: %s ...zHashing: %s:%sN)
listkeyssortupdater   r   dictlogginginfo_Hash)r   configsha1r   r@   vinner_vs          r   r   JobIdGeneratorFingerprint._HashT  s    D 	IIK
kk$$$S)*
+a	At		%s+

1a&,G
**W
#  	%s.D&&q)* r   c                     [         R                  " 5       nU R                  X5        SUR                  5       < 3n[        R
                  " SX5        U$ )Nbqjob_czFingerprinting: %s:
%s)hashlibr   r   	hexdigestr   r   )r   r   s1r(   s       r   r   "JobIdGeneratorFingerprint.Generateh  s=    	BJJ %LLN,FLL*,=FMr   r   N)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   L  s    =$
+(r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )JobIdGeneratorIncrementingip  z7Generates job ids that increment each time we're asked.c                 <   > [         TU ]  5         Xl        SU l        g )Nr   )superr   _inner_retry)r   inner	__class__s     r   r   #JobIdGeneratorIncrementing.__init__s  s    	GKDKr   c                     U =R                   S-  sl         SU R                  R                  U5      U R                   4-  $ )Nr]   z%s_%d)r   r   r   r   s     r   r   #JobIdGeneratorIncrementing.Generatex  s4    KK1Kdkk**+<=t{{KKKr   )r   r   )	r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r   p  s    ?
L Lr   r   c                 T     [        U 5      $ ! [         a    [        SU < 35      ef = f)NzInvalid value for wait: )r   rq   )waits    r   NormalizeWaitr   }  s.    =t9	 =
T;
<<=s   
 'c                 |    U R                  S5      (       a  SU [        S5      S  4$ U R                  S5      u  pnX4$ )Nzplx.google:z
plx.google:)r|   r`   
rpartition)r    r&   _
dataset_ids       r   _ParseDatasetIdentifierr     sI    =))C$6$8999 * 5 5c :J:!!r   r   table_idc                     U (       a  U(       d  X4$ U R                  S5      nUS   S:w  d  US;   a  X4$ SR                  USS 5      SU-   4$ )zDMoves "INFORMATION_SCHEMA" to table_id for dataset qualified tables.r[   INFORMATION_SCHEMA)SCHEMATASCHEMATA_OPTIONSNzINFORMATION_SCHEMA.)r_   ra   )r   r   dataset_partss      r   _ShiftInformationSchemar     sf    	8""3'-2..( ? 3 	-$	%'<x'G	GGr   c                     [         R                  " SU 5      (       a  U SS4$ U R                  S5      u  pnSU;   a  UR                  S5      u  pBnOU(       a  UnSnOSnUn[        XE5      u  pEXU4$ )a!  Parses identifier into a tuple of (possibly empty) identifiers.

This will parse the identifier into a tuple of the form
(project_id, dataset_id, table_id) without doing any validation on
the resulting names; missing names are returned as ''. The
interpretation of these identifiers depends on the context of the
caller. For example, if you know the identifier must be a job_id,
then you can assume dataset_id is the job_id.

Args:
  identifier: string, identifier to parse

Returns:
  project_id, dataset_id, table_id: (string, string, string)
z ^\w[\w.]*\.[\w.]+:\w[\w\d_-]*:?$rx   r   r[   )r)   r,   r   r   )r    r&   r   dataset_and_table_idr   r   s         r   _ParseIdentifierr     s    $ YY2J??r2(2(=(=c(B%*%  2==cBJ8 &JH J#H0F*		))r   id_fallbacksIDSr&   c                 R   [        U5      u  p#n U=(       d    U=(       d    U R                  nU(       d.  U(       a'  [        R                  R                  R                  US9$ US:X  a  [        R                  " S5      e[        R                  " SU< 35      e! [         a     NBf = f)z=Determine a project reference from an identifier or fallback.)	projectIdrx   zPlease provide a project ID.z&Cannot determine project described by )	r   r&   r   ApiClientHelperProjectReferenceCreaterq   r   BigqueryClientError)r   r    r&   r   r   s        r   GetProjectReferencer     s     &6j%A"*(		 BxB<+B+BJ*((99@@ A  
 2

&
&'E
FF

&
&6@B  
 		s   AB 
B&%B&c                    U(       d  U R                   OUn[        U5      u  p#nU(       a  U(       d  U(       d  U R                  nUnONU(       a  U(       a  U(       a	  US-   U-   nO0U(       a  U(       a  U(       d  O[        R                  " SU< 35      e [
        R                  R                  R                  X#S9$ ! [         a   n[        R                  " SU< 35      UeSnAff = f)z<Determine a DatasetReference from an identifier or fallback.r[   z&Cannot determine dataset described by )r   	datasetIdN)
r   r   r&   r   r8   r   r   DatasetReferencer   rq   r   r    r&   r   r   ru   s         r   GetDatasetReferencer     s     /9|&&j*%5j%A"*(j((JJjXc!H,Jj

 
 6@B &&77>> ?   
 

 
 6@Bs   &B> >
C(C##C(default_dataset_idc                 N   [        U5      u  p4nU(       d  [        U R                  5      u  p4U(       a	  U(       d  Un [        R                  R
                  R                  U=(       d    U R                  UUS9$ ! [         a   n[        R                  " SU< 35      UeSnAff = f)z<Determine a TableReference from an identifier and fallbacks.)r   r   tableIdz$Cannot determine table described by N)r   r   r   r   r   TableReferencer   r&   rq   r   r8   )r   r    r   r&   r   r   ru   s          r   GetTableReferencer   	  s     &6j%A"*(	4\5L5LMJ
#J	&&55<<7 7 7 =  
 
 

 
 4>@s   ;A: :
B$BB$table_identifier	policy_idc                     [        X5      n[        R                  R                  R	                  UR
                  UR                  UR                  US9$ ! [         a$  n[        R                  " SU< SU< 35      UeSnAff = f)zFDetermine a RowAccessPolicyReference from an identifier and fallbacks.)r   r   r   policyIdz0Cannot determine row access policy described by z and N)r   r   r   RowAccessPolicyReferencer   r   r   r   rq   r   r8   )r   r   r   table_referenceru   s        r   GetRowAccessPolicyReferencer   &  s    'GO&&??FF!++!++''	 G   
 

 
 Y	( s   AA 
BA>>Bc                 ,   [        U5      u  p#nU(       d  [        U R                  5      u  p# [        R                  R
                  R                  U=(       d    U R                  UUS9$ ! [         a  n[        R                  " SU-  5      UeSnAff = f)z,Returns a ModelReference from an identifier.)r   r   modelIdz&Cannot determine model described by %sN)r   r   r   r   r   ModelReferencer   r&   rq   r   r8   r   s         r   GetModelReferencer   A  s     &6j%A"*(	4\5L5LMJ	&&55<<7 7 7 =  
 
 

 
 0:=   ;A* *
B4BBc                 ,   [        U5      u  p#nU(       d  [        U R                  5      u  p# [        R                  R
                  R                  U=(       d    U R                  UUS9$ ! [         a  n[        R                  " SU-  5      UeSnAff = f)z.Returns a RoutineReference from an identifier.)r   r   	routineIdz(Cannot determine routine described by %sN)r   r   r   r   r   RoutineReferencer   r&   rq   r   r8   r   s         r   GetRoutineReferencer  [  s     &6j%A"*(	4\5L5LMJ	&&77>>7 7 7 ?  
 
 

 
 2Z?r   c                 H    [        U 5      u  p[        US9nU(       a  XS'   U$ )N)r   r   )r   r   )r    parsed_project_idparsed_dataset_idresults       r   GetQueryDefaultDatasetr	  u  s+    )@)L&+,&+;	-r   c                     [        X5      $ ! [        R                   a     Of = f [        X5      $ ! [        R                   a     Of = f [	        X5      $ ! [        R                   a     Of = f[        R                  " SU< S35      e)a  Try to deduce a project/dataset/table reference from a string.

If the identifier is not compound, treat it as the most specific
identifier we don't have as a flag, or as the table_id. If it is
compound, fill in any unspecified part.

Args:
  id_fallbacks: The IDs cached on BigqueryClient.
  identifier: string, Identifier to create a reference for.

Returns:
  A valid ProjectReference, DatasetReference, or TableReference.

Raises:
  bq_error.BigqueryError: if no valid reference can be determined.
z Cannot determine reference for "")r   r   r8   r   r   )r   r    s     r   GetReferencer  }  s    <	\66			 			|88			 			|88			 		/9;	 s-   
 $$
3 A
	A

A A0/A0default_locationc                 
   [        U5      u  p4nU(       d  U R                  nU(       d  UnU(       a)   [        R                  R                  R                  X5US9$ [        R                  " SU< 35      e! [         a     N&f = f)zEDetermine a JobReference from an identifier, location, and fallbacks.)r   jobIdr'   z"Cannot determine job described by )	r4   r&   r   r   JobReferencer   rq   r   r8   )r   r    r  r&   r'   r(   s         r   GetJobReferencer    s     "5Z!@*	((J	H((55<<x =  
 	0:<	   
s   'A5 5
BBapi_versiondefault_reservation_idcheck_reservation_projectc                    [        US9u  pVnU(       aP  U(       aI  U R                  (       a8  XPR                  :w  a)  [        R                  " SU R                  < SU< S35      eU=(       d    U R                  nU(       d  [        R                  " S5      eU=(       d    UnU(       d  [        R                  " S5      eU(       aA  UR	                  5       UR	                  5       :w  a  [        R                  " SU< SU< S35      eU=(       d    UnU(       d  [        R                  " S	5      e[
        R                  R                  XVUS
9$ )ADetermine a ReservationReference from an identifier and location.)r    Specified project 'z8' should be the same as the project of the reservation ''.Project id not specified.Location not specified.Specified location 'z9' should be the same as the location of the reservation 'zReservation name not specified.)r   r'   reservationId)r9   r&   r   r8   lowerr   r   ReservationReference)r   r    r  r  r  r&   r'   r6   s           r   GetReservationReferencer    s    *E*&*  


!
!
//
/

 
 +66
	D  4\44*	

 
 !<
==))(	

 
 !:
;;(..*.>.D.D.FF

 
 /	;  ";%;.	

 
 !B
CC&&;;~ <  r   c                     U R                   nU(       d  [        R                  " S5      eUnU(       d  [        R                  " S5      e[        R                  R
                  R                  X#S9$ )r  r  r  )r   r'   )r&   r   r8   r   r   BiReservationReferencer   )r   r  r&   r'   s       r   GetBiReservationReferencer"    sf     &&*	

 
 !<
==(	

 
 !:
;;		$	$	;	;	B	B 
C 
 r   default_capacity_commitment_idc                    Ub  [        X5      u  pgnO(Ub  [        U5      u  pgnO[        R                  " S5      eU=(       d    U R                  nU(       d  [        R                  " S5      eU=(       d    UnU(       d  [        R                  " S5      eU=(       d    UnU(       d  [        R                  " S5      e[
        R                  R                  R                  UUUS9$ )zHDetermine a CapacityCommitmentReference from an identifier and location.,Either identifier or path must be specified.r  r  z%Capacity commitment id not specified.)r   r'   capacityCommitmentId)	rK   rM   r   r8   r&   r   r   CapacityCommitmentReferencer   )	r   r    r:   r  r#  rH   r&   r'   rJ   s	            r   GetCapacityCommitmentReferencer(    s     *:D 1J0 3N40J0 
 
 !O
PP4\44*	

 
 !<
==))(	

 
 !:
;;> >  
 

 
 !H
II		$	$	@	@	G	G1 
H 
 r   !default_reservation_assignment_idc                    Ub  [        U5      u  pgpO(Ub  [        U5      u  pgpO[        R                  " S5      eU=(       d    U R                  nU(       d  [        R                  " S5      eU=(       d    UnU(       d  [        R                  " S5      eU=(       d    UnU	=(       d    Un	[
        R                  R                  R                  UUUU	S9$ )z7Determine a ReservationAssignmentReference from inputs.r%  r  r  )r   r'   r  reservationAssignmentId)	rP   rR   r   r8   r&   r   r   ReservationAssignmentReferencer   )
r   r    r:   r  r  r)  r&   r'   r6   rO   s
             r   !GetReservationAssignmentReferencer-  9  s     -j9 FZ>+D &t, FZ>+D 
 
 !O
PP4\44*	

 
 !<
==))(	

 
 !:
;;!;%;.D#D  
	$	$	C	C	J	J"7	 
K 
 r   default_reservation_group_idcheck_reservation_group_projectc                    Ub  [        U5      u  pgnO(Ub  [        U5      u  pgnO[        R                  " S5      eU(       aP  U(       aI  U R                  (       a8  X`R                  :w  a)  [        R                  " SU R                  < SU< S35      eU=(       d    U R                  nU(       d  [        R                  " S5      eU=(       d    UnU(       d  [        R                  " S5      eU(       aA  UR                  5       UR                  5       :w  a  [        R                  " SU< SU< S35      eU=(       d    UnU(       d  [        R                  " S	5      e[        R                  R                  UUUS
9$ )z3Determines a ReservationGroupReference from inputs.r%  r  z>' should be the same as the project of the reservation group 'r  r  r  r  z?' should be the same as the location of the reservation group 'z#Reservation group id not specified.)r   r'   reservationGroupId)	rU   rY   r   r8   r&   r  r   r   ReservationGroupReference)	r   r    r:   r  r.  r/  r&   r'   rT   s	            r   GetReservationGroupReferencer3  d  sW    (4 1Z/ 3L40Z/ 
 
 !O
PP &


!
!
//
/

 
 %1%<%<j	J  4\44*	

 
 !<
==))(	

 
 !:
;;(..*.>.D.D.FF

 
 %5x	A 
 .M1M	

 
 !F
GG&&@@/ A  r   default_connection_idc                    Ub  [        U5      u  pVnOUb  [        U5      u  pVnW=(       d    U R                  nU(       d  [        R                  " S5      eW=(       d    UnU(       d  [        R                  " S5      eW=(       d    UnU(       d  [        R                  " S5      e[
        R                  R                  R                  XVUS9$ )z@Determine a ConnectionReference from an identifier and location.r  r  zConnection name not specified.)r   r'   connectionId)	re   rg   r&   r   r8   r   r   ConnectionReferencer   )r   r    r:   r  r4  r&   r'   rd   s           r   GetConnectionReferencer8    s     ,F-)Z= ,?,E)Z=4\44*	

 
 !<
==))(	

 
 !:
;;8#8-	

 
 !A
BB		$	$	8	8	?	?M 
@ 
 r   c                 |    U R                  S0 5      R                  S0 /5      S   n[        R                  " X/ 5      e)zARaises an appropriate BigQuery error given the json error result.errorerrorsr   )r.   r   CreateBigqueryError)r  r:  s     r   r   r     s9    
**Wb
!
%
%h
5a
8%$$UB77r   c                 ,    SU R                  S0 5      ;   $ )z3Predicate to determine whether or not a job failed.errorResultrz   )r.   )jobs    r   IsFailedJobr@    s    	#''(B/	//r   c           
          [        U 5      (       af  U S   S   nU S   R                  S/ 5      n[        R                  " UUU[	        [
        R                  " U 5      5      [
        R                  " U 5      S9eU $ )zRaises a BigQueryError if the job is in an error state.

Args:
  job: a Job resource.

Returns:
  job, if it is not in an error state.

Raises:
  bq_error.BigqueryError: A bq_error.BigqueryError instance
    based on the job's error description.
rz   r>  r;  )job_ref
session_id)r@  r.   r   r<  r   r   ConstructObjectReferenceGetSessionId)r?  r:  error_lss      r   RaiseIfJobErrorrG    sx     M-(E8}  2.H

&
&&??DE%2237  
*r   schemac           	         U R                  [        R                  5      (       a  [        R                  " S5      eS nU (       d  [        R                  " S5      e[
        R                  R                  U 5      (       a\  [        U 5       n [        R                  " U5      n SSS5        [        W[        5      (       d  [        R                  " SU -  5      eU$ [        R                  " S	U 5      b  [        R                  " S
U < S35      eU R!                  S5       Vs/ s H
  oQ" U5      PM     sn$ ! [         a$  n[        R                  " SU < SU< S35      eSnAff = f! , (       d  f       N= fs  snf )a  Create a schema from a string or a filename.

If schema does not contain ':' and is the name of an existing
file, read it as a JSON schema. If not, it must be a
comma-separated list of fields in the form name:type.

Args:
  schema: A filename or schema.

Returns:
  The new schema (as a dict).

Raises:
  bq_error.BigquerySchemaError: If the schema is invalid or the
    filename does not exist.
z"Cannot load schema files from GCS.c                 "   U R                  S5      u  pnU R                  S5      S:  d  UR                  5       (       d  [        R                  " SU < 35      eUR                  5       UR                  5       R                  5       =(       d    SS.$ )Nr   r]   zInvalid schema entry: STRING)nametype)	partitioncountstripr   BigquerySchemaErrorupper)entryrL  r   
field_types       r   NewFieldReadSchema.<locals>.NewField  sq    //#.DZ{{3!4::<<((u)NOO

  "((*6h r   zSchema cannot be emptyz%Error decoding JSON schema from file z: z3
To specify a one-column schema, use "name:string".Nz=Error in "%s": Table schemas must be specified as JSON lists.z[./\\]zError reading schema: "rj   ,)r|   r   GCS_SCHEME_PREFIXr   rQ  rl   r:   rm   rn   ro   rp   rq   r   r   r)   r3   r_   )rH  rU  rs   rt   ru   rS  s         r   
ReadSchemarY    sC   $ );;<<

&
&'K
LL 


&
&'?
@@	ww~~f	f	
iil 
 k4((((
I  	xx	6". 
&
&	 
 *0c):;):HUO):;;/  
**
 q	
 	

 
4 <s0   
E&D5!E75
E#?EE##E&&
E4	referencec                     Uc
   [        U S9$ U$ ! [        R                   a  n[        R                  " S5      UeS nAff = f)N)r   z2Project reference or a default project is required)r   r   r   )r   rZ  ru   s      r   NormalizeProjectReferencer\  $  sS      l;;
 
	 '' ((
>s    A ;A )rx   )rx   rx   )rx   N)NNNTr=   )NNNNN)NNNNT)NNNN)[r   
__future__r   r   r   r   enumr   ro   r   rl   r   r)   r   r   typingr   r   r   r	   r
   r   abslr   utilsr   r   r    MAX_SUPPORTED_IAM_POLICY_VERSIONEnumr   r   r4   r9   rG   boolrK   rM   rP   rR   rU   rY   re   rg   rv   r   r   ABCr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r  r  r  r  r  r!  r"  r'  r(  r,  r-  r2  r3  r7  r8  r   r@  rG  rY  r\  r   r   r   <module>rf     s	   ) &  % 
     	  	 
  A A    $#$   ++
8C=(3-#67+\$0$0
8C=(3-#67$0N&0
&0
8C=(3-#67&0R0808#+D>08
8C=(3-#6708f#8
#8
8C=(3-#67#8L*K*K
8C=(3-#EF*KZ*K
*K
8C=(3-#EF*KZ$6$6
8C=(3-#67$6N$6
$6
8C=(3-#67$6N//
8C=(3-#67/D!/
!/
8C=(3-#67!/H*3 *Z$;SWW ; > ! !H
L 
L=" "c3h "H Hs HuS#X H"%* %*sC})= %*^ 8C=)	
    11J #8C=)8C=)	
# #   11#\  8C=)8C=)	
     //J 8C=)8C=)	
     99F 8C=)8C=)	
    //D 8C=)8C=)	
    114s tCH~   ,8C=)8C=)	
, , ..00002,l &*8C=)	
  sm   --H !%&*,0&*.8C=)HSM*	
. . sm. %SM.  $.   55.p '+8C=)	
 sm   778 !%&*48#'(8C=)	
( ( 3-( sm( %-SM( 4.(   <<(f !%&*,07;(8C=)HSM*	
( ( 3-( sm( %SM( (0}(   ??(d !%&*26,088C=)	
8 8 3-8 sm8 #+3-8 &*8   ::8F !%&*+/8C=)HSM*	
  3- sm $C=   44D80
4;<s ;<tCy ;<|8C=)	
 **;;   11r   