o
    i                     @  s`  U d dl mZ d dl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 d dl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mZ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%m&Z&m'Z' d dl(m)Z) dddZ*dddZ+dddZ,G dd deeZ-ed Z.d e/d!< dd&d'Z0G d(d) d)Z1G d*d+ d+Z2G d,d- d-eZ3G d.d/ d/e1e2eZ4G d0d1 d1e4Z5G d2d de1e3eZ6G d3d4 d4e6e e7 Z8G d5d6 d6e-Z9G d7d8 d8e-Z:G d9d: d:e-Z;G d;d< d<e-e)e e< Z=G d=d> d>e-e)e e< Z>G d?d@ d@e2e-eZ?G dAdB dBe3e-eZ@G dCdD dDe@eZAG dEdF dFe?eZBG dGdH dHeBZCG dIdJ dJeBZDG dKdL dLeAZEG dMdN dNeAZFG dOdP dPeBZGG dQdR dReBZHG dSdT dTeAZIG dUdV dVeAZJG dWdX dXe@eZKG dYdZ dZe?eZLG d[d\ d\eLZMG d]d^ d^eLZNG d_d` d`eKZOG dadb dbeKeZPG dcdd dde@eZQG dedf dfe?eZRG dgdh dheRZSG didj djeRZTG dkdl dleRZUG dmdn dneRZVG dodp dpeRZWG dqdr dreRZXG dsdt dteRZYG dudv dveRZZG dwdx dxeQZ[G dydz dzeQZ\G d{d| d|eQZ]G d}d~ d~eQZ^G dd deQZ_G dd deQZ`G dd deQZaG dd deQZbdS )    )annotations)ABCabstractmethod)CallableIterableSequence)cached_property)Any	TypeAlias)Literal)
