L iހ6UddlmZddlmZmZddlmZmZmZm Z m Z ddl m Z ddl mZddlmZmZmZer.evaluate_output_names7sQx..r22A66r))r2rreturn Sequence[str])r3r4s` r'combine_evaluate_output_namesr82s 7 ! r)c4djydfd }|S)Nrc2dj|ddSr/_alias_output_names)namesr3s r'alias_output_namesz6combine_alias_output_names..alias_output_namesCsQx++E22A66r))r=r6r5r6r;)r3r>s` r'combine_alias_output_namesr?=s% Qx##+7 r)c|j|}|j|n|j|}|rR|jjj r.t t ||Dcgc] \}}||vr||fc}}\}}||fScc}}wN)r1r< _metadataexpansion_kindis_multi_unnamedr )exprr2exclude output_namesaliasesxaliass r'!evaluate_output_names_and_aliasesrKIs..r2L  # # +   % %l 3 4>>00AAC *!+< AAuG#E !  g   s+B cFeZdZdZeZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eZ eddZeddZeddZy)ExprKindz6Describe which kind of expression we are dealing with.c|tjtjtjtjhvSrA)rMORDERABLE_WINDOWWINDOWORDERABLE_AGGREGATIONORDERABLE_FILTRATIONselfs r' is_orderablezExprKind.is_orderables8  % % OO  * *  ) )    r)c |tjtjtjtjtj tj tjtjtjh vSrA) rMALLCOL ELEMENTWISEEXCLUDELITERALNTHSELECTORSERIES WHEN_THENrSs r'is_elementwisezExprKind.is_elementwises_ LL LL        LL    OO      r)cd|tjtjtjhvSrA)rM AGGREGATIONr[rQrSs r'is_scalar_likezExprKind.is_scalar_likes/      * *   r)Nr5bool)__name__ __module__ __qualname____doc__rr[rbrQrYrOrP FILTRATIONrROVERrXr\rZrWr]r_r^propertyrUr`rcr7r)r'rMrM]s@fG&KY FX&KgvH VFNJG6= 6D5 &C( &C(fG, &C(vH>IP VF3         r)rMc.|jjSrA)rBrc)r&s r'rcrcs == ' ''r)cXeZdZdZeZ eZ eZ ddZddZ ddZ y) ExpansionKindz8Describe what kind of expansion the expression performs.c&|tjuSrAro MULTI_UNNAMEDrSs r'rDzExpansionKind.is_multi_unnameds}2222r)cF|tjtjhvSrA)ro MULTI_NAMEDrrrSs r'is_multi_outputzExpansionKind.is_multi_outputs 11=3N3NOOOr)c|tjur"|tjurtjSd|d|d}t|)Nz+Unsupported ExpansionKind combination, got z and z, please report a bug.)rorrAssertionError)rTothermsgs r'__and__zExpansionKind.__and__sI =.. .5Mz$ExprNode.__repr__..sCc!fCr=zcol()c32K|]}t|ywrArrs r'rz$ExprNode.__repr__..s9c!f9rc30K|]\}}|d|yw)=Nr7)rkeyvalues r'rz$ExprNode.__repr__..sWZS%3%q 0Ws()rjoinrr3itemsappend)rTr=arg_str expr_repr kwargs_reprs r'__repr__zExprNode.__repr__s 99 IICdkk'.BCCE%? "II9djj99 iiW4;;CTCTCVWW :: NN9 % ;; NN; '))Adii0133r)c|j|j|j|j|j|j dS)Nrrr3rr~rrrSs r'as_dictzExprNode.as_dicts:IIIIZZkk//"&"9"9   r)c |j|j|jg|jd|ji|S)Nr~) __class__rrr3r~)rTrs r' _with_kwargszExprNode._with_kwargs sBt~~ IItyy #':: :>// MS  r)cg}|jd}|jd}|jD]}t|s|j| |r=t d|j Dr!|j|j |_|r=td|j Ds!|j|j ||j|||_y)Norder_by partition_byc3<K|]}|jywrArUr expr_nodes r'rz9ExprNode._push_down_over_node_in_place..s,-6 &&(,c3<K|]}|jywrAr`rs r'rz9ExprNode._push_down_over_node_in_place..!s4/8 ((*4r)rr3r(rany_nodes_with_over_nodeall)rT over_nodeover_node_without_order_byr3over_node_order_byover_node_partition_byrEs r'_push_down_over_node_in_placez&ExprNode._push_down_over_node_in_places46'--j9!*!1!1.!AJJ #D4= T"#,:>++,) T11)<='4<@KK41 T112LMN T" # r)c|j_|jjrd|_|jStd|jDrd|_|jSd|_|jS)NTc3jK|]+}t|rtd|jD-yw)c3<K|]}|jywrArrnodes r'rz2ExprNode.is_orderable...1s@DD%%'@rNr(rrrrEs r'rz(ExprNode.is_orderable..0s.4=@DKK@@13F)rrrUrr3rSs r'rUzExprNode.is_orderable*s|  $ $ ,yy%%,0)((( JJ -1)(((-2)(((r)c|j_|jjsd|_|jStd|jDrd|_|jSd|_|jS)NFc3jK|]+}t|rtd|jD-yw)c3>K|]}|j ywrArrs r'rz4ExprNode.is_elementwise...AsF$++--FsNrrs r'rz*ExprNode.is_elementwise..@s.4=F$++FFrT)rrr`rr3rSs r'r`zExprNode.is_elementwise:s|  & & .99++.3+*** JJ /4+***/3+***r)N)rrMrrr3zIntoExpr | NonNestedLiteralr~rerrerrr5Noner5r)r5zdict[str, Any])rrr5r})rr}rr}r5rrd) rfrgrhrirrrrrrUr`r7r)r'r}r}s $!#(888 , 8  8!88 8* 4   !?G 0) +r)r}ceZdZdZdZdddddddd d"dZd#d Zd$d Zd%d Ze d&d Z d'd Z e d(dZ e d(dZ e d(dZe d(dZe d(dZe d(dZe d(dZe d&dZed)dZd*dZ d*dZ d'dZd*dZ d*dZd*dZ d*dZd*dZ d&dZ d*d Zd%d!Z y)+ ExprMetadataaIExpression 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_noderC has_windowsr` is_literalrcn_orderable_opspreserves_lengthprevFrTN)rrrr`rcrrc|r|sJ||_||_||_||_||_||_||_||_| |_yrA) rCrrr`rrcrrr) rTrCrrrr`rcrrrs r'rzExprMetadata.__init__isU ! !>-;!,$3$2&6$2 *&2)- r)c6d|j}t|)NzCannot subclass )rf TypeError)clsargskwdsrys r'__init_subclass__zExprMetadata.__init_subclass__s  01nr)cttt|j}d|jd|jd|j d|j d|jd|jd|jd|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, )) tuplereversediter_nodes_reversedrCrrr`rrcr)rTnodess r'rzExprMetadata.__repr__shuT%=%=%?@AB!!%!4!4 56"../0""&"6"6!78!!%!4!4 56##'#8#8"9:!!%!4!4 56!__-.w  r)c#PK|}||j|j}|yyw)z/Iterate through all nodes from current to root.Nrr)rTcurrents r'rz ExprMetadata.iter_nodes_reverseds.'+!&& &llG!s!&&c4t|j|g|SrA)KIND_TO_METADATA_CONSTRUCTORrrrcompliant_exprss r' from_nodezExprMetadata.from_nodes,DII6tNoNNr)c8t|j|||g|SrA)KIND_TO_METADATA_UPDATERrrTrcompliant_exprcompliant_expr_argss r' with_nodezExprMetadata.with_nodes) ( 2 $ )<  r)c:|tjddd|dS)NFT)r`rrcrrror{rrs r'from_aggregationzExprMetadata.from_aggregations'   "   r)c <|tjdddd|dS)NTF)r`rrrcrrrrs r' from_literalzExprMetadata.from_literals*  "  r)c4|tj|dSNrrrs r' from_serieszExprMetadata.from_seriess=''dFFr)ct|jddk(r|tj|dS|j |S)Nr=r0rlenrror{from_selector_multi_namedrs r'from_colzExprMetadata.from_colsH 4;;w'(A-  $$4d C ..t4 r)ct|jddk(r|tj|dS|j |S)Nindicesr0rrrs r'from_nthzExprMetadata.from_nthsH4;;y)*a/  $$4d C ..t4 r)c4|tj|dSr)rortrs r'rz&ExprMetadata.from_selector_multi_nameds=,,4dKKr)c4|tj|dSrrqrs r'from_selector_multi_unnamedz(ExprMetadata.from_selector_multi_unnameds=..TMMr)ct|d|ddS)NTto_single_outputrr)combine_metadatars r'from_elementwisezExprMetadata.from_elementwises t$T  r)c:|j xr |j SrA)rrcrSs r' is_filtrationzExprMetadata.is_filtrations(((D1D1D-DDr)c |jr d}t|t|j|j|j dddd|| S)N4Can't apply aggregations to scalar-like expressions.FTrrrr`rcrrrrcrrrCrrrTr_cerys r'with_aggregationzExprMetadata.with_aggregationsU   HC', ,   (( 00"   r)c |jr d}t|t|j|j|j dzdddd|| S)Nrr0FTrrrs r'with_orderable_aggregationz'ExprMetadata.with_orderable_aggregations\   HC', ,   (( 0014"   r)c8t|g|d||jdS)NFr)rrBrs r'with_elementwisezExprMetadata.with_elementwises2    #))   r)c |jr d}t|t|j|j|j |j ddd|| S)Nz;Can't apply window (e.g. `rank`) to scalar-like expression.FrrcrrrCrrrrs r' with_windowzExprMetadata.with_windows]   OC', ,   ((!00!22    r)c |jr d}t|t|j|j|j dz|j ddd|| S)NzNCan't apply orderable window (e.g. `diff`, `shift`) to scalar-like expression.r0Frr rs r'with_orderable_windowz"ExprMetadata.with_orderable_window2sb   bC', ,   (( 0014!22    r)c |jr d}t||js |jr d}t||j}|s@t |j jtjur d}t|t |j jjr |dkDr|dz}t|jd|dddd|| S) NCannot nest `over` statements.oCannot use `over` on expressions which are elementwise (e.g. `abs`) or which change length (e.g. `drop_nulls`).aPCannot 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` rr0TFr) rrr`rrnextop_nodes_reversedrrMrPrUrrC)rTrrryrs r'with_ordered_overzExprMetadata.with_ordered_overEs   2C', ,   $"4"4K (, ,..T++-.338??JK (, , &&( ) . . ; ;RS@S q O   +!    r)c |jr d}t||js |jr d}t|t |j d|j dddd|| S)NrrTFr)rrr`rrrCrrs r'with_partitioned_overz"ExprMetadata.with_partitioned_overlsz   2C', ,   $"4"4K (, ,    00!    r)c|jdr|j||S|jds d}t||j||S)Nrrz?At least one of `partition_by` or `order_by` must be specified.)rrrrrs r' with_overzExprMetadata.with_oversP ;;z "))$4 4{{>*SC', ,))$44r)c |jr d}t|td|D}td|D}t |j ||dddd|| S)NECan't apply filtration (e.g. `drop_nulls`) to scalar-like expression.c3HK|]}|jjywrA)rBrrs r'rz/ExprMetadata.with_filtration..s RQ!8!8 R "c3HK|]}|jjywrA)rBrrs r'rz/ExprMetadata.with_filtration..s$ZQQ[[%@%@$ZrFr)rcrrsumrrC)rTrrryresult_has_windowsresult_n_orderable_opss r'with_filtrationzExprMetadata.with_filtrationsm   YC', , R/ RR!$$Z/$Z!Z   *2"    r)c |jr d}t|t|j|j|j dzdddd|| S)Nrr0Frrrs r'with_orderable_filtrationz&ExprMetadata.with_orderable_filtrations\   YC', ,   (( 0014"    r)c#vK|jD]"}|jjdr|$yw)N)zname.rJ)rr startswith)rTrs r'rzExprMetadata.op_nodes_reverseds9,,. Dyy##$67J  s79)rCrorrerintrrer`rercrerrerr}rExprMetadata | Noner5r)rrrrr5rr)r5zIterator[ExprNode])rr}rrr5r)rr}rrrrr5r)rr}r5rrd)rr}rrr5r)!rfrgrhri __slots__rrrr classmethodrrrrrrrrrrrlrrr r rrrrrr#r%rr7r)r'rrKs" I " !%#$ $(.%. .  .  .....". .2  #OO/?O OO   ) /          GG    LLNN  /?   EE   #3  &    )  /     &  #3  &% N  #3  05  0@  (  #3  $r)rz2dict[ExprKind, Callable[[ExprNode], ExprMetadata]]rz+dict[ExprKind, Callable[..., ExprMetadata]]rc >d}tj}d}d}d}d} d} d} t|D]\} } | j}|J|jj r|j}|s | dkDr||zn|}||j z}||jz }||jz}| |jz} | |jz} | |jz} |t|jz }|dkDr d}t||r|r d}t|t||||| | | || S)aICombine 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. rFTr0zXLength-changing expressions can only be used in isolation, or followed by an aggregationzVCannot combine length-changing expressions with length-preserving ones or aggregationsr)ror{ enumeraterBrCrurrrr`rcrr(rrr)rrrr n_filtrationsresult_expansion_kindr!r"result_preserves_lengthresult_is_elementwiseresult_is_scalar_likeresult_is_literalicemetadatarCrys r'rrspM)00#  ?+52<<###  " " 2 2 4%44N#>?!e)N:& h222(":"::8#<#<<!8!88!8!88X000X3344 !5"qh#C((=f#C(( &.0,,$!   r)cJtd|Dsd|d}t|y)Nc3HK|]}|jjywrA)rBrrs r'rz4check_expressions_preserve_length..s:q{{++:rzBExpressions which aggregate or change length cannot be passed to 'z'.)rr) function_namerrys r'!check_expressions_preserve_lengthr:s3 :T: :RS`Raacd#C(( ;r)FNT)r~backend allow_literalc2ddlm}m}m}t |t r |s||St |r|d||jSt|r|jSt|r|S|stjt|||S)Nr)rlit new_series)r;) narwhals.functionsrr>r?r%rr _to_exprr,r(r from_invalid_typetype)argr~r;r<rr>r?s r'_parse_into_exprrFs87#sJ3x"c73<<>>~||~s| "44T#Y?? s8Or)c'K|D]`}t|||jj|}|s1|jjj r d}t ||byw)N)r~r;z9Multi-output expressions are not allowed in this context.)rF_implementation_to_compliant_exprrBrCrur)nsr~rr3rEretrys r'evaluate_into_exprsrL4sk  Z1C1C  R  "cmm&B&B&R&R&TMC,S1 1 sA'A)ctd|D}g}|D]S}|r>t|r3|j}|j|_|j |C|j |U|S)Nc34K|]}t| ywrA)rc)rr5s r'rz&maybe_broadcast_ces..EsErr**Es)rrc broadcastrB _opt_metadatar)rrOresultsr_compliant_exprs r'maybe_broadcast_cesrSDskE_EEI&(G)+ 70>0H0H0JO,:,D,DO ) NN? + NN> *+ Nr)c |jdvr)t||j|jd}g}nd|jvr5|jjd\}}tt|||}nt||j}t t |j ||j|jd}td||i|j}tj|g|}||_ |S)N>rrFr=.rJr~rr) rgetattrrsplitrSrLr3r~rr rrrP)rrJr5cesmodulemethodfuncmds r'evaluate_root_noder^Rs yy&&$WR #T[[%9 : $)) !YY__S1NFF72v.7D2tyy)D! ??#'#:#:  $dC&?4;;&? @    +s +BB Ir)c |j}t|gt|j||j|j d^}}|j ||g|}d|jvr5|jjd\}}tt|||}nt||j}td||i|j}||_ |S)NrVrUr) rBrSrLr3r~rrrrXrWr rrP) rrrJr]raccessorr[r\rKs r' evaluate_noderals&//B+>,  ZZ#66  ,(N( dN A-@ AB dii99??3/&w~x8&A~tyy1 !4)<#L #L MCC Jr)cRt|d|}|ddD]}t|||}|Sr/)r^ra)rrJr5rs r'evaluate_nodesrcs< E!Hb )Bab ) 2tR () Ir))r&rr5z TypeIs[Expr])r&rr5zTypeIs[Series[Any]])r3z#CompliantExpr[CompliantFrameT, Any]r5zEvalNames[CompliantFrameT])r3rr5zAliasNames | None)rErr2rrFr6r5z#tuple[Sequence[str], Sequence[str]])r&rr5re) rrrrerr}rr)r5r)rrr9rr5r) rE&IntoExpr | NonNestedLiteral | _1DArrayr~rer;rr<rer5r) r3rdrJrr~rerrer5zIterator[CompliantExprAny])rrr5zlist[CompliantExprAny])rr}rJrr5r)rrrr}rJrr5r)rzSequence[ExprNode]rJrr5r)] __future__renumrrtypingrrrr r narwhals._utilsr narwhals.dependenciesr narwhals.exceptionsr rrcollections.abcrrtyping_extensionsrrnarwhals._compliantrrnarwhals._compliant.typingrrrrrr$rr+rnarwhals.typingrr r!r(r,r8r?rKrMrcror}rrbrrWrrYrrZr^rrXrr[rr\rr]r__annotations__rr rjr#rQr rRr%rkrrOrrPrrrr:rFrLrSr^rarcr7r)r'rqs #>>&3 2/B#&DD!#! /!! ! ! 1!> OO\-- LL,'' l// LL,'' |?? TP  ,77 ,77 55 ""L$K$K !!<#I#I MM<)) |AA OO\-- IE =&===  =  =@ )  ),/ )  )  /     ,  2       4$,4:O0 #8r)