
    }im                       U d dl 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 d dlmZ d dlmZmZmZ er<d dlmZmZ d dl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# d dl$m%Z% d dl&m'Z'm(Z(m)Z) dUdZ*dVdZ+dWdZ,dXdZ-dYd"Z. G d# d$e          Z/dZd&Z0 G d' d(e          Z1 G d) d*          Z2 G d+ d,          Z3e/j4        e3j5        e/j6        e3j7        e/j8        e3j9        e/j:        e3j7        e/j;        e3j<        e/j=        e3j>        e/j?        e3j@        e/jA        e3jB        e/jC        e3j7        i	ZDd-eEd.<   e/j4        e3jF        e/j8        e3jG        e/jH        e3jI        e/jJ        e3jK        e/jL        e3jM        e/jN        e3jO        e/jP        e3jQ        e/jR        e3jS        iZTd/eEd0<   d[d6ZUd\d;ZVd<d=d>d?d]dFZWd^dKZXd_dMZYd`dOZZdadQZ[dbdTZ\d=S )c    )annotations)Enumauto)TYPE_CHECKINGAnyCallableLiteralcast)
zip_strict)is_numpy_array_1d)InvalidIntoExprErrorInvalidOperationErrorMultiOutputExpressionError)IteratorSequence)NeverTypeIs)CompliantExprCompliantFrameT)
AliasNamesCompliantExprAnyCompliantFrameAnyCompliantNamespaceAny	EvalNamesExprSeries)IntoExprNonNestedLiteral_1DArrayobjr   returnTypeIs[Expr]c                .    ddl m} t          | |          S )'Check whether `obj` is a Narwhals Expr.r   r   )narwhals.exprr   
isinstance)r"   r   s     P/home/jrussi/.local/lib/python3.11/site-packages/narwhals/_expression_parsing.pyis_exprr*   $   s$    """"""c4       TypeIs[Series[Any]]c                .    ddl m} t          | |          S )r&   r   r   )narwhals.seriesr   r(   )r"   r   s     r)   	is_seriesr/   +   s$    &&&&&&c6"""r+   exprs#CompliantExpr[CompliantFrameT, Any]EvalNames[CompliantFrameT]c                      d fd}|S )Ndfr   r#   Sequence[str]c                J    d                              |           d d         S Nr      )_evaluate_output_names)r4   r0   s    r)   evaluate_output_namesz<combine_evaluate_output_names.<locals>.evaluate_output_names7   s$    Qx..r222A266r+   )r4   r   r#   r5    )r0   r:   s   ` r)   combine_evaluate_output_namesr<   2   s)    
7 7 7 7 7 7 ! r+   r   AliasNames | Nonec                 2      d         j         d S d fd}|S )Nr   namesr5   r#   c                J    d                              |           d d         S r7   _alias_output_names)r?   r0   s    r)   alias_output_namesz6combine_alias_output_names.<locals>.alias_output_namesC   s$    Qx++E222A266r+   )r?   r5   r#   r5   rA   )r0   rC   s   ` r)   combine_alias_output_namesrD   =   s=     Qx#+t7 7 7 7 7 7 r+   exprr4   r   excluder5   #tuple[Sequence[str], Sequence[str]]c                    |                      |          }| j        x} ||          n|}rD| j        j                                        r&t          fdt          ||          D              \  }}||fS )Nc                &    g | ]\  }}|v	||fS r;   r;   ).0xaliasrF   s      r)   
<listcomp>z5evaluate_output_names_and_aliases.<locals>.<listcomp>T   s6       AuG## E
###r+   )r9   rB   	_metadataexpansion_kindis_multi_unnamedr   )rE   r4   rF   output_namesalias_fnaliasess     `   r)   !evaluate_output_names_and_aliasesrT   I   s     ..r22L 00H= 	 
  
4>0AACC 
 *    *< A A  !