ConfigDictFieldSerializeAsAnymodel_validator)ValidatorFunctionWrapHandler)AboveMaxBelowMinr   literal)AccessorSchema)IcebergBaseModelIcebergRootModelLLiteralValueStructProtocol)
DoubleType	FloatTypeNestedField)	Singletontermstr | UnboundTermreturnUnboundTermc                 C  s   t | tr	t| S | S N)
isinstancestr	Referencer    r(   \/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/pyiceberg/expressions/__init__.py_to_unbound_term$   s   r*   values"Iterable[L] | Iterable[Literal[L]]set[Literal[L]]c                 C  s   dd | D S )Nc                 S  s   h | ]}t |qS r(   )_to_literal).0vr(   r(   r)   	<setcomp>)       z"_to_literal_set.<locals>.<setcomp>r(   )r+   r(   r(   r)   _to_literal_set(   s   r3   valueL | Literal[L]
Literal[L]c                 C  s   t | tr| S t| S r#   )r$   r   r   r4   r(   r(   r)   r.   ,   s   
r.   c                   @  sJ   e Zd ZdZedddZdddZddd	Zed
de	dddZ
dS )BooleanExpressionz*An expression that evaluates to a boolean.r!   c                 C     dS )2Transform the Expression into its negated version.Nr(   selfr(   r(   r)   
__invert__6       zBooleanExpression.__invert__otherc                 C  "   t |tstd| t| |S )z,Perform and operation on another expression.!Expected BooleanExpression, got: )r$   r8   
ValueErrorAndr<   r?   r(   r(   r)   __and__:      

zBooleanExpression.__and__c                 C  r@   )z+Perform or operation on another expression.rA   )r$   r8   rB   OrrD   r(   r(   r)   __or__A   rF   zBooleanExpression.__or__wrap)moder0   r	   handlerr   c                 C  s  t |tr|S t |tr|du rt S t S t |tr|d }r|dkr-tdi |S |dkr8tdi |S |dkrCt	di |S |dkrNt
di |S |dkrYtdi |S |dkrdtdi |S |d	krotdi |S |d
krztdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S |dkrtdi |S ||S )z5Apply custom deserialization logic before validation.Ttypeis-nullnot-nullis-nannot-nanltlt-eqgtgt-eqeqnot-eqstarts-withnot-starts-withinnot-inandornotNr(   )r$   r8   bool
AlwaysTrueAlwaysFalsedictgetIsNullNotNullIsNaNNotNaNLessThanLessThanOrEqualGreaterThanGreaterThanOrEqualEqualTo
NotEqualTo
StartsWithNotStartsWithInNotInrC   rG   Not)clsr0   rK   
field_typer(   r(   r)   handle_primitive_typeH   sP   

z'BooleanExpression.handle_primitive_typeNr!   r8   )r?   r8   r!   r8   )r0   r	   rK   r   r!   r8   )__name__
__module____qualname____doc__r   r=   rE   rH   r   classmethodrt   r(   r(   r(   r)   r8   3   s    

r8   r
   SerializableBooleanExpression	operator_CCallable[[BooleanExpression, BooleanExpression], BooleanExpression]itemsSequence[BooleanExpression]c                 C  sZ   |st dt|dkr|d S t|d }t| |d| }t| ||d }| ||S )u  
    Recursively constructs a balanced binary tree of BooleanExpressions using the provided binary operator.

    This function is a safer and more scalable alternative to:
        reduce(operator_, items)

    Using `reduce` creates a deeply nested, unbalanced tree (e.g., operator_(a, operator_(b, operator_(c, ...)))),
    which grows linearly with the number of items. This can lead to RecursionError exceptions in Python
    when the number of expressions is large (e.g., >1000).

    In contrast, this function builds a balanced binary tree with logarithmic depth (O(log n)),
    helping avoid recursion issues and ensuring that expression trees remain stable, predictable,
    and safe to traverse — especially in tools like PyIceberg that operate on large logical trees.

    Parameters:
        operator_ (Callable): A binary operator function (e.g., pyiceberg.expressions.Or, And) that takes two
            BooleanExpressions and returns a combined BooleanExpression.
        items (Sequence[BooleanExpression]): A sequence of BooleanExpression objects to combine.

    Returns:
        BooleanExpression: The balanced combination of all input BooleanExpressions.

    Raises:
        ValueError: If the input sequence is empty.
    zNo expressions to combine   r      N)rB   len_build_balanced_tree)r|   r~   midleftrightr(   r(   r)   r      s   
r   c                   @     e Zd ZdZdS )Termz.A simple expression that evaluates to a value.Nrv   rw   rx   ry   r(   r(   r(   r)   r          r   c                   @  r   )Boundz$Represents a bound value expression.Nr   r(   r(   r(   r)   r      r   r   c                   @  s2   e Zd ZdZeddd	d
ZeedddZdS )Unboundz'Represents an unbound value expression.Tschemar   case_sensitiver^   r!   Bound | BooleanExpressionc                 C     d S r#   r(   r<   r   r   r(   r(   r)   bind      zUnbound.bindtype[Bound]c                 C  r   r#   r(   r;   r(   r(   r)   as_bound      zUnbound.as_boundNT)r   r   r   r^   r!   r   )r!   r   )rv   rw   rx   ry   r   r   propertyr   r(   r(   r(   r)   r      s    r   c                   @  s,   e Zd ZdZedddZedd	d
ZdS )	BoundTermzRepresents a bound term.r!   BoundReferencec                 C  r9   )zReturn the bound reference.Nr(   r;   r(   r(   r)   ref   r>   zBoundTerm.refstructr   r	   c                 C  r9   )zcReturn the value at the referenced field's position in an object that abides by the StructProtocol.Nr(   r<   r   r(   r(   r)   eval   r>   zBoundTerm.evalNr!   r   r   r   r!   r	   )rv   rw   rx   ry   r   r   r   r(   r(   r(   r)   r      s    r   c                   @  s^   e Zd ZU dZ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   zA reference bound to a field in a schema.

    Args:
        field (NestedField): A referenced field in an Iceberg schema.
        accessor (Accessor): An Accessor object to access the value at the field's position.
    r   fieldr   accessorc                 C  s   || _ || _d S r#   r   r   )r<   r   r   r(   r(   r)   __init__   s   
zBoundReference.__init__r   r   r!   r	   c                 C  s   | j |S )aP  Return the value at the referenced field's position in an object that abides by the StructProtocol.

        Args:
            struct (StructProtocol): A row object that abides by the StructProtocol and returns values given a position.
        Returns:
            Any: The value at the referenced field's position in `struct`.
        )r   rb   r   r(   r(   r)   r      s   zBoundReference.evalr?   r^   c                 C     t |tr| j|jkS dS )zAReturn the equality of two instances of the BoundReference class.F)r$   r   r   rD   r(   r(   r)   __eq__      zBoundReference.__eq__r%   c                 C     dt | j dt | j dS )z=Return the string representation of the BoundReference class.zBoundReference(field=z, accessor=))reprr   r   r;   r(   r(   r)   __repr__      zBoundReference.__repr__c                 C  s   | S r#   r(   r;   r(   r(   r)   r      r   zBoundReference.refintc                 C  s   t t| S )z.Return hash value of the BoundReference class.)hashr%   r;   r(   r(   r)   __hash__      zBoundReference.__hash__N)r   r   r   r   r   r?   r	   r!   r^   r!   r%   r   )r!   r   )rv   rw   rx   ry   __annotations__r   r   r   r   r   r   r(   r(   r(   r)   r      s   
 





r   c                   @  s    e Zd ZdZeddd	d
ZdS )r"   zRepresents an unbound term.Tr   r   r   r^   r!   r   c                 C  r   r#   r(   r   r(   r(   r)   r      r   zUnboundTerm.bindNr   )r   r   r   r^   r!   r   )rv   rw   rx   ry   r   r   r(   r(   r(   r)   r"      s    c                      sn   e Zd ZU dZe Zded< d fddZdd	d
ZdddZ	ddddZ
edddZedddZ  ZS )r&   zA reference not yet bound to a field in a schema.

    Args:
        name (str): The name of the field.

    Note:
        An unbound reference is sometimes referred to as a "named" reference.
    r%   rootnamer!   Nonec                   s   t  | d S r#   superr   )r<   r   	__class__r(   r)   r     s   zReference.__init__c                 C     dt | j dS z8Return the string representation of the Reference class.zReference(name=r   r   r   r;   r(   r(   r)   r   
     zReference.__repr__c                 C  r   r   r   r;   r(   r(   r)   __str__  r   zReference.__str__Tr   r   r   r^   r   c                 C  s*   |j | j|d}||j}| j||dS )a  Bind the reference to an Iceberg schema.

        Args:
            schema (Schema): An Iceberg schema.
            case_sensitive (bool): Whether to consider case when binding the reference to the field.

        Raises:
            ValueError: If an empty name is provided.

        Returns:
            BoundReference: A reference bound to the specific field in the Iceberg schema.
        )
