
                              S r SSKrSSK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\R,                  5      rg)z#The BigQuery CLI partition command.    N)Optionalcast)flags)
client_job)client_table)utils)bigquery_command)bq_cached_client)utils_flags)utils_formatting)bq_id_utils)
stringutilc                   h   ^  \ rS rSrSrS\S\R                  4U 4S jjrS\S\S\	\
   4S	 jrS
rU =r$ )	Partition   z)partition source_prefix destination_tablenamefvc                    > [         [        U ]  X5        [        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 partition.n)
short_nameflag_valuestime_partitioning_typeDAYzEnables time based partitioning on the table and set the type. The default value is DAY, which will generate one partition per day. Other supported values are HOUR, MONTH, and YEAR.)r   time_partitioning_expirationzEnables time based partitioning on the table and sets the number of seconds for which to keep the storage for the partitions in the table. The storage in a partition will have an expiration time of its partition time plus this value.)superr   __init__r   DEFINE_booleanDEFINE_stringDEFINE_integer_ProcessCommandRc)selfr   r   	__class__s      )platform/bq/frontend/command_partition.pyr   Partition.__init__   s    	)T#D-	1 
 	<  
&	*  	2    source_prefixdestination_tablereturnc           	      
   [         R                  R                  5       n[        R                  " 5       n[
        R                  " X1S9n[        R                  " U[        R                  R                  SU< S3SS9  [        [        R                  R                  U5      n[
        R                  " X2S9n[        R                  " U[        R                  R                  SU< S3SS9  [        [        R                  R                  U5      nUR                  5       n[        R                  " UR                  5      n[!        U5      n[        R"                  " 5       n	[%        U	[
        R&                  5      (       a  U	R)                  XR/5      n	UR                  5       n
[*        R,                  " U[        R                  R                  5        [/        [*        R0                  [2        R4                  " UR6                  USS95      n/ nSnS	nU R8                  S
:X  a  SnO%U R8                  S:X  a  SnOU R8                  S:X  a  SnU H|  n[        R                  " US   5      nUR;                  U5      (       d  M4  UUS n [<        R<                  R?                  UU5      nURA                  URC                  U5      5        UnM~     U(       d  [G        S5        g[G        S[!        U5      U4-  5        [2        RH                  " UR6                  US9(       d  US   nURK                  U5      n[2        RL                  " UR6                  US9nU(       a  US   n[N        RP                  " U R8                  U RR                  5      n[G        SU< SU< SU< 35        [2        RT                  " UR6                  UUUS9  [G        U< S35        U GHb  nUR                  < SU< 3nU< U< 3nU	< U< 3nURK                  U5      nU
RK                  U5      nSnU RV                  (       a9  [2        RH                  " UR6                  US9nU=(       a    [Y        US   5      S:  nU(       a  [G        SU< S 35        M  [G        S!U< S"U< 35        S#US$.n[Z        R\                  R^                  (       a  [Z        R\                  R^                  US%'   [`        Rb                  " UU/U40 UD6n[Z        Rd                  R^                  (       d  U Rg                  U5        GMO  [G        S&U< S"U< 35        GMe     g! [D         a     GM  f = f)'a  Copies source tables into partitioned tables.

Usage:
bq partition <source_table_prefix> <destination_partitioned_table>

Copies tables of the format <source_table_prefix><time_unit_suffix> to a
destination partitioned table, with the <time_unit_suffix> of the source
tables becoming the partition ID of the destination table partitions. The
suffix is <YYYYmmdd> by default, <YYYY> if the time_partitioning_type flag
is set to YEAR, <YYYYmm> if set to MONTH, and <YYYYmmddHH> if set to HOUR.

If the destination table does not exist, one will be created with
a schema and that matches the last table that matches the supplied
prefix.

Examples:
  bq partition dataset1.sharded_ dataset2.partitioned_table
)id_fallbacks
identifierz(Cannot determine table associated with ""T)is_usage_errori@B )	apiclient	referencemax_resultsNz%Y%m%dHOURz%Y%m%d%HMONTHz%Y%mYEARz%YtableIdzNo matching source tables foundz"Copying %d source partitions to %s)r/   r0   )r/   
table_dictfieldszCreating table: z with schema from z and partition spec )r/   r0   schematime_partitioningz successfully created.$FnumBytesr   zTable 'z' already exists, skippingzCopying z to WRITE_TRUNCATE)write_dispositionjob_idlocationzSuccessfully copied )4r
   ClientGetr   get_formatter_from_flagsbq_client_utilsGetReferencer   	typecheckApiClientHelperTableReferencer   GetDatasetReferencer   
ensure_strr5   lenget_job_id_from_flags
isinstanceJobIdGeneratorGenerater   configure_formattermapformat_table_infor   list_tablesr/   r   
startswithdatetimestrptimeappendstrftime
ValueErrorprinttable_existsGetTableReferenceget_table_schemafrontend_utilsParseTimePartitioningr   create_tabler   intbq_flagsLOCATIONvaluer   	CopyTableSYNCHRONOUS_MODEPrintJobStartInfo)r"   r'   r(   client	formattersource_table_prefixsource_datasetsource_id_prefixsource_id_lenjob_id_prefixdestination_datasetresultspartition_idsrepresentative_tabletime_formatresulttable_idsuffixpartition_idsource_table_idsource_table_refsource_table_schemar9   destination_table_idcurrent_job_idsource_tabledestination_partition
avoid_copymaybe_destination_partitionkwdsjobs                                  r$   RunWithArgsPartition.RunWithArgs9   s   , $$((*F446I)66 ##229FH	 ##224G (44 ##229JL	 ##224E )<<>N!,,-@-H-HI()M557M-!?!?@@#,,
2m ,??A((;..== **  &&$#	
G MK""f,k		$	$	/k		$	$	.k&&vi'89h			-	.	.-.)	!**33FKH,


|44[A
B!'
   -.	,}0
1	2 $$""# -Y7o'99/J(99$$%
 
1(;(>>

%
%t'H'H  02CE
 $$%$-	 *;=>%(9(A(A<P"2LAo!.=n#55oFl1CC
 j	&2&?&?&&+'
#
 ( A/
;<q@ 	
 
7LNOL2GHI!1$
 ""%..44$z
""\N$9
=A
 ((..

 
 
%
46K &Y  	
	s   >AT11
U ?U  )__name__
__module____qualname____firstlineno__usagestrr   
FlagValuesr   r   r`   r   __static_attributes____classcell__)r#   s   @r$   r   r      sL    
9%3 E$4$4 8ss36s}s sr&   r   )__doc__rT   typingr   r   abslr   ra   clientsr   r   r   rC   frontendr	   r
   r]   r   r   r   pyglibr   BigqueryCmdr   r   r&   r$   <module>r      sG    )  !      , % % ,   %  R ,, Rr&   