g   r+   c                     e Zd ZdZ e            Z	  e            Z	  e            Z	  e            Z	  e            Z		  e            Z
	  e            Z	  e            Z	  e            Z	  e            Z	  e            Z	  e            Z	  e            Z	  e            Z	  e            Z	  e            Z	 ed	d            Zed	d            Zed	d            Zed	d            ZdS )
ExprKindz6Describe which kind of expression we are dealing with.r#   boolc                b    | t           j        t           j        t           j        t           j        hv S N)rV   ORDERABLE_WINDOWWINDOWORDERABLE_AGGREGATIONORDERABLE_FILTRATIONselfs    r)   is_orderablezExprKind.is_orderable   s-     %O*)	
 
 	
r+   c           
         | t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j	        h	v S rY   )
rV   ALLCOLELEMENTWISEEXCLUDELITERALNTHSELECTORSERIES	WHEN_THENr^   s    r)   is_elementwisezExprKind.is_elementwise   sH     LL LO

 

 
	
r+   c                D    | j         p| t          j        t          j        hv S rY   )rk   rV   r[   rZ   r^   s    r)   preserves_lengthzExprKind.preserves_length   s    "Zdx@Y.Z&ZZr+   c                L    | t           j        t           j        t           j        hv S rY   )rV   AGGREGATIONrf   r\   r^   s    r)   is_scalar_likezExprKind.is_scalar_like   s&     *
 
 	
r+   Nr#   rW   )__name__
__module____qualname____doc__r   rf   ro   r\   rd   rZ   r[   
FILTRATIONr]   OVERrc   rg   re   rb   rh   rj   ri   propertyr`   rk   rm   rp   r;   r+   r)   rV   rV   ]   s       @@dffG$&&KY DFFX$&&KgtvvHTVVFNJG466=466D5
$&&C(
$&&C(dffG,
$&&C(tvvH>IPTVVF3
 
 
 X
 
 
 
 X
 [ [ [ X[ 
 
 
 X
 
 
r+   rV   rW   c                    | j         j        S rY   )rN   rp   )r"   s    r)   rp   rp      s    =''r+   c                  l    e Zd ZdZ e            Z	  e            Z	  e            Z	 d
dZd
dZ	ddZ
d	S )ExpansionKindz8Describe what kind of expansion the expression performs.r#   rW   c                    | t           j        u S rY   r{   MULTI_UNNAMEDr^   s    r)   rP   zExpansionKind.is_multi_unnamed   s    }222r+   c                6    | t           j        t           j        hv S rY   )r{   MULTI_NAMEDr~   r^   s    r)   is_multi_outputzExpansionKind.is_multi_output   s    1=3NOOOr+   other$Literal[ExpansionKind.MULTI_UNNAMED]c                    | t           j        u r|t           j        u rt           j        S d|  d| d}t          |          )Nz+Unsupported ExpansionKind combination, got z and z, please report a bug.)r{   r~   AssertionError)r_   r   msgs      r)   __and__zExpansionKind.__and__   sL    =...5M<W3W3W ..dDddudddS!!!r+   Nrq   )r   r{   r#   r   )rr   rs   rt   ru   r   SINGLEr   r~   rP   r   r   r;   r+   r)   r{   r{      s        BBTVVF9$&&K!DFFM'3 3 3 3P P P P" " " " " "r+   r{   c                  T    e Zd ZdZddddddZddZddZd dZd!dZd"dZ	d"dZ
dS )#ExprNodea  An operation to create or modify an expression.

    Parameters:
        kind: ExprKind of operation.
        name: Name of function, as defined in the compliant protocols.
        exprs: Expressifiable arguments to function.
        str_as_lit: Whether to interpret strings as literals when they
            are present in `exprs`.
        allow_multi_output: Whether to allow any of `exprs` to be multi-output.
        kwargs: Other (non-expressifiable) arguments to function.
    r;   F)r0   
str_as_litallow_multi_outputkindrV   namestrr0   %Sequence[IntoExpr | NonNestedLiteral]r   rW   r   kwargsr   r#   Nonec              v    || _         || _        || _        || _        || _        || _        d | _        d | _        d S rY   )r   r   r0   r   r   r   _is_orderable_cached_is_elementwise_cached)r_   r   r   r0   r   r   r   s          r)   __init__zExprNode.__init__   sG     #		<A
&, *(: 26!37###r+   c                   | j         dk    r0d                    d | j        d         D                       }d| dS g }d                    d | j        D                       }d                    d | j                                        D                       }| j        r|                    |           | j        r|                    |           | j          d	d                    |           dS )
Ncolz, c              3  4   K   | ]}t          |          V  d S rY   r   rJ   rK   s     r)   	<genexpr>z$ExprNode.__repr__.<locals>.<genexpr>   s(      CCc!ffCCCCCCr+   r?   zcol()c              3  4   K   | ]}t          |          V  d S rY   r   r   s     r)   r   z$ExprNode.__repr__.<locals>.<genexpr>   s(      99c!ff999999r+   c              3  *   K   | ]\  }}| d | V  dS )=Nr;   )rJ   keyvalues      r)   r   z$ExprNode.__repr__.<locals>.<genexpr>   s4      WWZS%3 0 0 0 0WWWWWWr+   ()r   joinr   r0   itemsappend)r_   r?   arg_str	expr_reprkwargs_reprs        r)   __repr__zExprNode.__repr__   s    9IICCdk'.BCCCCCE"%???"II99dj99999	iiWW4;CTCTCVCVWWWWW: 	&NN9%%%; 	(NN;''')33dii003333r+   dict[str, Any]c                P    | j         | j        | j        | j        | j        | j        dS )Nr   r   r0   r   r   r   r   r^   s    r)   as_dictzExprNode.as_dict  s2     IIZk/"&"9
 
 	
r+   c                N     | j         | j        | j        f| j        | j        d|S )N)r0   r   )	__class__r   r   r0   r   )r_   r   s     r)   _with_kwargszExprNode._with_kwargs  s<    t~Ity
(,
t
 
RX
 
 	
r+   	over_nodeover_node_without_order_byc                   g }|j         d         }|j         d         }| j        D ]}t          |          s|                    |           '|rGt	          d |j        D                       r)|                    |                    |                     p|rGt          d |j        D                       s)|                    |                    |                     |                    |           || _        d S )Norder_bypartition_byc              3  >   K   | ]}|                                 V  d S rY   r`   rJ   	expr_nodes     r)   r   z9ExprNode._push_down_over_node_in_place.<locals>.<genexpr>"  s?       , ,-6	&&((, , , , , ,r+   c              3  >   K   | ]}|                                 V  d S rY   rk   r   s     r)   r   z9ExprNode._push_down_over_node_in_place.<locals>.<genexpr>&  s?       4 4/8	((**4 4 4 4 4 4r+   )r   r0   r*   r   any_nodes_with_over_nodeall)r_   r   r   r0   over_node_order_byover_node_partition_byrE   s          r)   _push_down_over_node_in_placez&ExprNode._push_down_over_node_in_place  s6    46 '-j9!*!1.!AJ 	# 	#D4== #T""""# 
# , ,:>+, , , ) ) 
# T11)<<====' # 4 4<@K4 4 4 1 1 # T112LMMNNNN T""""


