o
    uyi0                     @  s  d dl m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
mZmZmZmZ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/m0Z0m1Z1m2Z2m3Z3m4Z4 e	rd dl5Z6d dl7m8Z8 dZ9G dd deZ:eG dd dZ;G dd de*d Z<G dd deee/  Z=G dd dee>e?f Z@G dd dee> ZAd+d!d"ZBd,d%d&ZCd-d)d*ZDdS ).    )annotationsN)copy)	dataclass)Enum)TYPE_CHECKINGAnyDictListOptionalSetTupleUnion)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 r7   r7   `/home/ubuntu/maya3_transcribe/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Optional[int]other_field_id)r1   r2   r3   __annotations__r@   r7   r7   r7   r8   r9   D   s
   
 r9   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_namesDict[int, List[NestedField]]_addsDict[int, NestedField]_updatesSet[int]_deletes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Optional[Schema]name_mappingOptional[NameMapping]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.   r;   r:   rZ   r<   c                   s(    j j| d}|d u rtd|  |S )N)	column_idzCould not find field-id: )rC   find_column_name
ValueError)r;   column_nameselfr7   r8   get_column_namew   s   z.UpdateSchema.__init__.<locals>.get_column_namec                   s   i | ]	\}}| |qS r7   r7   ).0r;   parent_field_id)rb   r7   r8   
<dictcomp>}   s    z)UpdateSchema.__init__.<locals>.<dictcomp>)r;   r:   rZ   r<   )super__init__
isinstancer   rC   	itertoolscounthighest_field_idrD   _transactiontable_metadatarV   last_column_id_name_mappingidentifier_field_namesrE   rG   rI   setrK   rM   rN   _lazy_id_to_parentitemsrO   rQ   rR   )ra   rS   rT   rU   rV   rX   	__class__)rb   ra   r8   rg   \   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
        )rR   )ra   rU   r7   r7   r8   rU      s   	zUpdateSchema.case_sensitiver/   
new_schemaUnion[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)rx   r,   update_schemaexisting_schemarU   partner_schemarU   )pyiceberg.catalogry   r   _convert_schema_if_needed_UnionByNameVisitorrC   rR   PartnerIdByNameAccessor)ra   rv   rx   ry   r7   r7   r8   union_by_name   s   zUpdateSchema.union_by_namepathUnion[str, Tuple[str, ...]]
field_typer%   docOptional[str]requireddefault_valueOptional[L]c              
   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: r;   namer   r   r   initial_defaultwrite_default)rh   r<   r^   joinTABLE_ROOT_IDlenrC   
find_fieldrR   r   r'   value_fieldr&   element_field	is_structr;   rK   assign_new_column_idr   r   tovaluerQ   rN   rO   r(   rG   append)ra   r   r   r   r   r   r   parentr=   parent_full_path	parent_idparent_fieldparent_typeexisting_fieldnew_idnew_typer   efieldr7   r7   r8   
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   rU   z+Cannot delete a column that has additions: z)Cannot delete a column that has updates: )rh   r<   r   rC   r   rR   r;   rG   r^   rI   rK   add)ra   r   r   r=   r   r7   r7   r8   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)ra   r   r   r7   r7   r8   set_default_value  s   	zUpdateSchema.set_default_value	path_fromnew_namer<   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: r;   r   r   r   r   r   r   N)rh   tupler   rC   r   rR   r;   rK   r^   rI   getr(   r   r   r   r   r   r]   rE   remover   r   r   )ra   r   r   
field_fromupdatedfrom_field_correct_casingnew_identifier_pathr7   r7   r8   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)ra   r   r7   r7   r8   make_column_optionalV  s   	z!UpdateSchema.make_column_optionalfieldsc                 G  s   t || _d S N)rq   rE   )ra   r   r7   r7   r8   set_identifier_fieldsb  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   )rh   r<   r   rC   r   rR   r   optionalrQ   r^   r;   rK   rI   r   r(   r   r   r   r   r   )ra   r   r   r   r   r   r7   r7   r8   r   e  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   )rh   r<   r   rC   r   rR   r   r   r   r   r^   r   r   rQ   r;   rK   rI   r   r(   r   r   r   )ra   r   r   r   r   r   r   r7   r7   r8   r     sF   

z&UpdateSchema._set_column_default_valueOptional[IcebergType]Optional[bool]c           	   
   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   )rh   r<   r   rC   r   rR   r;   rK   r^   r   is_primitiver   rQ   r   r   rI   r   r(   r   r   r   r   r   r   )	ra   r   r   r   r   r=   r   r   r   r7   r7   r8   update_column  sN   


