o
    <i                     @  s  d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZmZmZ d dlmZmZmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZm Z m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z'm(Z(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 erd dl9m:Z: d dl;m<Z< edZ=G dd deee= Z>G dd de1Z?G dd de1Z@G dd de1ZAG dd  d e1ZBG d!d" d"e1ZCG d#d$ d$e1ZDG d%d& d&e1ZEG d'd( d(e1ZFG d)d* d*e1ZGG d+d, d,e1ZHG d-d. d.e1ZIG d/d0 d0e1ZJG d1d2 d2e1ZKG d3d4 d4e1ZLG d5d6 d6e1ZMG d7d8 d8e1ZNG d9d: d:e1ZOG d;d< d<e1ZPG d=d> d>e1ZQG d?d@ d@e1ZRG dAdB dBe1ZSee?e@B eAB eBB eCB eDB eEB eFB eGB eHB eIB eJB eKB eLB eMB eNB eOB ePB eQB eRB eSB edCdDf ZTG dEdF dFZUe	ddMdNZVeVWe?ddOdPZXeVWeKddQdPZXeVWe@ddRdPZXeVWeLddSdPZXeVWeMddTdPZXeVWeAddUdPZXeVWeBddVdPZXeVWeCddWdPZXeVWeDddXdPZXeVWeGddYdPZXeVWeHddZdPZXeVWeIdd[dPZXeVWeJdd\dPZXeVWeEdd]dPZXeVWeFdd^dPZXeVWeNdd_dPZXeVWeOdd`dPZXeVWePddadPZXeVWeQddbdPZXeVWeRddcdPZXeVWeSddddPZX	e	dddldmZYddqdrZZG dsdt dte1Z[G dudv dve[Z\G dwdx dxe[Z]G dydz dze[Z^G d{d| d|e[Z_G d}d~ d~e[Z`G dd de[ZaG dd de[ZbG dd de[Zcee\e]B e^B e_B e`B eaB ebB ecB eddDf ZdeeeeeTdf eeeddf f ZfdS )    )annotationsN)ABCabstractmethod)datetime)singledispatch)TYPE_CHECKING	AnnotatedAnyGenericLiteralTypeVarcast)Fieldfield_validatormodel_serializermodel_validatorCommitFailedException)PARTITION_FIELD_ID_STARTPartitionSpec)Schema)SUPPORTED_TABLE_FORMAT_VERSIONTableMetadataTableMetadataUtil)MAIN_BRANCHSnapshotRefSnapshotRefType)MetadataLogEntrySnapshotSnapshotLogEntry)	SortOrder)PartitionStatisticsFileStatisticsFile filter_statistics_by_snapshot_id)IcebergBaseModel
Propertiestransform_dict_value_to_str)datetime_to_millis)property_as_int)ModelWrapSerializerWithoutInfo)TransactionUc                   @  sL   e Zd ZU ded< dddZedd	d
ZdddZdddZdddZ	dS )UpdateTableMetadatar+   _transactiontransactionreturnNonec                 C  s
   || _ d S N)r.   )selfr/    r4   \/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/pyiceberg/table/update/__init__.py__init__A      
zUpdateTableMetadata.__init__UpdatesAndRequirementsc                 C  s   d S r2   r4   r3   r4   r4   r5   _commitD   s   zUpdateTableMetadata._commitc                 C  s   | j j|    d S r2   )r.   _applyr:   r9   r4   r4   r5   commitG   s   zUpdateTableMetadata.commit_r	   value	tracebackc                 C  s   |    dS )zClose and commit the change.N)r<   )r3   r=   r>   r?   r4   r4   r5   __exit__J   s   zUpdateTableMetadata.__exit__r,   c                 C  s   | S )zUpdate the table.r4   r9   r4   r4   r5   	__enter__N   s   zUpdateTableMetadata.__enter__N)r/   r+   r0   r1   )r0   r8   r0   r1   )r=   r	   r>   r	   r?   r	   r0   r1   )r0   r,   )
__name__
__module____qualname____annotations__r6   r   r:   r<   r@   rA   r4   r4   r4   r5   r-   >   s   
 


r-   c                   @  (   e Zd ZU eddZded< ded< dS )AssignUUIDUpdatezassign-uuiddefaultzLiteral['assign-uuid']action	uuid.UUIDuuidNrC   rD   rE   r   rK   rF   r4   r4   r4   r5   rH   S      
 rH   c                   @  2   e Zd ZU eddZded< eddZded< d	S )
UpgradeFormatVersionUpdatezupgrade-format-versionrI   z!Literal['upgrade-format-version']rK   zformat-versionaliasintformat_versionN)rC   rD   rE   r   rK   rF   rU   r4   r4   r4   r5   rQ   X      
 rQ   c                   @  rP   )
AddSchemaUpdatez
add-schemarI   zLiteral['add-schema']rK   schemarR   r   schema_N)rC   rD   rE   r   rK   rF   rY   r4   r4   r4   r5   rW   ]   rV   rW   c                   @  6   e Zd ZU eddZded< eddddZd	ed
< dS )SetCurrentSchemaUpdatezset-current-schemarI   zLiteral['set-current-schema']rK   z	schema-idz;Schema ID to set as current, or -1 to set last added schemarS   descriptionrJ   rT   	schema_idN)rC   rD   rE   r   rK   rF   r_   r4   r4   r4   r5   r[   b   
   
 r[   c                   @  rG   )AddPartitionSpecUpdatezadd-specrI   zLiteral['add-spec']rK   r   specNrN   r4   r4   r4   r5   ra   i   rO   ra   c                   @  rZ   )SetDefaultSpecUpdatezset-default-specrI   zLiteral['set-default-spec']rK   zspec-idzEPartition spec ID to set as the default, or -1 to set last added specr\   r]   rT   spec_idN)rC   rD   rE   r   rK   rF   rd   r4   r4   r4   r5   rc   n   r`   rc   c                   @  rP   )
AddSortOrderUpdatezadd-sort-orderrI   zLiteral['add-sort-order']rK   z
sort-orderrR   r    
sort_orderN)rC   rD   rE   r   rK   rF   rf   r4   r4   r4   r5   re   u   rV   re   c                   @  rZ   )SetDefaultSortOrderUpdatezset-default-sort-orderrI   z!Literal['set-default-sort-order']rK   zsort-order-idzGSort order ID to set as the default, or -1 to set last added sort orderr\   r]   rT   sort_order_idN)rC   rD   rE   r   rK   rF   rh   r4   r4   r4   r5   rg   z   r`   rg   c                   @  rG   )AddSnapshotUpdatezadd-snapshotrI   zLiteral['add-snapshot']rK   r   snapshotNrN   r4   r4   r4   r5   ri      rO   ri   c                   @  sd   e Zd ZU eddZded< eddZded< d	ed
< eddZded< ded< ded< ded< dS )SetSnapshotRefUpdatezset-snapshot-refrI   zLiteral['set-snapshot-ref']rK   ref-namerR   strref_namez4Literal[SnapshotRefType.TAG, SnapshotRefType.BRANCH]typesnapshot-idrT   snapshot_idzBAnnotated[int | None, Field(alias='max-ref-age-ms', default=None)]max_ref_age_mszGAnnotated[int | None, Field(alias='max-snapshot-age-ms', default=None)]max_snapshot_age_mszIAnnotated[int | None, Field(alias='min-snapshots-to-keep', default=None)]min_snapshots_to_keepN)rC   rD   rE   r   rK   rF   rn   rq   r4   r4   r4   r5   rk      s   
 rk   c                   @  rP   )
RemoveSnapshotsUpdatezremove-snapshotsrI   zLiteral['remove-snapshots']rK   zsnapshot-idsrR   	list[int]snapshot_idsN)rC   rD   rE   r   rK   rF   rw   r4   r4   r4   r5   ru      rV   ru   c                   @  rP   )
RemoveSnapshotRefUpdatezremove-snapshot-refrI   zLiteral['remove-snapshot-ref']rK   rl   rR   rm   rn   N)rC   rD   rE   r   rK   rF   rn   r4   r4   r4   r5   rx      rV   rx   c                   @  rG   )SetLocationUpdatezset-locationrI   zLiteral['set-location']rK   rm   locationNrN   r4   r4   r4   r5   ry      rO   ry   c                   @  s>   e Zd ZU eddZded< ded< eddddddZdS )SetPropertiesUpdatezset-propertiesrI   zLiteral['set-properties']rK   dict[str, str]updatesbeforemode
propertiesr%   r0   c                 C  s   t |S r2   r&   )clsr   r4   r4   r5   &transform_properties_dict_value_to_str   s   z:SetPropertiesUpdate.transform_properties_dict_value_to_strN)r   r%   r0   r|   )rC   rD   rE   r   rK   rF   r   r   r4   r4   r4   r5   r{      s
   
 
r{   c                   @  rG   )RemovePropertiesUpdatezremove-propertiesrI   zLiteral['remove-properties']rK   z	list[str]removalsNrN   r4   r4   r4   r5   r      rO   r   c                   @  sR   e Zd ZU eddZded< ded< eddd	d
Zded< edddddZdS )SetStatisticsUpdatezset-statisticsrI   zLiteral['set-statistics']rK   r"   
statisticsNrp   z~snapshot-id is **DEPRECATED for REMOVAL** since it contains redundant information. Use `statistics.snapshot-id` field instead.)rS   r^   
int | Nonerq   r~   r   datadict[str, Any]r0   c                 C  sF   |d }t |tr|j}nt |trtt|d}nd }||d< |S )Nr   rp   rq   )
isinstancer"   rq   dictr   rT   get)r   r   statsrq   r4   r4   r5   validate_snapshot_id   s   

z(SetStatisticsUpdate.validate_snapshot_id)r   r   r0   r   )	rC   rD   rE   r   rK   rF   rq   r   r   r4   r4   r4   r5   r      s   
 r   c                   @  rP   )
RemoveStatisticsUpdatezremove-statisticsrI   zLiteral['remove-statistics']rK   rp   rR   rT   rq   NrC   rD   rE   r   rK   rF   rq   r4   r4   r4   r5   r      rV   r   c                   @  rP   )
RemovePartitionSpecsUpdatezremove-partition-specsrI   z!Literal['remove-partition-specs']rK   zspec-idsrR   rv   spec_idsN)rC   rD   rE   r   rK   rF   r   r4   r4   r4   r5   r      rV   r   c                   @  rP   )
RemoveSchemasUpdatezremove-schemasrI   zLiteral['remove-schemas']rK   z
schema-idsrR   rv   
schema_idsN)rC   rD   rE   r   rK   rF   r   r4   r4   r4   r5   r      rV   r   c                   @  rG   )SetPartitionStatisticsUpdatezset-partition-statisticsrI   z#Literal['set-partition-statistics']rK   r!   partition_statisticsNrN   r4   r4   r4   r5   r      rO   r   c                   @  rP   )
RemovePartitionStatisticsUpdatezremove-partition-statisticsrI   z&Literal['remove-partition-statistics']rK   rp   rR   rT   rq   Nr   r4   r4   r4   r5   r      rV   r   rK   )discriminatorc                   @  s\   e Zd ZU ded< dddZdd	d
ZdddZdddZd ddZd!ddZ	d"ddZ
dS )#_TableMetadataUpdateContextzlist[TableUpdate]_updatesr0   r1   c                 C  s
   g | _ d S r2   )r   r9   r4   r4   r5   r6      r7   z$_TableMetadataUpdateContext.__init__updateTableUpdatec                 C  s   | j | d S r2   )r   append)r3   r   r4   r4   r5   
add_update   s   z&_TableMetadataUpdateContext.add_updaterq   rT   boolc                      t  fdd| jD S )Nc                 3  &    | ]}t |tr|jj kV  qd S r2   )r   ri   rj   rq   .0r   rq   r4   r5   	<genexpr>      


z@_TableMetadataUpdateContext.is_added_snapshot.<locals>.<genexpr>anyr   )r3   rq   r4   r   r5   is_added_snapshot     z-_TableMetadataUpdateContext.is_added_snapshotr_   c                   r   )Nc                 3  r   r2   )r   rW   rY   r_   r   r_   r4   r5   r        $ z>_TableMetadataUpdateContext.is_added_schema.<locals>.<genexpr>r   )r3   r_   r4   r   r5   is_added_schema     z+_TableMetadataUpdateContext.is_added_schemard   c                   r   )Nc                 3  r   r2   )r   ra   rb   rd   r   rd   r4   r5   r     r   zF_TableMetadataUpdateContext.is_added_partition_spec.<locals>.<genexpr>r   )r3   rd   r4   r   r5   is_added_partition_spec
  r   z3_TableMetadataUpdateContext.is_added_partition_specrh   c                   r   )Nc                 3  r   r2   )r   re   rf   order_idr   rh   r4   r5   r     r   zB_TableMetadataUpdateContext.is_added_sort_order.<locals>.<genexpr>r   )r3   rh   r4   r   r5   is_added_sort_order  r   z/_TableMetadataUpdateContext.is_added_sort_orderc                 C  s   t | jdkS )Nr   )lenr   r9   r4   r4   r5   has_changes  s   z'_TableMetadataUpdateContext.has_changesNrB   )r   r   r0   r1   )rq   rT   r0   r   )r_   rT   r0   r   )rd   rT   r0   r   )rh   rT   r0   r   )r0   r   )rC   rD   rE   rF   r6   r   r   r   r   r   r   r4   r4   r4   r5   r      s   
 





r   r   r   base_metadatar   contextr0   c                 C  s   t d|  )a7  Apply a table update to the table metadata.

    Args:
        update: The update to be applied.
        base_metadata: The base metadata to be updated.
        context: Contains previous updates and other change tracking information in the current transaction.

    Returns:
        The updated metadata.

    zUnsupported table update: )NotImplementedErrorr   r   r   r4   r4   r5   _apply_table_update  s   r   c                 C  s,   | j |jkr|S ||  |jd| j idS )N
table_uuidr   )rM   r   r   
model_copyr   r4   r4   r5   r=   &  s   
r=   c                 C  s   | |  |jd| jidS )Nrz   r   )r   r   rz   r   r4   r4   r5   r=   /  s   
c                 C  st   | j tkrtd| j  | j |j k rtd|j  d| j  | j |j kr'|S |jd| j id}||  t|S )Nz"Unsupported table format version: zCannot downgrade vz table to vrU   r   )rU   r   
ValueErrorr   r   r   _construct_without_validation)r   r   r   updated_metadatar4   r4   r5   r=   5  s   


c                 C  sB   t | jdkr	|S t|j}|| j ||  |jd|idS Nr   r   r   )r   r}   r   r   r   r   r   )r   r   r   r   r4   r4   r5   r=   H  s   

c                 C  sL   t | jdkr	|S t|j}| jD ]}|| q||  |jd|idS r   )r   r   r   r   popr   r   )r   r   r   r   keyr4   r4   r5   r=   T  s   


c                 C  s6   t |j| jj|j| jg d}||  |j|dS )N)last_column_idschemasr   )maxr   rY   highest_field_idr   r   r   )r   r   r   metadata_updatesr4   r4   r5   r=   a  s
   
c                 C  ~   | j }|dkrtdd |jD }||std||jkr!|S ||}|d u r2td| d||  |jd|idS )	Nr\   c                 s      | ]}|j V  qd S r2   r   r   rX   r4   r4   r5   r   q      _.<locals>.<genexpr>zLCannot set current schema to last added schema when no schema has been addedzSchema with id  does not existcurrent_schema_idr   )	r_   r   r   r   r   r   schema_by_idr   r   )r   r   r   new_schema_idrX   r4   r4   r5   r=   l  s   



c                 C  s   |j D ]}|j| jjkr|| jkrtd|j d| q|j | jg ttdd | jjD dd|jp6td d}||  |j	|d	S )
NPartition spec with id  already exists: c                 S  s   g | ]}|j qS r4   )field_id)r   fieldr4   r4   r5   
<listcomp>  s    _.<locals>.<listcomp>r   rI      )partition_specslast_partition_idr   )
r   rd   rb   r   r   fieldsr   r   r   r   )r   r   r   rb   r   r4   r4   r5   r=     s   

c                 C  s   | j }|dkrtdd |jD }||std||jkr!|S d}|jD ]}|j |k}|r1 nq&|s;td| ||  |jd|idS )	Nr\   c                 s  r   r2   r   r   rb   r4   r4   r5   r     r   r   zYCannot set current partition spec to last added one when no partition spec has been addedFzFailed to find spec with id default_spec_idr   )rd   r   r   r   r   r   r   r   )r   r   r   new_spec_idfound_spec_idrb   r4   r4   r5   r=     s"   




c                 C  sx  t |jdkrtdt |jdkrtdt |jdkr!td|| jjd ur4td| jj d|jdkrY| jj	d urY| jj	|j
krY| jjd urYtd| jj	 d	|j
 |jd
krh| jjd u rhtd|jd
kr| jjd ur|jd ur| jj|jk rtd| jj d|j ||  |j| jj| jj	|j| jg |jd
kr|jd ur| jjd ur|j| jj nd ddS )Nr   z5Attempting to add a snapshot before a schema is addedz=Attempting to add a snapshot before a partition spec is addedz9Attempting to add a snapshot before a sort order is addedzSnapshot with id z already exists   z)Cannot add snapshot with sequence number z! older than last sequence number    z(Cannot add snapshot without first row idzMCannot add a snapshot with first row id smaller than the table's next-row-id z < )last_updated_mslast_sequence_number	snapshotsnext_row_idr   )r   r   r   r   sort_orderssnapshot_by_idrj   rq   rU   sequence_numberr   parent_snapshot_idfirst_row_idr   r   r   timestamp_msr   
added_rowsr   r4   r4   r5   r=     sT   





c                 C  s   t | j| j| j| j| jd}|j| j}|d ur||kr|S |	|j}|d u r5t
d| j d|j i }||jrB|j|d< | jtkri|j|d< d|vrZtt  |d< |jt|j|d dg |d< i |j| j|i|d< ||  |j|d	S )
N)rq   snapshot_ref_typert   rs   rr   zCannot set z to unknown snapshot r   current_snapshot_id)rq   r   snapshot_logrefsr   )r   rq   ro   rt   rs   rr   r   r   rn   r   r   r   r   r   r(   r   now
astimezoner   r   r   r   )r   r   r   snapshot_refexisting_refrj   r   r4   r4   r5   r=     s:   




c           
        s   j D ] t fdd|jD std  d|j qfdd|jD }fdd|jD }fdd|j D }fd	d|jD }t	||}|}|D ]}	t
|	||}qQ| |j||d
dS )Nc                 3      | ]}|j  kV  qd S r2   r   r   rj   )remove_snapshot_idr4   r5   r         r   zSnapshot with snapshot id z does not exist: c                   s8   g | ]}|j  jvr|j jv r|jd didn|qS )r   Nr   )rq   rw   r   r   r   r   r4   r5   r     s    r   c                      g | ]
}|j  jvr|qS r4   )rq   rw   )r   snapshot_log_entryr   r4   r5   r     s
    c                 3  s*    | ]\}}|j  jv rt|d V  qdS )rn   N)rq   rw   rx   r   rn   refr   r4   r5   r     s    c                 3  s(    | ]}|j  jv rt|j d V  qdS )r   N)rq   rw   r   )r   statistics_filer   r4   r5   r     s    
)r   r   r   )rw   r   r   r   r   r   itemsr   	itertoolschainr   r   r   )
r   r   r   r   r   remove_ref_updatesremove_statistics_updatesr}   new_metadataupdr4   )r   r   r5   r=      s,   


	


c                   s    j |jvr|S |j j  }||jd u r"td j  d|j  j tkr)d n|j} fdd|j D }|  |j	||ddS )NzCannot remove z ref with unknown snapshot c                   s    i | ]\}}| j kr||qS r4   r   r   r   r4   r5   
<dictcomp>3  s     z_.<locals>.<dictcomp>)r   r   r   )
rn   r   r   rq   r   r   r   r   r   r   )r   r   r   r   r   metadata_refsr4   r   r5   r=   (  s   
c                 C  s^   |j D ]}|j| jjkr|| jkrtd|j d| q||  |jd|j | jg idS )NzSort-order with id r   r   r   )r   r   rf   r   r   r   )r   r   r   sortr4   r4   r5   r=   8  s   

c                 C  r   )	Nr\   c                 s  r   r2   )r   )r   rf   r4   r4   r5   r   P  r   r   zUCannot set current sort order to the last added one when no sort order has been addedzSort order with id r   default_sort_order_idr   )	rh   r   r   r   r   r
  sort_order_by_idr   r   )r   r   r   new_sort_order_idrf   r4   r4   r5   r=   G  s   



c                 C  2   t |j| jj}||  |jd|| jg idS )Nr   r   )r#   r   rq   r   r   r   r   r   r   r4   r4   r5   r=   _  s   
c                   R   t  fdd|jD std j dt|j j}|  |jd|idS )Nc                 3      | ]	}|j  j kV  qd S r2   r   )r   statr   r4   r5   r   i      r   zStatistics with snapshot id r   r   r   )r   r   r   rq   r#   r   r   r  r4   r   r5   r=   g  s
   
c                   s~   j D ] t fdd|jD std  dq|jj v r(td|j fdd|jD }| |jd|id	S )
Nc                 3  r   r2   r   r   )remove_spec_idr4   r5   r   u  r   r   r   r   z&Cannot remove default partition spec: c                   r   r4   )rd   r   r   r   r4   r5   r   {      r   r   r   )r   r   r   r   r   r   r   )r   r   r   r   r4   )r  r   r5   r=   r  s   

c                   sz   j D ]" t fdd|jD std  d|j kr%td  qfdd|jD }| |jd|id	S )
Nc                 3  r   r2   r   r   )remove_schema_idr4   r5   r     r   r   zSchema with schema id r   z%Cannot remove current schema with id c                   r   r4   )r_   r   r   r   r4   r5   r     r  r   r   r   )r   r   r   r   r   r   r   )r   r   r   r   r4   )r  r   r5   r=     s   


c                 C  r  )Nr   r   )r#   r   rq   r   r   )r   r   r   r   r4   r4   r5   r=     s
   

c                   r  )Nc                 3  r  r2   r   )r   	part_statr   r4   r5   r     r  r   z&Partition Statistics with snapshot id r   r   r   )r   r   r   rq   r#   r   r   r  r4   r   r5   r=     s
   
Fr}   tuple[TableUpdate, ...]enforce_validationr   metadata_location
str | Nonec                 C  s   t  }| }|D ]}t|||}q| r1|rt||| j}| j|jkr1|jdtt 	 id}|
 |  | |  |rLt| S |jddS )a  Update the table metadata with the given updates in one transaction.

    Args:
        base_metadata: The base metadata to be updated.
        updates: The updates in one transaction.
        enforce_validation: Whether to trigger validation after applying the updates.
        metadata_location: Current metadata location of the table

    Returns:
        The metadata with the updates applied.
    r   r   T)deep)r   r   r   _update_table_metadata_logr   r   r(   r   r   r   rb   check_compatiblerX   rf   r   	parse_obj
model_dump)r   r}   r  r  r   r  r   r4   r4   r5   update_table_metadata  s   r   rm   r   rT   c                 C  s|   ddl m} 	 tdt| j|j|j}| j}t| j|kr-t| j| d }| j|d  }d|t	||dg i}| j
|dS )Nr   )TablePropertiesr   metadata_log)metadata_filer   r   )pyiceberg.tabler!  r   r)   r   METADATA_PREVIOUS_VERSIONS_MAX&METADATA_PREVIOUS_VERSIONS_MAX_DEFAULTr"  r   r   r   )r   r  r   r!  max_metadata_log_entriesprevious_metadata_logremove_indexr   r4   r4   r5   r    s"   r  c                   @  s$   e Zd ZU ded< ed
ddZd	S )ValidatableTableRequirementrm   ro   r   TableMetadata | Noner0   r1   c                 C  s   dS )zValidate the requirement against the base metadata.

        Args:
            base_metadata: The base metadata to be validated against.

        Raises:
            CommitFailedException: When the requirement is not met.
        Nr4   r3   r   r4   r4   r5   validate  s   
z$ValidatableTableRequirement.validateNr   r+  r0   r1   )rC   rD   rE   rF   r   r-  r4   r4   r4   r5   r*    s   
 r*  c                   @  s.   e Zd ZU dZeddZded< dd
dZdS )AssertCreatez?The table must not already exist; used for create transactions.zassert-createrI   zLiteral['assert-create']ro   r   r+  r0   r1   c                 C  s   |d urt dd S )NzTable already existsr   r,  r4   r4   r5   r-    s   zAssertCreate.validateNr.  rC   rD   rE   __doc__r   ro   rF   r-  r4   r4   r4   r5   r/    s   
 r/  c                   @  s6   e Zd ZU dZeddZded< ded< dddZdS )AssertTableUUIDz3The table UUID must match the requirement's `uuid`.zassert-table-uuidrI   zLiteral['assert-table-uuid']ro   rL   rM   r   r+  r0   r1   c                 C  s8   |d u rt d| j|jkrt d| j d|j d S )N5Requirement failed: current table metadata is missingzTable UUID does not match: z != )r   rM   r   r,  r4   r4   r5   r-    s
   zAssertTableUUID.validateNr.  r0  r4   r4   r4   r5   r2    s
   
 r2  c                   @  sf   e Zd ZU dZeddZded< edZded< ed	d
dZded< e	dddddZ
dddZd	S )AssertRefSnapshotIdzThe table branch or tag identified by the requirement's `ref` must reference the requirement's `snapshot-id`.

    if `snapshot-id` is `null` or missing, the ref must not already exist.
    zassert-ref-snapshot-idrI   z!Literal['assert-ref-snapshot-id']ro   .rm   r   Nrp   )rJ   rS   r   rq   wrapr   handlerr*   r0   r   c                 C  s   || }i |d| j iS )Nrp   r   )r3   r6  partial_resultr4   r4   r5   serialize_model"  s   z#AssertRefSnapshotId.serialize_modelr   r+  r1   c              	   C  s   |d u rt dt|jdkr| jtkrt dt d|j| j }rT|j}| jd u r9t d| d| j d| j|jkrRt d| d| j d| j d	|j d S | jd uret d
| j d| j d S )Nr3  r   zJRequirement failed: Table has no snapshots and can only be written to the z BRANCH.zRequirement failed:  z was created concurrentlyz has changed: expected id , found z"Requirement failed: branch or tag z is missing, expected )	r   r   r   r   r   r   r   r   rq   )r3   r   r   ref_typer4   r4   r5   r-  (  s,   


zAssertRefSnapshotId.validate)r6  r*   r0   r   r.  )rC   rD   rE   r1  r   ro   rF   r   rq   r   r8  r-  r4   r4   r4   r5   r4    s   
 r4  c                   @  B   e Zd ZU dZeddZded< edddZd	ed
< dddZdS )AssertLastAssignedFieldIdzZThe table's last assigned column id must match the requirement's `last-assigned-field-id`.zassert-last-assigned-field-idrI   z(Literal['assert-last-assigned-field-id']ro   .zlast-assigned-field-idrR   rT   last_assigned_field_idr   r+  r0   r1   c                 C  8   |d u rt d|j| jkrt d| j d|j d S )Nr3  zARequirement failed: last assigned field id has changed: expected r:  )r   r   r>  r,  r4   r4   r5   r-  B     z"AssertLastAssignedFieldId.validateNr.  )	rC   rD   rE   r1  r   ro   rF   r>  r-  r4   r4   r4   r5   r=  <  
   
 r=  c                   @  r<  )AssertCurrentSchemaIdzOThe table's current schema id must match the requirement's `current-schema-id`.zassert-current-schema-idrI   z#Literal['assert-current-schema-id']ro   .zcurrent-schema-idrR   rT   r   r   r+  r0   r1   c                 C  8   |d u rt d| j|jkrt d| j d|j d S )Nr3  z<Requirement failed: current schema id has changed: expected r:  )r   r   r,  r4   r4   r5   r-  R  r@  zAssertCurrentSchemaId.validateNr.  )	rC   rD   rE   r1  r   ro   rF   r   r-  r4   r4   r4   r5   rB  L  rA  rB  c                   @  r<  )AssertLastAssignedPartitionIdzaThe table's last assigned partition id must match the requirement's `last-assigned-partition-id`.z!assert-last-assigned-partition-idrI   z,Literal['assert-last-assigned-partition-id']ro   .zlast-assigned-partition-idrR   r   last_assigned_partition_idr   r+  r0   r1   c                 C  r?  )Nr3  zERequirement failed: last assigned partition id has changed: expected r:  )r   r   rE  r,  r4   r4   r5   r-  b  r@  z&AssertLastAssignedPartitionId.validateNr.  )	rC   rD   rE   r1  r   ro   rF   rE  r-  r4   r4   r4   r5   rD  \  rA  rD  c                   @  r<  )AssertDefaultSpecIdzKThe table's default spec id must match the requirement's `default-spec-id`.zassert-default-spec-idrI   z!Literal['assert-default-spec-id']ro   .zdefault-spec-idrR   rT   r   r   r+  r0   r1   c                 C  rC  )Nr3  z:Requirement failed: default spec id has changed: expected r:  )r   r   r,  r4   r4   r5   r-  r  r@  zAssertDefaultSpecId.validateNr.  )	rC   rD   rE   r1  r   ro   rF   r   r-  r4   r4   r4   r5   rF  l  rA  rF  c                   @  r<  )AssertDefaultSortOrderIdzWThe table's default sort order id must match the requirement's `default-sort-order-id`.zassert-default-sort-order-idrI   z'Literal['assert-default-sort-order-id']ro   .zdefault-sort-order-idrR   rT   r
  r   r+  r0   r1   c                 C  rC  )Nr3  z@Requirement failed: default sort order id has changed: expected r:  )r   r
  r,  r4   r4   r5   r-    r@  z!AssertDefaultSortOrderId.validateNr.  )	rC   rD   rE   r1  r   ro   rF   r
  r-  r4   r4   r4   r5   rG  |  rA  rG  ro   .)r   r   r   r   r   r   r0   r   )r   rH   r   r   r   r   r0   r   )r   ry   r   r   r   r   r0   r   )r   rQ   r   r   r   r   r0   r   )r   r{   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )r   rW   r   r   r   r   r0   r   )r   r[   r   r   r   r   r0   r   )r   ra   r   r   r   r   r0   r   )r   rc   r   r   r   r   r0   r   )r   ri   r   r   r   r   r0   r   )r   rk   r   r   r   r   r0   r   )r   ru   r   r   r   r   r0   r   )r   rx   r   r   r   r   r0   r   )r   re   r   r   r   r   r0   r   )r   rg   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )r   r   r   r   r   r   r0   r   )FN)
r   r   r}   r  r  r   r  r  r0   r   )r   r   r  rm   r   rT   r0   r   )g
__future__r   r  rM   abcr   r   r   	functoolsr   typingr   r   r	   r
   r   r   r   pydanticr   r   r   r   pyiceberg.exceptionsr   pyiceberg.partitioningr   r   pyiceberg.schemar   pyiceberg.table.metadatar   r   r   pyiceberg.table.refsr   r   r   pyiceberg.table.snapshotsr   r   r   pyiceberg.table.sortingr    pyiceberg.table.statisticsr!   r"   r#   pyiceberg.typedefr$   r%   pyiceberg.typesr'   pyiceberg.utils.datetimer(   pyiceberg.utils.propertiesr)   pydantic.functional_serializersr*   r$  r+   r,   r-   rH   rQ   rW   r[   ra   rc   re   rg   ri   rk   ru   rx   ry   r{   r   r   r   r   r   r   r   r   r   r   registerr=   r   r  r*  r/  r2  r4  r=  rB  rD  rF  rG  TableRequirementtupler8   r4   r4   r4   r5   <module>   sR  $
		

/&'
	
( 
$ 