r+   c                    | j         A| j        j        rd| _         n-t          d | j        D                       rd| _         nd| _         | j         S )NTc              3  p   K   | ]1}t          |          t          d  |j        D                       V  2dS )c              3  >   K   | ]}|                                 V  d S rY   r   rJ   nodes     r)   r   z2ExprNode.is_orderable.<locals>.<genexpr>.<genexpr>6  s.      @@DD%%''@@@@@@r+   Nr*   r   r   rJ   rE   s     r)   r   z(ExprNode.is_orderable.<locals>.<genexpr>5  s[        4==@@DK@@@@@     r+   F)r   r   r`   r   r0   r^   s    r)   r`   zExprNode.is_orderable/  ss    $, y% 	2,0))   J     2
 -1)),1)((r+   c                    | j         A| j        j        sd| _         n-t          d | j        D                       rd| _         nd| _         | j         S )NFc              3  p   K   | ]1}t          |          t          d  |j        D                       V  2dS )c              3  @   K   | ]}|                                  V  d S rY   r   r   s     r)   r   z4ExprNode.is_elementwise.<locals>.<genexpr>.<genexpr>F  s1      FF$++---FFFFFFr+   Nr   r   s     r)   r   z*ExprNode.is_elementwise.<locals>.<genexpr>E  s[        4==FF$+FFFFF     r+   T)r   r   rk   r   r0   r^   s    r)   rk   zExprNode.is_elementwise?  ss    &. 9+ 	3.3++   J     3
 /4++.2+**r+   N)r   rV   r   r   r0   r   r   rW   r   rW   r   r   r#   r   r#   r   )r#   r   )r   r   r#   r   )r   r   r   r   r#   r   rq   )rr   rs   rt   ru   r   r   r   r   r   r`   rk   r;   r+   r)   r   r      s        
 
