o
    hD                     @  sL  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rd 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*dod$d%Z+dpd)d*Z,dqd-d.Z-drd6d7Z.dsd>d?Z/G d@dA dAeZ0dtdDdEZ1G dFdG dGeZ2dudJdKZ3G dLdM dMeZ4G dNdO dOZ5dvdTdUZ6dwdXdYZ7dxd\d]Z8dyd^d_Z9dzdcddZ:d{dfdgZ;d|dhdiZ<d}dldmZ=dnS )~    )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Tobjr   returnTypeIs[Expr]c                 C  s   ddl m} t| |S )z'Check whether `obj` is a Narwhals Expr.r   r   )narwhals.exprr   
isinstance)r"   r    r'   P/var/www/vscode/kcb/lib/python3.10/site-packages/narwhals/_expression_parsing.pyis_expr+   s   
r)   exprs#CompliantExpr[CompliantFrameT, Any]EvalNames[CompliantFrameT]c                    s:   t  d sdt d  d}t|d
 fdd	}|S )Nr   z3Safety assertion failed, expected expression, got: z. Please report a bug.dfr   r#   Sequence[str]c                       d  | d d S Nr      )_evaluate_output_names)r-   r*   r'   r(   evaluate_output_names;      z<combine_evaluate_output_names.<locals>.evaluate_output_names)r-   r   r#   r.   )r   typeAssertionError)r*   msgr4   r'   r3   r(   combine_evaluate_output_names2   s
   r9   r   AliasNames | Nonec                    s$    d j d u r	d S d fdd}|S )Nr   namesr.   r#   c                   r/   r0   _alias_output_names)r;   r3   r'   r(   alias_output_namesG   r5   z6combine_alias_output_names.<locals>.alias_output_names)r;   r.   r#   r.   r<   )r*   r>   r'   r3   r(   combine_alias_output_namesA   s   r?   plxr   other&IntoExpr | NonNestedLiteral | _1DArray
str_as_litbool#CompliantExprAny | NonNestedLiteralc                C  sf   t |r	|| S t|tr|s| |S t|r|j S t|r1t	d| }|j
j||d S |S )Nr   )context)r)   _to_compliant_exprr&   strcolr   _compliant_series_to_exprr   r   _series
from_numpy)r@   rA   rC   nsr'   r'   r(   extract_compliantM   s   



rO   exprr-   r   excluder.   #tuple[Sequence[str], Sequence[str]]c                   st   |  |}|sg g fS | jd u r|n| |} r6| jd us J | jj r6t fddt||D  \}}||fS )Nc                   s    g | ]\}}| vr||fqS r'   r'   ).0xaliasrQ   r'   r(   
<listcomp>n   s
    z5evaluate_output_names_and_aliases.<locals>.<listcomp>)r2   r=   	_metadataexpansion_kindis_multi_unnamedzip)rP   r-   rQ   output_namesaliasesr'   rV   r(   !evaluate_output_names_and_aliases_   s    


r^   c                   @  s`   e Zd ZdZe Z	 e Z	 e Z	 e Z	 e Z		 dddZ
dddZddd	Zdd
dZdS )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
    r#   rD   c                 C     | t jt jhv S N)r_   	TRANSFORMWINDOWselfr'   r'   r(   preserves_length      zExprKind.preserves_lengthc                 C  
   | t ju S ra   )r_   rc   rd   r'   r'   r(   	is_window      
zExprKind.is_windowc                 C  rh   ra   )r_   
FILTRATIONrd   r'   r'   r(   is_filtration   rj   zExprKind.is_filtrationc                 C  s   t | S ra   is_scalar_likerd   r'   r'   r(   rn      s   zExprKind.is_scalar_likeNr#   rD   )__name__
__module____qualname____doc__r   LITERALAGGREGATIONrb   rc   rk   rf   ri   rl   rn   r'   r'   r'   r(   r_   w   s     



r_   kind7TypeIs[Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]]c                 C  r`   ra   )r_   ru   rt   )rv   r'   r'   r(   rn      s   rn   c                   @  s2   e Zd ZdZe Z	 e Z	 e Z	 dddZdS )ExpansionKindz8Describe what kind of expansion the expression performs.r#   rD   c                 C  rh   ra   )rx   MULTI_UNNAMEDrd   r'   r'   r(   rZ      rj   zExpansionKind.is_multi_unnamedNro   )	rp   rq   rr   rs   r   SINGLEMULTI_NAMEDry   rZ   r'   r'   r'   r(   rx      s    rx   rY   GTypeIs[Literal[ExpansionKind.MULTI_NAMED, ExpansionKind.MULTI_UNNAMED]]c                 C  r`   ra   )rx   r{   ry   )rY   r'   r'   r(   is_multi_output   s   r}   c                   @  sN   e Zd ZdZe Z	 e Z	 e Z	 e Z	 dddZ	dddZ
ddd	Zd
S )
WindowKindz5Describe what kind of window the expression contains.r#   rD   c                 C  r`   ra   )r~   UNCLOSEABLE	CLOSEABLErd   r'   r'   r(   is_open   rg   zWindowKind.is_openc                 C  rh   ra   )r~   CLOSEDrd   r'   r'   r(   	is_closed   rj   zWindowKind.is_closedc                 C  rh   ra   )r~   r   rd   r'   r'   r(   is_uncloseable   rj   zWindowKind.is_uncloseableNro   )rp   rq   rr   rs   r   NONEr   r   r   r   r   r   r'   r'   r'   r(   r~      s    

