o
    <iy                      @   s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZ G d
d deZG dd deZG dd deZdZG dd deZd ZeedZefdededede def
ddZ!dS )    )Callable)Enum)	AnnotatedAny)BeforeValidatorFieldPlainSerializerWithJsonSchemamodel_validator)ValidationError)Schema)IdentityTransform	Transformparse_transform)IcebergBaseModel)IcebergTypec                   @   0   e Zd ZdZdZdefddZdefddZdS )	SortDirectionascdescreturnc                 C   s   | j S )<Return the string representation of the SortDirection class.nameself r   T/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/pyiceberg/table/sorting.py__str__)   s   zSortDirection.__str__c                 C      d| j  S )r   zSortDirection.r   r   r   r   r   __repr__-      zSortDirection.__repr__N)__name__
__module____qualname__ASCDESCstrr   r    r   r   r   r   r   %   
    r   c                   @   r   )		NullOrderznulls-firstz
nulls-lastr   c                 C   s   | j ddS )8Return the string representation of the NullOrder class._ )r   replacer   r   r   r   r   6      zNullOrder.__str__c                 C   r   )r*   z
NullOrder.r   r   r   r   r   r    :   r!   zNullOrder.__repr__N)r"   r#   r$   NULLS_FIRST
NULLS_LASTr'   r   r    r   r   r   r   r)   2   r(   r)   c                       s8  e Zd ZU dZ				ddedB deeef eegeeef f B dB de	dB de
dB def
 fdd	Zed
ddeeef deeef fddZed
dededefddZeddZeed< ee dZeeeeedd ededdiddf ed< e Ze	ed< eddZe
ed< defddZ  ZS )	SortFieldu$  Sort order field.

    Args:
      source_id (int): Source column id from the table’s schema.
      transform (str): Transform that is used to produce values to be sorted on from the source column.
                       This is the same transform as described in partition transforms.
      direction (SortDirection): Sort direction, that can only be either asc or desc.
      null_order (NullOrder): Null order that describes the order of null values when sorted.
                              Can only be either nulls-first or nulls-last.
    N	source_id	transform	direction
null_orderdatac                    sV   |d ur||d< |d ur||d< |d ur||d< |d ur ||d< t  jdi | d S )N	source-idr3   r4   
null-orderr   super__init__)r   r2   r3   r4   r5   r6   	__class__r   r   r;   K   s   zSortField.__init__before)modevaluesr   c                 C   sH   | dr	|d ntj|d< | ds"|d tjkrtjntj|d< |S )Nr4   r8   )getr   r%   r)   r/   r0   )clsr@   r   r   r   set_null_order]   s   
zSortField.set_null_orderc                 C   s`   t |tr.d|vr.|d  }r.t |tr.t|dkrtdt|dkr(td|d |d< |S )Nr7   z
source-idsr   zEmpty source-ids is not allowed   z/Multi argument transforms are not yet supported)
isinstancedictlistlen
ValueError)rB   r6   
source_idsr   r   r   map_source_ids_onto_source_idd   s   

z'SortField.map_source_ids_onto_source_idr7   )alias)defaultc                 C   s   t | S Nr'   )cr   r   r   <lambda>u   s    zSortField.<lambda>)return_typetypestringserializationr8   c                 C   sH   t | jtr| j d| j d| j S | j d| j d| j d| j S )z8Return the string representation of the SortField class.r,   (z) )rE   r3   r   r2   r4   r5   r   r   r   r   r   {   s   "zSortField.__str__)NNNN) r"   r#   r$   __doc__intr   r   r   r   r   r)   r;   r
   rF   r'   rC   classmethodrK   r   r2   __annotations__r   r3   r   r   r   r   r	   r4   r5   r   __classcell__r   r   r<   r   r1   ?   sD   
 $$
r1   rD   c                       s   e Zd ZU dZededZeed< ee	dZ
e	e ed< dedef fdd	Zed
efddZd
efddZd
efddZded
dfddZ  ZS )	SortOrdera  Describes how the data is sorted within the table.

    Users can sort their data within partitions by columns to gain performance.

    The order of the sort fields within the list defines the order in which the sort is applied to the data.

    Args:
      fields (List[SortField]): The fields how the table is sorted.

    Keyword Args:
      order_id (int): An unique id of the sort-order of a table.
    zorder-id)rL   rM   order_id)default_factoryfieldsr6   c                    s"   |r||d< t  jdi | d S )Nr_   r   r9   )r   r_   r6   r<   r   r   r;      s   zSortOrder.__init__r   c                 C   s   t | jdkS )Nr   )rH   r_   r   r   r   r   is_unsorted   r.   zSortOrder.is_unsortedc                 C   s8   d}| j r|dddd | j D  d 7 }|d7 }|S )8Return the string representation of the SortOrder class.[z
  c                 S   s   g | ]}t |qS r   rO   ).0fieldr   r   r   
<listcomp>   s    z%SortOrder.__str__.<locals>.<listcomp>
])r_   join)r   
result_strr   r   r   r      s
   "zSortOrder.__str__c                 C   s:   | j rddd | j D  dnd}d| d| j dS )ra   z, c                 s   s    | ]}t |V  qd S rN   )repr)rc   columnr   r   r   	<genexpr>   s    z%SortOrder.__repr__.<locals>.<genexpr> z
SortOrder(z	order_id=))r_   rh   r]   )r   r_   r   r   r   r       s   &zSortOrder.__repr__schemaNc                 C   s~   | j D ]9}|j|j}|d u rtd| |jjs"td| |j|js<td|j	 d|j dd|j  qd S )Nz*Cannot find source column for sort field: z+Cannot sort by non-primitive source field: zInvalid source field z with type r,   zfor transform: )
r_   _lazy_id_to_fieldrA   r2   r   
field_typeis_primitiver3   can_transformr   )r   ro   rd   source_fieldr   r   r   check_compatible   s   

zSortOrder.check_compatible)r"   r#   r$   rW   r   INITIAL_SORT_ORDER_IDr]   rX   rZ   rG   r_   r1   r   r;   propertyboolr`   r'   r   r    r   ru   r[   r   r   r<   r   r\      s   
 r\   )r]   
sort_order
old_schemafresh_schemasort_order_idr   c              	   C   s   | j rtS g }| jD ]2}||j}|d u rtd| ||}|d u r-td| |t|j	|j
|j|jd q
t|d|iS )NzCould not find in old schema: z&Could not find field in fresh schema: )r2   r3   r4   r5   r]   )r`   UNSORTED_SORT_ORDERr_   find_column_namer2   rI   
find_fieldappendr1   field_idr3   r4   r5   r\   )ry   rz   r{   r|   fresh_fieldsrd   original_fieldfresh_fieldr   r   r   assign_fresh_sort_order_ids   s&   

	r   N)"collections.abcr   enumr   typingr   r   pydanticr   r   r   r	   r
   pyiceberg.exceptionsr   pyiceberg.schemar   pyiceberg.transformsr   r   r   pyiceberg.typedefr   pyiceberg.typesr   r   r)   r1   rv   r\   UNSORTED_SORT_ORDER_IDr}   rX   r   r   r   r   r   <module>   s8   E5
