
    (                        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JrJ	r	J
r
  SSKJr  SSKrSSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\R:                  5      rg)zThe BigQuery CLI copy command.    )absolute_import)division)print_functionN)ListOptionalTuple)flags)bigquery_client)client_dataset)
client_job)client_table)utils)bigquery_command)bq_cached_client)utils_flags)utils_formatting)bq_error)bq_id_utilsc                   x  ^  \ rS rSrSrSrSrSrSrSr	Sr
S	rS
\S\R                  4U 4S jjrS\R"                  S\\R(                  R*                     S\\\\   4   4S jrS\R"                  S\\R(                  R*                     S\S\R(                  R*                  S\S\4S jrS\S\S\\   4S jrSrU =r$ )Copy!   z5cp [-n] <source_table>[,<source_table>]* <dest_table>z**** NOTE! **** z8Dataset %s not found. Please enter a valid dataset name.z\Warning: This operation is a cross-region copy operation. This may incur additional charges.zWarning: This operation is a cross-region copy operation. This may incur additional charges and take a long time to complete.
This command is running in sync mode. It is recommended to use async mode (-sync=false) for cross-region copy operation.z1cp: Proceed with cross-region copy of %s? [y/N]: z7cp: Table %s already exists. Replace the table? [y/N]: z %s, exiting.namefvc                   > [         [        U ]  X5        [        R                  " SSSSUS9  [        R                  " SSSSUS9  [        R                  " S	SS
SUS9  [        R
                  " SS SUS9  [        R                  " SSSSUS9  [        R                  " SSSSUS9  [        R                  " SS SUS9  [        R                  " SSSUS9  U R                  U5        g )N
no_clobberFz#Do not overwrite an existing table.n)
short_nameflag_valuesforcez1Ignore existing destination tables, don't prompt.fappend_tablezAppend to an existing table.adestination_kms_keyz;Cloud KMS key for encryption of the destination table data.)r   snapshotz(Create a table snapshot of source table.srestorezNRestore table snapshot to a live table. Deprecated, please use clone  instead.r
expirationz?Expiration time, in seconds from now, of the destination table.clonezCreate a clone of source table.)superr   __init__r	   DEFINE_booleanDEFINE_stringDEFINE_integer_ProcessCommandRc)selfr   r   	__class__s      $platform/bq/frontend/command_copy.pyr+   Copy.__init__6   s   	$t(	- 
; 
& 
E	 
2 
	 
I	 
9r 	2    clientsource_referencesreturnc                 "   SnSn[        U5       Hz  u  pVUR                  5       n[        R                  " UR                  US9nUc1  [
        R                  " U R                  [        U5      4-  SS0/ 5      eUc  UnMo  XH:w  d  Mv  Sn  X44$    X44$ )a  Checks whether all source datasets are from same region.

Args:
  client: Bigquery client
  source_references: Source reference

Returns:
  true  - all source datasets are from the same region. Includes the
          scenario in which there is only one source dataset
  false - all source datasets are not from the same region.
Raises:
  bq_error.BigqueryNotFoundError: If unable to compute the dataset
    region
TN	apiclient	referencereasonnotFoundF)		enumerateGetDatasetReferencer   GetDatasetRegionr:   r   BigqueryNotFoundError_DATASET_NOT_FOUNDstr)	r0   r5   r6   "all_source_datasets_in_same_regionfirst_source_region_valsource_datasetsource_regions	            r2   :_CheckAllSourceDatasetsInSameRegionAndGetFirstSourceRegion?Copy._CheckAllSourceDatasetsInSameRegionAndGetFirstSourceRegionm   s    & *.&-...0n$55$$"m 
	,,##s>':&<<z"
 	

 
	$+/-2*-BB# /" .BBr4   source_references_strdest_referencedestination_regionc                    UR                  5       n U R                  X5      u  pxUc  [        R                  " UR                  US9nUc1  [
        R                  " U R                  [        U5      4-  SS0/ 5      eU(       a  XX:X  a  g[        U R                  [        R                  R                  (       a  SU R                  -   OSU R                   -   5        U R"                  (       a  gS[$        R&                  " U R(                  U4-  5      :w  a  [        U R*                  U4-  5        g	g! [
        R                   a!  n	[        S[        U	5      -   5         Sn	A	gSn	A	ff = f)
a  Checks if it is a Cross Region Copy operation and obtains confirmation.

Args:
  client: Bigquery client
  source_references: Source reference
  source_references_str: Source reference string
  dest_reference: Destination dataset reference
  destination_region: Destination dataset region

Returns:
  true  - it is not a cross-region operation, or user has used force option,
          or cross-region operation is verified confirmed with user, or
          Insufficient permissions to query datasets for validation
  false - if user did not allow cross-region operation, or
          Dataset does not exist hence operation can't be performed.
Raises:
  bq_error.BigqueryNotFoundError: If unable to compute the dataset
    region
Nr9   z^Unable to determine source or destination dataset location, skipping cross-region validation: Tr<   r=   
yF)r?   rJ   r   r@   r:   r   BigqueryAccessDeniedErrorprintrC   rA   rB   _NOTEbq_flagsSYNCHRONOUS_MODEvalue_SYNC_FLAG_ENABLED_WARNING_CROSS_REGION_WARNINGr   frontend_utilsPromptYN_CONFIRM_CROSS_REGION_NOT_COPYING)
r0   r5   r6   rL   rM   rN   destination_datasetrD   rE   errs
             r2   #shouldContinueAfterCrossRegionCheck(Copy.shouldContinueAfterCrossRegionCheck   se   6 )<<>

I
I >(
 
	#+<<&&2E
 !**

!
!S)<%=$?
?Z
 
 
 *1	

$$** 	t...D...	 zz
n%%""&;%==  D!6 889= -- 'H
 s   5D. .E#EE#source_tables
dest_tablec                 l   [         R                  R                  5       nUR                  S5       Vs/ s H  n[        R
                  " X4S9PM     nnSR                  S U 5       5      n[        R
                  " X2S9nU R                  (       a  SnSn	OU R                  (       a  SnSn	OSnS	n	S
n
U	(       dx  U R                  (       dg  [        R                  " UR                  US9n
U
(       aA  S[        R                  " U R                  U-  5      :w  a  [!        U R"                  U4-  5        gU R%                  UUUUU
5      (       d  gSnU R&                  (       a  SnSnO.U R(                  (       a  SnSnOU R*                  (       a  SnSnOSnUU	[,        R.                  " 5       US.n[0        R2                  R4                  (       a  [0        R2                  R4                  US'   U R6                  (       a  SU R6                  0US'   U R8                  (       aa  [:        R:                  R=                  [?        U R8                  [@        R@                  " 5       -   5      5      n[        RB                  " U5      US'   [D        RF                  " X5U40 UD6nUc  [!        SU< S35        g
[0        RH                  R4                  (       d  U RK                  U5        g
[M        U5      S:  a  SOSn[!        S U< S!U< S"U< S#U< S$3	5        [        RN                  " [P        RR                  " U5      5        g
s  snf )%zCopies one table to another.

Examples:
  bq cp dataset.old_table dataset2.new_table
  bq cp --destination_kms_key=kms_key dataset.old_table dataset2.new_table
,)id_fallbacks
identifierz, c              3   8   #    U  H  n[        U5      v   M     g 7f)N)rC   ).0srcs     r2   	<genexpr>#Copy.RunWithArgs.<locals>.<genexpr>   s     %L:K3c#hh:Ks   WRITE_APPENDTWRITE_EMPTYWRITE_TRUNCATEFNr9   rQ   r   copiedSNAPSHOTsnapshottedRESTORErestoredCLONEclonedCOPY)write_dispositionignore_already_existsjob_idoperation_typelocation
kmsKeyNameencryption_configurationdestination_expiration_timezTable 'z' already exists, skipping   r%    Tablez 'z' successfully z to '')*r   ClientGetsplitbq_client_utilsGetTableReferencejoinr!   r   r   r   get_table_regionr:   rZ   r[   _CONFIRM_OVERWRITErS   r]   r`   r$   r&   r)   r   get_job_id_from_flagsrU   LOCATIONrW   r#   r(   datetimeutcfromtimestampinttimeFormatRfc3339r   	CopyTablerV   PrintJobStartInfolenPrintJobMessagesr   format_job_info)r0   rb   rc   r5   rj   r6   rL   rM   rx   ry   rN   	operationr{   kwdsdatetime_utcjob	pluralitys                    r2   RunWithArgsCopy.RunWithArgs   s    $$((*F !&&s++C 	))vN+   !II%L:K%LL$66N ("	'"*#  '88$$ 
~'>'>

!
!^
4( ! 	d#8"::;33  I}}!ni	 ni	nin.!6335(	D !**00d:
00*d%& &&77
doo		+
,l -;,H,H
-d() 

>59C {^EF&&,,
S!./!3#i-y.J
 %%&6&F&Fs&KLms   L1 )__name__
__module____qualname____firstlineno__usagerT   rB   rY   rX   r\   r   r]   rC   r	   
FlagValuesr+   r
   BigqueryClientr   r   ApiClientHelperTableReferencer   boolr   rJ   r`   r   r   __static_attributes____classcell__)r1   s   @r2   r   r   !   s9   
E%
%@  
*  NP ,53 5E$4$4 5n&C,,&C k99HHI&C T8C= !	&CPE,,E k99HHIE !	E
 "11@@E E EN^Ms ^M ^M ^M ^Mr4   r   )__doc__
__future__r   r   r   r   r   typingr   r   r   abslr	   rU   clientsr
   r   r   r   r   r   frontendr   r   rZ   r   r   r   r   BigqueryCmdr   r   r4   r2   <module>r      s^    $ &  %   ( (   # "    , % % ,   %  YM'' YMr4   