
    ?\              ,          S r SSK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SKJr  SSKJr  S	rS
\R$                  S\R&                  R(                  4S jr   SDS\R$                  S\R&                  R(                  S\\\R,                        S\\   S\\   S\\   4S jjr    SES
\R$                  S\R&                  R(                  S\\   S\\   S\\   S\\   4S jjr  SFS
\R$                  S\R&                  R8                  S\\   S\\   4S jjrS\R$                  S\R&                  R(                  4S jrS\R$                  S\R&                  R(                  4S  jrS
\R$                  S\R&                  R(                  S!\\   4S" jr S
\R$                  S\R&                  R(                  4S# jr!                        SGS
\R$                  S\R&                  R(                  S$\S%\\   S&\\   S'\\   S(\\   S)\\   S*\\   S+\\   S,\\   S-\\   S.\\   S/\\\\4      S0\\\\\   4      S1\\   S2\\   S3\\   S4\\   S5\\\\4      4(S6 jjr"                           SHS
\R$                  S\R&                  R(                  S&\\   S'\\   S(\\   S)\\   S*\\   S+\\   S,\\   S-\\   S.\\   S7\\\\4      S8\\\      S0\\\\\   4      S1\\   S9\\   S3\\   S:\S;\\\\4      S<\\\      S=\4*S> jjr#S
\R$                  S\R&                  R(                  4S? jr$  SIS
\R$                  S\R&                  R(                  S:\S9\\   4S@ jjr%S
\R$                  S\R&                  R(                  4SA jr& SJS
\R$                  S\R&                  R(                  SB\4SC jjr'g)Kz&The BigQuery CLI table client library.    )DictListOptionalcast)	discovery)table_reader)utils)bq_error)bq_id_utils)bq_processor_utilsexternalCatalogTableOptions	apiclient
table_dictc                     U R                  5       R                  " S0 UD6R                  5       nUR                  S0 5      $ )Nschema )tablesgetexecute)r   r   
table_infos      #platform/bq/clients/client_table.pyget_table_schemar      s:     !%%3
3;;=*	"	%%    Ninsert_clientinsertsskip_invalid_rowsignore_unknown_valuestemplate_suffixc                     S nU R                  5       R                  " SS[        UUU[        [	        Xb5      5      S90UD6nUR                  5       $ )a/  Insert rows into a table.

Arguments:
  insert_client: The apiclient used to make the request.
  table_dict: table reference into which rows are to be inserted.
  inserts: array of InsertEntry tuples where insert_id can be None.
  skip_invalid_rows: Optional. Attempt to insert any valid rows, even if
    invalid rows are present.
  ignore_unknown_values: Optional. Ignore any values in a row that are not
    present in the schema.
  template_suffix: Optional. The suffix used to generate the template table's
    name.

Returns:
  result of the operation.
c                 l    [        U R                  S9nU R                  (       a  U R                  US'   U$ )N)jsoninsertId)dictrecord	insert_id)insertencodeds     r   _encode_insert)insert_table_rows.<locals>._encode_insert2   s.    &G",,gjNr   body)skipInvalidRowsignoreUnknownValuestemplateSuffixrowsr   )	tabledata	insertAllr#   listmapr   )r   r   r   r   r   r   r(   ops           r   insert_table_rowsr4      s]    2  ** +3(C01	 	" 
r   	table_ref	start_rowmax_rowsselected_fieldsmax_rows_per_requestc                     Uc  [        S5      eUc  [        S5      e[        R                  " XU5      nUR                  UUUS9$ )aQ  Convenience method to get the schema and rows from a table.

Arguments:
  apiclient: The apiclient used to make the request.
  table_ref: table reference.
  start_row: first row to read.
  max_rows: number of rows to read.
  selected_fields: a subset of fields to return.
  max_rows_per_request: the maximum number of rows to read per request.

Returns:
  A tuple where the first item is the list of fields and the
  second item a list of rows.

Raises:
  ValueError: will be raised if start_row is not explicitly provided.
  ValueError: will be raised if max_rows is not explicitly provided.
zstart_row is requiredzmax_rows is required)r8   )
ValueErrorbq_table_readerTableTableReaderReadSchemaAndRows)r   r5   r6   r7   r8   r9   r   s          r   read_schema_and_rowsr?   D   s`    4 
,
--
+
,, 11y, 
	'	'% 
( 
 r   	referencemax_results
page_tokenc                 >   [         R                  " U[         R                  R                  SS9  [        R
                  " XU5      nU R                  5       R                  " S0 UD6R                  5       nUR                  S/ 5      nUb  SU;   a  [        U5      U:  a  U[        U5      -
  US'   US   US'   U R                  5       R                  " S0 UD6R                  5       nUR                  UR                  S/ 5      5        SU;   a  [        U5      U:  a  M  U$ )z/List the tables associated with this reference.list_tablesmethodr   nextPageToken
maxResults	pageTokenr   )r   	typecheckApiClientHelperDatasetReferencer   PrepareListRequestr   r1   r   r   lenextend)r   r@   rA   rB   requestresultresultss          r   rD   rD   l   s    !!22
 11j' ""-W-557&JJx$'
V
#G{(B)CL8gl#O4gk!&&1199;fnnVZZ"-.	 V
#G{(B
 
.r   iampolicy_clientc                    [         R                  " U[         R                  R                  SS9  SUR                  < SUR
                  < SUR                  < 3nU R                  5       R                  US9R                  5       $ )a3  Gets IAM policy for the given table resource.

Arguments:
  iampolicy_client: The apiclient used to make the request.
  reference: the TableReference for the table resource.

Returns:
  The IAM policy attached to the given table resource.

Raises:
  BigqueryTypeError: if reference is not a TableReference.
get_table_iam_policyrE   	projects/
/datasets//tables/)resource)
r   rJ   rK   TableReference	projectId	datasetIdtableIdr   getIamPolicyr   )rS   r@   formatted_resources      r   rU   rU      ss      !!00#  |/|0wyr   c                    [         R                  " U[         R                  R                  SS9  SUR                  < SUR
                  < SUR                  < 3nSU0nU R                  5       R                  XCS9R                  5       $ )ag  Sets IAM policy for the given table resource.

Arguments:
  iampolicy_client: The apiclient used to make the request.
  reference: the TableReference for the table resource.
  policy: The policy string in JSON format.

Returns:
  The updated IAM policy attached to the given table resource.

Raises:
  BigqueryTypeError: if reference is not a TableReference.
set_table_iam_policyrE   rV   rW   rX   policy)r*   rY   )
r   rJ   rK   rZ   r[   r\   r]   r   setIamPolicyr   )rS   r@   rb   r_   rP   s        r   ra   ra      s|    $ !!00# 
 v'||>wyr   returnc                    [         R                  " U[         R                  R                  SS9   U R	                  5       R
                  " S0 [        U5      D6R                  5       S   $ ! [        R                   a     gf = f)z*Returns the region of a table as a string.get_table_regionrE   locationNr   
r   rJ   rK   rZ   r   r   r#   r   r
   BigqueryNotFoundErrorr   r@   s     r   rf   rf      ss    
 !!00
!!4DO4<<>zJJ		'	' s   9A* *B Bc                    [         R                  " U[         R                  R                  SS9   U R	                  5       R
                  " S0 [        U5      D6R                  5       $ ! [        R                   a     gf = f)z!Returns true if the table exists.table_existsrE   Fr   rh   rj   s     r   rl   rl      sn    
 !!00
!!4DO4<<>>		'	' s   6A' 'A>=A>ignore_existingr   descriptiondisplay_name
expiration
view_querymaterialized_view_queryenable_refreshrefresh_interval_msmax_stalenessuse_legacy_sqllabels
clusteringrequire_partition_filterdestination_kms_keyrg   table_constraintsresource_tagsc                    [         R                  " U[         R                  R                  SS9   [        R
                  " U5      nUb  SU0US'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  S	U0nUb  UUS
'   UUS'   Ub  UUS'   Ub  S	U0nU	b  U	US'   U
b  U
US'   UUS'   Ub  Ub  UUS'   UUS'   Ub  UUS'   Ub  [        R                  " U5      US'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  SU0US'   Ub  UUS'   Ub  UUS'   Ub  UUS'   [        XU5        g! [        R                   a    U(       d  e  gf = f)a.
  Create a table corresponding to TableReference.

Args:
  apiclient: The apiclient used to make the request.
  reference: the TableReference to create.
  ignore_existing: (boolean, default False) If False, raise an exception if
    the dataset already exists.
  schema: an optional schema for tables.
  description: an optional description for tables or views.
  display_name: an optional friendly name for the table.
  expiration: optional expiration time in milliseconds since the epoch for
    tables or views.
  view_query: an optional Sql query for views.
  materialized_view_query: an optional standard SQL query for materialized
    views.
  enable_refresh: for materialized views, an optional toggle to enable /
    disable automatic refresh when the base table is updated.
  refresh_interval_ms: for materialized views, an optional maximum frequency
    for automatic refreshes.
  max_staleness: INTERVAL value that determines the maximum staleness allowed
    when querying a materialized view or an external table. By default no
    staleness is allowed.
  external_data_config: defines a set of external resources used to create an
    external table. For example, a BigQuery table backed by CSV files in GCS.
  biglake_config: specifies the configuration of a BigLake managed table.
  external_catalog_table_options: Specifies the configuration of an external
    catalog table.
  view_udf_resources: optional UDF resources used in a view.
  use_legacy_sql: The choice of using Legacy SQL for the query is optional. If
    not specified, the server will automatically determine the dialect based
    on query information, such as dialect prefixes. If no prefixes are found,
    it will default to Legacy SQL.
  labels: an optional dict of labels to set on the table.
  time_partitioning: if set, enables time based partitioning on the table and
    configures the partitioning.
  clustering: if set, enables and configures clustering on the table.
  range_partitioning: if set, enables range partitioning on the table and
    configures the partitioning.
  require_partition_filter: if set, partition filter is required for queiries
    over this table.
  destination_kms_key: User specified KMS key for encryption.
  location: an optional location for which to create tables or views.
  table_constraints: an optional primary key and foreign key configuration for
    the table.
  resource_tags: an optional dict of tags to attach to the table.

Raises:
  BigqueryTypeError: if reference is not a TableReference.
  BigqueryDuplicateError: if reference exists and ignore_existing
    is False.
create_tablerE   Nfieldsr   friendlyNamern   expirationTimequeryuserDefinedFunctionResourcesviewuseLegacySqlenableRefreshrefreshIntervalMsmaterializedViewmaxStalenessexternalDataConfigurationbiglakeConfigurationr   rw   timePartitioningrx   rangePartitioningrequirePartitionFilter
kmsKeyNameencryptionConfigurationrg   r{   resourceTags)r   rJ   rK   rZ   r   ConstructObjectInfobq_frontend_utilsGetJson_execute_insert_table_requestr
   BigqueryDuplicateError)r   r@   rm   r   rn   ro   rp   rq   rr   rs   rt   ru   external_data_configbiglake_configexternal_catalog_table_optionsview_udf_resourcesrv   rw   time_partitioningrx   range_partitioningry   rz   rg   r{   r|   r*   	view_argsmaterialized_view_argss                                r   r~   r~      s   ^ !!00711)<D &)d8n)d>'d=)dJ'i		'4F	01d6l		#$2	.!* ')@A		#2@/		(6I23!7d'		",^*>d&'!%3d!"%1,=,E,E
(-d() d8n$!2d%d<%"4d+'?d#$&)57J(Kd$%!d:$"3d *d>!)=		(	(  s   C:D+ +E
	E
labels_to_setlabel_keys_to_removeetagautodetect_schematags_to_attachtags_to_removeclear_all_tagsc                    [         R                  " U[         R                  R                  SS9  0 nU(       a	  [	        XS9n[
        R                  " U5      nSnUb  SU0US'   OU(       d  SUS'   Ub  UUS'   Ub  UUS	'   Ub  UUS
'   Ub  US:X  a  SUS'   OUUS'   Ub  SU0n Ub  UU S'   Ub  UU S'   U US'   0 n!Ub  UU!S'   Ub  UU!S'   U	b  U	U!S'   U!(       a  U!US'   Ub  UUS'   U
b  U
US'   SU;  a  0 US'   [        [        [        [        [           4   US   5      n"U"c  [        S5      eU(       a  UR                  5        H  u  n#n$U$U"U#'   M     U(       a  U H  n#SU"U#'   M
     Ub  UUS'   Ub  UUS'   Ub  U0 :X  a  SUS'   OUUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   0 n%U(       a  SU;   a  US    H  n&SU%U&'   M
     OU=(       d    /  H  n&SU%U&'   M
     U=(       d    0  H  n&UU&   U%U&'   M     U%US'   UbD  [	        XS9nUR                  [        0 5        [        R                   " U[           U5      U[        '   [#        U UUUUS9  g) a
  Updates a table.

Args:
  apiclient: The apiclient used to make the request.
  reference: the TableReference to update.
  schema: an optional schema for tables.
  description: an optional description for tables or views.
  display_name: an optional friendly name for the table.
  expiration: optional expiration time in milliseconds since the epoch for
    tables or views. Specifying 0 removes expiration time.
  view_query: an optional Sql query to update a view.
  materialized_view_query: an optional Standard SQL query for materialized
    views.
  enable_refresh: for materialized views, an optional toggle to enable /
    disable automatic refresh when the base table is updated.
  refresh_interval_ms: for materialized views, an optional maximum frequency
    for automatic refreshes.
  max_staleness: INTERVAL value that determines the maximum staleness allowed
    when querying a materialized view or an external table. By default no
    staleness is allowed.
  external_data_config: defines a set of external resources used to create an
    external table. For example, a BigQuery table backed by CSV files in GCS.
  external_catalog_table_options: Specifies the configuration of an external
    catalog table.
  view_udf_resources: optional UDF resources used in a view.
  use_legacy_sql: The choice of using Legacy SQL for the query is optional. If
    not specified, the server will automatically determine the dialect based
    on query information, such as dialect prefixes. If no prefixes are found,
    it will default to Legacy SQL.
  labels_to_set: an optional dict of labels to set on this table.
  label_keys_to_remove: an optional list of label keys to remove from this
    table.
  time_partitioning: if set, enables time based partitioning on the table and
    configures the partitioning.
  range_partitioning: if set, enables range partitioning on the table and
    configures the partitioning.
  clustering: if set, enables clustering on the table and configures the
    clustering spec.
  require_partition_filter: if set, partition filter is required for queiries
    over this table.
  etag: if set, checks that etag in the existing table matches.
  encryption_configuration: Updates the encryption configuration.
  location: an optional location for which to update tables or views.
  autodetect_schema: an optional flag to perform autodetect of file schema.
  table_constraints: an optional primary key and foreign key configuration for
    the table.
  tags_to_attach: an optional dict of tags to attach to the table
  tags_to_remove: an optional list of tag keys to remove from the table
  clear_all_tags: if set, clears all the tags attached to the table

Raises:
  BigqueryTypeError: if reference is not a TableReference.
update_tablerE   rj   FNr   r   r   r   rn   r   r   r   r   r   r   r   r   r   r   r   rw   zMissing labels in table.r   r   rx   r   rg   r{   r   )r   r@   tabler   r   )r   rJ   rK   rZ   _execute_get_table_requestr   r   r   r   strr   r;   items
setdefault*_EXTERNAL_CATALOG_TABLE_OPTIONS_FIELD_NAMEr   !UpdateExternalCatalogTableOptions_execute_patch_table_request)'r   r@   r   rn   ro   rp   rq   rr   rs   rt   ru   r   r   r   rv   r   r   r   r   rx   ry   r   encryption_configurationrg   r   r{   r   r   r   existing_tabler   maybe_skip_schemar   r   table_labels	label_keylabel_valuer|   tags'                                          r   r   r   z  s:   h !!00 . 0N 
0
0
;%(E(OE(O)'?E
#$(E.&E-Q $e *e*%I%2Di./!"0iE&M(&=7#.<?+$2E./ 6E
%)=E
%& +eNUE(Od3-.h@,
/
00"/"5"5"7	; +l9 #8)	 $l9 *" 1E
#!3E
R eL&eL)&>E
"# E*"!2E
-.8n-mC . ##mC $!r!c',M# "
 (%#//N H"M;;EF*	
 

45 )r   c                 p    U R                  5       R                  " S0 [        U5      D6R                  5       $ )Nr   )r   r   r#   r   rj   s     r   r   r   ?  s.     
						2$y/	2	:	:	<<r   c                     U R                  5       R                  " SX2S.[        U5      D6nU(       a  U(       a  UOUS   UR                  S'   UR	                  5         g)a0  Executes request to patch table.

Args:
  apiclient: The apiclient used to make the request.
  reference: the TableReference to patch.
  table: the body of request
  autodetect_schema: an optional flag to perform autodetect of file schema.
  etag: if set, checks that etag in the existing table matches.
)r   r*   r   zIf-MatchNr   )r   patchr#   headersr   )r   r@   r   r   r   rP   s         r   r   r   F  sV      $$ )9=i' 
*.$E&MGOOJ	//r   c                     U R                  5       R                  " SSU0[        UR                  5       5      D6R	                  5         g )Nr*   r   )r   r&   r#   GetDatasetReferencer   )r   r@   r*   s      r   r   r   c  s@    
  	5578GIr   ignore_not_foundc                    [         R                  " U[         R                  R                  SS9   U R	                  5       R
                  " S0 [        U5      D6R                  5         g! [        R                   a    U(       d  e  gf = f)ah  Deletes TableReference reference.

Args:
  apiclient: The apiclient used to make the request.
  reference: the TableReference to delete.
  ignore_not_found: Whether to ignore "not found" errors.

Raises:
  BigqueryTypeError: if reference is not a TableReference.
  bq_error.BigqueryNotFoundError: if reference does not exist and
    ignore_not_found is False.
delete_tablerE   Nr   )
r   rJ   rK   rZ   r   deleter#   r   r
   ri   )r   r@   r   s      r   r   r   m  ss    " !!00
0Y088:		'	'  s   7A( (BB)NNN)NNNN)NN)FNNNNNNNNNNNNNNNNNNNNNNN)NNNNNNNNNNNNNNNNNNNNNNFNNNF)FN)F)(__doc__typingr   r   r   r   googleapiclientr   clientsr   r<   frontendr	   r   r
   r   r   r   ResourcerK   rZ   r   InsertEntryboolintr4   r   r?   rL   rD   rU   ra   rf   rl   r~   r   r   r   r   r   r   r   r   <module>r      sQ   , - - % 3 /   $-J *&!!&++::& )-,0%)(%%(++::( (-99:;(  ~	(
 $D>( c](\  $"%)*.%!!%**99% }% sm	%
 c]% #3-%V "& $	!!**;; # 	4((**99B!((!**99!H!!**99 c] !!**99& " !%"& $ $-1%))-#'#'%)'+15/3)-"'+.25L!!L**99L L SM	L
 #L 3-L L L &c]L TNL "#L C=L" TN#L$ T#s(^$%L( c49n-.)L, 'tn-L. "#/L0 sm1L2  }3L4 DcN+5Ld !%"& $ $-1%))-#'#'%).20415/3!"#/3*. ;B!!B**99B #	B
 3-B B B &c]B TNB "#B C=B TNB  DcN+!B" #49-#B( c49n-.)B* 'tn+B, 3--B0 sm1B2 3B6 T#s(^,7B8 T#Y'9B: ;BJ=!!=**99= $!!**99 	
 3-:!!**99 #!!**99 r   