
    O3j8                       d dl mZ d dlZd dl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! erd dl"m#Z#m$Z$m%Z% d dl&m'c m(Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 d dlm9Z9 d dl:m;Z;m<Z<m=Z=  ede)j|                        Z?e2e4e)j|                  f   Z@e-e)j|                     ZA G d ded         ZBy)    )annotationsN)TYPE_CHECKINGAny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)
NO_DEFAULTImplementationVersionextend_boolnot_implemented)CallableIteratorSequence)Self)WindowInputs)
AliasNames	EvalNames
EvalSeriesWindowFunction)IbisLazyFrameIbisNamespace)	NoDefault)_LimitedContext)	IntoDType
RankMethodRollingInterpolationMethodExprT)boundc                     e Zd Zej                  Z	 d&ej                  d	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZed(d       Z	 	 	 	 d)ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d*dZ	d+dZ
d+dZd,d	Zd-d
Zd.dZeddd	 	 	 	 	 	 	 d/d       Ze	 	 	 	 	 	 	 	 d0d       Zed1d       Zd2dZ	 	 	 	 	 	 d3dZed4d       Zd.dZd.dZ	 	 	 	 	 	 d5dZd6dZd.dZd.dZd.dZd.dZd.dZd7dZ d8dZ!d9dZ"d.dZ#d:d Z$	 	 	 	 	 	 	 	 	 	 d;d!Z%ed<d"       Z&ed=d#       Z'ed>d$       Z(ed?d%       Z) e*       Z+ e*       Z, e*       Z- e*       Z. e*       Z/y)@IbisExprN)implementationc               J    || _         || _        || _        || _        || _        y N)_call_evaluate_output_names_alias_output_names_version_window_function)selfcallwindow_functionevaluate_output_namesalias_output_namesversionr3   s          @/DATA/.local/lib/python3.12/site-packages/narwhals/_ibis/expr.py__init__zIbisExpr.__init__:   s*     
&;##5 ;J    c                <     	 	 	 	 	 	 d fd} j                   xs |S )Nc                     |       D cg c]I  }|j                  t        j                  |j                   j                  |j
                               K c}S c c}w )Ngroup_byorder_by)overibiswindowpartition_by_sortrH   )dfwindow_inputsexprr;   s      rA   default_window_funcz5IbisExpr.window_function.<locals>.default_window_funcL   sd     !H %D 		KK!.!;!;!+]-C-C!D %  s   AA)rN   r'   rO   IbisWindowInputsreturnzSequence[ir.Value])r:   )r;   rQ   s   ` rA   r=   zIbisExpr.window_functionJ   s4    		.>		 $$;(;;rC   
descending
nulls_lastc          
         |	|| |d}n|d|i}n
|d| i}ni }|xs d}	|xs d}
t        j                  d| | j                  ||	|
dd|}|j                  |      S )N)	preceding	followingrY   rX   FrT   rF    )rJ   rK   rM   rI   )r;   rP   rL   rH   
rows_startrows_endrU   rV   rows_betweendesclastrK   s               rA   _window_expressionzIbisExpr._window_expression[   s     !h&:*48LL!'2L#'*5LL"U"U 
!TZZdtL
 

 yy  rC   c                V    t        d|      j                   | j                  | d      S N	ir.ColumnT)rH   include_null)r   firstrM   r;   rP   rH   s      rA   _firstzIbisExpr._firstw   s1    K&,,TZZ* - 
 	
rC   c                V    t        d|      j                   | j                  | d      S rb   )r   r_   rM   rf   s      rA   _lastzIbisExpr._last|   s1    K&++TZZ* , 
 	
rC   c               b    t        d|      }|r|j                         S |j                  d      S )Nrc   T)rd   )r   	arbitraryre   )r;   rP   ignore_nullss      rA   
_any_valuezIbisExpr._any_value   s/     K&#/t~~RTZZTZ5RRrC   c                4    ddl m}  || j                        S )Nr   r(   )r@   )narwhals._ibis.namespacer)   r9   )r;   r)   s     rA   __narwhals_namespace__zIbisExpr.__narwhals_namespace__   s    :T]]33rC   c                    | S r5   rZ   r;   s    rA   	broadcastzIbisExpr.broadcast   s    rC   Fc              '     K   t        |      }t        | |      } t        ||      }t        t        t        t
        d}t        || |d      D ]  \  }}} |||f   |        y w)N))FF)FT)TF)TTT)strict)lenr   r   r   r   r   zip)rU   rV   colsnmappingcol_desc_nulls_lasts           rA   rM   zIbisExpr._sort   ss      I Q/
 Q/