$ 8: #(8 8 8 8 8 8,4 4 4 4	
 	
 	
 	

 
 
 

   0) ) ) ) + + + + + +r+   r   c                     e Zd ZdZdZddddddddd>dZd?dZd@dZdAd!Ze	dBd%            Z
dCd(Ze	dBd)            Ze	dDd*            Ze	dDd+            Ze	dDd,            Ze	dDd-            Ze	dDd.            Ze	dDd/            Ze	dBd0            ZedEd1            ZdFd3ZdFd4ZdCd5ZdFd6ZdFd7ZdFd8ZdFd9ZdFd:ZdBd;ZdFd<ZdAd=Z dS )GExprMetadataaI  Expression metadata.

    Parameters:
        expansion_kind: What kind of expansion the expression performs.
        has_windows: Whether it already contains window functions.
        is_elementwise: Whether it can operate row-by-row without context
            of the other rows around it.
        is_literal: Whether it is just a literal wrapped in an expression.
        is_scalar_like: Whether it is a literal or an aggregation.
        n_orderable_ops: The number of order-dependent operations. In the
            lazy case, this number must be `0` by the time the expression
            is evaluated.
        preserves_length: Whether the expression preserves the input length.
        current_node: The current ExprNode in the linked list.
        prev: Reference to the previous ExprMetadata in the linked list (None for root).
    )	current_noderO   has_windowsrk   
is_literalrp   n_orderable_opsrm   prevFr   TN)r   r   rm   rk   rp   r   r   rO   r{   r   rW   r   intrm   rk   rp   r   r   r   r   ExprMetadata | Noner#   r   c                   |r|sJ || _         || _        || _        || _        || _        || _        || _        || _        |	| _        d S rY   )	rO   r   r   rk   rm   rp   r   r   r   )
r_   rO   r   r   rm   rk   rp   r   r   r   s
             r)   r   zExprMetadata.__init__n  sb      	"!!!!-;!,$3$2&6$2 *&2)-			r+   argsr   kwdsr   c               4    d| j         }t          |          )NzCannot subclass )rr   	TypeError)clsr   r   r   s       r)   __init_subclass__zExprMetadata.__init_subclass__  s    111nnr+   r   c                    t          t          t          |                                                               }d| j         d| j         d| j         d| j         d| j         d| j         d| j	         d| d	S )
Nz ExprMetadata(
  expansion_kind: z,
  has_windows: z,
  n_orderable_ops: z,
  is_elementwise: z,
  preserves_length: z,
  is_scalar_like: z,
  is_literal: z,
  nodes: z,
))
tuplereversediter_nodes_reversedrO   r   r   rk   rm   rp   r   )r_   nodess     r)   r   zExprMetadata.__repr__  s    huT%=%=%?%?@@AABB	!%!4	 	".	 	 #'"6	 	 "&!4		 	
 $(#8	 	 "&!4	 	 "_	 	 	 	 		
r+   Iterator[ExprNode]c              #  :   K   | }||j         V  |j        }|dS dS )z/Iterate through all nodes from current to root.Nr   r   )r_   currents     r)   r   z ExprMetadata.iter_nodes_reversed  s;      '+!&&&&lG !!!!!r+   r   compliant_exprsr   c                2    t          |j                 |g|R  S rY   )KIND_TO_METADATA_CONSTRUCTORr   r   r   r   s      r)   	from_nodezExprMetadata.from_node  s!     ,DI6tNoNNNNr+   compliant_exprcompliant_expr_argsc                6    t          |j                 | ||g|R  S rY   )KIND_TO_METADATA_UPDATERr   r_   r   r   r   s       r)   	with_nodezExprMetadata.with_node  s1     (	2$
)<
 
 
 	
