o
    <i                     @  s  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d d	lmZmZmZmZmZmZmZmZ d d
lmZmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d dl&m'Z'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ e
rd dl0Z1d dl2m3Z3 dZ4G dd deZ5eG dd dZ6G dd de%d Z7G dd dee*dB  Z8G dd dee9e:f Z;G dd dee9 Z<d+d!d"Z=d,d%d&Z>d-d)d*Z?dS ).    )annotationsN)copy)	dataclass)Enum)TYPE_CHECKINGAny)ResolveErrorValidationError)literal)PartnerAccessorSchemaSchemaVisitorSchemaWithPartnerVisitorassign_fresh_schema_idspromotevisitvisit_with_partner)NameMappingupdate_mapping)AddSchemaUpdateAssertCurrentSchemaIdSetCurrentSchemaUpdateSetPropertiesUpdateTableRequirementTableUpdateUpdatesAndRequirementsUpdateTableMetadata)LTableVersion)IcebergTypeListTypeMapTypeNestedFieldPrimitiveType
StructType)Transactionc                   @  s   e Zd ZdZdZdZdS )_MoveOperation         N)__name__
__module____qualname__FirstBeforeAfter r1   r1   Z/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/pyiceberg/table/update/schema.pyr'   ?   s    r'   c                   @  s2   e Zd ZU ded< ded< ded< dZded	< dS )
_Moveintfield_idstr	full_namer'   opN
int | Noneother_field_id)r+   r,   r-   __annotations__r:   r1   r1   r1   r2   r3   E   s
   
 r3   c                      sn  e Zd ZU ded< ded< ded< i Zded< i Zd	ed
< e Zded< i Zded< i Z	ded< i Z
ded< ded< ded< 				dhdi fd#d$Zdjd%d&Z	'dkdld,d-Z			dmdnd7d8Zdod9d:Zdpd;d<Zdqd@dAZdodBdCZdrdEdFZdsdGdHZdtdJdKZ			dudvdNdOZdwdRdSZdxdVdWZdodXdYZdyd[d\Zdzd^d_Zd{dadbZd|dcddZd}dfdgZ  ZS )~UpdateSchemar   _schemazitertools.count[int]_last_column_idzset[str]_identifier_field_nameszdict[int, list[NestedField]]_addsdict[int, NestedField]_updatesset[int]_deleteszdict[int, list[_Move]]_moveszdict[str, int]_added_name_to_idzdict[int, str]_id_to_parentbool_allow_incompatible_changes_case_sensitiveFTNtransactionr%   allow_incompatible_changescase_sensitiveschemaSchema | Nonename_mappingNameMapping | NonereturnNonec                   s   t  | t|tr|_td|j _nj	j
 _tdj	j
j _|_j _i _i _t _i _i _d
fdd  fdd	jj D _|_|_|_	d S )Nr(   r5   r4   rR   r6   c                   s(    j j| d}|d u rtd|  |S )N)	column_idzCould not find field-id: )r=   find_column_name
ValueError)r5   column_nameselfr1   r2   get_column_namex   s   z.UpdateSchema.__init__.<locals>.get_column_namec                   s   i | ]	\}}| |qS r1   r1   ).0r5   parent_field_id)rZ   r1   r2   
<dictcomp>~   s    z)UpdateSchema.__init__.<locals>.<dictcomp>)r5   r4   rR   r6   )super__init__
isinstancer   r=   	itertoolscounthighest_field_idr>   _transactiontable_metadatarN   last_column_id_name_mappingidentifier_field_namesr?   r@   rB   setrD   rE   rF   _lazy_id_to_parentitemsrG   rI   rJ   )rY   rK   rL   rM   rN   rP   	__class__)rZ   rY   r2   r_   ]   s(   



zUpdateSchema.__init__c                 C  s
   || _ | S )zDetermine if the case of schema needs to be considered when comparing column names.

        Args:
            case_sensitive: When false case is not considered in column name comparisons.

        Returns:
            This for method chaining
        )rJ   )rY   rM   r1   r1   r2   rM      s   	zUpdateSchema.case_sensitiver)   
