o
    $&]i6                     @  s  d dl mZ d dlZd dlmZmZmZmZ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 d dl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 m!Z!m"Z"m#Z# erd dl$m%Z%m&Z& d dl'm(  m)Z* d dl+m,Z, d dl-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dlm:Z: d dl;m<Z<m=Z=m>Z> ede*j?dZ@e3e5e*j?f ZAe.e*j? ZBG dd ded ZCdS )    )annotationsN)TYPE_CHECKINGAnyCallableTypeVarcastIbisExprDateTimeNamespaceIbisExprListNamespaceIbisExprStringNamespaceIbisExprStructNamespace)
IntoColumnasc_nulls_firstasc_nulls_lastdesc_nulls_firstdesc_nulls_lastis_floatinglitnarwhals_to_native_dtype)SQLExpr)ImplementationVersionextend_bool
no_defaultnot_implemented
zip_strict)IteratorSequence)Self)WindowInputs)
AliasNames	EvalNames
EvalSeriesWindowFunction)IbisLazyFrameIbisNamespace)	NoDefault)_LimitedContext)	IntoDType
RankMethodRollingInterpolationMethodExprT)boundc                   @  s  e Zd ZejZ	dejddddZedddZ				dddddd$d%Z	dd'd(Z
dd)d*Zdd-d.Zdd0d1Zdd3d4Zed5d5ddd:d;ZeddAdBZeddEdFZddJdKZddMdNZeddQdRZddSdTZddYdZZdd[d\Zdd]d^Zdd_d`ZddadbZddcddZddfdgZddmdnZddqdrZ ddsdtZ!ddwdxZ"dddZ#edddZ$edddZ%edddZ&edddZ'e( Z)e( Z*e( Z+e( Z,e( Z-dS )IbisExprN)implementationcall#EvalSeries[IbisLazyFrame, ir.Value]window_functionIbisWindowFunction | Noneevaluate_output_namesEvalNames[IbisLazyFrame]alias_output_namesAliasNames | Noneversionr   r2   r   returnNonec                C  s"   || _ || _|| _|| _|| _d S N)_call_evaluate_output_names_alias_output_names_version_window_function)selfr3   r5   r7   r9   r;   r2    rE   Q/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/_ibis/expr.py__init__;   s
   

zIbisExpr.__init__IbisWindowFunctionc                   s   d	 fdd} j p|S )
Ndfr'   window_inputsIbisWindowInputsr<   Sequence[ir.Value]c                   s    fdd| D S )Nc                   s*   g | ]}| tjj jj d qS )group_byorder_by)overibiswindowpartition_by_sortrO   .0expr)rD   rJ   rE   rF   
<listcomp>P   s    
zIIbisExpr.window_function.<locals>.default_window_func.<locals>.<listcomp>rE   )rI   rJ   rD   )rJ   rF   default_window_funcM   s   z5IbisExpr.window_function.<locals>.default_window_func)rI   r'   rJ   rK   r<   rL   )rC   )rD   rZ   rE   rY   rF   r5   K   s   
zIbisExpr.window_functionrE   
descending
nulls_lastrW   ir.ValuerS   Sequence[str | ir.Value]rO   Sequence[IntoColumn]
rows_start
int | Nonerows_endr\   Sequence[bool] | Noner]   c                C  s   |d ur|d ur| |d}n|d urd|i}n|d ur"d| i}ni }|p'd}	|p+d}
t jd|| j||	|
dd|}||S )N)	preceding	followingrf   re   Fr[   rM   rE   )rQ   rR   rT   rP   )rD   rW   rS   rO   ra   rc   r\   r]   rows_betweendesclastrR   rE   rE   rF   _window_expression\   s    

zIbisExpr._window_expressionstrc                 G     t d|j| j| ddS N	ir.ColumnT)rO   include_null)r   firstrT   rD   rW   rO   rE   rE   rF   _firstx      

zIbisExpr._firstc                 G  rl   rm   )r   ri   rT   rq   rE   rE   rF   _last}   rs   zIbisExpr._lastignore_nullsboolc                C  s"   t d|}|r| S |jddS )Nrn   T)ro   )r   	arbitraryrp   )rD   rW   ru   rE   rE   rF   
_any_value   s   
zIbisExpr._any_valuer)   c                 C  s   ddl m} || jdS )Nr   r(   )r;   )narwhals._ibis.namespacer)   rB   )rD   r)   rE   rE   rF   __narwhals_namespace__   s   zIbisExpr.__narwhals_namespace__r!   c                 C  s   | S r>   rE   rY   rE   rE   rF   	broadcast   s   zIbisExpr.broadcastFcolsr   Sequence[bool] | boolIterator[ir.Column]c                 g  sZ    t |}t| |} t||}ttttd}t|| |D ]\}}}|||f |V  qd S )N))FF)FT)TF)TT)lenr   r   r   r   r   r   )r\   r]   r|   nmappingcol_desc_nulls_lastrE   rE   rF   rT      s   

zIbisExpr._sortcls
type[Self]evaluate_column_namescontextr+   c                 s    d fdd}| | d |j dS )	NrI   r'   r<   Sequence[ir.Column]c                   s    fdd D S )Nc                      g | ]} j | qS rE   native)rV   namerI   rE   rF   rX          z<IbisExpr.from_column_names.<locals>.func.<locals>.<listcomp>rE   r   r   r   rF   func   s   z(IbisExpr.from_column_names.<locals>.funcr7   r9   r;   rI   r'   r<   r   )rB   )r   r   r   r   rE   r   rF   from_column_names   s   zIbisExpr.from_column_namescolumn_indicesintc                  s&   d fdd}| ||   d |jdS )	NrI   r'   r<   r   c                   s    fddD S )Nc                   r   rE   r   )rV   ir   rE   rF   rX      r   z>IbisExpr.from_column_indices.<locals>.func.<locals>.<listcomp>rE   r   r   r   rF   r         z*IbisExpr.from_column_indices.<locals>.funcr   r   )_eval_names_indicesrB   )r   r   r   r   rE   r   rF   from_column_indices   s   zIbisExpr.from_column_indicesopCallable[..., ir.Value]otherc                 C  s   | j ||dS )Nr   _with_callable)rD   r   r   rE   rE   rF   _with_binary      zIbisExpr._with_binaryexpressifiable_argsc                K  s   | j |fi |S r>   r   )rD   r   r   rE   rE   rF   _with_elementwise   s   zIbisExpr._with_elementwiser/   r   c                C  s   t d||S )Nr/   )r   r   )r   rW   r   rE   rE   rF   _alias_native   s   zIbisExpr._alias_nativec                 C  s   t dtj}| |S )Nr   )r   operatorinvertr   )rD   r   rE   rE   rF   
__invert__   s   
zIbisExpr.__invert__quantilefloatinterpolationr.   c                   s&   |dkr
d}t ||  fddS )NlinearzBOnly linear interpolation methods are supported for Ibis quantile.c                   
   |   S r>   r   rW   r   rE   rF   <lambda>      
 z#IbisExpr.quantile.<locals>.<lambda>NotImplementedErrorr   )rD   r   r   msgrE   r   rF   r      s   zIbisExpr.quantilec                 C     |  dd S )Nc                 S  s   |   |   d S )Nint8)nuniqueisnullanyr   r   rE   rE   rF   r      s    z#IbisExpr.n_unique.<locals>.<lambda>r   rY   rE   rE   rF   n_unique      zIbisExpr.n_uniquec                   s&   d fdd} j | j j jdS )	NrI   r'   r<   Sequence[ir.IntegerScalar]c                   s    fdd  D S )Nc                   s   g | ]} j  qS rE   )r   count)rV   _r   rE   rF   rX      r   z.IbisExpr.len.<locals>.func.<locals>.<listcomp>)r@   r   rY   r   rF   r      s   zIbisExpr.len.<locals>.funcr   )rI   r'   r<   r   )	__class__r@   rA   rB   rD   r   rE   rY   rF   r      s   zIbisExpr.lenc                 C  r   )Nc                 S  s   |    S r>   )r   sumr   rE   rE   rF   r      s    z%IbisExpr.null_count.<locals>.<lambda>r   rY   rE   rE   rF   
null_count   r   zIbisExpr.null_countc                 C     ddd}|  |S )NrW   ir.FloatingValuer<   r^   c                 S  s*   t |  r
|  nd}t|  d |S )NF)r   typeisnanrQ   ifelser   )rW   	otherwiserE   rE   rF   r      s   zIbisExpr.is_nan.<locals>.func)rW   r   r<   r^   r   r   rE   rE   rF   is_nan   s   

zIbisExpr.is_nanc                 C  r   )NrW   "ir.IntegerValue | ir.FloatingValuer<   r^   c                 S  s>   t |  rtd| } |  |  B  S t|  d tdS )Nr   T)	r   r   r   isinfr   rQ   r   r   r   r   rE   rE   rF   r      s   
z IbisExpr.is_finite.<locals>.func)rW   r   r<   r^   r   r   rE   rE   rF   	is_finite   s   

zIbisExpr.is_finiteSequence[Any]c                   s   |   fddS )Nc                   r   r>   )isinr   r   rE   rF   r      r   z IbisExpr.is_in.<locals>.<lambda>r   )rD   r   rE   r   rF   is_in   r   zIbisExpr.is_invalueSelf | Nonestrategyr   limitc                 C  sL   |d ur
d}t ||d urd}t |ddd	}|d usJ | j||d
S )Nz0`strategy` is not supported for the Ibis backendz-`limit` is not supported for the Ibis backendrW   r^   r   	ir.Scalarr<   c                 S  s
   |  |S r>   )	fill_null)rW   r   rE   rE   rF   
_fill_null	  s   
z&IbisExpr.fill_null.<locals>._fill_null)r   )rW   r^   r   r   r<   r^   r   )rD   r   r   r   r   r   rE   rE   rF   r      s   
zIbisExpr.fill_nulldtyper,   c                   s   d fdd} |S )NrW   rn   r<   r^   c                   s   t  j}| |S r>   )r   rB   r   )rW   native_dtyper   rD   rE   rF   _func  s   
zIbisExpr.cast.<locals>._funcrW   rn   r<   r^   r   )rD   r   r   rE   r   rF   r     s   
zIbisExpr.castc                 C  r   )Nc                 S  s   |    tj| ddkS )NrN      )r   r   rP   rQ   rR   r   rE   rE   rF   r     s    z$IbisExpr.is_unique.<locals>.<lambda>r   rY   rE   rE   rF   	is_unique  r   zIbisExpr.is_uniquemethodr-   c                  s.   dfdd d fdd}  |S )NrW   rn   r<   r^   c                   s   t j|  dd}tj|d}dkr| }ndkr%t |}n| }|td }dkrJtj| gd}| 	 |}|| td }n#d	krmtj| gd}| 	 |}t
d
|td td }|| }t|  |fS )NTr[   )rO   denseordinalr   maxr   averagezir.NumericValueg       @)nextrT   rQ   rR   
dense_rank
row_numberrP   rankr   r   r   casesnotnull)rW   rO   rR   rank_	partitioncntavg)r\   r   rD   rE   rF   _rank  s$   
zIbisExpr.rank.<locals>._rankrI   r'   inputsWindowInputs[ir.Value]list[ir.Value]c                   s*    j r	d}t| fdd| D S )NzV`rank` followed by `over` with `order_by` specified is not supported for Ibis backend.c                   s*   g | ]} t d |tjjdqS )rn   r   )r   rP   rQ   rR   rS   rU   )r   r   rE   rF   rX   =  s    z3IbisExpr.rank.<locals>.window_f.<locals>.<listcomp>)rO   r   )rI   r   r   )r   rD   )r   rF   window_f9  s   zIbisExpr.rank.<locals>.window_fr   )rI   r'   r   r   r<   r   r   )rD   r   r\   r   rE   )r   r\   r   rD   rF   r     s   zIbisExpr.rankdefaultIbisExpr | NoDefaultoldnewreturn_dtypeIntoDType | Nonec                  sj    t u r
d}t| t|t|}t|d	 fdd}j|d jjj	dS )
NzK`replace_strict` requires an explicit value for `default` for ibis backend.rI   r'   r<   r   c                   sL   |    fdd| D }r$tjfdd|D S |S )Nc                   s$   g | ]} ||  qS rE   )_whenr   rU   )default_colkeysmapping_exprnsrE   rF   rX   Z  s    z9IbisExpr.replace_strict.<locals>.func.<locals>.<listcomp>c                   s   g | ]}|  qS rE   )r   )rV   res)r   rE   rF   rX   a  r   )_evaluate_single_output_exprr   rB   )rI   resultsr   r   r   r   r   rD   )r   r   rF   r   W  s   
z%IbisExpr.replace_strict.<locals>.funcr   )rI   r'   r<   r   )
r   
ValueErrorrz   listrQ   mapr   r@   rA   rB   )rD   r   r   r   r   r   valuesr   rE   r  rF   replace_strictF  s   zIbisExpr.replace_strictr   c                 C     t | S r>   r   rY   rE   rE   rF   rk   l     zIbisExpr.strr	   c                 C  r  r>   r   rY   rE   rE   rF   dtp  r	  zIbisExpr.dtr   c                 C  r  r>   r
   rY   rE   rE   rF   r  t  r	  zIbisExpr.listr   c                 C  r  r>   r   rY   rE   rE   rF   structx  r	  zIbisExpr.structr>   )r3   r4   r5   r6   r7   r8   r9   r:   r;   r   r2   r   r<   r=   )r<   rH   )rE   rE   NN)rW   r^   rS   r_   rO   r`   ra   rb   rc   rb   r\   rd   r]   rd   r<   r^   )rW   r^   rO   rk   r<   r^   )rW   r^   ru   rv   r<   r^   )r<   r)   )r<   r!   )r|   r   r\   r}   r]   r}   r<   r~   )r   r   r   r8   r   r+   r<   r!   )r   r   r   r+   r<   r!   )r   r   r   r!   r<   r!   )r   r   r   r!   r<   r!   )rW   r/   r   rk   r<   r/   )r   r   r   r.   r<   r!   )r   r   r<   r!   )r   r   r   r   r   rb   r<   r!   )r   r,   r<   r!   )r   r-   r\   rv   r<   r!   )
r   r   r   r   r   r   r   r   r<   r!   )r<   r   )r<   r	   )r<   r   )r<   r   ).__name__
__module____qualname__r   IBIS_implementationrG   propertyr5   rj   rr   rt   rx   rz   r{   staticmethodrT   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rk   r
  r  r  r   cum_prodskewkurtosis_count_star_push_down_window_functionrE   rE   rE   rF   r1   8   sv    














	




*&
r1   )r'   r^   )D
__future__r   r   typingr   r   r   r   r   rQ   narwhals._ibis.expr_dtr	   narwhals._ibis.expr_listr   narwhals._ibis.expr_strr   narwhals._ibis.expr_structr   narwhals._ibis.utilsr   r   r   r   r   r   r   r   narwhals._sql.exprr   narwhals._utilsr   r   r   r   r   r   collections.abcr   r    ibis.expr.typesrW   typesirtyping_extensionsr!   narwhals._compliantr"   narwhals._compliant.typingr#   r$   r%   r&   narwhals._ibis.dataframer'   ry   r)   narwhals._typingr*   r+   narwhals.typingr,   r-   r.   Valuer/   rH   rK   r1   rE   rE   rE   rF   <module>   s4    (
 	
