
    zhD                    ~   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 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 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 d dlmZ er\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 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# d dl$m%Z% d dl&m'Z' d dl&m(Z( d dl&m)Z)  ed      Z*d3d Z+	 	 	 	 d4d!Z,d5d"Z-	 	 	 	 	 	 	 	 d6d#Z.	 	 	 	 	 	 	 	 d7d$Z/ G d% d&e      Z0	 	 	 	 d8d'Z1 G d( d)e      Z2 G d* d+e      Z3 G d, d-      Z4	 	 	 	 	 	 	 	 	 	 d9d.Z5d:d/Z6d;d0Z7	 	 	 	 	 	 	 	 	 	 d<d1Z8y2)=    )annotations)Enum)auto)chain)TYPE_CHECKING)Any)Literal)Sequence)TypeVar)cast)is_narwhals_series)is_numpy_array)LengthChangingExprError)MultiOutputExpressionError)
ShapeError)is_compliant_expr)Never)TypeIs)CompliantExpr)CompliantFrameT)
AliasNames)CompliantExprAny)CompliantFrameAny)CompliantNamespaceAny)EagerNamespaceAny)	EvalNamesExpr)IntoExpr)NonNestedLiteral)_1DArrayTc                &    ddl m} t        | |      S )z'Check whether `obj` is a Narwhals Expr.r   r   )narwhals.exprr   
isinstance)objr   s     Z/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/narwhals/_expression_parsing.pyis_exprr(   +   s    "c4      c                 j     t         d         sdt         d          d}t        |      d fd}|S )Nr   z3Safety assertion failed, expected expression, got: z. Please report a bug.c                2    d   j                  |       d d S Nr      )_evaluate_output_names)dfexprss    r'   evaluate_output_namesz<combine_evaluate_output_names.<locals>.evaluate_output_names;   s    Qx..r22A66r)   )r/   r   returnSequence[str])r   typeAssertionError)r0   msgr1   s   `  r'   combine_evaluate_output_namesr7   2   sA    
 U1X&CDqNCSSijS!!7 ! r)   c                 4      d   j                   y d fd}|S )Nr   c                2    d   j                  |       d d S r,   _alias_output_names)namesr0   s    r'   alias_output_namesz6combine_alias_output_names.<locals>.alias_output_namesG   s    Qx++E22A66r)   )r<   r3   r2   r3   r:   )r0   r=   s   ` r'   combine_alias_output_namesr>   A   s%     Qx##+7 r)   c               R   t        |      r|j                  |       S t        |t              r|s| j	                  |      S t        |      r|j                  j                         S t        |      r7t        d|       }|j                  j                  ||      j                         S |S )Nr   )context)r(   _to_compliant_exprr%   strcolr   _compliant_series_to_exprr   r   _series