r~   c                   @  s   e Zd ZdZd*d
dZd+ddZd,ddZed-ddZed.ddZ	ed/ddZ
d0ddZd1ddZd0dd Zd0d!d"Zed1d#d$Zed1d%d&Zed1d'd(Zd)S )2ExprMetadata)_expansion_kind_kind_window_kindrv   r_   window_kindr~   rY   rx   r#   Nonec               C  s   || _ || _|| _d S ra   r   r   r   )re   rv   r   rY   r'   r'   r(   __init__   s   
zExprMetadata.__init__argsr   kwdsr   c                O  s   d| j }t|)NzCannot subclass )rp   	TypeError)clsr   r   r8   r'   r'   r(   __init_subclass__   s   zExprMetadata.__init_subclass__rH   c                 C  s   d| j  d| j d| j dS )NzExprMetadata(kind: z, window_kind: z, expansion_kind: )r   rd   r'   r'   r(   __repr__   s   zExprMetadata.__repr__c                 C     | j S ra   )r   rd   r'   r'   r(   rv         zExprMetadata.kindc                 C  r   ra   )r   rd   r'   r'   r(   r      r   zExprMetadata.window_kindc                 C  r   ra   )r   rd   r'   r'   r(   rY      r   zExprMetadata.expansion_kindc                C  s   t || j| jdS )z<Change metadata kind, leaving all other attributes the same.r   rY   )r   r   r   re   rv   r'   r'   r(   	with_kind  
   zExprMetadata.with_kindc                 C  s,   | j tju rd}t|t| jtj| jdS )z:Add uncloseable window, leaving other attributes the same.&Unreachable code, please report a bug.r   )r   r~   r   r7   r   rv   r   r   )re   r8   r'   r'   r(   with_uncloseable_window  s   z$ExprMetadata.with_uncloseable_windowc                C  sB   | j tju r
tj}n| j tju rd}t|tj}t||| jd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   r7   r   r   r   )re   rv   r   r8   r'   r'   r(   with_kind_and_closeable_window  s   z+ExprMetadata.with_kind_and_closeable_windowc                C  s   t |tj| jdS )z8Change metadata kind and set window kind to uncloseable.r   )r   r~   r   r   r   r'   r'   r(    with_kind_and_uncloseable_window(  r   z-ExprMetadata.with_kind_and_uncloseable_windowc                   C     t tjtjtjdS Nr   )r   r_   rb   r~   r   rx   rz   r'   r'   r'   r(   selector_single0  
   zExprMetadata.selector_singlec                   C  r   r   )r   r_   rb   r~   r   rx   r{   r'   r'   r'   r(   selector_multi_named9  r   z!ExprMetadata.selector_multi_namedc                   C  r   r   )r   r_   rb   r~   r   rx   ry   r'   r'   r'   r(   selector_multi_unnamedB  r   z#ExprMetadata.selector_multi_unnamedN)rv   r_   r   r~   rY   rx   r#   r   )r   r   r   r   r#   r   )r#   rH   )r#   r_   )r#   r~   )r#   rx   )rv   r_   r#   r   )r#   r   )rp   rq   rr   	__slots__r   r   r   propertyrv   r   rY   r   r   r   r   staticmethodr   r   r   r'   r'   r'   r(   r      s*    






r   r   IntoExpr | object | Noneallow_multi_outputto_single_outputc                 G  s  d}d}d}d}t j}d}	d}
d}t|D ]|\}}t|tr#| s#d}qt|rt|jjrK|dkr9|s9d}t	||sK|dkrD|jj}nt
||jj}|jj}|tju rWd}n|tju r_d}n|tju ri|d7 }n| rpd}nd}t||jj}|tju rd}
q|tju rd}	q|tju rd}q|r|s|s|stj}n#|dkrd}t||r|rd}t||rtj}n	|rtj}ntj}|
s|	rtj}n	|rtj}ntj}t|||d	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.r1   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   )rx   rz   	enumerater&   rH   r)   r}   rX   rY   r   resolve_expansion_kindrv   r_   ru   rt   rk   rf   r7   r   r~   r   r   r   r   r   rb   r   r   )rC   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argr8   rv   r   result_kindresult_window_kindr'   r'   r(   combine_metadataL  s   







r   lhsrhsc                 C  s4   | 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.)rx   ry   r7   )r   r   r8   r'   r'   r(   r     s   r   r   r   c                 C  s   t | |ddddS )NTFrC   r   r   r   )r   r   r'   r'   r(   combine_metadata_binary_op  s   
r   c                  G  s   t | ddddS )NFTr   r   r3   r'   r'   r(   combine_metadata_horizontal_op  s   r   function_namerH   r   c                   s:   ddl m  t fdd|D sd|  d}t|d S )Nr   Seriesc                 3  s2    | ]}t |r|jj pt|t fV  qd S ra   )r)   rX   rv   rf   r&   rH   rS   rT   r   r'   r(   	<genexpr>  s    
z4check_expressions_preserve_length.<locals>.<genexpr>zBExpressions which aggregate or change length cannot be passed to 'z'.)narwhals.seriesr   allr   )r   r   r8   r'   r   r(   !check_expressions_preserve_length  s   r   kwargsc                  O  s    t | | }tdd |D S )Nc                 s  s$    | ]}t |o|jj V  qd S ra   )r)   rX   rv   rn   r   r'   r'   r(   r     s   " z,all_exprs_are_scalar_like.<locals>.<genexpr>)r   valuesr   )r   r   r*   r'   r'   r(   all_exprs_are_scalar_like  s   r   c                C  s:   t | r| jjS t| st| st| tr|stjS tj	S ra   )
r)   rX   rv   r   r   r&   rH   r_   rb   rt   )r"   rC   r'   r'   r(   
infer_kind  s   r   function
comparandsc                  sX   fdd|D }fdd|D }t dd |D   fddt||D }|| S )Nc                 3  s    | ]
}t  |d V  qdS )rC   N)rO   rS   	comparand)r@   rC   r'   r(   r     s
    