+)+)	
 (+4ZPT'U#C/'5+./44 (Vs   A#A%c              <    dfd} | |d |j                         S )Nc                T     |       D cg c]  }| j                   |    c}S c c}w r5   native)rN   nameevaluate_column_namess     rA   funcz(IbisExpr.from_column_names.<locals>.func   s*    0Eb0IJ0IBIIdO0IJJJs   %r>   r?   r@   rN   r'   rS   zSequence[ir.Column])r9   )clsr   contextr   s    `  rA   from_column_nameszIbisExpr.from_column_names   s*    	K "7#$$	
 	
rC   c               Z    dfd} | || j                        d |j                        S )Nc                H    D cg c]  }| j                   |    c}S c c}w r5   r   )rN   icolumn_indicess     rA   r   z*IbisExpr.from_column_indices.<locals>.func   s#    *89.QBIIaL.999s   r   r   )_eval_names_indicesr9   )r   r   r   r   s     ` rA   from_column_indiceszIbisExpr.from_column_indices   s4    	: "%"9"9."I#$$	
 	
rC   c                (    | j                  ||      S )N)other_with_callable)r;   opr   s      rA   _with_binaryzIbisExpr._with_binary   s    ""2U"33rC   c               (     | j                   |fi |S r5   r   )r;   r   expressifiable_argss      rA   _with_elementwisezIbisExpr._with_elementwise   s     #t""2=)<==rC   c               8    t        d|j                  |            S )Nr/   )r   r   )r   rP   r   s      rA   _alias_nativezIbisExpr._alias_native   s    GTYYt_--rC   c                X    t        dt        j                        }| j                  |      S NCallable[..., ir.Value])r   operatorinvertr   )r;   r   s     rA   
__invert__zIbisExpr.__invert__   s$    /A""6**rC   c                X    t        dt        j                        }| j                  |      S r   )r   r   negr   )r;   r   s     rA   __neg__zIbisExpr.__neg__   s$    ,hll;""3''rC   c                P    |dk7  rd}t        |      | j                  fd      S )NlinearzBOnly linear interpolation methods are supported for Ibis quantile.c                &    | j                        S r5   )quantile)rP   r   s    rA   <lambda>z#IbisExpr.quantile.<locals>.<lambda>   s    h0GrC   NotImplementedErrorr   )r;   r   interpolationmsgs    `  rA   r   zIbisExpr.quantile   s/     H$VC%c**""#GHHrC   c                2    dfd}| j                  |      S )Nc           	         t        d|       }t        j                  | t        d      k  t        t	        d            f| t        d      k(  t        t	        d            f|j                  t                          S )Nir.NumericValuer   nanz-inf)else_)r   rJ   casesr   floatlog)rP   numeric_exprbases     rA   _logzIbisExpr.log.<locals>._log   se     148L::AE%L 12QU6]!34"&&s4y1 rC   )rP   ir.ValuerS   r   )r   )r;   r   r   s    ` rA   r   zIbisExpr.log   s    	 %%d++rC   c                &    | j                  d       S )Nc                ~    | j                         | j                         j                         j                  d      z   S )Nint8)nuniqueisnullanyr   rP   s    rA   r   z#IbisExpr.n_unique.<locals>.<lambda>   s)    $++-*;*;*=*B*B6*JJrC   r   rr   s    rA   n_uniquezIbisExpr.n_unique   s    ""J
 	
rC   c                v     d fd} j                  | j                   j                   j                        S )Nc                |    j                  |       D cg c]  }| j                  j                          c}S c c}w r5   )r7   r   count)rN   _r;   s     rA   r   zIbisExpr.len.<locals>.func   s3    /3/J/J2/NO/N!BIIOO%/NOOOs   !9r   )rN   r'   rS   zSequence[ir.IntegerScalar])	__class__r7   r8   r9   r;   r   s   ` rA   rv   zIbisExpr.len   s=    	P ~~"&"="=#77MM	  
 	