from_numpy)plxother
str_as_litnss       r'   extract_compliantrL   M   s     u~'',,%jwwu~% &&//11e%s+zz$$UB$7@@BBLr)   c                :   | j                  |      }| j                  |n| j                  |      }|r`| j                  J | j                  j                  j	                         r.t        t        ||      D cg c]  \  }}||vr||f c}} \  }}||fS c c}}w N)r.   r;   	_metadataexpansion_kindis_multi_unnamedzip)exprr/   excludeoutput_namesaliasesxaliass          r'   !evaluate_output_names_and_aliasesrY   _   s     ..r2L ##+ 	%%l3 
 ~~)))>>((99;$' %(g$> 5' J%!L'   s   9B
c                      e Zd ZdZ e       Z	  e       Z	  e       Z	  e       Z	  e       Z		 ddZ
ddZddZddZe	 	 	 	 	 	 d	d       Zy)
ExprKindam  Describe which kind of expression we are dealing with.

    Commutative composition rules are:
    - LITERAL vs LITERAL -> LITERAL
    - FILTRATION vs (LITERAL | AGGREGATION) -> FILTRATION
    - FILTRATION vs (FILTRATION | TRANSFORM | WINDOW) -> raise
    - (TRANSFORM | WINDOW) vs (...) -> TRANSFORM
    - AGGREGATION vs (LITERAL | AGGREGATION) -> AGGREGATION
    c                F    | t         j                  t         j                  hv S rN   )r[   	TRANSFORMWINDOWselfs    r'   preserves_lengthzExprKind.preserves_length   s    **HOO<<<r)   c                &    | t         j                  u S rN   )r[   r^   r_   s    r'   	is_windowzExprKind.is_window   s    x&&r)   c                &    | t         j                  u S rN   )r[   
FILTRATIONr_   s    r'   is_filtrationzExprKind.is_filtration   s    x****r)   c                    t        |       S rN   is_scalar_liker_   s    r'   ri   zExprKind.is_scalar_like   s    d##r)   c                   t        |      r|j                  j                  S t        |      st	        |      st        |t              r|st        j                  S t        j                  S rN   )
r(   rO   kindr   r   r%   rB   r[   r]   LITERAL)clsr&   rJ   s      r'   from_into_exprzExprKind.from_into_expr   sO     3<==%%%s#c"3$Z%%%r)   Nr2   bool)r&   &IntoExpr | NonNestedLiteral | _1DArrayrJ   rp   r2   r[   )__name__
__module____qualname____doc__r   rl   AGGREGATIONr]   r^   re   ra   rc   rf   ri   classmethodrn    r)   r'   r[   r[   u   s     fG&K#I6VF	 J)='+$  8 IM 	   r)   r[   c                F    | t         j                  t         j                  hv S rN   )r[   rv   rl   )rk   s    r'   ri   ri      s      H(((*:*:;;;r)   c                  X    e Zd ZdZ e       Z	  e       Z	  e       Z	 ddZddZ	ddZ
y)ExpansionKindz8Describe what kind of expansion the expression performs.c                &    | t         j                  u S rN   )r{   MULTI_UNNAMEDr_   s    r'   rQ   zExpansionKind.is_multi_unnamed   s    }2222r)   c                F    | t         j                  t         j                  hv S rN   )r{   MULTI_NAMEDr}   r_   s    r'   is_multi_outputzExpansionKind.is_multi_output   s    11=3N3NOOOr)   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}   r5   )r`   rI   r6   s      r'   __and__zExpansionKind.__and__   sI    =...5M<W<W3W ...;D6ugMcdS!!r)   Nro   )rI   r{   r2   z$Literal[ExpansionKind.MULTI_UNNAMED])rr   rs   rt   ru   r   SINGLEr   r}   rQ   r   r   rx   r)   r'   r{   r{      s3    BVF9&K!FM'3P"r)   r{   c                  h    e Zd ZdZ e       Z	  e       Z	  e       Z	  e       Z	 ddZ	ddZ
ddZy)
WindowKindz5Describe what kind of window the expression contains.c                F    | t         j                  t         j                  hv S rN   )r   UNCLOSEABLE	CLOSEABLEr_   s    r'   is_openzWindowKind.is_open   s    
..
0D0DEEEr)   c                &    | t         j                  u S rN   )r   CLOSEDr_   s    r'   	is_closedzWindowKind.is_closed   s    z((((r)   c                &    | t         j                  u S rN   )r   r   r_   s    r'   is_uncloseablezWindowKind.is_uncloseable   s    z----r)   Nro   )rr   rs   rt   ru   r   NONEr   r   r   r   r   r   rx   r)   r'   r   r      sA    ?6D/Ig&K VF:F).r)   r   c                      e Zd ZdZ	 	 	 	 	 	 	 	 ddZddZddZedd       Zedd       Z	edd       Z
dd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dd       Zy)ExprMetadata)_expansion_kind_kind_window_kindc              .    || _         || _        || _        y rN   r   r   r   )r`   rk   window_kindrP   s       r'   __init__zExprMetadata.__init__   s      $
'-r)   c               6    d| j                   }t        |      )NzCannot subclass )rr   	TypeError)rm   argskwdsr6   s       r'   __init_subclass__zExprMetadata.__init_subclass__  s      01nr)   c                V    d| j                    d| j                   d| j                   dS )NzExprMetadata(kind: z, window_kind: z, expansion_kind: )r   r_   s    r'   __repr__zExprMetadata.__repr__  s2    $TZZL@Q@Q?RRdeieyeydzz{||r)   c                    | j                   S rN   )r   r_   s    r'   rk   zExprMetadata.kind  s    zzr)   c                    | j                   S rN   )r   r_   s    r'   r   zExprMetadata.window_kind  s       r)   c                    | j                   S rN   )r   r_   s    r'   rP   zExprMetadata.expansion_kind  s    ###r)   c               F    t        || j                  | j                        S )z<Change metadata kind, leaving all other attributes the same.r   rP   )r   r   r   r`   rk   s     r'   	with_kindzExprMetadata.with_kind  s$    ))//
 	