new_schemaSchema | pa.Schemaformat_versionr   c                 C  sB   ddl m} t|j||ddt| | j| jdt| j| jd | S )Nr   )Catalog)rp   r&   update_schemaexisting_schemarM   partner_schemarM   )pyiceberg.catalogrq   r   _convert_schema_if_needed_UnionByNameVisitorr=   rJ   PartnerIdByNameAccessor)rY   rn   rp   rq   r1   r1   r2   union_by_name   s   zUpdateSchema.union_by_namepathstr | tuple[str, ...]
field_typer   doc
str | Nonerequireddefault_valueL | Nonec              
   C  s  t |trd|v rtd| d|f}|d }|dd }d|}d|}	t}
t|dkr]| j|	| j}|j	}t |t
rD|j}nt |trL|j}|j	jsZtd| d|	 |j}
d}z
| j|| j}W n	 tyr   Y nw |dur|j| jvrtd	| |  }t|| j}|durz
t||j}W n ty } ztd
| |d}~ww |}|r|du r| jstdd| || j|< |	| j|< t|||||||d}|
| jv r| j|
 | | S |g| j|
< | S )a&  Add a new column to a nested struct or Add a new top-level column.

        Because "." may be interpreted as a column path separator or may be used in field names, it
        is not allowed to add nested column by passing in a string. To add to nested structures or
        to add fields with names that contain "." use a tuple instead to indicate the path.

        If type is a nested type, its field IDs are reassigned when added to the existing schema.

        Args:
            path: Name for the new column.
            field_type: Type for the new column.
            doc: Documentation string for the new column.
            required: Whether the new column is required.
            default_value: Default value for the new column.

        Returns:
            This for method chaining.
        .z'Cannot add column with ambiguous name: z, provide a tuple insteadr&   Nr   zCannot add column 'z' to non-struct type: z(Cannot add column, name already exists: Invalid default value: z1Incompatible change: cannot add required column: r5   namer~   r   r   initial_defaultwrite_default)r`   r6   rV   joinTABLE_ROOT_IDlenr=   
find_fieldrJ   r~   r!   value_fieldr    element_field	is_structr5   rD   assign_new_column_idr   r
   tovaluerI   rF   rG   r"   r@   append)rY   r|   r~   r   r   r   r   parentr7   parent_full_path	parent_idparent_fieldparent_typeexisting_fieldnew_idnew_typer   efieldr1   r1   r2   
add_column   sn   








zUpdateSchema.add_columnc                 C  sv   t |tr|fn|}d|}| jj|| jd}|j| jv r%td| |j| j	v r2td| | j
|j | S )zDelete a column from a table.

        Args:
            path: The path to the column.

        Returns:
            The UpdateSchema with the delete operation staged.
        r   rM   z+Cannot delete a column that has additions: z)Cannot delete a column that has updates: )r`   r6   r   r=   r   rJ   r5   r@   rV   rB   rD   add)rY   r|   r   r7   r   r1   r1   r2   delete_column  s   	
zUpdateSchema.delete_columnc                 C  s   |  || | S )zSet the default value of a column.

        Args:
            path: The path to the column.

        Returns:
            The UpdateSchema with the delete operation staged.
        )_set_column_default_value)rY   r|   r   r1   r1   r2   set_default_value  s   	zUpdateSchema.set_default_value	path_fromnew_namer6   c              	   C  s   t |tr
d|n|}| j|| j}|j| jv r!td| | j	
|j }r@t|j||j|j|j|j|jd| j	|j< nt|j||j|j|j|j|jd| j	|j< | j|j}|| jv r{| j| |dt|j   | }| j| | S )zUpdate the name of a column.

        Args:
            path_from: The path to the column to be renamed.
            new_name: The new path of the column.

        Returns:
            The UpdateSchema with the rename operation staged.
        r   z-Cannot rename a column that will be deleted: r5   r   r~   r   r   r   r   N)r`   tupler   r=   r   rJ   r5   rD   rV   rB   getr"   r~   r   r   r   r   rU   r?   remover   r   r   )rY   r   r   