r+   c                    t          |d|d dS NTto_single_outputr   r   combine_metadatar   s      r)   from_aggregationzExprMetadata.from_aggregation  $      t$T
 
 
 	
r+   c           	     :     | t           j        dddd|d           S )NTF)rk   rm   r   rp   r   r   r{   r   r   r   s     r)   from_literalzExprMetadata.from_literal  s3    s "
 
 
 	
r+   c                2     | t           j        |d           S Nr   r   r   s     r)   from_serieszExprMetadata.from_series  s    s='dFFFFr+   c                    t          |j        d                   dk    r | t          j        |d           n|                     |          S )Nr?   r8   r   lenr   r{   r   from_selector_multi_namedr   s     r)   from_colzExprMetadata.from_col  sP    
 4;w'((A-- C$4dCCCC..t44	
r+   c                    t          |j        d                   dk    r | t          j        |d           n|                     |          S )Nindicesr8   r   r  r   s     r)   from_nthzExprMetadata.from_nth  sP     4;y)**a// C$4dCCCC..t44	
r+   c                2     | t           j        |d           S r   )r{   r   r   s     r)   r  z&ExprMetadata.from_selector_multi_named  s     s=,4dKKKKr+   c                2     | t           j        |d           S r   r}   r   s     r)   from_selector_multi_unnamedz(ExprMetadata.from_selector_multi_unnamed  s     s=.TMMMMr+   c                    t          |d|d dS r   r   r   s      r)   from_elementwisezExprMetadata.from_elementwise  r   r+   c                "    | j          o| j         S rY   )rm   rp   r^   s    r)   is_filtrationzExprMetadata.is_filtration  s    ((D1D-DDr+   _cec                    | j         rd}t          |          t          | j        | j        | j        dddd|| 	  	        S )N4Can't apply aggregations to scalar-like expressions.FTr   r   rm   rk   rp   r   r   r   rp   r   r   rO   r   r   r_   r   r  r   s       r)   with_aggregationzExprMetadata.with_aggregation  s[     	-HC',,,( 0" 

 

 

 
	
r+   c                    | j         rd}t          |          t          | j        | j        | j        dz   dddd|| 	  	        S )Nr  r8   FTr  r  r  s       r)   with_orderable_aggregationz'ExprMetadata.with_orderable_aggregation   sb      	-HC',,,( 014" 

 

 

 
	
r+   c                0    t          |g|R d||j        dS )NFr   )r   rN   r   s       r)   with_elementwisezExprMetadata.with_elementwise  s>      
 
 
 #)
 
 
 	
r+   c                    | j         rd}t          |          t          | j        | j        | j        | j        ddd|| 	  	        S )Nz;Can't apply window (e.g. `rank`) to scalar-like expression.Fr  rp   r   r   rO   r   r   rm   r  s       r)   with_windowzExprMetadata.with_window!  s`     	-OC',,,( !0!2  
 
 
 	
r+   c                    | j         rd}t          |          t          | j        | j        | j        dz   | j        ddd|| 	  	        S )NzNCan't apply orderable window (e.g. `diff`, `shift`) to scalar-like expression.r8   Fr  r  r  s       r)   with_orderable_windowz"ExprMetadata.with_orderable_window4  se      	-bC',,,( 014!2  

 

 

 
	
r+   c                   | j         rd}t          |          | j        s| j        rd}t          |          | j        }|sCt          |                                           j        t          j	        urd}t          |          t          |                                           j        j
        r|dk    r|dz  }t          | j        d|dddd|| 	  	        S )	NCannot nest `over` statements.oCannot use `over` on expressions which are elementwise