rC   c                &    | j                  d       S )Nc                >    | j                         j                         S r5   )r   sumr   s    rA   r   z%IbisExpr.null_count.<locals>.<lambda>   s    0A0A0CrC   r   rr   s    rA   
null_countzIbisExpr.null_count   s    ""#CDDrC   c                ,    dd}| j                  |      S )Nc                    t        | j                               r| j                         nd}t        j                  | j                         d |      S )NF)r   typeisnanrJ   ifelser   )rP   	otherwises     rA   r   zIbisExpr.is_nan.<locals>.func   s5    (3DIIK(@

eI;;t{{}dI>>rC   )rP   ir.FloatingValuerS   r   r   r   s     rA   is_nanzIbisExpr.is_nan   s    	? ""4((rC   c                ,    dd}| j                  |      S )Nc                    t        | j                               r.t        d|       } | j                         | j	                         z   S t        j                  | j                         d t        d            S )Nr   T)	r   r   r   isinfr   rJ   r   r   r   r   s    rA   r   z IbisExpr.is_finite.<locals>.func  sT    499;'.5

455;;t{{}dCI>>rC   )rP   z"ir.IntegerValue | ir.FloatingValuerS   r   r   r   s     rA   	is_finitezIbisExpr.is_finite  s    	? ""4((rC   c                ,    | j                  fd      S )Nc                &    | j                        S r5   )isin)rP   r   s    rA   r   z IbisExpr.is_in.<locals>.<lambda>  s    		%0@rC   r   )r;   r   s    `rA   is_inzIbisExpr.is_in
  s    ""#@AArC   c                t    |d}t        |      |d}t        |      dd}|J | j                  ||      S )Nz0`strategy` is not supported for the Ibis backendz-`limit` is not supported for the Ibis backendc                $    | j                  |      S r5   )	fill_null)rP   values     rA   
_fill_nullz&IbisExpr.fill_null.<locals>._fill_null  s    >>%((rC   )r   )rP   r   r   z	ir.ScalarrS   r   r   )r;   r   strategylimitr   r   s         rA   r   zIbisExpr.fill_null  sY     DC%c**AC%c**	)    "":U";;rC   c                6     d fd} j                  |      S )Nc                R    t        j                        }| j                  |      S r5   )r   r9   r   )rP   native_dtypedtyper;   s     rA   _funczIbisExpr.cast.<locals>._func  s"    3E4==IL99\**rC   rP   rc   rS   r   r   )r;   r   r   s   `` rA   r   zIbisExpr.cast  s    	+
 ""5))rC   c                &    | j                  d       S )Nc                    | j                         j                         j                  t        j                  |             dk(  S )NrG      )r   r   rI   rJ   rK   r   s    rA   r   z$IbisExpr.is_unique.<locals>.<lambda>'  s-    ,,.33DKK$4PQUVVrC   r   rr   s    rA   	is_uniquezIbisExpr.is_unique%  s    ""V
 	
rC   c               L     d fdd fd} j                  |      S )Nc                   t        	j                  | d            }t        j                  |      }dk(  r|j	                         }n9dk(  r$t        j
                         j                  |      }n|j                         }|t        d      z   }dk(  rHt        j                  | g      }| j                         j                  |      }||z   t        d      z
  }ndd	k(  r_t        j                  | g      }| j                         j                  |      }t        d
|t        d      z
  t        d      z        }||z   }t        j                  | j                         |f      S )NTrT   )rH   denseordinalr   maxr   averager   g       @)nextrM   rJ   rK   
dense_rank
row_numberrI   rankr   r   r   r   notnull)
rP   rH   rK   rank_	partitioncntavgrU   methodr;   s
          rA   _rankzIbisExpr.rank.<locals>._rank+  s)   DJJt
tJTUH[[(3F  ++-9$)..v6  CFNE  KK$8	jjl''	2c!f,9$ KK$8	jjl''	2,sSV|s3x.GH::t||~u566rC   c           	         |j                   rd}t        |       |       D cg c]A  } t        d|            j                  t	        j
                  |j                              C c}S c c}w )NzV`rank` followed by `over` with `order_by` specified is not supported for Ibis backend.rc   r   )rH   r   r   rI   rJ   rK   rL   )rN   inputsr   rP   r   r;   s       rA   window_fzIbisExpr.rank.<locals>.window_fG  sr    n)#..
 !H	 %D d;-.33KK)<)<= %	  s   AA.r   )rN   r'   r   zWindowInputs[ir.Value]rS   list[ir.Value]r   )r;   r   rU   r   r   s   ``` @rA   r   zIbisExpr.rank*  s"    	78		 ""5(33rC   c               .   	
 t         u rd}t        |       j                         