zUpdateSchema.update_columnr   r?   c                 C  s4   z
| j || jjW S  ty   Y nw | j|S r   )rC   r   rR   r;   r^   rN   r   )ra   r   r7   r7   r8   _find_for_move  s   zUpdateSchema._find_for_movemover9   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: )rO   r   r;   rC   r   rR   r   r   r^   r>   r-   r6   r5   r@   r=   rM   r   )ra   r   parent_namer   other_structr7   r7   r8   _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: )r;   r=   r>   )	rh   r   r   r   r^   r   r9   r-   r4   )ra   r   r=   r;   r7   r7   r8   
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r;   r=   r@   r>   )	rh   r   r   r   r^   r   r9   r-   r5   )ra   r   r   r=   r;   before_full_namebefore_field_idr7   r7   r8   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   )	rh   r   r   r   r^   r   r9   r-   r6   )ra   r   r   r=   r;   
after_pathafter_field_idr7   r7   r8   
move_afterO  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rc   rV   rv   r7   r8   	<genexpr>s  s    z'UpdateSchema._commit.<locals>.<genexpr>Nr7   )current_schema_id)rV   r,   r   )updates)pyiceberg.tabler   _applynextrl   rm   schemasrC   r   r   r   r   ro   r   rI   rG   r   DEFAULT_NAME_MAPPINGmodel_dump_json)ra   r   existing_schema_idrequirementsr   rX   updated_name_mappingr7   r   r8   _commitl  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   r7   r7   r8   r     s    z&UpdateSchema._apply.<locals>.<genexpr>)r   identifier_field_ids)r   rC   _ApplyChangesrG   rI   rK   rM   r^   r   r   pyiceberg.partitioningr   rl   rm   partition_specsr   	transform	source_idrq   rE   r   rR   r   r;   maxr   )ra   structrv   r   specpartition_field	field_idsr   r   r   txnnext_schema_idr7   r7   r8   r     s<   




