o
    $&]iހ                     @  sX  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rqd 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) dcddZ*ddddZ+deddZ,dfddZ-dgd&d'Z.G d(d) d)eZ/dhd+d,Z0G d-d. d.eZ1G d/d0 d0Z2G d1d2 d2Z3e/j4e3j5e/j6e3j7e/j8e3j9e/j:e3j7e/j;e3j<e/j=e3j>e/j?e3j@e/jAe3jBe/jCe3j7i	ZDd3eEd4< e/j4e3jFe/j8e3jGe/jHe3jIe/jJe3jKe/jLe3jMe/jNe3jOe/jPe3jQe/jRe3jSiZTd5eEd6< did<d=ZUdjdBdCZVdDdEdFdGdkdNdOZWdldTdUZXdmdWdXZYdndZd[ZZdod]d^Z[dpdadbZ\dES )q    )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                 C     ddl m} t| |S )'Check whether `obj` is a Narwhals Expr.r   r   )narwhals.exprr   
isinstance)r!   r    r(   Z/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/_expression_parsing.pyis_expr$      
r*   TypeIs[Series[Any]]c                 C  r$   )r%   r   r   )narwhals.seriesr   r'   )r!   r   r(   r(   r)   	is_series+   r+   r.   exprs#CompliantExpr[CompliantFrameT, Any]EvalNames[CompliantFrameT]c                    s   d fdd}|S )Ndfr   r"   Sequence[str]c                       d  | d d S Nr      )_evaluate_output_names)r2   r/   r(   r)   evaluate_output_names7      z<combine_evaluate_output_names.<locals>.evaluate_output_names)r2   r   r"   r3   r(   )r/   r9   r(   r8   r)   combine_evaluate_output_names2   s   r;   r   AliasNames | Nonec                    s$    d j d u r	d S d fdd}|S )Nr   namesr3   r"   c                   r4   r5   _alias_output_names)r=   r8   r(   r)   alias_output_namesC   r:   z6combine_alias_output_names.<locals>.alias_output_names)r=   r3   r"   r3   r>   )r/   r@   r(   r8   r)   combine_alias_output_names=   s   rA   exprr2   r   excluder3   #tuple[Sequence[str], Sequence[str]]c                   sZ   |  |}| jd u r|n| |} r)| jj r)t fddt||D  \}}||fS )Nc                   s    g | ]\}}| vr||fqS r(   r(   ).0xaliasrC   r(   r)   
<listcomp>T   s
    z5evaluate_output_names_and_aliases.<locals>.<listcomp>)r7   r?   	_metadataexpansion_kindis_multi_unnamedr
   )rB   r2   rC   output_namesaliasesr(   rH   r)   !evaluate_output_names_and_aliasesI   s   


rO   c                   @  s   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dZedddZeddd	Zd
S )ExprKindz6Describe which kind of expression we are dealing with.r"   boolc                 C  s   | t jt jt jt jhv S N)rP   ORDERABLE_WINDOWWINDOWORDERABLE_AGGREGATIONORDERABLE_FILTRATIONselfr(   r(   r)   is_orderable   s   zExprKind.is_orderablec              
   C  s,   | t jt jt jt jt jt jt jt jt j	h	v S rR   )