(e.g. `abs`) or which change length (e.g. `drop_nulls`).aP  Cannot use `order_by` in `over` on expression which isn't orderable.
If your expression is orderable, then make sure that `over(order_by=...)`
comes immediately after the order-dependent expression.

Hint: instead of
  - `(nw.col('price').diff() + 1).over(order_by='date')`
write:
  + `nw.col('price').diff().over(order_by='date') + 1`
r   r8   TFr  )r   r   rk   r  r   nextop_nodes_reversedr   rV   r[   r`   r   rO   )r_   r   r  r   r   s        r)   with_ordered_overzExprMetadata.with_ordered_overG  s    	-2C',,, 	-$"4 	-K  (,,,.	-T++--..38?JJK  (,,,&&(()).; 	!RS@S@Sq O+!  

 

 

 
	
r+   c                    | j         rd}t          |          | j        s| j        rd}t          |          t	          | j        d| j        dddd|| 	  	        S )Nr"  r#  TFr  )r   r   rk   r  r   rO   r   r  s       r)   with_partitioned_overz"ExprMetadata.with_partitioned_overn  s      	-2C',,, 	-$"4 	-K  (,,, 0!  

 

 

 
	
r+   c                    |j         d         r|                     ||          S |j         d         sd}t          |          |                     ||          S )Nr   r   z?At least one of `partition_by` or `order_by` must be specified.)r   r&  r   r(  r  s       r)   	with_overzExprMetadata.with_over  s_    ;z" 	5))$444{>* 	-SC',,,))$444r+   c                    | j         rd}t          |          t          d |D                       }t          d |D                       }t	          | j        ||dddd|| 	  	        S )NECan't apply filtration (e.g. `drop_nulls`) to scalar-like expression.c              3  .   K   | ]}|j         j        V  d S rY   )rN   r   r   s     r)   r   z/ExprMetadata.with_filtration.<locals>.<genexpr>  s'       R RQ!8 R R R R R Rr+   c              3  .   K   | ]}|j         j        V  d S rY   )rN   r   r   s     r)   r   z/ExprMetadata.with_filtration.<locals>.<genexpr>  s'      $Z$ZQQ[%@$Z$Z$Z$Z$Z$Zr+   Fr  )rp   r   r   sumr   rO   )r_   r   r   r   result_has_windowsresult_n_orderable_opss         r)   with_filtrationzExprMetadata.with_filtration  s      	-YC',,,  R R/ R R RRR!$$Z$Z/$Z$Z$Z!Z!Z*2"  

 

 

 
	
r+   c                    | j         rd}t          |          t          | j        | j        | j        dz   dddd|| 	  	        S )Nr,  r8   Fr  r  r  s       r)   with_orderable_filtrationz&ExprMetadata.with_orderable_filtration  sb      	-YC',,,( 014"  

 

 

 
	
r+   c              #  v   K   |                                  D ]!}|j                            d          r|V  "d S )N)zname.rL   )r   r   
startswith)r_   r   s     r)   r%  zExprMetadata.op_nodes_reversed  sR      ,,.. 	 	Dy##$677 JJJJ		 	r+   )rO   r{   r   rW   r   r   rm   rW   rk   rW   rp   rW   r   rW   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   r   r#   r   rq   )r   r   r  r   r#   r   )!rr   rs   rt   ru   	__slots__r   r   r   r   classmethodr   r   r   r   r  r  r	  r  r  r  rx   r  r  r  r  r  r   r&  r(  r*  r2  r4  r%  r;   r+   r)   r   r   P  s        "
I  " !%#$ $(. . . . . .2   
 
 
 
# # # # O O O [O

 
 
 
 
 
 
 [
 	
 	
 	
 [	
 G G G [G 
 
 
 [
 
 
 
 [
 L L L [L N N N [N 
 
 
 [
 E E E XE
 
 
 
 
 
 
 
&
 
 
 

 
 
 
&
 
 
 
&%
 %
 %
 %
N
 
 
 
05 5 5 5
 
 
 
(
 
 
 
$     r+   r   z2dict[ExprKind, Callable[[ExprNode], ExprMetadata]]r   z+dict[ExprKind, Callable[..., ExprMetadata]]r   r   r   r   r   r   c                   d}d}t           j        }d}d}|j        j        }	d}
t	          |          D ]\  }}|j        }|J |j                                        r|j        }| s|dk    r||z  n|}||j        z  }||j	        z  }|	|j        z  }	|
|j
        z  }
|t          |j                  z  }|t          |j                  z  }|j        j        p|t          |          k    }|j        j        o|dk    o| }|r-|dk    s||z   t          |          k     rd}t!          |          t#          |||||	||
||	  	        S )aI  Combine metadata from `args`.

    Arguments:
        compliant_exprs: Expression arguments.
        to_single_output: Whether the result is always single-output, regardless
            of the inputs (e.g. `nw.sum_horizontal`).
        current_node: The current node being added.
        prev: ExprMetadata of previous node.
    r   FTNr8   zXLength-changing expressions can only be used in isolation, or followed by an aggregationr  )r{   r   r   rk   	enumeraterN   rO   r   r   r   r   r   r  rp   r  rm   r   r   )r   r   r   r   n_filtrationsn_scalar_likeresult_expansion_kindr0  r1  result_is_elementwiseresult_is_literalicemetadatarO   result_is_scalar_likeresult_preserves_lengthr   s                     r)   r   r     s    MM)0(-<?++ 6 62<###"2244 	%4N# >?!ee)N:: & 	h22("::!88X00X3444X4555 )-< _---  	* 	&Q	&%%   )			 =O@T@T T Th#C(((&.0,,$!
 
 
 
r+   r   function_namer   r   c                b    t          d |D                       sd|  d}t          |          d S )Nc              3  .   K   | ]}|j         j        V  d S rY   )rN   rm   r   s     r)   r   z4check_expressions_preserve_length.<locals>.<genexpr>&  s'      ::q{+::::::r+   zBExpressions which aggregate or change length cannot be passed to 'z'.)r   r   )rE  r   r   s      r)   !check_expressions_preserve_lengthrH    sL     ::T::::: )dS`ddd#C((() )r+   FNT)r   backendallow_literalarg&IntoExpr | NonNestedLiteral | _1DArrayr   rI  rJ  r   c               |   ddl m}m}m} t	          | t
                    r|s ||           S t          |           r  |d| |                                          S t          |           r|                                 S t          |           r| S |s!t          j        t          |                      ||           S )Nr   )r   lit