"zUpdateSchema._applyr:   c                 C  s
   t | jS r   )r   rD   r`   r7   r7   r8   r     s   
z!UpdateSchema.assign_new_column_id)FTNN)rS   r+   rT   rP   rU   rP   rV   rW   rX   rY   rZ   r[   )rU   rP   rZ   rB   )r/   )rv   rw   rx   r$   rZ   rB   )NFN)r   r   r   r%   r   r   r   rP   r   r   rZ   rB   )r   r   rZ   rB   )r   r   r   r   rZ   rB   )r   r   r   r<   rZ   rB   )r   r<   rZ   r[   )r   r   r   rP   rZ   r[   )r   r   r   r   rZ   r[   )NNN)
r   r   r   r   r   r   r   r   rZ   rB   )r   r<   rZ   r?   )r   r9   rZ   r[   )r   r   r   r   rZ   rB   )r   r   r   r   rZ   rB   )rZ   r!   )rZ   r   )rZ   r:   ) r1   r2   r3   rA   rG   rI   rq   rK   rM   rN   rO   rg   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r7   r7   rt   r8   rB   L   sV   
 
)
b


.


%/
B



"

 (rB   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   rF   rG   rH   rI   rJ   rK   rL   rM   addsr   deletesmovesrZ   r[   c                 C  s   || _ || _|| _|| _d S r   )rG   rI   rK   rM   )ra   r   r   r   r   r7   r7   r8   rg     s   
z_ApplyChanges.__init__rV   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: )	rG   r   r   rM   rh   r*   r^   _add_and_move_fieldsr   )ra   rV   r   addedr   
new_fieldsr7   r7   r8   rV     s   
z_ApplyChanges.schemar   r*   field_resultsList[Optional[IcebergType]]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   rI   r   r;   r   r   r(   r   r*   )ra   r   r   has_changesr   idxresult_typer   r   r   r   r   updater7   r7   r8   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   )r;   rK   rI   r   r   rh   r*   rG   rM   r^   r   r   )ra   r   r  r  r   r   r   r7   r7   r8   r     s   
z_ApplyChanges.field	list_typer&   element_resultc                 C  s6   |  |j|}|d u rtd| t|j||jdS )Nz&Cannot delete element type from list: )
element_idelementelement_required)r   r   r^   r&   r	  r  )ra   r  r  element_typer7   r7   r8   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| t	|j
|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_fieldr;   rK   r^   rI   rG   r  r   r   r'   r  r  r  )ra   r  r  r  r  r   r  r7   r7   r8   map)  s(   



z_ApplyChanges.map	primitiver)   c                 C  s   |S r   r7   )ra   r  r7   r7   r8   r  G     z_ApplyChanges.primitiveN)
r   rF   r   rH   r   rJ   r   rL   rZ   r[   )rV   r   r   r   rZ   r   )r   r*   r   r   rZ   r   )r   r(   r  r   rZ   r   )r  r&   r  r   rZ   r   )r  r'   r  r   r  r   rZ   r   )r  r)   rZ   r   )r1   r2   r3   rA   rg   rV   r   r   r  r  r  r7   r7   r7   r8   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 )Dr   rB   r{   r   r|   rP   rU   rZ   r[   c                 C  s   || _ || _|| _d S r   rz   )ra   r{   r|   rU   r7   r7   r8   rg   P  s   
z_UnionByNameVisitor.__init__rV   
partner_idr?   r   c                 C  s   |S r   r7   )ra   rV   r  r   r7   r7   r8   rV   U  r  z_UnionByNameVisitor.schemar   r*   missing_positions
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   r^   r  _add_columnfield_by_namer   rU   _update_column)
ra   r   r  r  r   partner_structposmissingr   nested_fieldr7   r7   r8   r   X  s   
z_UnionByNameVisitor.structr   r:   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   )r|   r]   r   r{   r   r   r   r   )ra   r   r   r   r   r7   r7   r8   r  l  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   )r|   r]   r;   r^   r   r   r{   r   r   r   r   r   r   r   )ra   r   r   r=   _r7   r7   r8   r   t  s   z"_UnionByNameVisitor._update_columnr;   r%   c                 C  s    |dkr	| j  S | j |jS Nr,   )r|   	as_structr   r   )ra   r;   r7   r7   r8   r    s   
z$_UnionByNameVisitor._find_field_typer  c                 C     |d u S r   r7   )ra   r   r  r  r7   r7   r8   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)r^   r  rh   r&   r   r   )ra   r  r*  r+  partner_list_typer7   r7   r8   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)r^   r  rh   r'   r   r  r   )ra   r  r-  r.  r/  partner_map_typer7   r7   r8   r    s   

z_UnionByNameVisitor.mapr  r)   primitive_partner_idc                 C  r(  r   r7   )ra   r  r1  r7   r7   r8   r    r)  z_UnionByNameVisitor.primitiveN)r{   rB   r|   r   rU   rP   rZ   r[   )rV   r   r  r?   r   rP   rZ   rP   )r   r*   r  r?   r  r  rZ   rP   )r   r:   r   r(   rZ   r[   )r   r(   r   r(   rZ   r[   )r;   r:   rZ   r%   )r   r(   r  r?   r  rP   rZ   rP   )r  r&   r*  r?   r+  rP   rZ   rP   )
r  r'   r-  r?   r.  rP   r/  rP   rZ   rP   )r  r)   r1  r?   rZ   rP   )r1   r2   r3   rA   rg   rV   r   r  r   r  r   r  r  r  r7   r7   r7   r8   r   K  s   
 








r   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 )"r   r   r~   rP   rU   rZ   r[   c                 C  s   || _ || _d S r   r}   )ra   r~   rU   r7   r7   r8   rg     s   
z PartnerIdByNameAccessor.__init__partnerr?   c                 C  s   dS r&  r7   )ra   r2  r7   r7   r8   schema_partner  r  z&PartnerIdByNameAccessor.schema_partnerpartner_field_idr;   r:   
field_namer<   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   rU   )	r~   r'  r   r   r   r^   r  rU   r;   )ra   r4  r;   r5  r   r   r7   r7   r8   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: )r~   r   rh   r   r&   r^   r   r;   )ra   r7  r   r7   r7   r8   list_element_partner  
   
z,PartnerIdByNameAccessor.list_element_partnerpartner_map_idc                 C  r8  NzExpected MapType: )r~   r   rh   r   r'   r^   r  r;   ra   r;  r   r7   r7   r8   map_key_partner  r:  z'PartnerIdByNameAccessor.map_key_partnerc                 C  r8  r<  )r~   r   rh   r   r'   r^   r   r;   r=  r7   r7   r8   map_value_partner  r:  z)PartnerIdByNameAccessor.map_value_partnerN)r~   r   rU   rP   rZ   r[   )r2  r?   rZ   r?   )r4  r?   r;   r:   r5  r<   rZ   r?   )r7  r?   rZ   r?   )r;  r?   rZ   r?   )
r1   r2   r3   rA   rg   r3  r6  r9  r>  r?  r7   r7   r7   r8   r     s   
 




r   r   Tuple[NestedField, ...]r   Optional[List[NestedField]]rZ   c                 C  s   |pg }| t | S r   )r   )r   r   r7   r7   r8   _add_fields  s   rB  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   r;   rc   r   r   r7   r8   r     s    z_move_fields.<locals>.<genexpr>c                   s   g | ]
}|j  j kr|qS r7   rD  rE  rF  r7   r8   
<listcomp>  s    z _move_fields.<locals>.<listcomp>c                 3  s"    | ]\}}|j  kr|V  qd S r   rD  )rc   ir   )r@   r7   r8   r     s     r.   zUnknown operation: )r  r   r   r>   r-   r4   r5   r6   r@   r  insertr^   r   )r   r   	reorderedr   other_field_posr7   )r   r@   r8   _move_fields  s   rL  List[NestedField]!Optional[Tuple[NestedField, ...]]c                 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   rB  rL  r   )r   r   r   r   r7   r7   r8   r     s   


"r   )r   r@  r   rA  rZ   r@  )r   r@  r   rC  rZ   r@  )r   r@  r   rM  r   rC  rZ   rN  )E
__future__r   ri   r   dataclassesr   enumr   typingr   r   r   r	   r
   r   r   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-   r9   rB   r   r:   rP   r   r   rB  rL  r   r7   r7   r7   r8   <module>   s@   ((
(
     p m
2