r)   c                    | j                   t        j                  u rd}t        |      t	        | j
                  t        j                  | j                        S )z:Add uncloseable window, leaving other attributes the same.&Unreachable code, please report a bug.r   )r   r   r   r5   r   rk   r   r   )r`   r6   s     r'   with_uncloseable_windowz$ExprMetadata.with_uncloseable_window  sL    
 1 11:C %%II"..//
 	
r)   c                   | j                   t        j                  u rt        j                  }n9| j                   t        j                  u rd}t        |      t        j                  }t        ||| j                        S )zChange metadata kind and add closeable window.

        If we already have an uncloseable window, the window stays uncloseable.
        r   r   )	r   r   r   r   r   r5   r   r   r   )r`   rk   r   r6   s       r'   with_kind_and_closeable_windowz+ExprMetadata.with_kind_and_closeable_window'  sl    
 
/$..K*"3"33:C %%$00K#//
 	
r)   c               N    t        |t        j                  | j                        S )z8Change metadata kind and set window kind to uncloseable.r   )r   r   r   r   r   s     r'    with_kind_and_uncloseable_windowz-ExprMetadata.with_kind_and_uncloseable_window9  s$    "..//
 	
r)   c                 r    t        t        j                  t        j                  t
        j                        S Nr   )r   r[   r]   r   r   r{   r   rx   r)   r'   selector_singlezExprMetadata.selector_singleA  s*     "(//
 	
r)   c                 r    t        t        j                  t        j                  t
        j                        S r   )r   r[   r]   r   r   r{   r   rx   r)   r'   selector_multi_namedz!ExprMetadata.selector_multi_namedJ  s*     "(44
 	
r)   c                 r    t        t        j                  t        j                  t
        j                        S r   )r   r[   r]   r   r   r{   r}   rx   r)   r'   selector_multi_unnamedz#ExprMetadata.selector_multi_unnamedS  s*     "(66
 	
r)   c               "    t        ||ddd      S )NTFrJ   allow_multi_outputto_single_outputcombine_metadata)rm   lhsrhss      r'   from_binary_opzExprMetadata.from_binary_op\  s      %RW
 	
r)   c                    t        |ddddS )NFTr   r   )rm   r0   s     r'   from_horizontal_opzExprMetadata.from_horizontal_opd  s    uPT
 	
r)   N)rk   r[   r   r   rP   r{   r2   None)r   r   r   r   r2   r   )r2   rB   )r2   r[   )r2   r   )r2   r{   )rk   r[   r2   r   )r2   r   )r   r   r   r   r2   r   )r0   r   r2   r   )rr   rs   rt   	__slots__r   r   r   propertyrk   r   rP   r   r   r   r   staticmethodr   r   r   rw   r   r   rx   r)   r'   r   r      s    <I
.
.
  
. &
. 

.}   ! ! $ $
	