name_or_idr   r   )
find_fieldr   accessor_for_fieldfield_idr   )r<   r   r   r   r   r(   r(   r)   r     s   zReference.bindc                 C     | j S r#   )r   r;   r(   r(   r)   r   #     zReference.nametype[BoundReference]c                 C     t S r#   )r   r;   r(   r(   r)   r   '  r   zReference.as_bound)r   r%   r!   r   r   r   )r   r   r   r^   r!   r   )r!   r   )rv   rw   rx   ry   r   r   r   r   r   r   r   r   r   r   __classcell__r(   r(   r   r)   r&      s   
 	

r&   c                      s   e Zd ZU dZeddZedddZded< e Z	ded	< e Z
ded
< d# fddZd$ fddZd%ddZd&ddZd&ddZd'ddZd(d!d"Z  ZS ))rC   z/AND operation expression - logical conjunction.Tarbitrary_types_allowedr[   rL   defaultaliaszTypingLiteral['and']r{   r   r   r8   rest_r	   r!   r   c                   >   t | trt| dst| dst j||d d S d S d S d S Nr   r   r   r   )r$   rC   hasattrr   r   r<   r   r   r   r   r   r(   r)   r   5     zAnd.__init__c                   Z   |rt t||g|R S |t u s|t u rt S |t u r |S |t u r'|S t | S r#   )r   rC   r`   r_   r   __new__rr   r   r   r   r   r   r(   r)   r   9     

zAnd.__new__r?   r^   c                 C  &   t |tr| j|jko| j|jkS dS )z6Return the equality of two instances of the And class.F)r$   rC   r   r   rD   r(   r(   r)   r   E     &z
And.__eq__r%   c                 C  r   z2Return the string representation of the And class.z	And(left=, right=r   )r%   r   r   r;   r(   r(   r)   r   I  r   zAnd.__str__c                 C  r   r   r   r   r   r;   r(   r(   r)   r   M  r   zAnd.__repr__c                 C     t | j | j S r:   )rG   r   r   r;   r(   r(   r)   r=   Q     zAnd.__invert__+tuple[BooleanExpression, BooleanExpression]c                 C     | j | jfS )zPickle the And class.r   r;   r(   r(   r)   __getnewargs__V  r   zAnd.__getnewargs__
r   r8   r   r8   r   r8   r   r	   r!   r   
r   r8   r   r8   r   r8   r   r	   r!   r8   r   r   ru   r!   r   )rv   rw   rx   ry   r   model_configr   rL   r   r   r   r   r   r   r   r   r=   r   r   r(   r(   r   r)   rC   ,     
 




rC   c                      s   e Zd ZU dZeddZedddZded< e Z	ded	< e Z
ded
< d# fddZd$ fddZd%ddZd&ddZd%ddZd'ddZd(d!d"Z  ZS ))rG   z.OR operation expression - logical disjunction.Tr   r\   rL   r   zTypingLiteral['or']r{   r   r   r8   r   r   r	   r!   r   c                   r   r   )r$   rG   r   r   r   r   r   r(   r)   r   d  r   zOr.__init__c                   r   r#   )r   rG   r_   r`   r   r   r   r   r(   r)   r   h  r   z
Or.__new__r%   c                 C  *   t | jj dt| j dt| j dS )1Return the string representation of the Or class.z(left=r   r   )r%   r   rv   r   r   r   r;   r(   r(   r)   r   t     *z
Or.__str__r?   r^   c                 C  r   )z5Return the equality of two instances of the Or class.F)r$   rG   r   r   rD   r(   r(   r)   r   x  r   z	Or.__eq__c                 C  r   )r   zOr(left=r   r   r   r;   r(   r(   r)   r   |  r   zOr.__repr__c                 C  r   r   )rC   r   r   r;   r(   r(   r)   r=     r   zOr.__invert__r   c                 C  r   )zPickle the Or class.r   r;   r(   r(   r)   r     r   zOr.__getnewargs__r   r   r   r   ru   r   )rv   rw   rx   ry   r   r   r   rL   r   r   r   r   r   r   r   r   r=   r   r   r(   r(   r   r)   rG   [  r   rG   c                      s   e Zd ZU dZeddZeddZded< e Z	ded	< d! fddZ
d" fddZd#ddZd#ddZd$ddZd%ddZd&dd Z  ZS )'rq   z,NOT operation expression - logical negation.Tr   r]   r   zTypingLiteral['not']rL   r{   childr8   r   r	   r!   r   c                   s   t  j|d d S )Nr   r   )r<   r   r   r   r(   r)   r     s   zNot.__init__c                   s<   |t  u rt S |t u rt  S t|tr|jS t | S r#   )r_   r`   r$   rq   r   r   r   )rr   r   r   r   r(   r)   r     s   


zNot.__new__r%   c                 C  s   d| j  dS z2Return the string representation of the Not class.z
Not(child=r   r   r;   r(   r(   r)   r        zNot.__str__c                 C  r   r   )r   r   r;   r(   r(   r)   r     r   zNot.__repr__r?   r^   c                 C  r   )z6Return the equality of two instances of the Not class.F)r$   rq   r   rD   r(   r(   r)   r     r   z
Not.__eq__c                 C  r   r   r   r;   r(   r(   r)   r=     r   zNot.__invert__tuple[BooleanExpression]c                 C     | j fS )zPickle the Not class.r   r;   r(   r(   r)   r        zNot.__getnewargs__)r   r8   r   r	   r!   r   )r   r8   r   r	   r!   r8   r   r   ru   )r!   r   )rv   rw   rx   ry   r   r   r   rL   r   r   r   r   r   r   r   r=   r   r   r(   r(   r   r)   rq     s   
 





rq   c                   @  <   e Zd ZU dZdZded< dddZdd
dZdddZdS )r_   zTRUE expression.Tr^   r   r!   r`   c                 C     t  S r   )r`   r;   r(   r(   r)   r=     r   zAlwaysTrue.__invert__r%   c                 C  r9   z9Return the string representation of the AlwaysTrue class.zAlwaysTrue()r(   r;   r(   r(   r)   r     r   zAlwaysTrue.__str__c                 C  r9   r   r(   r;   r(   r(   r)   r     r   zAlwaysTrue.__repr__N)r!   r`   r   	rv   rw   rx   ry   r   r   r=   r   r   r(   r(   r(   r)   r_        
 

r_   c                   @  r   )r`   zFALSE expression.Fr^   r   r!   r_   c                 C  r   r   )r_   r;   r(   r(   r)   r=     r   zAlwaysFalse.__invert__r%   c                 C  r9   z:Return the string representation of the AlwaysFalse class.zAlwaysFalse()r(   r;   r(   r(   r)   r     r   zAlwaysFalse.__str__c                 C  r9   r   r(   r;   r(   r(   r)   r     r   zAlwaysFalse.__repr__N)r!   r_   r   r   r(   r(   r(   r)   r`     r   r`   c                      sX   e Zd ZU eddZded< d fd	d
ZdddZdddZe	e
dddZ  ZS )BoundPredicateTr   r   r   kwargsr	   r!   r   c                   s   t  jdd|i| d S Nr   r(   r   r<   r   r   r   r(   r)   r     s   zBoundPredicate.__init__r?   r^   c                 C     t || jr| j|jkS dS )zAReturn the equality of two instances of the BoundPredicate class.Fr$   r   r   rD   r(   r(   r)   r     s   zBoundPredicate.__eq__r%   c                 C  s   | j j dt| j dS )z=Return the string representation of the BoundPredicate class.(term=r   )r   rv   r%   r   r;   r(   r(   r)   r     r   zBoundPredicate.__str__type[UnboundPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   
as_unbound  r   zBoundPredicate.as_unbound)r   r   r   r	   r!   r   r   r   )r!   r  )rv   rw   rx   r   r   r   r   r   r   r   r   r  r   r(   r(   r   r)   r     s   
 


r   c                      s^   e Zd ZU eddZded< d fd
dZdddZeddddZ	e
edddZ  ZS )UnboundPredicateTr   r"   r   r    r   r	   r!   r   c                   s   t  jddt|i| d S r   )r   r   r*   r   r   r(   r)   r     s   zUnboundPredicate.__init__r?   r^   c                 C  r   )zCReturn the equality of two instances of the UnboundPredicate class.Fr   rD   r(   r(   r)   r     s   zUnboundPredicate.__eq__r   r   r   r8   c                 C  r   r#   r(   r   r(   r(   r)   r     r   zUnboundPredicate.bindtype[BoundPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   r     r   zUnboundPredicate.as_bound)r   r    r   r	   r!   r   r   r   )r   r   r   r^   r!   r8   )r!   r  )rv   rw   rx   r   r   r   r   r   r   r   r   r   r   r(   r(   r   r)   r    s   
 

r  c                      sh   e Zd ZU e Zded< ddiZd fddZdddZddddZ	dddZ
eed ddZ  ZS )!UnaryPredicatez9TypingLiteral['is-null', 'not-null', 'is-nan', 'not-nan']rL   r   Tr   r    r   r	   r!   r   c                   s   t |}t j|d d S )Nr'   )r*   r   r   )r<   r   r   unboundr   r(   r)   r     s   zUnaryPredicate.__init__r%   c                 C  s   t | jj dt | j dS z=Return the string representation of the UnaryPredicate class.r  r   )r%   r   rv   r   r;   r(   r(   r)   r     s   zUnaryPredicate.__str__r   r   r   r^   BoundUnaryPredicatec                 C  s   | j ||}| j}||S r#   )r   r   r   )r<   r   r   
bound_term
bound_typer(   r(   r)   r     s   zUnaryPredicate.bindc                 C     t | jj dt| j dS r  r%   r   rv   r   r   r;   r(   r(   r)   r     r   zUnaryPredicate.__repr__type[BoundUnaryPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   r     r   zUnaryPredicate.as_bound)r   r    r   r	   r!   r   r   r   )r   r   r   r^   r!   r	  )r!   r  )rv   rw   rx   r   rL   r   r   r   r   r   r   r   r   r   r   r(   r(   r   r)   r    s   
 

r  c                   @  s2   e Zd ZdddZeedddZdd	d
ZdS )r	  r!   r%   c                 C  r  )zBReturn the string representation of the BoundUnaryPredicate class.r  r   r  r;   r(   r(   r)   r      r   zBoundUnaryPredicate.__repr__type[UnaryPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   r  $  r   zBoundUnaryPredicate.as_unboundtuple[BoundTerm]c                 C  r   )z%Pickle the BoundUnaryPredicate class.r'   r;   r(   r(   r)   r   (  r   z"BoundUnaryPredicate.__getnewargs__Nr   )r!   r  )r!   r  )rv   rw   rx   r   r   r   r  r   r(   r(   r(   r)   r	    s    
r	  c                      6   e Zd Zd fddZddd	ZedddZ  ZS )BoundIsNullr   r   r!   r8   c                      |  jjr	t S t | S r#   )r   r   requiredr`   r   r   rr   r   r   r(   r)   r   .     zBoundIsNull.__new__BoundNotNullc                 C  
   t | jS r   )r  r   r;   r(   r(   r)   r=   3     
zBoundIsNull.__invert__type[IsNull]c                 C  r   r#   )rc   r;   r(   r(   r)   r  7  r   zBoundIsNull.as_unboundr   r   r!   r8   )r!   r  )r!   r  rv   rw   rx   r   r=   r   r  r   r(   r(   r   r)   r  -  
    
r  c                      r  )r  r   r   r!   r8   c                   r  r#   )r   r   r  r_   r   r   r  r   r(   r)   r   =  r  zBoundNotNull.__new__r  c                 C  r  r   )r  r   r;   r(   r(   r)   r=   B  r  zBoundNotNull.__invert__type[NotNull]c                 C  r   r#   )rd   r;   r(   r(   r)   r  F  r   zBoundNotNull.as_unboundr  )r!   r  )r!   r  r  r(   r(   r   r)   r  <  r  r  c                   @  8   e Zd ZU eddZded< dddZedd
dZdS )rc   rM   r   zTypingLiteral['is-null']rL   r!   rd   c                 C  r  r   )rd   r   r;   r(   r(   r)   r=   N  r  zIsNull.__invert__type[BoundIsNull]c                 C  r   r#   )r  r;   r(   r(   r)   r   R  r   zIsNull.as_boundN)r!   rd   )r!   r   	rv   rw   rx   r   rL   r   r=   r   r   r(   r(   r(   r)   rc   K  
   
 
rc   c                   @  r  )rd   rN   r   zTypingLiteral['not-null']rL   r!   rc   c                 C  r  r   )rc   r   r;   r(   r(   r)   r=   Z  r  zNotNull.__invert__type[BoundNotNull]c                 C  r   r#   )r  r;   r(   r(   r)   r   ^  r   zNotNull.as_boundN)r!   rc   )r!   r#  r!  r(   r(   r(   r)   rd   W  r"  rd   c                      r  )
BoundIsNaNr   r   r!   r8   c                   ,   |  jj}t|ttfrt | S t S r#   )	r   r   rs   r$   r   r   r   r   r`   rr   r   r  r   r(   r)   r   d     zBoundIsNaN.__new__BoundNotNaNc                 C  r  r   )r(  r   r;   r(   r(   r)   r=   j  r  zBoundIsNaN.__invert__type[IsNaN]c                 C  r   r#   )re   r;   r(   r(   r)   r  n  r   zBoundIsNaN.as_unboundr  )r!   r(  )r!   r)  r  r(   r(   r   r)   r$  c  
    
