
                         ~   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ dZ G d dej:                        Zdedeee   ee   ee   f   fdZ dedeee   ee   ee   f   fdZ!dedeee   ee   ee   f   fdZ"dedee#   deee   ee   ee   f   fdZ$dedeee   ee   ee   f   fdZ%dedeee   ee   ee   ee   f   fdZ&dedeee   ee   ee   ee   f   fdZ'dedeee   ee   ee   f   fdZ(dedeee   ee   ee   f   fdZ)dedeee   ee   ee   f   fdZ*dedeee   ee   ee   f   fdZ+defdZ,d Z-d  Z. G d! d"ej^                        Z0 G d# d$e0      Z1 G d% d&e0      Z2 G d' d(e0      Z3 G d) d*e0      Z4d+ Z5dedeeef   fd,Z6d-ed.edeeef   fd/Z7dedeeeef   fd0Z8	 dVd1 ed2d3ee   fg      dedejr                  jt                  fd4Z;	 dVd1 ed2d3ee   fd-ee   fg      dedejr                  jx                  fd5Z=	 	 dWd1 ed2d3ee   fd-ee   fg      ded6edejr                  j|                  fd7Z?	 	 dWd1 ed2d3ee   fd-ee   fg      d8ed9edejr                  j                  fd:ZA	 dVd1 ed2d3ee   fd-ee   fg      dedejr                  j                  fd;ZC	 dVd1 ed2d3ee   fd-ee   fg      dedejr                  j                  fd<ZEdedeeef   fd=ZF	 dVd1 ed2d3ee   fd-ee   fg      dedeejr                  j|                  ejr                  jx                  ejr                  jt                  f   fd>ZG	 	 dXd1 ed2d3ee   fg      ded?ee   dejr                  j                  fd@ZI	 	 	 	 dYd1 ed2d3ee   fdAee   fg      dee   d?ee   dBee   dCe#dejr                  j                  fdDZK	 dZd1 ed2d3ee   fg      d?ee   dejr                  j                  fdEZM	 	 	 	 	 d[d1 ed2d3ee   fg      dee   dee   d?ee   dFee   dee#   dejr                  j                  fdGZO	 	 	 	 	 d[d1 ed2d3ee   fdAee   fg      dee   dee   d?ee   dBee   dHee   dejr                  j                  fdIZQ	 	 	 	 	 d\d1 ed2d3ee   fg      dee   dee   d?ee   dJee   dKe#dejr                  j                  fdLZS	 	 	 	 d]d1 ed2d3ee   fdAee   fg      dee   dee   d?ee   dMee   dejr                  j                  fdNZUdO ZVdP ZWdQ ZXdRedee   fdSZYd1 ed2d3ee   fg      dTejr                  jt                  dejr                  jt                  fdUZZy)^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                       e Zd ZdZdZdZdZy)