$
 
 
 
 
 
 
 
 
 
 
r)   r   c                <   d}d}d}d}t         j                  }d}	d}
d}t        |      D ]8  \  }}t        |t              r| sd}t        |      s(|j                  }|j                  j                         r/|j                  }|dkD  r|sd}t        |      |s|dk(  r|}n||z  }|j                  }|t        j                  u rd}nM|t        j                  u rd}n8|t        j                  u r|dz  }n |j                         rd}nd}t!        |      |j"                  }|t$        j&                  u rd}
|t$        j(                  u rd}	#|t$        j*                  u s7d}; |r|s|s|st        j                  }nY|dkD  rd}t-        |      |r|rd}t/        |      |rt        j                  }n#|rt        j0                  }nt        j                  }|
s|	rt$        j&                  }n#|rt$        j*                  }nt$        j2                  }t5        |||	      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-   zunreachable codezXLength-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%   rB   r(   rO   rP   r   r   rk   r[   rv   rl   re   ra   r5   r   r   r   r   r   r   r   r]   r   r   )rJ   r   r   r   n_filtrationshas_transforms_or_windowshas_aggregationshas_literalsresult_expansion_kindhas_closeable_windowshas_uncloseable_windowshas_closed_windowsiargmetadatarP   r6   rk   r   result_kindresult_window_kinds                        r'   r   r   k  s+    M %L)00!#D/ &*3c3
(,%S\}}H&&668!)!8!8q5!3=  5S99'Av0>-0E0V-==Dx+++#' )))#,,,"&&(,0)($S))"..Kj444*.'
 4 44(,%
 1 11%)"M&*R 	 )&&		h%c**	4fo	))	"((**"7'33	'..'__!3DY r)   c                \    ddl m t        fd|D              sd|  d}t        |      y )Nr   )Seriesc              3     K   | ]I  }t        |      xr$ |j                  j                  j                         xs t	        |t
        f       K y wrN   )r(   rO   rk   ra   r%   rB   ).0rW   r   s     r'   	<genexpr>z4check_expressions_preserve_length.<locals>.<genexpr>  sO        
	;((99; 	(a#v'	(s   AAzBExpressions which aggregate or change length cannot be passed to 'z'.)narwhals.seriesr   allr   )function_namer   r6   r   s      @r'   !check_expressions_preserve_lengthr     sB     '   
 SS`Raacdor)   c                 Z    t        | |j                               }t        d |D              S )Nc              3  ~   K   | ]5  }t        |      xr$ |j                  j                  j                          7 y wrN   )r(   rO   rk   ri   )r   rW   s     r'   r   z,all_exprs_are_scalar_like.<locals>.<genexpr>  s/     OAwqz?akk..==??Os   ;=)r   valuesr   )r   kwargsr0   s      r'   all_exprs_are_scalar_liker     s'     $(EOOOOr)   c                     fd|D        }|D cg c]  }t         j                  |       }}t        d |D              fdt        ||      D        } || S c c}w )Nc              3  :   K   | ]  }t        |         yw)rJ   N)rL   )r   	comparandrH   rJ   s     r'   r   z(apply_n_ary_operation.<locals>.<genexpr>  s%       	#yZ@@s   r   c              3  >   K   | ]  }|j                            y wrN   rh   )r   rk   s     r'   r   z(apply_n_ary_operation.<locals>.<genexpr>  s     @$++--@s   c              3  z   K   | ]2  \  }}r't        |      rt        |      r|j                  |      n| 4 y wrN   )r   ri   	broadcast)r   compliant_exprrk   r   s      r'   r   z(apply_n_ary_operation.<locals>.<genexpr>  sF       !ND *>:~d?S 	  &	s   8;)r[   rn   anyrR   )rH   functionrJ   
comparandscompliant_exprsr   kindsr   s   ` `    @r'   apply_n_ary_operationr     s}    #O $ 		jAE 
 @%@@I %($?	O _%%s   A N)r&   r   r2   zTypeIs[Expr])r0   z#CompliantExpr[CompliantFrameT, Any]r2   zEvalNames[CompliantFrameT])r0   r   r2   zAliasNames | None)rH   r   rI   rq   rJ   rp   r2   z#CompliantExprAny | NonNestedLiteral)rS   r   r/   r   rT   r3   r2   z#tuple[Sequence[str], Sequence[str]])rk   r[   r2   z7TypeIs[Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]])
r   zIntoExpr | object | NonerJ   rp   r   rp   r   rp   r2   r   )r   r   r   rB   r2   r   )r   r   r   r   r2   rp   )
rH   r   r   r   r   rq   rJ   rp   r2   r   )9
__future__r   enumr   r   	itertoolsr   typingr   r   r	   r
   r   r   narwhals.dependenciesr   r   narwhals.exceptionsr   r   r   narwhals.utilsr   typing_extensionsr   r   narwhals._compliantr   r   narwhals._compliant.typingr   r   r   r   r   r   r$   r   narwhals.typingr   r    r!   r"   r(   r7   r>   rL   rY   r[   ri   r{   r   r   r   r   r   r   rx   r)   r'   <module>r      s   #           4 0 7 : * ,'(135;<@<4"(0(A!!/!!		1 	
 )$!
! 1!<I!(!,; t ; |<
<<<"D "6. .:v
 v
r]#]] ] 	]
 ]@P&	&& 8& 	&
 &r)   