t        |      t        |      }t	        j
                  |      	d	
 fd} j                  |d  j                   j                   j                        S )NzK`replace_strict` requires an explicit value for `default` for ibis backend.c                   | j                        } |       D cg c]'  }	j                  |j                        |   |      ) }}
r5t        
j                        }|D cg c]  }|j                  |       c}S |S c c}w c c}w r5   )_evaluate_single_output_expr_whenr   r   r9   r   )rN   default_colrP   resultsr   resdefaultkeysmapping_exprnsreturn_dtyper;   s         rA   r   z%IbisExpr.replace_strict.<locals>.funce  s    99'BK !H$D 4,t*<kJ$  
 7dmmT:AB'3.'BBN Cs   ,B'B	r   )rN   r'   rS   r  )
r   
ValueErrorrp   listrJ   mapr   r7   r8   r9   )r;   r	  oldnewr  r   valuesr   r
  r  r  s   ``  `   @@@rA   replace_strictzIbisExpr.replace_strictT  s     j _CS/!((*Cycxxf-	 	 ~~"&"="=#77MM  
 	
rC   c                    t        |       S r5   r   rr   s    rA   strzIbisExpr.strz      &t,,rC   c                    t        |       S r5   r   rr   s    rA   dtzIbisExpr.dt~  s    (..rC   c                    t        |       S r5   r
   rr   s    rA   r  zIbisExpr.list  s    $T**rC   c                    t        |       S r5   r   rr   s    rA   structzIbisExpr.struct  r  rC   r5   )r<   z#EvalSeries[IbisLazyFrame, ir.Value]r=   zIbisWindowFunction | Noner>   EvalNames[IbisLazyFrame]r?   zAliasNames | Noner@   r   r3   r   rS   None)rS   IbisWindowFunction)rZ   rZ   NN)rP   r   rL   zSequence[str | ir.Value]rH   zSequence[IntoColumn]r[   
int | Noner\   r   rU   Sequence[bool] | NonerV   r!  rS   r   )rP   r   rH   r  rS   r   )rP   r   rl   boolrS   r   )rS   r)   )rS   r!   )rx   r   rU   Sequence[bool] | boolrV   r#  rS   zIterator[ir.Column])r   z
type[Self]r   r  r   r+   rS   r!   )r   intr   r+   rS   r!   )r   r   r   r!   rS   r!   )r   r   r   r!   rS   r!   )rP   r/   r   r  rS   r/   )r   r   r   r.   rS   r!   )r   r   rS   r!   )r   Sequence[Any]rS   r!   )r   zSelf | Noner   r   r   r   rS   r!   )r   r,   rS   r!   )r   r-   rU   r"  rS   r!   )
r	  zIbisExpr | NoDefaultr  r%  r  r%  r  zIntoDType | NonerS   r!   )rS   r   )rS   r	   )rS   r   )rS   r   )0__name__
__module____qualname__r   IBIS_implementationrB   propertyr=   r`   rg   ri   rm   rp   rs   staticmethodrM   classmethodr   r   r   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   r   r  r  r  r  r  r   cum_prodskewkurtosis_count_star_push_down_window_functionrZ   rC   rA   r2   r2   7   s   $))O
 6:K *8)<)<K1K 3K
  8K .K K 'K 
K  < <& 24)+!%#! -1,0!! /! '	!
 ! ! *! *! 
!8



S4
  -2,155)5 *5 
	5 5" 

7

 !
 

 
" 	
 	
4>)>EI>	>
 . .+(II.HI	I	,

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

(4T$
%$
 $
 	$
 '$
 
$
L - - / / + + - -  H D H!#K "1!2rC   r2   )r'   r   )C
__future__r   r   typingr   r   r   r   rJ   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   collections.abcr   r   r    ibis.expr.typesrP   typesirtyping_extensionsr!   narwhals._compliantr"   narwhals._compliant.typingr#   r$   r%   r&   narwhals._ibis.dataframer'   ro   r)   narwhals._typingr*   r+   narwhals.typingr,   r-   r.   Valuer/   r  rR   r2   rZ   rC   rA   <module>rG     s    "  4 4  < : ; >	 	 	 '  <<  &0  76*/QQG288,E'rxx(?@#BHH-]3w23 ]3rC   