
    q-Ph!                       U d dl mZ d dlZd dlmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZ d dlmZmZ d dlmZ erd d	lm Z mZ d d
l!m"Z" d dl#m$Z$m%Z% eedZ&de'd<   	 	 	 	 d2d3dZ(d4dZ)ed5d!            Z*e*+                    e          d6d#            Z,e*+                    e          d7d%            Z,e*+                    e          d8d'            Z,e*+                    e          d9d)            Z,e*+                    e          d:d+            Z,e*+                    e          d;d-            Z,e*+                    e          d<d/            Z,e*+                    e          d=d1            Z,dS )>    )annotationsN)GtELtLtE)	AttributeBinOpBitAndBitOrCallCompareConstantEqGtInvertListNameUnaryOp)singledispatch)TYPE_CHECKINGAnyCallable)
to_py_dateto_py_datetime)	pyiceberg)datedatetime)Table)	DataFrameSeriesz)dict[str, Callable[..., datetime | date]]_temporal_conversionstblr   with_columnslist[str] | None	predicate
str | Nonen_rows
int | Nonesnapshot_idkwargsr   returnDataFrame | Seriesc                J   ddl m} |                     ||          }|
 |j        | }|\	 t	          |          }t          |          }	n'# t          $ r}
d| }t          |          |
d}
~
ww xY w|                    |	          } ||                                          S )a"  
    Take the projected columns and materialize an arrow table.

    Parameters
    ----------
    tbl
        pyarrow dataset
    with_columns
        Columns that are projected
    predicate
        pyarrow expression that can be evaluated with eval
    n_rows:
        Materialize only n rows from the arrow dataset.
    snapshot_id:
        The snapshot ID to scan from.
    batch_size
        The maximum row count for scanned pyarrow record batches.
    kwargs:
        For backward compatibility

    Returns
    -------
    DataFrame
    r   )
from_arrow)limitr(   Nz*Could not convert predicate to PyIceberg: )	polarsr-   scanselect_to_ast_convert_predicate
ValueErrorfilterto_arrow)r!   r"   r$   r&   r(   r)   r-   r0   expr_astpyiceberg_expremsgs               X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/io/iceberg/_utils.py_scan_pyarrow_dataset_implr<   '   s    @ "!!!!!88&k8::Dt{L)	)y))H/99NN 	) 	) 	)JyJJCS//q(	) {{>**:dmmoo&&&s   A 
A0A++A0exprstrast.exprc                8    t          j        | d          j        S )a<  
    Converts a Python string to an AST.

    This will take the Python Arrow expression (as a string), and it will
    be converted into a Python AST that can be traversed to convert it to a PyIceberg
    expression.

    The reason to convert it to an AST is because the PyArrow expression
    itself doesn't have any methods/properties to traverse the expression.
    We need this to convert it into a PyIceberg expression.

    Parameters
    ----------
    expr
        The string expression

    Returns
    -------
    The AST representing the Arrow expression
    eval)mode)astparsebody)r=   s    r;   r2   r2   [   s    * 9T''',,    ac                *    d|  }t          |          )zJWalks the AST to convert the PyArrow expression to a PyIceberg expression.zUnexpected symbol: )r4   rG   r:   s     r;   r3   r3   s   s     $
#
#C
S//rF   r   c                    | j         S N)valuerG   s    r;   _rN   z   s	    7NrF   r   c                    | j         S rK   )idrM   s    r;   rN   rN      s	    4KrF   r   c                    t          | j        t                    r1t          j                            t          | j                            S d|  }t          |          )NzUnexpected UnaryOp: )	
isinstanceopr   r   expressionsNotr3   operand	TypeErrorrI   s     r;   rN   rN      sQ    !$ $(();AI)F)FGGG(Q((nnrF   r   c                   d | j         D             }t          | j                  }|dk    r|S |dk    r|d         S |t          v r!t          |         |                                 S t          | j        j                  d         }|dk    r&t          j                            ||d                   S |dk    rt          j        	                    |          S |dk    rt          j        
                    |          S d|}t          |          )	Nc                ,    g | ]}t          |          S  r3   ).0args     r;   
<listcomp>z_.<locals>.<listcomp>   s!    666s##666rF   fieldscalarr   isinis_nullis_nanzUnknown call: )argsr3   funcr    	isoformatrL   r   rT   InIsNullIsNaNr4   )rG   rd   frefr:   s        r;   rN   rN      s   66qv666D16""AG||	
hAw	
#	#	#$Q'.88::: ..q1;;(++Ca999)^^(//444(]](..s333
 1
 
 C
S//rF   r   c                    | j         S rK   )attrrM   s    r;   rN   rN      s	    6MrF   r   c                h   t          | j                  }t          | j                  }| j        }t	          |t
                    r t          j                            ||          S t	          |t                    r t          j        
                    ||          S d| d| d| }t          |          )Nz	Unknown:  )r3   leftrightrS   rR   r	   r   rT   Andr
   OrrW   )rG   lhsrhsrS   r:   s        r;   rN   rN      s    
QV
$
$C
QW
%
%C	
B"f 3$((c222"e $''S111*#****S**nnrF   r   c                   | j         d         }t          | j                  d         }t          | j        d                   }t	          |t
                    r t          j                            ||          S t	          |t                    r t          j        
                    ||          S t	          |t                    r t          j                            ||          S t	          |t                    r t          j                            ||          S t	          |t                    r t          j                            ||          S d| }t#          |          )Nr   zUnknown comparison: )opsr3   rp   comparatorsrR   r   r   rT   GreaterThanr   GreaterThanOrEqualr   EqualTor   LessThanr   LessThanOrEqualrW   )rG   rS   rt   ru   r:   s        r;   rN   rN      s&   	
qB
QV
$
$Q
'C
Q]1-
.
.C"b ;$00c:::"c B$77SAAA"b 7$,,S#666"b 8$--c3777"c $44S#>>>)R))nnrF   r   c                $    d | j         D             S )Nc                ,    g | ]}t          |          S rZ   r[   )r\   r9   s     r;   r^   z_.<locals>.<listcomp>   s!    222aq!!222rF   )eltsrM   s    r;   rN   rN      s    22162222rF   )NNNN)r!   r   r"   r#   r$   r%   r&   r'   r(   r'   r)   r   r*   r+   )r=   r>   r*   r?   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )rG   r   r*   r   )-
__future__r   rC   _astr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   	functoolsr   typingr   r   r   polars._utils.convertr   r   polars.dependenciesr   r   r   pyiceberg.tabler   r/   r   r   r    __annotations__r<   r2   r3   registerrN   rZ   rF   r;   <module>r      s   " " " " " " " 



                                        % $ $ $ $ $ / / / / / / / / / / < < < < < < < < ) ) ) ) ) ) )''''''''%%%%%%(((((((( $D D      &* "1' 1' 1' 1' 1'h- - - -0     X&&   '& T""   #" W%%   &% T""   #". Y''   (' U##   $# W%%   &%( T""3 3 3 #"3 3 3rF   