field_fromupdatedfrom_field_correct_casingnew_identifier_pathr1   r1   r2   rename_column)  s:   


zUpdateSchema.rename_columnc                 C  s   | j |dd | S )zMake a column optional.

        Args:
            path: The path to the field.

        Returns:
            The UpdateSchema with the requirement change staged.
        Fr   )_set_column_requirement)rY   r|   r1   r1   r2   make_column_optionalW  s   	z!UpdateSchema.make_column_optionalfieldsc                 G  s   t || _d S N)ri   r?   )rY   r   r1   r1   r2   set_identifier_fieldsc  s   z"UpdateSchema.set_identifier_fieldsc              	   C  s   t |tr|fn|}d|}| j|| j}|jr|s!|jr#|s#d S | js0|r0t	d| d|j
| jv r=t	d| | j|j
 }r]t|j
|j|j|j||j|jd| j|j
< d S t|j
|j|j|j||j|jd| j|j
< d S )Nr   z"Cannot change column nullability: z: optional -> required-Cannot update a column that will be deleted: r   )r`   r6   r   r=   r   rJ   r   optionalrI   rV   r5   rD   rB   r   r"   r   r~   r   r   r   )rY   r|   r   r   r   r   r1   r1   r2   r   f  s8   


z$UpdateSchema._set_column_requirementr   c              
   C  s.  t |tr|fn|}d|}| j|| j}|d ur;zt||jj	}W n t
y: } zt
d| |d }~ww |jrE||jkrEd S | jsS|jrS|d u rSt
d|j| jv r`t
d| | j|j }rt|j|j|j|j|j|j|d| j|j< d S t|j|j|j|j|j|j|d| j|j< d S )Nr   r   z?Cannot change change default-value of a required column to Noner   r   )r`   r6   r   r=   r   rJ   r
   r   r~   r   rV   r   r   rI   r5   rD   rB   r   r"   r   r   r   )rY   r|   r   r   r   r   r   r1   r1   r2   r     sF   

z&UpdateSchema._set_column_default_valueIcebergType | Nonebool | Nonec           	   
   C  s  t |tr|fn|}d|}|du r|du r|du r| S | j|| j}|j| jv r2td| |duro|j	j
sCtd|j	 d| jso|j	|krozt|j	| W n tyn } ztd| d|j	 d| |d}~ww | j|j }rt|j|j|p|j	|dur|n|j|j|j|jd| j|j< nt|j|j|p|j	|dur|n|j|j|j|jd| j|j< |dur| j||d	 | S )
a7  Update the type of column.

        Args:
            path: The path to the field.
            field_type: The new type
            required: If the field should be required
            doc: Documentation describing the column

        Returns:
            The UpdateSchema with the type update staged.
        r   Nr   zCannot change column type: z is not a primitivez: z -> r   r   )r`   r6   r   r=   r   rJ   r5   rD   rV   r~   is_primitiver	   rI   r   r   rB   r   r"   r   r   r   r   r   r   )	rY   r|   r~   r   r   r7   r   r   r   r1   r1   r2   update_column  sN   


zUpdateSchema.update_columnr   r9   c                 C  s4   z
| j || jjW S  ty   Y nw | j|S r   )r=   r   rJ   r5   rV   rF   r   )rY   r   r1   r1   r2   _find_for_move  s   zUpdateSchema._find_for_movemover3   c                 C  s&  | j |j }r[| jj|| jd}|jjstd|j |j	t
jks*|j	t
jkrJ|jd u r3td| j |j| j |jkrJtd|j d| j|jg |g | j|j< d S |j	t
jksg|j	t
jkr|jd u rptd| j |j }rtd|j d| | jtg |g | jt< d S )Nr   z'Cannot move fields in non-struct type: z6Expected other field when performing before/after movezCannot move field z to a different structz to a different struct: )rG   r   r5   r=   r   rJ   r~   r   rV   r8   r'   r0   r/   r:   r7   rE   r   )rY   r   parent_namer   other_structr1   r1   r2   _move  s    
"
zUpdateSchema._movec                 C  sR   t |tr
d|n|}| |}|du rtd| | t||tjd | S )zMove the field to the first position of the parent struct.

        Args:
            path: The path to the field.

        Returns:
            The UpdateSchema with the move operation staged.
        r   NCannot move missing column: )r5   r7   r8   )	r`   r   r   r   rV   r   r3   r'   r.   )rY   r|   r7   r5   r1   r1   r2   