z(apply_n_ary_operation.<locals>.<genexpr>c                   s   g | ]}t | d qS )r   )r   r   r   r'   r(   rW     s    z)apply_n_ary_operation.<locals>.<listcomp>c                 s  s    | ]}|   V  qd S ra   rm   )rS   rv   r'   r'   r(   r     s    c                 3  s6    | ]\}} rt |rt|r||n|V  qd S ra   )r   rn   	broadcast)rS   compliant_exprrv   )r   r'   r(   r     s    
)anyr[   )r@   r   rC   r   compliant_exprskindsr'   )r   r@   rC   r(   apply_n_ary_operation  s   
r   N)r"   r   r#   r$   )r*   r+   r#   r,   )r*   r   r#   r:   )r@   r   rA   rB   rC   rD   r#   rE   )rP   r   r-   r   rQ   r.   r#   rR   )rv   r_   r#   rw   )rY   rx   r#   r|   )
r   r   rC   rD   r   rD   r   rD   r#   r   )r   rx   r   rx   r#   rx   )r   r   r   r   r#   r   )r*   r   r#   r   )r   r   r   rH   r#   r   )r   r   r   r   r#   rD   )r"   rB   rC   rD   r#   r_   )
r@   r   r   r   r   rB   rC   rD   r#   r   )>
__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)   r9   r?   rO   r^   r_   rn   rx   r}   r~   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   sf   





0

k
`
	