r$  c                      r  )r(  r   r   r!   r8   c                   r%  r#   )	r   r   rs   r$   r   r   r   r   r_   r&  r   r(   r)   r   t  r'  zBoundNotNaN.__new__r$  c                 C  r  r   )r$  r   r;   r(   r(   r)   r=   z  r  zBoundNotNaN.__invert__type[NotNaN]c                 C  r   r#   )rf   r;   r(   r(   r)   r  ~  r   zBoundNotNaN.as_unboundr  )r!   r$  )r!   r+  r  r(   r(   r   r)   r(  s  r*  r(  c                   @  r  )re   rO   r   zTypingLiteral['is-nan']rL   r!   rf   c                 C  r  r   )rf   r   r;   r(   r(   r)   r=     r  zIsNaN.__invert__type[BoundIsNaN]c                 C  r   r#   )r$  r;   r(   r(   r)   r     r   zIsNaN.as_boundN)r!   rf   )r!   r,  r!  r(   r(   r(   r)   re     r"  re   c                   @  r  )rf   rP   r   zTypingLiteral['not-nan']rL   r!   re   c                 C  r  r   )re   r   r;   r(   r(   r)   r=     r  zNotNaN.__invert__type[BoundNotNaN]c                 C  r   r#   )r(  r;   r(   r(   r)   r     r   zNotNaN.as_boundN)r!   re   )r!   r-  r!  r(   r(   r(   r)   rf     r"  rf   c                      s   e Zd ZU eddZeddZded< eddZd	ed
< 	d*d+ fddZ	d,d-ddZ
d.ddZd.dd Zd/d"d#Zd0d%d&Zeed1d(d)Z  ZS )2SetPredicateTr   rY   r   zTypingLiteral['in', 'not-in']rL   r+   r   set[LiteralValue]literalsNr   r    -Iterable[Any] | Iterable[LiteralValue] | Noner   r	   r!   r   c                   sH   |d u rd|v r|d }|d u rt  }nt|}t jt||d d S )Nr+   )r   r+   )setr3   r   r   r*   )r<   r   r1  r   literal_setr   r(   r)   r     s   zSetPredicate.__init__r   r   r   r^   BoundSetPredicatec                   s.   | j || | j}|   fdd|D S )Nc                   s   h | ]}|   jjqS r(   )tor   r   rs   r/   litr
  r(   r)   r1     s    z$SetPredicate.bind.<locals>.<setcomp>)r   r   r1  r   )r<   r   r   r4  r(   r9  r)   r     s   zSetPredicate.bindr%   c                 C  >   d tdd | jD }t| jj dt| j d| dS );Return the string representation of the SetPredicate class., c                 S     g | ]}t |qS r(   r%   r/   r   r(   r(   r)   
<listcomp>  r2   z(SetPredicate.__str__.<locals>.<listcomp>(, {})joinsortedr1  r%   r   rv   r   r<   literals_strr(   r(   r)   r        $zSetPredicate.__str__c                 C  >   d tdd | jD }t| jj dt| j d| dS )r;  r<  c                 S  r=  r(   r   r?  r(   r(   r)   r@    r2   z)SetPredicate.__repr__.<locals>.<listcomp>rA  rB  rC  rE  rF  r1  r%   r   rv   r   r   r<   literals_reprr(   r(   r)   r     rI  zSetPredicate.__repr__r?   c                 C  (   t || jr| j|jko| j|jkS dS )z?Return the equality of two instances of the SetPredicate class.Fr$   r   r   r1  rD   r(   r(   r)   r        (zSetPredicate.__eq__tuple[UnboundTerm, set[Any]]c                 C  r   )zPickle the SetPredicate class.r   r1  r;   r(   r(   r)   r     r   zSetPredicate.__getnewargs__type[BoundSetPredicate]c                 C  r   r#   )r5  r;   r(   r(   r)   r     s   zSetPredicate.as_boundr#   )r   r    r1  r2  r   r	   r!   r   r   )r   r   r   r^   r!   r5  r   r   )r!   rR  )r!   rT  )rv   rw   rx   r   r   r   rL   r   r1  r   r   r   r   r   r   r   r   r   r   r(   r(   r   r)   r.    s   
 




r.  c                      sp   e Zd ZU ded< d fddZedd
dZdddZdddZdddZ	d ddZ
eed!ddZ  ZS )"r5  r0  r1  r   r   r!   r   c                   s   t |}t j||d d S )NrS  )r3   r   r   )r<   r   r1  r4  r   r(   r)   r     s   zBoundSetPredicate.__init__set[Any]c                 C  s   dd | j D S )Nc                 S  s   h | ]}|j qS r(   r7   r7  r(   r(   r)   r1     s    z.BoundSetPredicate.value_set.<locals>.<setcomp>)r1  r;   r(   r(   r)   	value_set  s   zBoundSetPredicate.value_setr%   c                 C  r:  )@Return the string representation of the BoundSetPredicate class.r<  c                 S  r=  r(   r>  r?  r(   r(   r)   r@    r2   z-BoundSetPredicate.__str__.<locals>.<listcomp>rA  rB  rC  rD  rG  r(   r(   r)   r     rI  zBoundSetPredicate.__str__c                 C  rJ  )rW  r<  c                 S  r=  r(   rK  r?  r(   r(   r)   r@    r2   z.BoundSetPredicate.__repr__.<locals>.<listcomp>rA  rB  rC  rL  rM  r(   r(   r)   r     rI  zBoundSetPredicate.__repr__r?   r	   r^   c                 C  rO  )zDReturn the equality of two instances of the BoundSetPredicate class.FrP  rD   r(   r(   r)   r     rQ  zBoundSetPredicate.__eq__#tuple[BoundTerm, set[LiteralValue]]c                 C  r   )z#Pickle the BoundSetPredicate class.rS  r;   r(   r(   r)   r     r   z BoundSetPredicate.__getnewargs__type[SetPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   r    r   zBoundSetPredicate.as_unbound)r   r   r1  r0  r!   r   )r!   rU  r   r   )r!   rX  )r!   rY  )rv   rw   rx   r   r   r   rV  r   r   r   r   r   r   r  r   r(   r(   r   r)   r5    s   
 



r5  c                      s@   e Zd Zd fddZdd
dZdddZedddZ  ZS )BoundInr   r   r1  r0  r!   r8   c                   <   t |}|dkrt S |dkrt|tt|S t | S Nr   r   )r   r`   BoundEqualTonextiterr   r   rr   r   r1  countr   r(   r)   r     s   zBoundIn.__new__
BoundNotInc                 C     t | j| jS r   )rb  r   r1  r;   r(   r(   r)   r=     r   zBoundIn.__invert__r?   r	   r^   c                 C  rO  )z:Return the equality of two instances of the BoundIn class.FrP  rD   r(   r(   r)   r     rQ  zBoundIn.__eq__type[In]c                 C  r   r#   )ro   r;   r(   r(   r)   r    r   zBoundIn.as_unboundr   r   r1  r0  r!   r8   )r!   rb  r   )r!   rd  )	rv   rw   rx   r   r=   r   r   r  r   r(   r(   r   r)   rZ    s    
	
rZ  c                      s6   e Zd Zd fddZdd
dZedddZ  ZS )rb  r   r   r1  r0  r!   r8   c                   r[  r\  )r   r_   BoundNotEqualTor^  r_  r   r   r`  r   r(   r)   r     s   zBoundNotIn.__new__rZ  c                 C  rc  r   )rZ  r   r1  r;   r(   r(   r)   r=     r   zBoundNotIn.__invert__type[NotIn]c                 C  r   r#   )rp   r;   r(   r(   r)   r    r   zBoundNotIn.as_unboundre  )r!   rZ  )r!   rg  r  r(   r(   r   r)   rb    s
    
rb  c                      P   e Zd ZU edddZded< 	dd fddZdddZedddZ	  Z
S )ro   rY   rL   r   zTypingLiteral['in']Nr   r    r1  r2  r   r	   r!   c                   l   |d u rd|v r|d }|d u rt  }nt|}t|}|dkr#t S |dkr0t|tt|S t | S Nr+   r   r   )	r3  r3   r   r`   rk   r^  r_  r   r   rr   r   r1  r   literals_setra  r   r(   r)   r   !     z
In.__new__rp   c                 C  rc  r   )rp   r   r1  r;   r(   r(   r)   r=   3  r   zIn.__invert__type[BoundIn]c                 C  r   r#   )rZ  r;   r(   r(   r)   r   7  r   zIn.as_boundr#   )r   r    r1  r2  r   r	   r!   ro   )r!   rp   )r!   rn  rv   rw   rx   r   rL   r   r   r=   r   r   r   r(   r(   r   r)   ro        
 
ro   c                      rh  )rp   rZ   rL   r   zTypingLiteral['not-in']Nr   r    r1  r2  r   r	   r!   c                   ri  rj  )	r3  r3   r   r_   rl   r^  r_  r   r   rk  r   r(   r)   r   ?  rm  zNotIn.__new__ro   c                 C  rc  r   )ro   r   r1  r;   r(   r(   r)   r=   Q  r   zNotIn.__invert__type[BoundNotIn]c                 C  r   r#   )rb  r;   r(   r(   r)   r   U  r   zNotIn.as_boundr#   )r   r    r1  r2  r   r	   r!   rp   )r!   ro   )r!   rq  ro  r(   r(   r   r)   rp   <  rp  rp   c                      s   e Zd ZU eddZded< ded< e Zded< edddd	Zd(d) fddZ	e
d*ddZd+d,ddZd-ddZd.d!d"Zd.d#d$Ze
ed/d&d'Z  ZS )0LiteralPredicaterL   r/  z]TypingLiteral['lt', 'lt-eq', 'gt', 'gt-eq', 'eq', 'not-eq', 'starts-with', 'not-starts-with']r"   r   r   r4   T)populate_by_namefrozenr   Nr    r   
Any | Noner   r	   r!   r   c                   s4   |d u rd|v r|d }t  jt|t|d d S )Nr4   )r   r4   )r   r   r*   r.   )r<   r   r   r   r   r(   r)   r   `  s   zLiteralPredicate.__init__c                 C  r   r#   r7   r;   r(   r(   r)   r   f  r   zLiteralPredicate.literalr   r   r   r^   BoundLiteralPredicatec                 C  s   | j ||}| j| jj}t|tr-t| t	t
tfr!t S t| tttfr,t S nt|trHt| tttfr=t S t| t	t
tfrHt S | ||S r#   )r   r   r   r6  r   r   rs   r$   r   rg   rh   rl   r_   ri   rj   rk   r`   r   r   )r<   r   r   r
  r8  r(   r(   r)   r   j  s   

zLiteralPredicate.bindr?   c                 C  rO  )zCReturn the equality of two instances of the LiteralPredicate class.Fr$   r   r   r   rD   r(   r(   r)   r   {     zLiteralPredicate.__eq__r%   c                 C  r   z?Return the string representation of the LiteralPredicate class.r  
, literal=r   r%   r   rv   r   r   r   r;   r(   r(   r)   r     r   zLiteralPredicate.__str__c                 C  r   ry  r{  r;   r(   r(   r)   r     r   zLiteralPredicate.__repr__type[BoundLiteralPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   r     r   zLiteralPredicate.as_boundr#   )r   r    r   ru  r   r	   r!   r   )r!   r   r   )r   r   r   r^   r!   rv  r   r   )r!   r|  )rv   rw   rx   r   rL   r   r4   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r   r)   rr  Z  s   
 


rr  c                      sX   e Zd ZU ded< d fddZdddZdddZdddZee	dddZ
  ZS )rv  r   r   r   r   c                   s   t  j||d d S )N)r   r   r   )r<   r   r   r   r(   r)   r     s   zBoundLiteralPredicate.__init__r?   r	   r!   r^   c                 C  rO  )zHReturn the equality of two instances of the BoundLiteralPredicate class.Frw  rD   r(   r(   r)   r     rx  zBoundLiteralPredicate.__eq__r%   c                 C  s&   | j j dt| j dt| j dS zDReturn the string representation of the BoundLiteralPredicate class.r  rz  r   )r   rv   r%   r   r   r   r;   r(   r(   r)   r     r   zBoundLiteralPredicate.__str__c                 C  r   r}  r{  r;   r(   r(   r)   r     r   zBoundLiteralPredicate.__repr__type[LiteralPredicate]c                 C  r   r#   r(   r;   r(   r(   r)   r    r   z BoundLiteralPredicate.as_unbound)r   r   r   r   r   r   )r!   r~  )rv   rw   rx   r   r   r   r   r   r   r   r  r   r(   r(   r   r)   rv    s   
 


rv  c                   @  $   e Zd Zd	ddZed
ddZdS )r]  r!   rf  c                 C  rc  r   )rf  r   r   r;   r(   r(   r)   r=     r   zBoundEqualTo.__invert__type[EqualTo]c                 C  r   r#   )rk   r;   r(   r(   r)   r    r   zBoundEqualTo.as_unboundN)r!   rf  )r!   r  rv   rw   rx   r=   r   r  r(   r(   r(   r)   r]        
r]  c                   @  r  )rf  r!   r]  c                 C  rc  r   )r]  r   r   r;   r(   r(   r)   r=     r   zBoundNotEqualTo.__invert__type[NotEqualTo]c                 C  r   r#   )rl   r;   r(   r(   r)   r    r   zBoundNotEqualTo.as_unboundN)r!   r]  )r!   r  r  r(   r(   r(   r)   rf    r  rf  c                   @  r  )BoundGreaterThanOrEqualr!   BoundLessThanc                 C  rc  r   )r  r   r   r;   r(   r(   r)   r=     r   z"BoundGreaterThanOrEqual.__invert__type[GreaterThanOrEqual]c                 C  r   r#   )rj   r;   r(   r(   r)   r    r   z"BoundGreaterThanOrEqual.as_unboundN)r!   r  )r!   r  r  r(   r(   r(   r)   r    r  r  c                   @  r  )BoundGreaterThanr!   BoundLessThanOrEqualc                 C  rc  r   )r  r   r   r;   r(   r(   r)   r=     r   zBoundGreaterThan.__invert__type[GreaterThan]c                 C  r   r#   )ri   r;   r(   r(   r)   r    r   zBoundGreaterThan.as_unboundN)r!   r  )r!   r  r  r(   r(   r(   r)   r    r  r  c                   @  r  )r  r!   r  c                 C  rc  r   )r  r   r   r;   r(   r(   r)   r=     r   zBoundLessThan.__invert__type[LessThan]c                 C  r   r#   )rg   r;   r(   r(   r)   r    r   zBoundLessThan.as_unboundN)r!   r  )r!   r  r  r(   r(   r(   r)   r    r  r  c                   @  r  )r  r!   r  c                 C  rc  r   )r  r   r   r;   r(   r(   r)   r=     r   zBoundLessThanOrEqual.__invert__type[LessThanOrEqual]c                 C  r   r#   )rh   r;   r(   r(   r)   r    r   zBoundLessThanOrEqual.as_unboundN)r!   r  )r!   r  r  r(   r(   r(   r)   r    r  r  c                   @  r  )BoundStartsWithr!   BoundNotStartsWithc                 C  rc  r   )r  r   r   r;   r(   r(   r)   r=     r   zBoundStartsWith.__invert__type[StartsWith]c                 C  r   r#   )rm   r;   r(   r(   r)   r    r   zBoundStartsWith.as_unboundN)r!   r  )r!   r  r  r(   r(   r(   r)   r    r  r  c                   @  r  )r  r!   r  c                 C  rc  r   )r  r   r   r;   r(   r(   r)   r=     r   zBoundNotStartsWith.__invert__type[NotStartsWith]c                 C  r   r#   )rn   r;   r(   r(   r)   r    r   zBoundNotStartsWith.as_unboundN)r!   r  )r!   r  r  r(   r(   r(   r)   r    r  r  c                   @  :   e Zd ZU edddZded< dddZedd
dZdS )rk   rU   rL   r   zTypingLiteral['eq']r!   rl   c                 C  rc  r   )rl   r   r   r;   r(   r(   r)   r=     r   zEqualTo.__invert__type[BoundEqualTo]c                 C  r   r#   )r]  r;   r(   r(   r)   r     r   zEqualTo.as_boundN)r!   rl   )r!   r  r!  r(   r(   r(   r)   rk     
   
 
rk   c                   @  r  )rl   rV   rL   r   zTypingLiteral['not-eq']r!   rk   c                 C  rc  r   )rk   r   r   r;   r(   r(   r)   r=     r   zNotEqualTo.__invert__type[BoundNotEqualTo]c                 C  r   r#   )rf  r;   r(   r(   r)   r   
  r   zNotEqualTo.as_boundN)r!   rk   )r!   r  r!  r(   r(   r(   r)   rl     r  rl   c                   @  r  )rg   rQ   rL   r   zTypingLiteral['lt']r!   rj   c                 C  rc  r   )rj   r   r   r;   r(   r(   r)   r=     r   zLessThan.__invert__type[BoundLessThan]c                 C  r   r#   )r  r;   r(   r(   r)   r     r   zLessThan.as_boundN)r!   rj   )r!   r  r!  r(   r(   r(   r)   rg     r  rg   c                   @  r  )rj   rT   rL   r   zTypingLiteral['gt-eq']r!   rg   c                 C  rc  r   )rg   r   r   r;   r(   r(   r)   r=     r   zGreaterThanOrEqual.__invert__type[BoundGreaterThanOrEqual]c                 C  r   r#   )r  r;   r(   r(   r)   r   "  r   zGreaterThanOrEqual.as_boundN)r!   rg   )r!   r  r!  r(   r(   r(   r)   rj     r  rj   c                   @  r  )ri   rS   rL   r   zTypingLiteral['gt']r!   rh   c                 C  rc  r   )rh   r   r   r;   r(   r(   r)   r=   *  r   zGreaterThan.__invert__type[BoundGreaterThan]c                 C  r   r#   )r  r;   r(   r(   r)   r   .  r   zGreaterThan.as_boundN)r!   rh   )r!   r  r!  r(   r(   r(   r)   ri   '  r  ri   c                   @  r  )rh   rR   rL   r   zTypingLiteral['lt-eq']r!   ri   c                 C  rc  r   )ri   r   r   r;   r(   r(   r)   r=   6  r   zLessThanOrEqual.__invert__type[BoundLessThanOrEqual]c                 C  r   r#   )r  r;   r(   r(   r)   r   :  r   zLessThanOrEqual.as_boundN)r!   ri   )r!   r  r!  r(   r(   r(   r)   rh   3  r  rh   c                   @  r  )rm   rW   rL   r   zTypingLiteral['starts-with']r!   rn   c                 C  rc  r   )rn   r   r   r;   r(   r(   r)   r=   B  r   zStartsWith.__invert__type[BoundStartsWith]c                 C  r   r#   )r  r;   r(   r(   r)   r   F  r   zStartsWith.as_boundN)r!   rn   )r!   r  r!  r(   r(   r(   r)   rm   ?  r  rm   c                   @  r  )rn   rX   rL   r   z TypingLiteral['not-starts-with']r!   rm   c                 C  rc  r   )rm   r   r   r;   r(   r(   r)   r=   N  r   zNotStartsWith.__invert__type[BoundNotStartsWith]c                 C  r   r#   )r  r;   r(   r(   r)   r   R  r   zNotStartsWith.as_boundN)r!   rm   )r!   r  r!  r(   r(   r(   r)   rn   K  r  rn   N)r   r    r!   r"   )r+   r,   r!   r-   )r4   r5   r!   r6   )r|   r}   r~   r   r!   r8   )c
__future__r   abcr   r   collections.abcr   r   r   	functoolsr   typingr	   r
   r   TypingLiteralpydanticr   r   r   r   pydantic_core.core_schemar   pyiceberg.expressions.literalsr   r   r   pyiceberg.schemar   r   pyiceberg.typedefr   r   r   r   r   pyiceberg.typesr   r   r   pyiceberg.utils.singletonr   r*   r3   r.   r8   r{   r   r   r   r   r   r   r   r"   r%   r&   rC   rG   rq   r^   r_   r`   r   r  r  r	  r  r  rc   rd   r$  r(  re   rf   r.  r5  rZ  rb  ro   rp   rr  rv  r]  rf  r  r  r  r  r  r  rk   rl   rg   rj   ri   rh   rm   rn   r(   r(   r(   r)   <module>   s   


O
')1//*1$4