move_first  s   	
zUpdateSchema.move_firstbefore_pathc                 C     t |tr
d|n|}| |}|du rtd| t |tr&d|n|}| |}|du r;td| d| ||krGtd| d| t|||tjd | S )zMove the field to before another field.

        Args:
            path: The path to the field.

        Returns:
            The UpdateSchema with the move operation staged.
        r   Nr   Cannot move z before missing column: z before itselfr5   r7   r:   r8   )	r`   r   r   r   rV   r   r3   r'   r/   )rY   r|   r   r7   r5   before_full_namebefore_field_idr1   r1   r2   move_before.  s"   	

zUpdateSchema.move_before
after_namec                 C  r   )zMove the field to after another field.

        Args:
            path: The path to the field.

        Returns:
            The UpdateSchema with the move operation staged.
        r   Nr   r   z after missing column: z after itselfr   )	r`   r   r   r   rV   r   r3   r'   r0   )rY   r|   r   r7   r5   
after_pathafter_field_idr1   r1   r2   
move_afterP  s   	

zUpdateSchema.move_afterr   c                   s   ddl m} |   t fdd| jjjD d}d}d}|| jjkr`|t	| jjdf7 }|du r>|t
 dtd	d
f7 }n|t|d
f7 }| j }r`t|| j| j}|t|j| idf7 }||fS )z%Apply the pending changes and commit.r   )TablePropertiesc                 3  s    | ]
}| kr|j V  qd S r   	schema_idr[   rN   rn   r1   r2   	<genexpr>t  s    z'UpdateSchema._commit.<locals>.<genexpr>Nr1   )current_schema_id)rN   r&   r   )updates)pyiceberg.tabler   _applynextrd   re   schemasr=   r   r   r   r   rg   r   rB   r@   r   DEFAULT_NAME_MAPPINGmodel_dump_json)rY   r   existing_schema_idrequirementsr   rP   updated_name_mappingr1   r   r2   _commitm  s*   
zUpdateSchema._commitc                 C  s   t | jt| j| j| j| j}|du rtdt|j	 }ddl
m} | jjjD ]}|j	D ]}||j|j|j|t  q,q't }| jD ](}z
|j|| jd}W n tyc }	 z	td| d|	d}	~	ww ||j qB| j }
rd|
jdurtd	d
 |
jjD nd }nd}t|j	||dS )zApply the pending changes to the original schema and returns the result.

        Returns:
            the result Schema when all pending updates are applied
        NzCould not apply changesr   )validate_partition_namer   zCannot find identifier field z:. In case of deletion, update the identifier fields first.r(   c                 s  s    | ]}|j V  qd S r   r   r   r1   r1   r2   r     s    z&UpdateSchema._apply.<locals>.<genexpr>)r   identifier_field_ids)r   r=   _ApplyChangesr@   rB   rD   rE   rV   r   r   pyiceberg.partitioningr   rd   re   partition_specsr   	transform	source_idri   r?   r   rJ   r   r5   maxr   )rY   structrn   r   specpartition_field	field_idsr   r   r   txnnext_schema_idr1   r1   r2   r     s<   




"zUpdateSchema._applyr4   c                 C  s
   t | jS r   )r   r>   rX   r1   r1   r2   r     s   
