
    }i6                       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ryd dl$m%Z%m&Z& d dl'm(c 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?                  Z@e3e5e*j?        f         ZAe.e*j?                 ZB G 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                  6   e Zd Zej        Z	 dnej        ddodZedpd            Z	 	 	 	 dqddddrd"Z	dsd$Z
dsd%Zdtd(Zdud*Zdvd,Zed-d-ddwd2            Zedxd8            Zedyd;            Zdzd?Zd{dAZed|dD            ZdvdEZd}dJZdvdKZdvdLZdvdMZdvdNZdvdOZd~dQZddWZddZZ dvd[Z!dd^Z"ddeZ#eddg            Z$eddi            Z%eddk            Z&eddm            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   r4   r   returnNonec               L    || _         || _        || _        || _        || _        d S N)_call_evaluate_output_names_alias_output_names_version_window_function)selfr5   r7   r9   r;   r=   r4   s          G/home/jrussi/.local/lib/python3.11/site-packages/narwhals/_ibis/expr.py__init__zIbisExpr.__init__;   s0     
&;##5 ;J    IbisWindowFunctionc                "     d fd} j         p|S )	Ndfr(   window_inputsIbisWindowInputsr>   Sequence[ir.Value]c                6    fd |           D             S )Nc           
         g | ]=}|                     t          j        j         j        j                              >S )group_byorder_by)overibiswindowpartition_by_sortrU   ).0exprrG   rN   s     rH   
<listcomp>zIIbisExpr.window_function.<locals>.default_window_func.<locals>.<listcomp>P   sb         		K!.!;!+]-C!D     rJ    )rM   rN   rG   s    `rH   default_window_funcz5IbisExpr.window_function.<locals>.default_window_funcM   s>         !DHH   rJ   )rM   r(   rN   rO   r>   rP   )rF   )rG   r_   s   ` rH   r7   zIbisExpr.window_functionK   s2    	 	 	 	 	 	 $;(;;rJ   r^   
descending
nulls_lastr\   ir.ValuerY   Sequence[str | ir.Value]rU   Sequence[IntoColumn]
rows_start
int | Nonerows_endra   Sequence[bool] | Nonerb   c          
         |	|| |d}n|d|i}n