rP   ALLCOLELEMENTWISEEXCLUDELITERALNTHSELECTORSERIES	WHEN_THENrW   r(   r(   r)   is_elementwise   s   zExprKind.is_elementwisec                 C  s   | t jt jt jhv S rR   )rP   AGGREGATIONr^   rU   rW   r(   r(   r)   is_scalar_like   s
   zExprKind.is_scalar_likeNr"   rQ   )__name__
__module____qualname____doc__r   r^   rd   rU   r\   rS   rT   
FILTRATIONrV   OVERr[   r_   r]   rZ   r`   rb   ra   propertyrY   rc   re   r(   r(   r(   r)   rP   ]   sP    
rP   rQ   c                 C  s   | j jS rR   )rJ   re   )r!   r(   r(   r)   re      s   re   c                   @  sF   e Zd ZdZe Z	 e Z	 e Z	 dddZdddZ	dd
dZ
dS )ExpansionKindz8Describe what kind of expansion the expression performs.r"   rQ   c                 C  s
   | t ju S rR   rn   MULTI_UNNAMEDrW   r(   r(   r)   rL      s   
zExpansionKind.is_multi_unnamedc                 C  s   | t jt jhv S rR   )rn   MULTI_NAMEDrp   rW   r(   r(   r)   is_multi_output   s   zExpansionKind.is_multi_outputother$Literal[ExpansionKind.MULTI_UNNAMED]c                 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.)rn   rp   AssertionError)rX   rs   msgr(   r(   r)   __and__   s   zExpansionKind.__and__Nrf   )rs   rn   r"   rt   )rg   rh   ri   rj   r   SINGLErq   rp   rL   rr   rw   r(   r(   r(   r)   rn      s    

rn   c                   @  s^   e Zd ZdZdddd#ddZd$ddZd%ddZd&ddZd'ddZd(ddZ	d(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.
    F)
str_as_litallow_multi_outputkindrP   namestrr/   IntoExpr | NonNestedLiteralrz   rQ   r{   kwargsr   r"   Nonec               O  s4   || _ || _|| _|| _|| _|| _d | _d | _d S rR   )r|   r}   r/   r   rz   r{   _is_orderable_cached_is_elementwise_cached)rX   r|   r}   rz   r{   r/   r   r(   r(   r)   __init__   s   

zExprNode.__init__c                 C  s   | j dkrddd | jd D }d| dS g }ddd | jD }dd	d | j D }| jr:|| | jrB|| | j  d
d| dS )Ncolz, c                 s      | ]}t |V  qd S rR   r~   rE   rF   r(   r(   r)   	<genexpr>       z$ExprNode.__repr__.<locals>.<genexpr>r=   zcol()c                 s  r   rR   r   r   r(   r(   r)   r      r   c                 s  s"    | ]\}}| d | V  qdS )=Nr(   )rE   keyvaluer(   r(   r)   r      s     ()r}   joinr   r/   itemsappend)rX   r=   arg_str	expr_reprkwargs_reprr(   r(   r)   __repr__   s   


zExprNode.__repr__dict[str, Any]c                 C  s   | j | j| j| j| j| jdS )Nr|   r}   r/   r   rz   r{   r   rW   r(   r(   r)   as_dict  s   zExprNode.as_dictc                 K  s&   | j | j| jg| jR d| ji|S )Nrz   )	__class__r|   r}   r/   rz   )rX   r   r(   r(   r)   _with_kwargs  s   zExprNode._with_kwargs	over_nodeover_node_without_order_byc                 C  s   g }|j d }|j d }| jD ];}t|s|| q|r0tdd |jD r0||| q|rEtdd |jD sE||| q|| q|| _d S )Norder_bypartition_byc                 s      | ]}|  V  qd S rR   rY   rE   	expr_noder(   r(   r)   r         
z9ExprNode._push_down_over_node_in_place.<locals>.<genexpr>c                 s  r   rR   rc   r   r(   r(   r)   r   !  r   )r   r/   r*   r   any_nodes_with_over_nodeall)rX   r   r   r/   over_node_order_byover_node_partition_byrB   r(   r(   r)   _push_down_over_node_in_place  s    



z&ExprNode._push_down_over_node_in_placec                 C  sJ   | j d u r"| jjrd| _ | j S tdd | jD rd| _ | j S d| _ | j S )NTc                 s  ,    | ]}t |rtd d |jD V  qdS )c                 s  r   rR   r   rE   noder(   r(   r)   r   1  r   z2ExprNode.is_orderable.<locals>.<genexpr>.<genexpr>Nr*   r   r   rE   rB   r(   r(   r)   r   0      
z(ExprNode.is_orderable.<locals>.<genexpr>F)r   r|   rY   r   r/   rW   r(   r(   r)   rY   *     
	zExprNode.is_orderablec                 C  sJ   | j d u r"| jjsd| _ | j S tdd | jD rd| _ | j S d| _ | j S )NFc                 s  r   )c                 s  s    | ]}|   V  qd S rR   r   r   r(   r(   r)   r   A      z4ExprNode.is_elementwise.<locals>.<genexpr>.<genexpr>Nr   r   r(   r(   r)   r   @  r   z*ExprNode.is_elementwise.<locals>.<genexpr>T)r   r|   rc   r   r/   rW   r(   r(   r)   rc   :  r   zExprNode.is_elementwiseN)r|   rP   r}   r~   r/   r   rz   rQ   r{   rQ   r   r   r"   r   r"   r~   )r"   r   )r   r   r"   ry   )r   ry   r   ry   r"   r   rf   )rg   rh   ri   rj   r   r   r   r   r   rY   rc   r(   r(   r(   r)   ry      s    




ry   c                   @  sR  e Zd ZdZdZdddddddddXddZdYddZdZd!d"Zd[d$d%Ze	d\d)d*Z
d]d-d.Ze	d^d/d0Ze	d^d1d2Ze	d^d3d4Ze	d^d5d6Ze	d^d7d8Ze	d^d9d:Ze	d^d;d<Ze	d\d=d>Zed_d?d@Zd`dBdCZd`dDdEZd]dFdGZd`dHdIZd`dJdKZd`dLdMZd`dNdOZd`dPdQZd\dRdSZd`dTdUZd[dVdWZ dS )a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_noderK   has_windowsrc   
is_literalre   n_orderable_opspreserves_lengthprevFr   TN)r   r   r   rc   re   r   r   rK   rn   r   rQ   r   intr   rc   re   r   r   ry   r   ExprMetadata | Noner"   r   c          
      C  sF   |r|sJ || _ || _|| _|| _|| _|| _|| _|| _|	| _d S rR   )	rK   r   r   rc   r   re   r   r   r   )
rX   rK   r   r   r   rc   re   r   r   r   r(   r(   r)   r   i  s   
zExprMetadata.__init__argsr   kwdsr   c                O  s   d| j }t|)NzCannot subclass )rg   	TypeError)clsr   r   rv   r(   r(   r)   __init_subclass__  s   zExprMetadata.__init_subclass__r~   c                 C  sX   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_reversedrK   r   r   rc   r   re   r   )rX   nodesr(   r(   r)   r     s&   zExprMetadata.__repr__Iterator[ExprNode]c                 c  s,    | }|dur|j V  |j}|dusdS dS )z/Iterate through all nodes from current to root.Nr   r   )rX   currentr(   r(   r)   r     s   z ExprMetadata.iter_nodes_reversedr   compliant_exprsr   c                 G  s   t |j |g|R  S rR   )KIND_TO_METADATA_CONSTRUCTORr|   r   r   r   r(   r(   r)   	from_node  s   zExprMetadata.from_nodecompliant_exprcompliant_expr_argsc                 G  s   t |j | ||g|R  S rR   )KIND_TO_METADATA_UPDATERr|   rX   r   r   r   r(   r(   r)   	with_node  s
   zExprMetadata.with_nodec                 C  s   | t jddd|d dS )NFT)rc   r   re   r   r   rn   rx   r   r   r(   r(   r)   from_aggregation  s   zExprMetadata.from_aggregationc              	   C  s   | t jdddd|d dS )NTF)rc   r   r   re   r   r   r   r   r(   r(   r)   from_literal  s   zExprMetadata.from_literalc                 C     | t j|d dS Nr   r   r   r(   r(   r)   from_series     zExprMetadata.from_seriesc                 C  ,   t |jd dkr| tj|d dS | |S )Nr=   r6   r   lenr   rn   rx   from_selector_multi_namedr   r(   r(   r)   from_col  s
   zExprMetadata.from_colc                 C  r   )Nindicesr6   r   r   r   r(   r(   r)   from_nth  s
   zExprMetadata.from_nthc                 C  r   r   )rn   rq   r   r(   r(   r)   r        z&ExprMetadata.from_selector_multi_namedc                 C  r   r   ro   r   r(   r(   r)   from_selector_multi_unnamed  r   z(ExprMetadata.from_selector_multi_unnamedc                 G  s   t |d|d dS )NTto_single_outputr   r   )combine_metadatar   r(   r(   r)   from_elementwise  s   zExprMetadata.from_elementwisec                 C  s   | j  o| j S rR   )r   re   rW   r(   r(   r)   is_filtration  r   zExprMetadata.is_filtration_cec                 C  s2   | j r	d}t|t| j| j| jdddd|| d	S )N4Can't apply aggregations to scalar-like expressions.FTr   r   r   rc   re   r   r   r   re   r   r   rK   r   r   rX   r   r   rv   r(   r(   r)   with_aggregation  s   zExprMetadata.with_aggregationc                 C  s6   | j r	d}t|t| j| j| jd dddd|| d	S )Nr   r6   FTr   r   r   r(   r(   r)   with_orderable_aggregation  s   z'ExprMetadata.with_orderable_aggregationc                 G  s   t |g|R d||jdS )NFr   )r   rJ   r   r(   r(   r)   with_elementwise  s   zExprMetadata.with_elementwisec                 C  s4   | j r	d}t|t| j| j| j| jddd|| d	S )Nz;Can't apply window (e.g. `rank`) to scalar-like expression.Fr   re   r   r   rK   r   r   r   r   r(   r(   r)   with_window  s   zExprMetadata.with_windowc                 C  s8   | j r	d}t|t| j| j| jd | jddd|| d	S )NzNCan't apply orderable window (e.g. `diff`, `shift`) to scalar-like expression.r6   Fr   r   r   r(   r(   r)   with_orderable_window2  s   z"ExprMetadata.with_orderable_windowc                 C  s   | j r	d}t|| js| jrd}t|| j}|s*t|  jtj	ur*d}t|t|  jj
r:|dkr:|d8 }t| jd|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   r6   TFr   )r   r   rc   r   r   nextop_nodes_reversedr|   rP   rT   rY   r   rK   )rX   r   r   rv   r   r(   r(   r)   with_ordered_overE  s6   	zExprMetadata.with_ordered_overc                 C  sH   | j r	d}t|| js| jrd}t|t| jd| jdddd|| d	S )Nr   r   TFr   )r   r   rc   r   r   rK   r   r   r(   r(   r)   with_partitioned_overl  s$   z"ExprMetadata.with_partitioned_overc                 C  s8   |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   r(   r(   r)   	with_over  s   

zExprMetadata.with_overc                 G  sR   | j r	d}t|tdd |D }tdd |D }t| j||dddd|| d	S )NECan't apply filtration (e.g. `drop_nulls`) to scalar-like expression.c                 s      | ]}|j jV  qd S rR   )rJ   r   r   r(   r(   r)   r     r   z/ExprMetadata.with_filtration.<locals>.<genexpr>c                 s  r   rR   )rJ   r   r   r(   r(   r)   r     r   Fr   )re   r   r   sumr   rK   )rX   r   r   rv   result_has_windowsresult_n_orderable_opsr(   r(   r)   with_filtration  s    zExprMetadata.with_filtrationc                 C  s6   | j r	d}t|t| j| j| jd dddd|| d	S )Nr   r6   Fr   r   r   r(   r(   r)   with_orderable_filtration  s   z&ExprMetadata.with_orderable_filtrationc                 c  s(    |   D ]}|jdrq|V  qd S )N)zname.rG   )r   r}   
startswith)rX   r   r(   r(   r)   r     s   zExprMetadata.op_nodes_reversed)rK   rn   r   rQ   r   r   r   rQ   rc   rQ   re   rQ   r   rQ   r   ry   r   r   r"   r   )r   r   r   r   r"   r   r   )r"   r   )r   ry   r   r   r"   r   )r   ry   r   r   r   r   r"   r   )r   ry   r"   r   rf   )r   ry   r   r   r"   r   )!rg   rh   ri   rj   	__slots__r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   rm   r   r   r   r   r   r   r   r   r   r  r  r   r(   r(   r(   r)   r   K  s\    












'


r   z2dict[ExprKind, Callable[[ExprNode], ExprMetadata]]r   z+dict[ExprKind, Callable[..., ExprMetadata]]r   r   r   r   r   r   c                 G  s   d}t j}d}d}d}d}	d}
d}t|D ]F\}}|j}|dus"J |j r6|j}| s6|dkr4||@ n|}||jO }||j7 }||jO }|	|j	M }	|
|j
M }
||jM }|t|j7 }q|dkrfd}t||rp|rpd}t|t|||||	|
|||d	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   FTNr6   zXLength-changing expressions can only be used in isolation, or followed by an aggregationzVCannot combine length-changing expressions with length-preserving ones or aggregationsr   )rn   rx   	enumeraterJ   rK   rr   r   r   r   rc   re   r   r   r   r   r   )r   r   r   r   n_filtrationsresult_expansion_kindr   r   result_preserves_lengthresult_is_elementwiseresult_is_scalar_likeresult_is_literalicemetadatarK   rv   r(   r(   r)   r     sP   






r   r   function_namer~   r   c                 G  s*   t dd |D sd|  d}t|d S )Nc                 s  r   rR   )rJ   r   r   r(   r(   r)   r     r   z4check_expressions_preserve_length.<locals>.<genexpr>zBExpressions which aggregate or change length cannot be passed to 'z'.)r   r   )r  r   rv   r(   r(   r)   !check_expressions_preserve_length  s   r  FNT)rz   backendallow_literalarg&IntoExpr | NonNestedLiteral | _1DArrayrz   r  r  r   c                C  sz   ddl m}m}m} t| tr|s|| S t| r"|d| |d S t| r*|  S t	| r0| S |s9t
t| || S )Nr   )r   lit
new_series )r  )narwhals.functionsr   r  r  r'   r~   r   _to_exprr.   r*   r   from_invalid_typetype)r  rz   r  r  r   r  r  r(   r(   r)   _parse_into_expr  s   r  nsr   r{   Iterator[CompliantExprAny]c                 g  sH    |D ]}t ||| jd| }|s|jj rd}t||V  qd S )N)rz   r  z9Multi-output expressions are not allowed in this context.)r  _implementation_to_compliant_exprrJ   rK   rr   r   )r  rz   r{   r/   rB   retrv   r(   r(   r)   evaluate_into_exprs4  s   r#  list[CompliantExprAny]c                  G  sV   t dd | D }g }| D ]}|r#t|r#| }|j|_|| q|| q|S )Nc                 s  s    | ]}t | V  qd S rR   )re   )rE   r  r(   r(   r)   r   E  r   z&maybe_broadcast_ces.<locals>.<genexpr>)r   re   	broadcastrJ   _opt_metadatar   )r   r%  resultsr   _compliant_exprr(   r(   r)   maybe_broadcast_cesD  s   r)  r   c                 C  s   | j dv rt|| j | jd  }g }n5d| 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   rC   r=   .r  rz   r{   r   )r}   getattrr   splitr)  r#  r/   rz   r{   r	   r   r   r&  )r   r  r  cesmodulemethodfuncmdr(   r(   r)   evaluate_root_nodeR  s&   

r3  r   c           	      C  s   | j }t| gt|j||j|jdR  ^} }|j|| g|R  }d|jv r7|jd\}}t	t	| ||}nt	| |j}t
d||i |j}||_|S )Nr+  r*  r   )rJ   r)  r#  r/   rz   r{   r   r}   r-  r,  r	   r   r&  )	r   r   r  r2  r   accessorr0  r1  r"  r(   r(   r)   evaluate_nodel  s&   	
r5  r   Sequence[ExprNode]c                 C  s0   t | d |}| dd  D ]}t|||}q|S r5   )r3  r5  )r   r  r  r   r(   r(   r)   evaluate_nodes  s   r7  )r!   r   r"   r#   )r!   r   r"   r,   )r/   r0   r"   r1   )r/   r   r"   r<   )rB   r   r2   r   rC   r3   r"   rD   )r!   r   r"   rQ   )
r   r   r   rQ   r   ry   r   r   r"   r   )r   r   r  r~   r"   r   )
r  r  rz   rQ   r  r   r  rQ   r"   r   )
r/   r  r  r   rz   rQ   r{   rQ   r"   r  )r   r   r"   r$  )r   ry   r  r   r"   r   )r   r   r   ry   r  r   r"   r   )r   r6  r  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;   rA   rO   rP   re   rn   ry   r   rd   r   rZ   r   r\   r   r]   ra   r   r[   r   r^   r   r_   r   r`   r   __annotations__r   r   rk   r  rU   r   rV   r  rl   r   rS   r   rT   r   r   r   r  r  r#  r)  r3  r5  r7  r(   r(   r(   r)   <module>   sp   





Wx  r

@