z!UpdateSchema.assign_new_column_id)FTNN)rK   r%   rL   rH   rM   rH   rN   rO   rP   rQ   rR   rS   )rM   rH   rR   r<   )r)   )rn   ro   rp   r   rR   r<   )NFN)r|   r}   r~   r   r   r   r   rH   r   r   rR   r<   )r|   r}   rR   r<   )r|   r}   r   r   rR   r<   )r   r}   r   r6   rR   r<   )r   r6   rR   rS   )r|   r}   r   rH   rR   rS   )r|   r}   r   r   rR   rS   )NNN)
r|   r}   r~   r   r   r   r   r   rR   r<   )r   r6   rR   r9   )r   r3   rR   rS   )r|   r}   r   r}   rR   r<   )r|   r}   r   r}   rR   r<   )rR   r   )rR   r   )rR   r4   ) r+   r,   r-   r;   r@   rB   ri   rD   rE   rF   rG   r_   rM   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r1   r1   rl   r2   r<   M   sV   
 
)
b


.


%/
B



"

 (r<   c                   @  st   e Zd ZU ded< ded< ded< ded< d2ddZd3ddZd4ddZd5d d!Zd6d%d&Zd7d+d,Z	d8d/d0Z
d1S )9r   %dict[int, builtins.list[NestedField]]r@   rA   rB   rC   rD   dict[int, builtins.list[_Move]]rE   addsr   deletesmovesrR   rS   c                 C  s   || _ || _|| _|| _d S r   )r@   rB   rD   rE   )rY   r   r   r   r   r1   r1   r2   r_     s   
z_ApplyChanges.__init__rN   r   struct_resultr   c                 C  sf   | j t}| jt}|d us|d ur1t|ts td| t|j|p&g |p)g  }r1t| S |S Nz!Cannot add fields to non-struct: )	r@   r   r   rE   r`   r$   rV   _add_and_move_fieldsr   )rY   rN   r   addedr   
new_fieldsr1   r1   r2   rN     s   
z_ApplyChanges.schemar   r$   field_results!builtins.list[IcebergType | None]c                 C  s   d}g }t |D ]f\}}|| }|d u rd}q|j| }|j}|j}	|j}
|j}| j|j }r=|j}|j}	|j}
|j}|j|kr\|j	|kr\|j|
kr\|j|	kr\|j|kr\|
| qd}|
t|j|||
|	|j|d q|rut| S |S )NFTr   )	enumerater   r   r   r   r   rB   r   r5   r~   r   r"   r   r$   )rY   r   r   has_changesr   idxresult_typer   r   r   r   r   updater1   r1   r2   r     sL   





z_ApplyChanges.structr   r"   field_resultc                 C  s   |j | jv rd S | j|j  }r|j|jkr|jS t|trS| j|j }| j|j }|d us5|d urSt|jtsBt	d| t
|j|pHg |pKg  }rSt| S |S r   )r5   rD   rB   r   r~   r`   r$   r@   rE   rV   r   r   )rY   r   r   r   r   r   r   r1   r1   r2   r     s   
z_ApplyChanges.field	list_typer    element_resultc                 C  sR   |  |j|}|d u rtd| |j}| j|j }r!|j}t|j||dS )Nz&Cannot delete element type from list: )
element_idelementelement_required)	r   r   rV   r  rB   r   r  r   r    )rY   r  r  element_typer  r   r1   r1   r2   list#  s   z_ApplyChanges.listmap_typer!   
key_resultvalue_resultc           	      C  s   |j j}|| jv rtd| || jv rtd| || jv r(td| |j|kr4td| |j}| ||}|d u rHtd| |j	}| j
|j }rW|j}t|j|j|j||dS )NzCannot delete map keys: zCannot update map keys: zCannot add fields to map keys: zCannot alter map keys: z#Cannot delete value type from map: )key_idkey_typevalue_id
value_typevalue_required)	key_fieldr5   rD   rV   rB   r@   r  r   r   r  r   r  r   r!   r  )	rY   r  r	  r
  r  r   r  r  r   r1   r1   r2   map.  s.   



z_ApplyChanges.map	primitiver#   c                 C  s   |S r   r1   )rY   r  r1   r1   r2   r  N     z_ApplyChanges.primitiveN)
r   r   r   rA   r   rC   r   r   rR   rS   )rN   r   r   r   rR   r   )r   r$   r   r   rR   r   )r   r"   r   r   rR   r   )r  r    r  r   rR   r   )r  r!   r	  r   r
  r   rR   r   )r  r#   rR   r   )r+   r,   r-   r;   r_   rN   r   r   r  r  r  r1   r1   r1   r2   r     s   
 