|d| i}ni }|pd}	|pd}
t          j        d| | j        ||	|
dd|}|                    |          S )N)	preceding	followingrl   rk   Fr`   rS   r^   )rW   rX   rZ   rV   )rG   r\   rY   rU   rf   rh   ra   rb   rows_betweendesclastrX   s               rH   _window_expressionzIbisExpr._window_expression\   s     !h&:*48LLLL!'2LL#'*5LLL"U"U 
!TZdtLLL
 
 
 

 yy   rJ   strc                \    t          d|                               | j        | d          S N	ir.ColumnT)rU   include_null)r   firstrZ   rG   r\   rU   s      rH   _firstzIbisExpr._firstx   s7    K&&,,TZ* - 
 
 	
rJ   c                \    t          d|                               | j        | d          S rs   )r   ro   rZ   rw   s      rH   _lastzIbisExpr._last}   s7    K&&++TZ* , 
 
 	
rJ   ignore_nullsboolc               z    t          d|          }|r|                                n|                    d          S )Nrt   T)ru   )r   	arbitraryrv   )rG   r\   r{   s      rH   
_any_valuezIbisExpr._any_value   s;     K&&#/Rt~~TZZTZ5R5RRrJ   r*   c                0    ddl m}  || j                  S )Nr   r)   )r=   )narwhals._ibis.namespacer*   rE   )rG   r*   s     rH   __narwhals_namespace__zIbisExpr.__narwhals_namespace__   s)    ::::::}T]3333rJ   r"   c                    | S rA   r^   rG   s    rH   	broadcastzIbisExpr.broadcast   s    rJ   Fcolsr   Sequence[bool] | boolIterator[ir.Column]c              '     K   t          |          }t          | |          } t          ||          }t          t          t          t
          d}t          || |          D ]\  }}} |||f         |          V  d S )N))FF)FT)TF)TT)lenr   r   r   r   r   r   )ra   rb   r   nmappingcol_desc_nulls_lasts           rH   rZ   zIbisExpr._sort   s       II Q//
 Q//
+)+)	
 
 (2$
J'O'O 	5 	5#C/'5+./444444	5 	5rJ   cls
type[Self]evaluate_column_namescontextr,   c              8    dfd} | |d |j                   S )NrM   r(   r>   Sequence[ir.Column]c                4      fd            D             S )Nc                *    g | ]}j         |         S r^   native)r[   namerM   s     rH   r]   z<IbisExpr.from_column_names.<locals>.func.<locals>.<listcomp>   s    JJJBIdOJJJrJ   r^   )rM   r   s   `rH   funcz(IbisExpr.from_column_names.<locals>.func   s*    JJJJ0E0Eb0I0IJJJJrJ   r9   r;   r=   rM   r(   r>   r   )rE   )r   r   r   r   s    `  rH   from_column_nameszIbisExpr.from_column_names   sQ    	K 	K 	K 	K 	K 	K s"7#$	
 
 
 	
rJ   column_indicesintc               ^    dfd} | ||                                d |j                  S )NrM   r(   r>   r   c                "      fdD             S )Nc                *    g | ]}j         |         S r^   r   )r[   irM   s     rH   r]   z>IbisExpr.from_column_indices.<locals>.func.<locals>.<listcomp>   s    999QBIaL999rJ   r^   )rM   r   s   `rH   r   z*IbisExpr.from_column_indices.<locals>.func   s    9999.9999rJ   r   r   )_eval_names_indicesrE   )r   r   r   r   s     ` rH   from_column_indiceszIbisExpr.from_column_indices   sY    	: 	: 	: 	: 	: 	: s"%"9"9."I"I#$	
 
 
 	
rJ   opCallable[..., ir.Value]otherc                0    |                      ||          S )N)r   _with_callable)rG   r   r   s      rH   _with_binaryzIbisExpr._with_binary   s    ""2U"333rJ   expressifiable_argsc                    | j         |fi |S rA   r   )rG   r   r   s      rH   _with_elementwisezIbisExpr._with_elementwise   s      #t"2==)<===rJ   r0   r   c               H    t          d|                    |                    S )Nr0   )r   r   )r   r\   r   s      rH   _alias_nativezIbisExpr._alias_native   s    GTYYt__---rJ   c                `    t          dt          j                  }|                     |          S )Nr   )r   operatorinvertr   )rG   r   s     rH   
__invert__zIbisExpr.__invert__   s(    /AA""6***rJ   quantilefloatinterpolationr/   c                b    |dk    rd}t          |          |                     fd          S )NlinearzBOnly linear interpolation methods are supported for Ibis quantile.c                .    |                                S rA   )r   )r\   r   s    rH   <lambda>z#IbisExpr.quantile.<locals>.<lambda>   s    h0G0G rJ   NotImplementedErrorr   )rG   r   r   msgs    `  rH   r   zIbisExpr.quantile   sA     H$$VC%c***""#G#G#G#GHHHrJ   c                .    |                      d           S )Nc                    |                                  |                                                                                     d          z   S )Nint8)nuniqueisnullanyr   r\   s    rH   r   z#IbisExpr.n_unique.<locals>.<lambda>   s5    $++--*;*;*=*=*B*B6*J*JJ rJ   r   r   s    rH   n_uniquezIbisExpr.n_unique   s     ""JJ
 
 	
rJ   c                `     d fd}                      | j         j         j                  S )NrM   r(   r>   Sequence[ir.IntegerScalar]c                H      fd                                D             S )Nc                B    g | ]}j                                         S r^   )r   count)r[   _rM   s     rH   r]   z.IbisExpr.len.<locals>.func.<locals>.<listcomp>   s%    OOO!BIOO%%OOOrJ   )rC   )rM   rG   s   `rH   r   zIbisExpr.len.<locals>.func   s,    OOOOt/J/J2/N/NOOOOrJ   r   )rM   r(   r>   r   )	__class__rC   rD   rE   rG   r   s   ` rH   r   zIbisExpr.len   sX    	P 	P 	P 	P 	P 	P ~~"&"=#7M	  
 
 	
rJ   c                .    |                      d           S )Nc                N    |                                                                  S rA   )r   sumr   s    rH   r   z%IbisExpr.null_count.<locals>.<lambda>   s    0A0A0C0C rJ   r   r   s    rH   
null_countzIbisExpr.null_count   s    ""#C#CDDDrJ   c                4    dd}|                      |          S )Nr\   ir.FloatingValuer>   rc   c                    t          |                                           r|                                 nd}t          j        |                                 d |          S )NF)r   typeisnanrW   ifelser   )r\   	otherwises     rH   r   zIbisExpr.is_nan.<locals>.func   sD    (3DIIKK(@(@K

eI;t{{}}dI>>>rJ   )r\   r   r>   rc   r   r   s     rH   is_nanzIbisExpr.is_nan   s,    	? 	? 	? 	? ""4(((rJ   c                4    dd}|                      |          S )Nr\   "ir.IntegerValue | ir.FloatingValuer>   rc   c                "   t          |                                           r:t          d|           } |                                 |                                 z   S t          j        |                                 d t          d                    S )Nr   T)	r   r   r   isinfr   rW   r   r   r   r   s    rH   r   z IbisExpr.is_finite.<locals>.func   sj    499;;'' 6.55

455;t{{}}dCII>>>rJ   )r\   r   r>   rc   r   r   s     rH   	is_finitezIbisExpr.is_finite   s,    	? 	? 	? 	? ""4(((rJ   Sequence[Any]c                4    |                      fd          S )Nc                .    |                                S rA   )isin)r\   r   s    rH   r   z IbisExpr.is_in.<locals>.<lambda>   s    		%0@0@ rJ   r   )rG   r   s    `rH   is_inzIbisExpr.is_in   s!    ""#@#@#@#@AAArJ   valueSelf | Nonestrategyr   limitc                    |d}t          |          |d}t          |          d
d}|J |                     ||	          S )Nz0`strategy` is not supported for the Ibis backendz-`limit` is not supported for the Ibis backendr\   rc   r   	ir.Scalarr>   c                ,    |                      |          S rA   )	fill_null)r\   r   s     rH   
_fill_nullz&IbisExpr.fill_null.<locals>._fill_null	  s    >>%(((rJ   )r   )r\   rc   r   r   r>   rc   r   )rG   r   r   r   r   r   s         rH   r   zIbisExpr.fill_null   sq     DC%c***AC%c***	) 	) 	) 	)    "":U";;;rJ   dtyper-   c                >     d fd}                      |          S )Nr\   rt   r>   rc   c                X    t          j                  }|                     |          S rA   )r   rE   r   )r\   native_dtyper   rG   s     rH   _funczIbisExpr.cast.<locals>._func  s&    3E4=IIL99\***rJ   r\   rt   r>   rc   r   )rG   r   r   s   `` rH   r   zIbisExpr.cast  s=    	+ 	+ 	+ 	+ 	+ 	+ 	+
 ""5)))rJ   c                .    |                      d           S )Nc                    |                                                                                      t          j        |                     dk    S )NrT      )r   r   rV   rW   rX   r   s    rH   r   z$IbisExpr.is_unique.<locals>.<lambda>  s;    ,,..33DK$4P4P4PQQUVV rJ   r   r   s    rH   	is_uniquezIbisExpr.is_unique  s     ""VV
 
 	