new_series )rI  )narwhals.functionsr   rN  rO  r(   r   r   _to_exprr/   r*   r   from_invalid_typetype)rK  r   rI  rJ  r   rN  rO  s          r)   _parse_into_exprrU  +  s     8777777777#s J s3xx ?z"c7333<<>>>~~ ||~~s|| 
 @"4T#YY???3s88Or+   nsr   r   Iterator[CompliantExprAny]c              '     K   |D ]a}t          ||| j                                      |           }|s/|j        j                                        rd}t          |          |V  bd S )N)r   rI  z9Multi-output expressions are not allowed in this context.)rU  _implementation_to_compliant_exprrN   rO   r   r   )rV  r   r   r0   rE   retr   s          r)   evaluate_into_exprsr\  A  s         Z1C
 
 


R
 
  	 " 	2cm&B&R&R&T&T 	2MC,S111				 r+   list[CompliantExprAny]c                     t          d | D                       }g }| D ]^}|rEt          |          r6|                                }|j        |_        |                    |           I|                    |           _|S )Nc              3  6   K   | ]}t          |           V  d S rY   )rp   )rJ   rA  s     r)   r   z&maybe_broadcast_ces.<locals>.<genexpr>R  s-      EErr***EEEEEEr+   )r   rp   	broadcastrN   _opt_metadatar   )r   r`  resultsr   _compliant_exprs        r)   maybe_broadcast_cesrd  Q  s    EE_EEEEEI&(G) + + 	+77 	+0>0H0H0J0JO,:,DO)NN?++++NN>****Nr+   r   c           	        | j         dv r& t          || j                   | j        d          }g }nd| j         v r<| j                             d          \  }}t          t          ||          |          }nt          || j                   }t	          t          | j        || j        | j        d }t          d ||i | j                  }t          j        | g|R  }||_        |S )N>   r   rF   r?   .rV  r   r   r   )r   getattrr   splitrd  r\  r0   r   r   r
   r   r   ra  )r   rV  rA  cesmodulemethodfuncmds           r)   evaluate_root_nodero  _  s    y&&& $WR##T[%9:$)!Y__S11NFF72v..77DD2ty))D! ?#'#:	  
 $ddC&?4;&?&?@@			+s	+	+	+BBIr+   r   c           
     ~   | j         }t          | gt          |j        ||j        |j        dR  ^} } |j        || g|R  }d|j        v r<|j                            d          \  }}t          t          | |          |          }nt          | |j                  }t          d ||i |j                  }||_        |S )Nrg  rf  r   )rN   rd  r\  r0   r   r   r   r   ri  rh  r
   r   ra  )	r   r   rV  rn  r   accessorrl  rm  r[  s	            r)   evaluate_noderr  y  s     &/B+>,	Z#6	

 

 