5

 r   c                   @  s   e Zd ZU ded< ded< 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
dAd+d,ZdBd2d3ZdCd7d8Zd9S )Dry   r<   rs   r   rt   rH   rM   rR   rS   c                 C  s   || _ || _|| _d S r   rr   )rY   rs   rt   rM   r1   r1   r2   r_   W  s   
z_UnionByNameVisitor.__init__rN   
partner_idr9   r   c                 C  s   |S r   r1   )rY   rN   r  r   r1   r1   r2   rN   \  r  z_UnionByNameVisitor.schemar   r$   missing_positionsbuiltins.list[bool]c           
      C  s   |d u rdS |j }| |}|jstd| t|D ]$\}}|r+| |||  q|| }|j|j| jd }	r@| 	||	 qdS )NTzExpected a struct, got: r   F)
r   _find_field_typer   rV   r   _add_columnfield_by_namer   rM   _update_column)
rY   r   r  r  r   partner_structposmissingr   nested_fieldr1   r1   r2   r   _  s   
z_UnionByNameVisitor.structr   r4   r   r"   c                 C  sB   | j | }r||jf}n|jf}| jj||j|j|jd d S )N)r|   r~   r   r   )rt   rU   r   rs   r   r~   r   r   )rY   r   r   r   r|   r1   r1   r2   r  s  s   z_UnionByNameVisitor._add_columnr   c                 C  s   | j |j}|d u rtd| |jr|jr| j| |jj	rD|j|jkrDz	t
|j|j}W n tyC   | jj||jd Y nw |jd urZ|j|jkr\| jj||jd d S d S d S )NzCould not find field: )r~   )r   )rt   rU   r5   rV   r   r   rs   r   r~   r   r   r   r   r   )rY   r   r   r7   _r1   r1   r2   r  {  s   z"_UnionByNameVisitor._update_columnr5   r   c                 C  s    |dkr	| j  S | j |jS Nr&   )rt   	as_structr   r~   )rY   r5   r1   r1   r2   r    s   
z$_UnionByNameVisitor._find_field_typer   c                 C     |d u S r   r1   )rY   r   r  r   r1   r1   r2   r        z_UnionByNameVisitor.fieldr  r    list_partner_idelement_missingc                 C  sN   |d u rdS |rt d| |}t|tst d| | |j|j dS )NTzAError traversing schemas: element is missing, but list is presentzExpected list-type, got: F)rV   r  r`   r    r  r   )rY   r  r$  r%  partner_list_typer1   r1   r2   r    s   

z_UnionByNameVisitor.listr  r!   map_partner_idkey_missingvalue_missingc                 C  sj   |d u rdS |rt d|rt d| |}t|ts#t d| | |j|j | |j|j dS )NTz<Error traversing schemas: key is missing, but map is presentz>Error traversing schemas: value is missing, but map is presentzExpected map-type, got: F)rV   r  r`   r!   r  r  r   )rY   r  r'  r(  r)  partner_map_typer1   r1   r2   r    s   

z_UnionByNameVisitor.mapr  r#   primitive_partner_idc                 C  r"  r   r1   )rY   r  r+  r1   r1   r2   r    r#  z_UnionByNameVisitor.primitiveN)rs   r<   rt   r   rM   rH   rR   rS   )rN   r   r  r9   r   rH   rR   rH   )r   r$   r  r9   r  r  rR   rH   )r   r4   r   r"   rR   rS   )r   r"   r   r"   rR   rS   )r5   r4   rR   r   )r   r"   r  r9   r   rH   rR   rH   )r  r    r$  r9   r%  rH   rR   rH   )
r  r!   r'  r9   r(  rH   r)  rH   rR   rH   )r  r#   r+  r9   rR   rH   )r+   r,   r-   r;   r_   rN   r   r  r  r  r   r  r  r  r1   r1   r1   r2   ry   R  s   
 








