
    hhxY                       d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZ d dlmZmZ d dlmZmZmZ d dlmZmZ erEd dl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,  ed          Z-dEdZ.dFdZ/dGdZ0dHdZ1dIdZ2dJd&Z3 G d' d(e          Z4dKd*Z5 G d+ d,e          Z6 G d- d.          Z7dLd5Z8dMd:Z9dNd<Z:dOdCZ;dDS )P    )annotations)Enumauto)chain)TYPE_CHECKINGAnyCallableLiteralTypeVar)is_compliant_expr
zip_strict)is_narwhals_seriesis_numpy_arrayis_numpy_array_1d)InvalidOperationErrorMultiOutputExpressionError)Sequence)NeverTypeIs)CompliantExprCompliantFrameT)
AliasNamesCompliantExprAnyCompliantFrameAnyCompliantNamespaceAny	EvalNamesExprSeries)IntoExprNonNestedLiteral_1DArrayT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     S/var/www/histauto/venv/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_seriesr2   *   s$    &&&&&&c6"""r.   +TypeIs[Expr | Series[Any] | str | _1DArray]c                h    ddl m} ddlm} t	          | ||t
          f          pt          |           S )Nr   r   r   )r*   r   r1   r    r+   strr   )r%   r   r    s      r,   is_into_expr_eagerr6   1   sK    """"""&&&&&&cFD#.//I3DS3I3IIr.   exprs#CompliantExpr[CompliantFrameT, Any]EvalNames[CompliantFrameT]c                      t           d                   s(dt           d                    d}t          |          d	 fd}|S )
Nr   z3Safety assertion failed, expected expression, got: z. Please report a bug.dfr   r&   Sequence[str]c                J    d                              |           d d         S Nr      )_evaluate_output_names)r;   r7   s    r,   evaluate_output_namesz<combine_evaluate_output_names.<locals>.evaluate_output_namesA   s$    Qx..r222A266r.   )r;   r   r&   r<   )r   typeAssertionError)r7   msgrA   s   `  r,   combine_evaluate_output_namesrE   8   sg    
 U1X&& "jDqNNjjjS!!!7 7 7 7 7 7 ! r.   r   AliasNames | Nonec                 2      d         j         d S d fd}|S )Nr   namesr<   r&   c                J    d                              |           d d         S r>   _alias_output_names)rH   r7   s    r,   alias_output_namesz6combine_alias_output_names.<locals>.alias_output_namesM   s$    Qx++E222A266r.   )rH   r<   r&   r<   rJ   )r7   rL   s   ` r,   combine_alias_output_namesrM   G   s=     Qx#+t7 7 7 7 7 7 r.   exprr;   r   excluder<   #tuple[Sequence[str], Sequence[str]]c                   |                      |          }| j        |n|                     |          }rM| j        J | j        j                                        r&t          fdt          ||          D              \  }}||fS )Nc                &    g | ]\  }}|v	||fS  rS   ).0xaliasrO   s      r,   
<listcomp>z5evaluate_output_names_and_aliases.<locals>.<listcomp>`   s6        5'' J'''r.   )r@   rK   	_metadataexpansion_kindis_multi_unnamedr   )rN   r;   rO   output_namesaliasess     `  r,   !evaluate_output_names_and_aliasesr]   S   s     ..r22L #+ 	%%l33 
  	~)))>(99;; 	$.   $.|W$E$E  %!L'   r.   c                  N   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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                6    | t           j        t           j        hv S N)r_   LITERALAGGREGATIONselfs    r,   is_scalar_likezExprKind.is_scalar_like   s    ((*>???r.   c                6    | t           j        t           j        hv S rb   )r_   ORDERABLE_WINDOWORDERABLE_AGGREGATIONre   s    r,   is_orderable_windowzExprKind.is_orderable_window   s    183QRRRr.   r%   r   c                    |j         }|j        rt          j        S |j        rt          j        S |j        rt          j        S t          j        S rb   )	rX   
is_literalr_   rc   rg   rd   is_elementwiseELEMENTWISEUNKNOWN)clsr%   metas      r,   	from_exprzExprKind.from_expr   sM    }? 	$## 	('' 	(''r.   &IntoExpr | NonNestedLiteral | _1DArray
str_as_litc                   t          |          r|                     |          S t          |          s&t          |          st	          |t
                    r|st          j        S t          j        S rb   )	r-   rs   r   r   r+   r5   r_   ro   rc   )rq   r%   ru   s      r,   from_into_exprzExprKind.from_into_expr   sv     3<< 	&==%%%s##	(c""	( 3$$	( .8	(
 ''r.   Nr&   r`   )r%   r   r&   r_   )r%   rt   ru   r`   r&   r_   )__name__
__module____qualname____doc__r   rc   rd   rj   ro   ri   WINDOW
FILTRATIONORDERABLE_FILTRATIONOVERrp   propertyrg   rk   classmethodrs   rw   rS   r.   r,   r_   r_   i   s/       @@dffG$&&KY DFFX$&&KgtvvHTVVFNJG466=466D5dffGL@ @ @ X@ S S S XS       [        [     r.   r_   7TypeIs[Literal[ExprKind.LITERAL, ExprKind.AGGREGATION]]c                    | j         S rb   rg   )r%   s    r,   rg   rg      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&   r`   c                    | t           j        u S rb   )r   MULTI_UNNAMEDre   s    r,   rZ   zExpansionKind.is_multi_unnamed   s    }222r.   c                6    | t           j        t           j        hv S rb   )r   MULTI_NAMEDr   re   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   rC   )rf   r   rD   s      r,   __and__zExpansionKind.__and__   sL    =...5M<W3W3W ..dDddudddS!!!r.   Nrx   )r   r   r&   r   )ry   rz   r{   r|   r   SINGLEr   r   rZ   r   r   rS   r.   r,   r   r      s        BBTVVF9$&&K!DFFM'3 3 3 3P P P P" " " " " "r.   r   c                  F   e Zd ZdZdZdddddddd4dZd5dZd6dZed7d            Z	d8dZ
d8dZd8d Zd8d!Zd8d"Zd8d#Zd8d$Zd8d%Zd8d&Zed8d'            Zed8d(            Zed8d)            Zed8d*            Zed8d+            Zed9d0            Zed:d2            Zd3S );ExprMetadataa  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.
        last_node: The ExprKind of the last node.
        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.
    )rY   has_windowsrn   rm   rg   	last_noden_orderable_opspreserves_lengthFr   Tr   r   r   rn   rg   rm   rY   r   r   r_   r   r`   r   intr   rn   rg   rm   r&   Nonec                   |r|sJ |r|sJ || _         || _        || _        || _        || _        || _        || _        || _        d S rb   rY   r   r   r   rn   r   rg   rm   )	rf   rY   r   r   r   r   rn   rg   rm   s	            r,   __init__zExprMetadata.__init__   sn      	"!!!! 	$####-;#,!,$3$2&6$2 *r.   argsr   kwdsr   c               4    d| j         }t          |          )NzCannot subclass )ry   	TypeError)rq   r   r   rD   s       r,   __init_subclass__zExprMetadata.__init_subclass__  s    111nnr.   r5   c                    d| j          d| j         d| j         d| j         d| j         d| j         d| j         d| j         d	S )
Nz ExprMetadata(
  expansion_kind: z,
  last_node: z,
  has_windows: z,
  n_orderable_ops: z,
  is_elementwise: z,
  preserves_length: z,
  is_scalar_like: z,
  is_literal: z,
)r   re   s    r,   __repr__zExprMetadata.__repr__  s    	!%!4	 	 N	 	 #.	 	 #'"6		 	
 "&!4	 	 $(#8	 	 "&!4	 	 "_	 	 		
r.   c                "    | j          o| j         S rb   )r   rg   re   s    r,   is_filtrationzExprMetadata.is_filtration  s    ((D1D-DDr.   c           
         | j         rd}t          |          t          | j        t          j        | j        | j        dddd          S )N4Can't apply aggregations to scalar-like expressions.FTr   )rg   r   r   rY   r_   rd   r   r   rf   rD   s     r,   with_aggregationzExprMetadata.with_aggregation  s[     	-HC',,, ( 0" 	
 	
 	
 		
r.   c           
         | j         rd}t          |          t          | j        t          j        | j        | j        dz   dddd          S )Nr   r?   FTr   )rg   r   r   rY   r_   rj   r   r   r   s     r,   with_orderable_aggregationz'ExprMetadata.with_orderable_aggregation'  s`     	-HC',,,*( 014" 	
 	
 	
 		
r.   c           
         t          | j        t          j        | j        | j        | j        | j        | j        | j	                  S )Nr   )
r   rY   r_   ro   r   r   r   rn   rg   rm   re   s    r,   with_elementwise_opz ExprMetadata.with_elementwise_op7  sH     ( 0!2..	
 	
 	
 		
r.   c           
         | j         rd}t          |          t          | j        t          j        | j        | j        | j        ddd          S )Nz;Can't apply window (e.g. `rank`) to scalar-like expression.Fr   )	rg   r   r   rY   r_   r}   r   r   r   r   s     r,   with_windowzExprMetadata.with_windowC  s_     	-OC',,,O( !0!2  
 
 
 	
r.   c           
         | j         rd}t          |          t          | j        t          j        | j        | j        dz   | j        ddd          S )NzNCan't apply orderable window (e.g. `diff`, `shift`) to scalar-like expression.r?   Fr   )	rg   r   r   rY   r_   ri   r   r   r   r   s     r,   with_orderable_windowz"ExprMetadata.with_orderable_windowU  sc     	-bC',,,%( 014!2  	
 	
 	
 		
r.   c           
     8   | j         rd}t          |          | j        s| j        rd}t          |          | j        }|s$| j        t          j        urd}t          |          | j        j        r|dz  }t          | j
        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?   TFr   )r   r   rn   r   r   r   r_   r}   rk   r   rY   r   )rf   rD   r   s      r,   with_ordered_overzExprMetadata.with_ordered_overe  s     	-2C',,, 	-$"4 	-K  (,,,. 
	-4>#H#HK  (,,,>- 	!q OM+!  	
 	
 	
 		
r.   c           
         | j         rd}t          |          | j        s| j        rd}t          |          t	          | j        t          j        d| j        dddd          S )Nr   r   TFr   )	r   r   rn   r   r   rY   r_   r   r   r   s     r,   with_partitioned_overz"ExprMetadata.with_partitioned_over  s     	-2C',,, 	-$"4 	-K  (,,,M 0!  	
 	
 	
 		
r.   c           
         | j         rd}t          |          t          | j        t          j        | j        | j        dddd          S )NECan't apply filtration (e.g. `drop_nulls`) to scalar-like expression.Fr   )rg   r   r   rY   r_   r~   r   r   r   s     r,   with_filtrationzExprMetadata.with_filtration  s[     	-YC',,,( 0"  	
 	
 	
 		
r.   c           
         | j         rd}t          |          t          | j        t          j        | j        | j        dz   dddd          S )Nr   r?   Fr   )rg   r   r   rY   r_   r   r   r   r   s     r,   with_orderable_filtrationz&ExprMetadata.with_orderable_filtration  s`     	-YC',,,)( 014"  	
 	
 	
 		
r.   c                 R    t          t          j        t          j        ddd          S )NFT)rn   r   rg   )r   r   r   r_   rd   rS   r.   r,   aggregationzExprMetadata.aggregation  s.       "
 
 
 	
r.   c                 T    t          t          j        t          j        dddd          S )NFT)rn   r   rm   rg   )r   r   r   r_   rc   rS   r.   r,   literalzExprMetadata.literal  s1      "
 
 
 	
r.   c                 J    t          t          j        t          j                  S rb   )r   r   r   r_   ro   rS   r.   r,   selector_singlezExprMetadata.selector_single  s     M0(2FGGGr.   c                 J    t          t          j        t          j                  S rb   )r   r   r   r_   ro   rS   r.   r,   selector_multi_namedz!ExprMetadata.selector_multi_named  s     M5x7KLLLr.   c                 J    t          t          j        t          j                  S rb   )r   r   r   r_   ro   rS   r.   r,   selector_multi_unnamedz#ExprMetadata.selector_multi_unnamed  s     M79MNNNr.   lhsr   rhsr!   c               *    t          ||ddd          S )NTFru   allow_multi_outputto_single_outputcombine_metadata)rq   r   r   s      r,   from_binary_opzExprMetadata.from_binary_op  s'      %RW
 
 
 	
r.   r7   c                    t          |ddddS )NFTr   r   )rq   r7   s     r,   from_horizontal_opzExprMetadata.from_horizontal_op  s#    uPT
 
 
 	
r.   N)rY   r   r   r_   r   r`   r   r   r   r`   rn   r`   rg   r`   rm   r`   r&   r   )r   r   r   r   r&   r   )r&   r5   rx   )r&   r   )r   r   r   r!   r&   r   )r7   r!   r&   r   )ry   rz   r{   r|   	__slots__r   r   r   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   rS   r.   r,   r   r      sD         	I  " !%#$ + + + + + +2   
 
 
 
 E E E XE
 
 
 

 
 
 
 

 

 

 


 
 
 
$
 
 
 
 !
 !
 !
 !
F
 
 
 
*
 
 
 

 
 
 
 
 
 
 \
 
 
 
 \
 H H H \H M M M \M O O O \O 
 
 
 [
 
 
 
 [
 
 
r.   r   r   IntoExpr | object | Noneru   r`   r   r   c           
        d}t           j        }d}d}d}d}	d}
d}t          |          D ]\  }}t          |t                    r| rt          |          rd}d}
d}2t          |          r|j        }|j        	                                r/|j        }|dk    r|sd}t          |          |s|dk    r||z  n|}||j        z  }||j        z  }||j        z  }|	|j        z  }	|
|j        z  }
||j        z  }|t#          |j                  z  }|dk    rd}t'          |          |r|rd}t'          |          t)          |t*          j        ||||	|
|          S )	a  Combine metadata from `args`.

    Arguments:
        args: Arguments, maybe expressions, literals, or Series.
        str_as_lit: Whether to interpret strings as literals or as column names.
        allow_multi_output: Whether to allow multi-output inputs.
        to_single_output: Whether the result is always single-output, regardless
            of the inputs (e.g. `nw.sum_horizontal`).
    r   FTz]Multi-output expressions (e.g. nw.col('a', 'b'), nw.all()) are not supported in this context.r?   zXLength-changing expressions can only be used in isolation, or followed by an aggregationzVCannot combine length-changing expressions with length-preserving ones or aggregationsr   )r   r   	enumerater+   r5   r2   r-   rX   rY   r   r   r   r   r   rn   rg   rm   r   r   r   r   r_   ro   )ru   r   r   r   n_filtrationsresult_expansion_kindresult_has_windowsresult_n_orderable_opsresult_preserves_lengthresult_is_elementwiseresult_is_scalar_likeresult_is_literaliargmetadatarY   rD   s                    r,   r   r     s    M)0#  D// 9 93sC   	9 	9	# 	9&*#$)! %S\\ 	9}H&6688 !)!8q55!35=  5S999'  q55 .>>+ * ("66"h&>>"#x'@@#!X%<<!!X%<<!!44S!7888Mqh#C((( )= )f#C(((&.0,,$
 
 
 
r.   r!   function_namer5   r   c                t    ddl m t          fd|D                       sd|  d}t          |          d S )Nr   r   c              3  |   K   | ]6}t          |          r|j        j        pt          |t          f          V  7d S rb   )r-   rX   r   r+   r5   )rT   rU   r    s     r,   	<genexpr>z4check_expressions_preserve_length.<locals>.<genexpr>C  sZ         
	44UAV}9U9U     r.   zBExpressions which aggregate or change length cannot be passed to 'z'.)r1   r    allr   )r   r   rD   r    s      @r,   !check_expressions_preserve_lengthr   =  s{     '&&&&&         ) eS`ddd#C((() )r.   kwargsc                 x    t          | |                                          }t          d |D                       S )Nc              3  L   K   | ]}t          |          o|j        j        V   d S rb   )r-   rX   rg   )rT   rU   s     r,   r   z,all_exprs_are_scalar_like.<locals>.<genexpr>P  s2      HHQwqzz8ak8HHHHHHr.   )r   valuesr   )r   r   r7   s      r,   all_exprs_are_scalar_liker   K  s8     $((EHH%HHHHHHr.   plxr   n_ary_functionCallable[..., CompliantExprAny]
comparandsrt   c                   | j         fd|D             }fd|D             }t          d |D                       fdt          ||          D             } || S )Nc              3  2   K   | ]} |           V  dS )ru   NrS   )rT   intoparseru   s     r,   r   z(apply_n_ary_operation.<locals>.<genexpr>Z  s2      QQduuTj999QQQQQQr.   c                H    g | ]}t                               |           S )r   )r_   rw   )rT   	comparandru   s     r,   rW   z)apply_n_ary_operation.<locals>.<listcomp>[  s<        		jAA  r.   c              3  &   K   | ]}|j          V  d S rb   r   )rT   kinds     r,   r   z(apply_n_ary_operation.<locals>.<genexpr>`  s(      >>++>>>>>>r.   c              3     K   | ]>\  }}r3t          |          r$t          |          r|                    |          n|V  ?d S rb   )r   rg   	broadcast)rT   compliant_exprr   r   s      r,   r   z(apply_n_ary_operation.<locals>.<genexpr>a  sx         !ND 	*>::	?Md?S?S	  &&&     r.   )parse_into_expranyr   )r   r   ru   r   compliant_exprskindsr   r   s     `   @@r,   apply_n_ary_operationr  S  s     EQQQQQjQQQO   #  E
 >>>>>>>I    %/$F$F	  O >?++r.   N)r%   r   r&   r'   )r%   r   r&   r/   )r%   r   r&   r3   )r7   r8   r&   r9   )r7   r   r&   rF   )rN   r   r;   r   rO   r<   r&   rP   )r%   r_   r&   r   )
r   r   ru   r`   r   r`   r   r`   r&   r   )r   r!   r   r5   r&   r   )r   r!   r   r!   r&   r`   )
r   r   r   r   r   rt   ru   r`   r&   r   )<
__future__r   enumr   r   	itertoolsr   typingr   r   r	   r
   r   narwhals._utilsr   r   narwhals.dependenciesr   r   r   narwhals.exceptionsr   r   collections.abcr   typing_extensionsr   r   narwhals._compliantr   r   narwhals._compliant.typingr   r   r   r   r   r*   r   r1   r    narwhals.typingr!   r"   r#   r$   r-   r2   r6   rE   rM   r]   r_   rg   r   r   r   r   r   r  rS   r.   r,   <module>r     s  
 # " " " " "               A A A A A A A A A A A A A A 9 9 9 9 9 9 9 9 W W W W W W W W W W Q Q Q Q Q Q Q Q ((((((////////BBBBBBBB              #"""""&&&&&&DDDDDDDDDDA! ! ! !# # # #J J J J! ! ! !	 	 	 	! ! ! !,@  @  @  @  @ t @  @  @ F   " " " " "D " " "6^
 ^
 ^
 ^
 ^
 ^
 ^
 ^
B	L L L L^) ) ) )I I I I, , , , , ,r.   