, , ,(N( 
dN	A-@	A	A	AB
di9??3//&w~x88&AA~ty11
!44)<#L#L#L
M
MCCJr+   r   Sequence[ExprNode]c                n    t          | d         |          }| dd          D ]}t          |||          }|S r7   )ro  rr  )r   rV  rA  r   s       r)   evaluate_nodesru    sF     
E!Hb	)	)Babb	 ) )2tR((Ir+   )r"   r   r#   r$   )r"   r   r#   r,   )r0   r1   r#   r2   )r0   r   r#   r=   )rE   r   r4   r   rF   r5   r#   rG   )r"   r   r#   rW   )
r   r   r   rW   r   r   r   r   r#   r   )r   r   rE  r   r#   r   )
rK  rL  r   rW   rI  r   rJ  rW   r#   r   )
r0   rL  rV  r   r   rW   r   rW   r#   rW  )r   r   r#   r]  )r   r   rV  r   r#   r   )r   r   r   r   rV  r   r#   r   )r   rs  rV  r   r#   r   )]
__future__r   enumr   r   typingr   r   r   r	   r
   narwhals._utilsr   narwhals.dependenciesr   narwhals.exceptionsr   r   r   collections.abcr   r   typing_extensionsr   r   narwhals._compliantr   r   narwhals._compliant.typingr   r   r   r   r   r'   r   r.   r   narwhals.typingr   r    r!   r*   r/   r<   rD   rT   rV   rp   r{   r   r   ro   r   rb   r  rd   r  re   ri   r  rc   r  rf   r   rg   r	  rh   r   __annotations__r  r  rv   r2  r\   r  r]   r4  rw   r*  rZ   r   r[   r  r   r   rH  rU  r\  rd  ro  rr  ru  r;   r+   r)   <module>r     s  
 # " " " " " "         > > > > > > > > > > > > > > & & & & & & 3 3 3 3 3 3           E22222222////////BBBBBBBB              #"""""&&&&&&DDDDDDDDDD! ! ! !# # # #! ! ! !	 	 	 	! ! ! !(X
 X
 X
 X
 X
t X
 X
 X
v( ( ( (" " " " "D " " "6v+ v+ v+ v+ v+ v+ v+ v+ri i i i i i i iZ ,7L,:,7l>O\-L,'l/L,'|?
T  
 
 
 
 ,7,75"L$K!<#IM<)|AO\-	I  	 	 	 	H H H HV	) 	) 	) 	)      ,          4   0     r+   