ry   c                   @  sZ   e Zd ZU ded< 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S )"rz   r   rv   rH   rM   rR   rS   c                 C  s   || _ || _d S r   ru   )rY   rv   rM   r1   r1   r2   r_     s   
z PartnerIdByNameAccessor.__init__partnerr9   c                 C  s   dS r   r1   )rY   r,  r1   r1   r2   schema_partner  r  z&PartnerIdByNameAccessor.schema_partnerpartner_field_idr5   r4   
field_namer6   c                 C  s\   |d ur,|dkr| j  }n| j |j}|jstd| |j|| jd }r,|jS d S )Nr&   zExpected StructType: )r   rM   )	rv   r!  r   r~   r   rV   r  rM   r5   )rY   r.  r5   r/  r   r   r1   r1   r2   field_partner  s   z%PartnerIdByNameAccessor.field_partnerpartner_list_idc                 C  @   |d ur| j | }rt|jtstd| |jjjS d S )NzExpected ListType: )rv   r   r`   r~   r    rV   r   r5   )rY   r1  r   r1   r1   r2   list_element_partner  
   
z,PartnerIdByNameAccessor.list_element_partnerpartner_map_idc                 C  r2  NzExpected MapType: )rv   r   r`   r~   r!   rV   r  r5   rY   r5  r   r1   r1   r2   map_key_partner  r4  z'PartnerIdByNameAccessor.map_key_partnerc                 C  r2  r6  )rv   r   r`   r~   r!   rV   r   r5   r7  r1   r1   r2   map_value_partner  r4  z)PartnerIdByNameAccessor.map_value_partnerN)rv   r   rM   rH   rR   rS   )r,  r9   rR   r9   )r.  r9   r5   r4   r/  r6   rR   r9   )r1  r9   rR   r9   )r5  r9   rR   r9   )
r+   r,   r-   r;   r_   r-  r0  r3  r8  r9  r1   r1   r1   r2   rz     s   
 




rz   r   tuple[NestedField, ...]r   list[NestedField] | NonerR   c                 C  s   |pg }| t | S r   )r   )r   r   r1   r1   r2   _add_fields  s   r<  r   list[_Move]c                   s   t t| }|D ][ t fdd|D } fdd|D } jtjkr*|g| }q jtjks6 jtjkr\ jtfddt	|D } jtjkrS|
|| q|
|d | qtd j t|S )Nc                 3  s     | ]}|j  j kr|V  qd S r   r5   r[   r   r   r1   r2   r     s    z_move_fields.<locals>.<genexpr>c                   s   g | ]
}|j  j kr|qS r1   r>  r?  r@  r1   r2   
<listcomp>  s    z _move_fields.<locals>.<listcomp>c                 3  s"    | ]\}}|j  kr|V  qd S r   r>  )r[   ir   )r:   r1   r2   r     s     r(   zUnknown operation: )r  r   r   r8   r'   r.   r/   r0   r:   r   insertrV   r   )r   r   	reorderedr   other_field_posr1   )r   r:   r2   _move_fields  s   rF  list[NestedField]tuple[NestedField, ...] | Nonec                 C  sh   t |dkrt| |}t |dkrt||S |S t |dkr#t| |S t |dkr+d S tg | |R  S )Nr   )r   r<  rF  r   )r   r   r   r   r1   r1   r2   r     s   


"r   )r   r:  r   r;  rR   r:  )r   r:  r   r=  rR   r:  )r   r:  r   rG  r   r=  rR   rH  )@
__future__r   builtinsra   r   dataclassesr   enumr   typingr   r   pyiceberg.exceptionsr   r	   pyiceberg.expressionsr
   pyiceberg.schemar   r   r   r   r   r   r   r   pyiceberg.table.name_mappingr   r   pyiceberg.table.updater   r   r   r   r   r   r   r   pyiceberg.typedefr   r   pyiceberg.typesr   r    r!   r"   r#   r$   pyarrowpar   r%   r   r'   r3   r<   r   r4   rH   ry   rz   r<  rF  r   r1   r1   r1   r2   <module>   sB   (
(
     p m
2