UpdateModezEnum for update modes.UPDATE_METADATA
UPDATE_ACLUPDATE_FULLN)__name__
__module____qualname____doc__r   r   r        platform/bq/clients/utils.pyr   r      s    %/*+r   r   
identifierreturnc                 T   d}d}d}t        j                  d|||dz  t         j                        }t        j                  ||       }|r_|j	                         j                  dd      |j	                         j                  dd      |j	                         j                  d	d      fS y
)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   _ParseJobIdentifierr1   %   s     ,&.JJ +&"	 	dd'" ))GZ
(%
lD1j$/h- 
 
r   c                 t   t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|||fS )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   r/   r0   r#   r$   r3   s         r   _ParseReservationIdentifierr6   S   s    $ JJ
 	dd' ))GZ
(%	

 
 4zA   $$\48*__"":t4(??$(()94@.
h	//r   pathc                    t        j                  dt         j                        }t        j                  ||       st	        j
                  d| z        fd} |d      } |d      } |d      xs  |d      }|||fS )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    j                         j                  | d       S Nr*   r+   keyr0   s    r   <lambda>z&ParseReservationPath.<locals>.<lambda>       eoo'++C6r   r#   r$   r3   bi_idr&   r'   r(   r)   r   r5   )r7   r/   groupr#   r$   r3   r0   s         @r   ParseReservationPathrC   z   s    $ JJ 	dd
' ))GT
"%	

 
 !G$!N
OO
6%\"*:()*<eGn.
h	//r   allow_commasc                    d}|r%t        j                  dt         j                        }n$t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|||fS )a]  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_idr4   )r   rD   r/   r0   r#   r$   rF   s          r   "_ParseCapacityCommitmentIdentifierrG      s    & 'jj	
 	G jj	
 	G ))GZ
(%	

 
 <zI   $$\48*__"":t4( ??,001I4P
h 6	77r   c                 t   t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|||fS )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$   rF   r4   )r7   r/   r0   r#   r$   rF   s         r   ParseCapacityCommitmentPathrI      s    " JJ
 	dd' ))GT
"%	

 
 6=   $$\48*__"":t4( ??,001I4P
h 6	77r   c                    t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }||||fS )a6  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$   r3   reservation_assignment_idr4   )r   r/   r0   r#   r$   r3   rK   s          r   %_ParseReservationAssignmentIdentifierrL      s    ( JJ 	dd' ))GZ
(%	

 
 ?*L   $$\48*__"":t4(??$(()94@.#oo/33!4 h0I	JJr   c                    t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }||||fS )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$   r3   rK   r4   )r7   r/   r0   r#   r$   r3   rK   s          r   ParseReservationAssignmentPathrN   )  s    ( JJ 	dd' ))GT
"%	

 
 9D@   $$\48*__"":t4(??$(()94@.#oo/33!4 h0I	JJr   c                 t   t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|||fS )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_idr4   )r   r/   r0   r#   r$   rP   s         r    _ParseReservationGroupIdentifierrQ   V  s    $ JJ
 	dd' ))GZ
(%	

 
 :ZG   $$\48*__"":t4(*../EtL
h 4	55r   c                     t        j                  dt         j                        }t        j                  ||       st	        j
                  d| z        fd} |d      } |d      } |d      }|||fS )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    j                         j                  | d       S r:   r;   r<   s    r   r>   z+ParseReservationGroupPath.<locals>.<lambda>  r?   r   r#   r$   rP   rA   )r7   r/   rB   r#   r$   rP   r0   s         @r   ParseReservationGroupPathrT   }  s    " JJ
 	dd' ))GT
"%	

 
 4t;  7%\"*:(56
h 4	55r   c                    | st        j                  d      | j                  d      }t        |      }|dkD  rt        j                  d| z        ||dz
     }|dkD  r||dz
     nd}|dkD  rdj	                  |d|dz
         nd}|||fS )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   r5   splitlenjoin)r   tokens
num_tokensconnection_idr$   r#   s         r   _ParseConnectionIdentifierr`     s    $ 


 
 !>
??C &6{*!^

 
 3j@  a(-'1A~VJN#4(5?!^sxx/a01*
h	..r   c                 t   t        j                  dt         j                        }t        j                  ||       }|st	        j
                  d| z        |j                         j                  dd      }|j                         j                  dd      }|j                         j                  dd      }|||fS )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$   r_   r4   )r7   r/   r0   r#   r$   r_   s         r   ParseConnectionPathrb     s    " JJ
 	dd' ))GT
"%	

 
 !F!M
NN $$\48*__"":t4(//#''>-
h	..r   table_constraintsc                    | st        j                  d      t        j                  j	                  |       r-t        |       5 }	 t        j                  |      }	 ddd       |S t        j                  d|       t        j                  d| d      	 t        j                  |       }|S # t        $ r}t        j                  d| d      |d}~ww xY w# 1 sw Y   S xY w# t        $ r}t        j                  d| d      |d}~ww xY w)	a  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 rV   Nz^[./~\\]z"Error reading table constraints: "+" looks like a filename, but was not found.z2Error decoding JSON table constraints from string )r   BigqueryTableConstraintsErrorosr7   existsopenjsonload
ValueErrorr&   r)   loads)rc   floaded_jsones       r   ReadTableConstrantsrq     s    


0
0+  WW^^%&		 Aiil 
! YY{-.: 
0
0 1	4 **./K 
)  44 #
 	 
!  
 

0
0	 sB   CB+C# +	C4CCCC #	D,DDc           	         | j                   j                  dd      j                  d      r:t        j                  | j
                  j                  d            }t        |       yd}t        j                  j                  rd}t        j                  d|d| j                   j                  dd	      d
| j
                        )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+   
startswithrj   rm   contentdecode
RaiseErrorr   FLAGSuse_regional_endpointsr   BigqueryCommunicationError)rp   rx   error_detailss      r   RaiseErrorFromHttpErrorr     s    VVZZ#../ABjj))'23Gw M{{))Nm

-
- !&&**X~>			K r   c                 2    t        j                  d|       )z-Raises a BigQueryError given a non-HttpError.z/Could not connect with BigQuery server due to: )r   r}   )rp   s    r   RaiseErrorFromNonHttpErrorr   )  s    ++=>@	 r   c                   :    e Zd ZdZd Zej                  d        Zy)JobIdGeneratorz!Base class for job id generators.c                      y r:   r   )selfs    r   __init__zJobIdGenerator.__init__3  s    r   c                      y)z0Generates a job_id to use for job_configuration.Nr   r   job_configurations     r   GeneratezJobIdGenerator.Generate6  s    r   N)r   r   r   r   r   abcabstractmethodr   r   r   r   r   r   0  s$    )	 ; ;r   r   c                       e Zd ZdZd Zy)JobIdGeneratorNonezGJob id generator that returns None, letting the server pick the job id.c                      y r:   r   r   s     r   r   zJobIdGeneratorNone.Generate>  s    r   Nr   r   r   r   r   r   r   r   r   r   ;  s
    Or   r   c                       e Zd ZdZd Zy)JobIdGeneratorRandomz"Generates random job id_fallbacks.c                     dt        j                         j                  dt        j                        t        t        j                         dz        fz  S )Nzbqjob_r%08x_%016xr   i  )randomSystemRandomrandintsysmaxsizeinttimer   s     r   r   zJobIdGeneratorRandom.GenerateE  sD    %%a5DIIK$"  r   Nr   r   r   r   r   r   B  s
    *r   r   c                   "    e Zd ZdZd Zd Zd Zy)JobIdGeneratorFingerprintz5Generates job ids that uniquely match the job config.c                 Z    t        |t              r|S t        |      j                  d      S )Nrt   )
isinstancebytesstrencode)r   objs     r   _HashableReprz'JobIdGeneratorFingerprint._HashableReprO  s$    #ujs8??7##r   c                    t        |j                               }|j                          |D ]  }|j                  | j	                  |             ||   }t        |t              r)t        j                  d|       | j                  ||       at        |t               r0t        j                  d|       |D ]  }| j                  ||        t        j                  d||       |j                  | j	                  |              y)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   zJobIdGeneratorFingerprint._HashT  s    D 	IIK
kk$$$S)*
+a	At	%s+

1da&,G
**Wd
#  	%sA.D&&q)* r   c                     t        j                         }| j                  ||       d|j                         }t	        j
                  d||       |S )Nbqjob_czFingerprinting: %s:
%s)hashlibr   r   	hexdigestr   r   )r   r   s1r%   s       r   r   z"JobIdGeneratorFingerprint.Generateh  sA    	BJJ "%LLN,FLL*,=vFMr   N)r   r   r   r   r   r   r   r   r   r   r   r   L  s    =$
+(r   r   c                   (     e Zd ZdZ fdZd Z xZS )JobIdGeneratorIncrementingz7Generates job ids that increment each time we're asked.c                 >    t         |           || _        d| _        y )Nr   )superr   _inner_retry)r   inner	__class__s     r   r   z#JobIdGeneratorIncrementing.__init__s  s    	GDKDKr   c                     | xj                   dz  c_         d| j                  j                  |      | j                   fz  S )NrX   z%s_%d)r   r   r   r   s     r   r   z#JobIdGeneratorIncrementing.Generatex  s4    KK1Kdkk**+<=t{{KKKr   )r   r   r   r   r   r   __classcell__)r   s   @r   r   r   p  s    ?
Lr   r   c                 P    	 t        |       S # t        $ r t        d|       w xY w)NzInvalid value for wait: )r   rl   )waits    r   NormalizeWaitr   }  s.    =t9	 =
T;
<<=s   
 %c                 v    | j                  d      rd| t        d      d  fS | j                  d      \  }}}||fS )Nzplx.google:z
plx.google:)rw   r[   
rpartition)r   r#   _
dataset_ids       r   _ParseDatasetIdentifierr     sJ    =)C$6$8999 * 5 5c :J:z!!r   r   table_idc                     | r|s| |fS | j                  d      }|d   dk7  s|dv r| |fS dj                  |dd       d|z   fS )zDMoves "INFORMATION_SCHEMA" to table_id for dataset qualified tables.rV   INFORMATION_SCHEMA)SCHEMATASCHEMATA_OPTIONSNzINFORMATION_SCHEMA.)rZ   r\   )r   r   dataset_partss      r   _ShiftInformationSchemar     sj    	8x""3'-2..( ? 3 x	-$	%'<x'G	GGr   c                     t        j                  d|       r| ddfS | j                  d      \  }}}d|v r|j                  d      \  }}}n|r|}d}nd}|}t        ||      \  }}|||fS )a7  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_-]*:?$rs   r   rV   )r&   r)   r   r   )r   r#   r   dataset_and_table_idr   r   s         r   _ParseIdentifierr     s    $ YY2J?r2(2(=(=c(B%*a%  2==cBJ8 &JH J#H0XF*h	Z	))r   id_fallbacksIDSr#   c                 (   t        |      \  }}}	 |xs |xs | j                  }|s,|r*t        j                  j                  j                  |      S |dk(  rt        j                  d      t        j                  d|      # t        $ r Y =w xY w)z=Determine a project reference from an identifier or fallback.)	projectIdrs   zPlease provide a project ID.z&Cannot determine project described by )	r   r#   r   ApiClientHelperProjectReferenceCreaterl   r   BigqueryClientError)r   r   r#   r   r   s        r   GetProjectReferencer     s     &6j%A"*j(		 BxB<+B+BJ*((99@@ A  
 2

&
&'E
FF

&
&6@B  
 		s   AB 	BBc                 r   |s| j                   n|}t        |      \  }}}|r|s|s| j                  }|}n.|r|r|r	|dz   |z   }n|r|r|snt        j                  d|      	 t
        j                  j                  j                  ||      S # t        $ r}t        j                  d|      |d}~ww xY w)z<Determine a DatasetReference from an identifier or fallback.rV   z&Cannot determine dataset described by )r   	datasetIdN)
r   r   r#   r   r5   r   r   DatasetReferencer   rl   r   r   r#   r   r   rp   s         r   GetDatasetReferencer     s     /9|&&j*%5j%A"*j(j((JJjXc!H,Jj

 
 6@B &&77>>
 ?   
 

 
 6@Bs   $*B 	B6B11B6default_dataset_idc                 *   t        |      \  }}}|st        | j                        \  }}|r|s|}	 t        j                  j
                  j                  |xs | j                  ||      S # t        $ r}t        j                  d|      |d}~ww xY w)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#   rl   r   r5   )r   r   r   r#   r   r   rp   s          r   GetTableReferencer   	  s     &6j%A"*j(	4\5L5LMJ

#J	&&55<<7 7 7 =  
 
 

 
 4>@s   9A+ +	B4BBtable_identifier	policy_idc                    	 t        | |      }t        j                  j                  j	                  |j
                  |j                  |j                  |      S # t        $ r!}t        j                  d|d|      |d}~ww xY w)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   rl   r   r5   )r   r   r   table_referencerp   s        r   GetRowAccessPolicyReferencer   &  s    '6FGO&&??FF!++!++''	 G   
 

 
 Y	( s   AA 	B"A>>Bc                    t        |      \  }}}|st        | j                        \  }}	 t        j                  j
                  j                  |xs | j                  ||      S # t        $ r}t        j                  d|z        |d}~ww xY w)z,Returns a ModelReference from an identifier.)r   r   modelIdz&Cannot determine model described by %sN)r   r   r   r   r   ModelReferencer   r#   rl   r   r5   r   s         r   GetModelReferencer   A  s     &6j%A"*j(	4\5L5LMJ
	&&55<<7 7 7 =  
 
 

 
 0:=   9A% %	B.BBc                    t        |      \  }}}|st        | j                        \  }}	 t        j                  j
                  j                  |xs | j                  ||      S # t        $ r}t        j                  d|z        |d}~ww xY w)z.Returns a RoutineReference from an identifier.)r   r   	routineIdz(Cannot determine routine described by %sN)r   r   r   r   r   RoutineReferencer   r#   rl   r   r5   r   s         r   GetRoutineReferencer   [  s     &6j%A"*j(	4\5L5LMJ
	&&77>>7 7 7 ?  
 
 

 
 2Z?r   c                 H    t        |       \  }}t        |      }|r||d<   |S )N)r   r   )r   r   )r   parsed_project_idparsed_dataset_idresults       r   GetQueryDefaultDatasetr   u  s0    )@)L&&+,&+F;	-r   c                    	 t        | |      S # t        j                  $ r Y nw xY w	 t        | |      S # t        j                  $ r Y nw xY w	 t	        | |      S # t        j                  $ r Y nw xY wt        j                  d|d      )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   r5   r   r   )r   r   s     r   GetReferencer   }  s    <	\:66			 			|Z88			 			|Z88			 		/9;	 s-    $$4 A
	A
A A0/A0default_locationc                     t        |      \  }}}|s| j                  }|s|}|r-	 t        j                  j                  j                  |||      S t        j                  d|      # t        $ r Y #w xY w)zEDetermine a JobReference from an identifier, location, and fallbacks.)r   jobIdr$   z"Cannot determine job described by )	r1   r#   r   r   JobReferencer   rl   r   r5   )r   r   r   r#   r$   r%   s         r   GetJobReferencer    s     "5Z!@*h	((J	H((55<<fx =  
 	0:<	   
s   +A) )	A54A5api_versiondefault_reservation_idcheck_reservation_projectc                 0   t        |      \  }}}|rC|rA| j                  r5|| j                  k7  r&t        j                  d| j                  d|d      |xs | j                  }|st        j                  d      |xs |}|st        j                  d      |r=|j	                         |j	                         k7  rt        j                  d|d|d      |xs |}|st        j                  d	      t
        j                  j                  |||
      S )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)r6   r#   r   r5   lowerr   r   ReservationReference)r   r   r   r  r  r#   r$   r3   s           r   GetReservationReferencer    s"    *E*&*h  


!
!
//
/

 
 +66
	D  4\44*	

 
 !<
==))(	

 
 !:
;;(..*.>.D.D.FF

 
 /	;  ";%;.	

 
 !B
CC&&;;x~ <  r   c                     | j                   }|st        j                  d      |}|st        j                  d      t        j                  j
                  j                  ||      S )r  r  r  )r   r$   )r#   r   r5   r   r   BiReservationReferencer   )r   r   r#   r$   s       r   GetBiReservationReferencer    sh     &&*	

 
 !<
==(	

 
 !:
;;		$	$	;	;	B	BX 
C 
 r   default_capacity_commitment_idc                    |t        ||      \  }}}n'|t        |      \  }}}nt        j                  d      |xs | j                  }|st        j                  d      |xs |}|st        j                  d      |xs |}|st        j                  d      t
        j                  j                  j                  |||      S )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)	rG   rI   r   r5   r#   r   r   CapacityCommitmentReferencer   )	r   r   r7   r   r  rD   r#   r$   rF   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                 r   |t        |      \  }}}}	n(|t        |      \  }}}}	nt        j                  d      |xs | j                  }|st        j                  d      |xs |}|st        j                  d      |xs |}|	xs |}	t
        j                  j                  j                  ||||	      S )z7Determine a ReservationAssignmentReference from inputs.r  r  r  )r   r$   r  reservationAssignmentId)	rL   rN   r   r5   r#   r   r   ReservationAssignmentReferencer   )
r   r   r7   r   r  r  r#   r$   r3   rK   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                    |t        |      \  }}}n'|t        |      \  }}}nt        j                  d      |rC|rA| j                  r5|| j                  k7  r&t        j                  d| j                  d|d      |xs | j                  }|st        j                  d      |xs |}|st        j                  d      |r=|j                         |j                         k7  rt        j                  d|d|d      |xs |}|st        j                  d	      t        j                  j                  |||
      S )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)	rQ   rT   r   r5   r#   r  r   r   ReservationGroupReference)	r   r   r7   r   r   r!  r#   r$   rP   s	            r   GetReservationGroupReferencer%  d  s]    (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                 b   |t        |      \  }}}n|t        |      \  }}}xs | j                  }|st        j                  d      xs |}|st        j                  d      xs |}|st        j                  d      t
        j                  j                  j                  |||      S )z@Determine a ConnectionReference from an identifier and location.r  r  zConnection name not specified.)r   r$   connectionId)	r`   rb   r#   r   r5   r   r   ConnectionReferencer   )r   r   r7   r   r&  r#   r$   r_   s           r   GetConnectionReferencer*    s     ,F-)Z= ,?,E)Z=4\44*	

 
 !<
==))(	

 
 !:
;;8#8-	

 
 !A
BB		$	$	8	8	?	?XM 
@ 
 r   c                 |    | j                  di       j                  di g      d   }t        j                  || g       )zARaises an appropriate BigQuery error given the json error result.errorerrorsr   )r+   r   CreateBigqueryError)r   r,  s     r   rz   rz     s;    
**Wb
!
%
%h
5a
8%$$UFB77r   c                 *    d| j                  di       v S )z3Predicate to determine whether or not a job failed.errorResultru   )r+   )jobs    r   IsFailedJobr2    s    	#''(B/	//r   c           
          t        |       rf| d   d   }| d   j                  dg       }t        j                  |||t	        t        j                  |             t        j                  |             | S )a  Raises 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.
  ru   r0  r-  )job_ref
session_id)r2  r+   r   r.  r   r   ConstructObjectReferenceGetSessionId)r1  r,  error_lss      r   RaiseIfJobErrorr9    su     M-(E8}  2.H

&
&&??DE%2237  
*r   schemac           	         | j                  t        j                        rt        j                  d      d }| st        j                  d      t
        j                  j                  |       rUt        |       5 }	 t        j                  |      }	 ddd       t        t              st        j                  d| z        |S t        j                  d	|       t        j                  d
| d      | j!                  d      D cg c]
  } ||       c}S # t        $ r!}t        j                  d| d|d      d}~ww xY w# 1 sw Y   xY wc c}w )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                 
   | j                  d      \  }}}| j                  d      dkD  s|j                         st        j                  d|       |j                         |j                         j                         xs ddS )Nr   rX   zInvalid schema entry: STRING)nametype)	partitioncountstripr   BigquerySchemaErrorupper)entryr>  r   
field_types       r   NewFieldzReadSchema.<locals>.NewField  sq    //#.D!Z{{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: "re   ,)rw   r   GCS_SCHEME_PREFIXr   rC  rg   r7   rh   ri   rj   rk   rl   r   r   r&   r0   rZ   )r:  rG  rn   ro   rp   rE  s         r   
ReadSchemarJ    s:   $ );;<

&
&'K
LL 


&
&'?
@@	ww~~f	f	
iil 
 k4(((
I  	xx	6". 
&
&	 
 *0c):;):HUO):;;/  
**
 q	
 	

 
4 <s0   9E;DE	E D<<EEE	referencec                     |	 t        |       S |S # t        j                  $ r}t        j                  d      |d }~ww xY w)N)r   z2Project reference or a default project is required)r   r   r   )r   rK  rp   s      r   NormalizeProjectReferencerM  $  sS      l;;
 
	 '' ((
>s    A ;A )rs   )rs   rs   )rs   N)NNNTr:   )NNNNN)NNNNT)NNNN)[r   
__future__r   r   r   r   enumr   rj   r   rg   r   r&   r   r   typingr   r   r   r	   r
   r   abslr   utilsr   r   r    MAX_SUPPORTED_IAM_POLICY_VERSIONEnumr   r   r1   r6   rC   boolrG   rI   rL   rN   rQ   rT   r`   rb   rq   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  r$  r%  r)  r*  rz   r2  r9  rJ  rM  r   r   r   <module>rW     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   