rJ   methodr.   c               T     d fdd fd}                      |          S )Nr\   rt   r>   rc   c                F   t          	                    | d                    }t          j        |          }dk    r|                                }nAdk    r't          j                                        |          }n|                                }|t          d          z   }dk    rSt          j        | g          }| 	                                                    |          }||z   t          d          z
  }nxd	k    rrt          j        | g          }| 	                                                    |          }t          d
|t          d          z
  t          d          z            }||z   }t          j        |                                 |f          S )NTr`   )rU   denseordinalr   maxr   averagezir.NumericValueg       @)nextrZ   rW   rX   
dense_rank
row_numberrV   rankr   r   r   casesnotnull)
r\   rU   rX   rank_	partitioncntavgra   r   rG   s
          rH   _rankzIbisExpr.rank.<locals>._rank  sm   DJJt
tJTTUUH[(333F   ++--9$$))..v66  CFFNE  K$888	jjll''	22c!ff,9$$ K$888	jjll''	22,sSVV|s3xx.GHH:t||~~u5666rJ   rM   r(   inputsWindowInputs[ir.Value]list[ir.Value]c                f    j         rd}t          |          fd |           D             S )NzV`rank` followed by `over` with `order_by` specified is not supported for Ibis backend.c                    g | ]F} t          d |                                        t          j        j                            GS )rt   r   )r   rV   rW   rX   rY   )r[   r\   r  r  s     rH   r]   z3IbisExpr.rank.<locals>.window_f.<locals>.<listcomp>=  sb         d;--..33K)<===   rJ   )rU   r   )rM   r  r   r  rG   s    ` rH   window_fzIbisExpr.rank.<locals>.window_f9  s[     /n)#...     !DHH	   rJ   r   )rM   r(   r  r  r>   r  r   )rG   r   ra   r  r  s   ``` @rH   r  zIbisExpr.rank  si    	7 	7 	7 	7 	7 	7 	7 	78		 		 		 		 		 		 		 ""5(333rJ   defaultIbisExpr | NoDefaultoldnewreturn_dtypeIntoDType | Nonec               8   	
 t           u rd}t          |                                           
t          |          t          |          }t	          j        |          	d	
 fd}                     |d  j         j         j	                  S )	NzK`replace_strict` requires an explicit value for `default` for ibis backend.rM   r(   r>   r  c                    |                                fd 	|           D             }r#t          	j                  fd|D             S |S )Nc                p    g | ]2}                     |                              |                   3S r^   )_whenr   )r[   r\   default_colkeysmapping_exprnss     rH   r]   z9IbisExpr.replace_strict.<locals>.func.<locals>.<listcomp>Z  sH        4,t*<kJJ  rJ   c                :    g | ]}|                               S r^   )r   )r[   resr   s     rH   r]   z9IbisExpr.replace_strict.<locals>.func.<locals>.<listcomp>a  s%    BBB3..BBBrJ   )_evaluate_single_output_exprr   rE   )
rM   resultsr  r   r  r  r  r   r  rG   s
     @@rH   r   z%IbisExpr.replace_strict.<locals>.funcW  s    99'BBK       DHH  G
  C7dmTTBBBB'BBBBNrJ   r   )rM   r(   r>   r  )
r   
ValueErrorr   listrW   mapr   rC   rD   rE   )rG   r  r  r  r  r   valuesr   r  r  r   s   ``  `   @@@rH   replace_strictzIbisExpr.replace_strictF  s     j  _CS//!((**Cyycxf--	 	 	 	 	 	 	 	 	 	 	 ~~"&"=#7M  
 
 	
rJ   r   c                     t          |           S rA   r   r   s    rH   rq   zIbisExpr.strl      &t,,,rJ   r
   c                     t          |           S rA   r	   r   s    rH   dtzIbisExpr.dtp  s    (...rJ   r   c                     t          |           S rA   r   r   s    rH   r&  zIbisExpr.listt  s    $T***rJ   r   c                     t          |           S rA   r   r   s    rH   structzIbisExpr.structx  r+  rJ   rA   )r5   r6   r7   r8   r9   r:   r;   r<   r=   r   r4   r   r>   r?   )r>   rK   )r^   r^   NN)r\   rc   rY   rd   rU   re   rf   rg   rh   rg   ra   ri   rb   ri   r>   rc   )r\   rc   rU   rq   r>   rc   )r\   rc   r{   r|   r>   rc   )r>   r*   )r>   r"   )r   r   ra   r   rb   r   r>   r   )r   r   r   r:   r   r,   r>   r"   )r   r   r   r,   r>   r"   )r   r   r   r"   r>   r"   )r   r   r   r"   r>   r"   )r\   r0   r   rq   r>   r0   )r   r   r   r/   r>   r"   )r   r   r>   r"   )r   r   r   r   r   rg   r>   r"   )r   r-   r>   r"   )r   r.   ra   r|   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_implementationrI   propertyr7   rp   rx   rz   r   r   r   staticmethodrZ   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r)  rq   r-  r&  r0  r   cum_prodskewkurtosis_count_star_push_down_window_functionr^   rJ   rH   r3   r3   8   s       $)O
 6:K *8)<K K K K K K  < < < X<& 24)+!%#! -1,0! ! ! ! ! !8
 
 
 


 
 
 

S S S S4 4 4 4
     -2,15 5 5 5 5 \5" 
 
 
 [
" 	
 	
 	
 [	
4 4 4 4> > > >
 . . . [.+ + + +I I I I
 
 
 

	
 	
 	
 	
E E E E) ) ) )) ) ) )B B B B< < < < * * * *
 
 
 

(4 (4 (4 (4T$
 $
 $
 $
L - - - X- / / / X/ + + + X+ - - - X-   H ?D  H!/##K "1!2!2rJ   r3   )r(   rc   )D
__future__r   r   typingr   r   r   r   r   rW   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.typesr\   typesirtyping_extensionsr"   narwhals._compliantr#   narwhals._compliant.typingr$   r%   r&   r'   narwhals._ibis.dataframer(   r   r*   narwhals._typingr+   r,   narwhals.typingr-   r.   r/   Valuer0   rK   rO   r3   r^   rJ   rH   <module>rR     s   " " " " " "  > > > > > > > > > > > > > >  < < < < < < : : : : : : ; ; ; ; ; ; > > > > > >	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ' & & & & &                 .22222222         &&&&&&000000            766666666666******//////QQQQQQQQQQGG28,,,E'rx(?@#BH-N3 N3 N3 N3 N3w23 N3 N3 N3 N3 N3rJ   