
    -PhS(                    r   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ er$d dlmZ d dlmZ d dlmZ d dlm Z m!Z! d dl"m#Z#m$Z$m%Z%  G d deeedf                   Z& G d deedef                   Z' G d deedef         e          Z(dS )    )annotationsN)reduce)TYPE_CHECKINGCallableIterableSequence)LazyNamespaceLazyThenLazyWhen)combine_alias_output_namescombine_evaluate_output_namesSparkLikeLazyFrameSparkLikeExpr)SparkLikeSelectorNamespace)import_functionsimport_native_dtypesnarwhals_to_native_dtype)Column)SQLFrameDataFrame)SparkWindowInputs)ImplementationVersion)ConcatMethod	IntoDTypeNonNestedLiteralc                      e Zd Zd2d	Zed3d            Zed4d            Zed5d            Zed             Zed             Z	d6dZ
d7dZd8dZd9dZd9dZd9dZd9d Zd9d!Zd9d"Zd:d(Zd;d-Zd<d0Zd1S )=SparkLikeNamespacebackend_versiontuple[int, ...]versionr   implementationr   returnNonec               0    || _         || _        || _        d S N)_backend_version_version_implementation)selfr    r"   r#   s       ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/narwhals/_spark_like/namespace.py__init__zSparkLikeNamespace.__init__!   s!     !0-    r   c                *    t          j        |           S r'   )r   from_namespacer+   s    r,   	selectorszSparkLikeNamespace.selectors,   s    )8>>>r.   type[SparkLikeExpr]c                    t           S r'   r   r1   s    r,   _exprzSparkLikeNamespace._expr0       r.   type[SparkLikeLazyFrame]c                    t           S r'   r   r1   s    r,   
_lazyframezSparkLikeNamespace._lazyframe4   s    !!r.   c                H    t           rddlm} |S t          | j                  S )Nr   )	functions)r   sqlframe.baser;   r   r*   )r+   r;   s     r,   _FzSparkLikeNamespace._F8   s3     	://////#D$8999r.   c                H    t           rddlm} |S t          | j                  S )Nr   )types)r   r<   r?   r   r*   )r+   r?   s     r,   _native_dtypesz!SparkLikeNamespace._native_dtypesA   s2     	>++++++L'(<===r.   func$Callable[[Iterable[Column]], Column]exprsr   c           	         d
fd}dfd}|                      ||t           t           | j        | j        | j        	          S )Ndfr   r$   list[Column]c                :      fdD             } |          gS )Nc              3  8   K   | ]} |          D ]}|V  d S r'    ).0r5   colrE   s      r,   	<genexpr>zESparkLikeNamespace._with_elementwise.<locals>.call.<locals>.<genexpr>N   s8      @@EeeBii@@sC@@@@@@@r.   rI   )rE   colsrC   rA   s   ` r,   callz2SparkLikeNamespace._with_elementwise.<locals>.callM   s-    @@@@U@@@DDJJ<r.   window_inputsr   c                >      fdD             } |          gS )Nc              3  N   K   | ]}|                               D ]}|V   d S r'   )window_function)rJ   r5   rK   rE   rO   s      r,   rL   zPSparkLikeNamespace._with_elementwise.<locals>.window_function.<locals>.<genexpr>T   sX        %2G2GM2Z2Z +.      r.   rI   )rE   rO   rM   rC   rA   s   `` r,   rR   z=SparkLikeNamespace._with_elementwise.<locals>.window_functionQ   sD        !&  D DJJ<r.   )rN   rR   evaluate_output_namesalias_output_namesr    r"   r#   rE   r   r$   rF   )rE   r   rO   r   r$   rF   r5   r   r   r(   r)   r*   )r+   rA   rC   rN   rR   s    ``  r,   _with_elementwisez$SparkLikeNamespace._with_elementwiseJ   s    	  	  	  	  	  	  	 	  	  	  	  	  	  	  zz+"?"G95A 1M/  
 
 	
r.   valuer   dtypeIntoDType | Nonec                n     d fd}                      |d d  j         j         j                  S )	NrE   r   r$   rF   c                    | j                                       }r1t          j        | j                  }|                    |          }|gS )N)r"   spark_types)r=   litr   r)   r@   cast)rE   columnnative_dtyperY   r+   rX   s      r,   _litz$SparkLikeNamespace.lit.<locals>._litd   sX    UYYu%%F 374=b>O       \228Or.   c                    dgS )NliteralrI   _dfs    r,   <lambda>z(SparkLikeNamespace.lit.<locals>.<lambda>p   s    yk r.   rN   rS   rT   r    r"   r#   rU   r5   r(   r)   r*   )r+   rX   rY   rb   s   ``` r,   r^   zSparkLikeNamespace.litc   sg    	 	 	 	 	 	 	 	 zz"9"9# 1M/  
 
 	
r.   c                `    dd}|                      |d d | j        | j        | j                  S )	NrE   r   r$   rF   c                8    | j                             d          gS )N*)r=   count)rE   s    r,   rA   z$SparkLikeNamespace.len.<locals>.funcx   s    EKK$$%%r.   c                    dgS )NlenrI   re   s    r,   rg   z(SparkLikeNamespace.len.<locals>.<lambda>}   s    ug r.   )rS   rT   r    r"   r#   rU   ri   )r+   rA   s     r,   ro   zSparkLikeNamespace.lenw   sP    	& 	& 	& 	& zz"5"5# 1M/  
 
 	
r.   c                &    dd} | j         |g|R  S )NrM   Iterable[Column]r$   r   c                6    t          t          j        |           S r'   )r   operatorand_rM   s    r,   rA   z/SparkLikeNamespace.all_horizontal.<locals>.func   s    (-...r.   rM   rq   r$   r   rW   r+   rC   rA   s      r,   all_horizontalz!SparkLikeNamespace.all_horizontal   s3    	/ 	/ 	/ 	/ &t%d3U3333r.   c                &    dd} | j         |g|R  S )NrM   rq   r$   r   c                6    t          t          j        |           S r'   )r   rs   or_ru   s    r,   rA   z/SparkLikeNamespace.any_horizontal.<locals>.func   s    (,---r.   rv   rw   rx   s      r,   any_horizontalz!SparkLikeNamespace.any_horizontal   s3    	. 	. 	. 	. &t%d3U3333r.   c                ,     d fd}  j         |g|R  S )NrM   rq   r$   r   c                "     j         j        |  S r'   )r=   greatestrM   r+   s    r,   rA   z/SparkLikeNamespace.max_horizontal.<locals>.func   s    #47#T**r.   rv   rw   rx   s   `  r,   max_horizontalz!SparkLikeNamespace.max_horizontal   s>    	+ 	+ 	+ 	+ 	+ 	+ &t%d3U3333r.   c                ,     d fd}  j         |g|R  S )NrM   rq   r$   r   c                "     j         j        |  S r'   )r=   leastr   s    r,   rA   z/SparkLikeNamespace.min_horizontal.<locals>.func   s     47=$''r.   rv   rw   rx   s   `  r,   min_horizontalz!SparkLikeNamespace.min_horizontal   s>    	( 	( 	( 	( 	( 	( &t%d3U3333r.   c                ,     d fd}  j         |g|R  S )NrM   rq   r$   r   c                P    t          t          j        fd| D                       S )Nc              3  ~   K   | ]7}j                             |j                             d                     V  8dS r   Nr=   coalescer^   rJ   rK   r+   s     r,   rL   zBSparkLikeNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>   sA      UUtw//TW[[^^DDUUUUUUr.   )r   rs   addr   s    r,   rA   z/SparkLikeNamespace.sum_horizontal.<locals>.func   s2    UUUUPTUUU  r.   rv   rw   rx   s   `  r,   sum_horizontalz!SparkLikeNamespace.sum_horizontal   s>    	 	 	 	 	 	
 &t%d3U3333r.   c                0     d fd}  j         |gR  S )NrM   rq   r$   r   c                   t          |           } d         j        }t          |dt          j                  } |t          t          j        fd| D                       t          t          j        fd| D                                 S )Nr   
try_dividec              3  ~   K   | ]7}j                             |j                             d                     V  8dS r   r   r   s     r,   rL   zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   sA      "Y"YS47#3#3CQ#H#H"Y"Y"Y"Y"Y"Yr.   c              3     K   | ]B}|                                                     j                                                  V  Cd S r'   )	isNotNullr_   r@   IntegerTyper   s     r,   rL   zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s\         ,,T-@-L-L-N-NOO     r.   )listr=   getattrrs   truedivr   r   )rM   FdividerC   r+   s      r,   rA   z0SparkLikeNamespace.mean_horizontal.<locals>.func   s    ::DaAQh.>??F6L"Y"Y"Y"YTX"Y"Y"Y  L   #'   	  r.   rv   rw   rx   s   `` r,   mean_horizontalz"SparkLikeNamespace.mean_horizontal   sD    	 	 	 	 	 	 	$ &t%d3U3333r.   itemsIterable[SparkLikeLazyFrame]howr   r   c                  d |D             }|dk    r|d         j         }t          |dd          d          D ]M\  }}|j         }t          |          t          |          k    r||k    sd| d| d| d	}t          |          Nt	          t          d
 |          | j        | j        | j                  S |dk    r1t	          t          d |          | j        | j        | j                  S t          )Nc                    g | ]	}|j         
S rI   )_native_frame)rJ   items     r,   
<listcomp>z-SparkLikeNamespace.concat.<locals>.<listcomp>   s    444dt!444r.   verticalr      )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
c                ,    |                      |          S r'   )unionxys     r,   rg   z+SparkLikeNamespace.concat.<locals>.<lambda>   s    QWWQZZ r.   )native_dataframer    r"   r#   diagonalc                0    |                      |d          S )NT)allowMissingColumns)unionByNamer   s     r,   rg   z+SparkLikeNamespace.concat.<locals>.<lambda>   s    qd!K!K r.   )
columns	enumeratero   	TypeErrorr   r   r(   r)   r*   NotImplementedError)	r+   r   r   dfscols_0irE   cols_currentmsgs	            r,   concatzSparkLikeNamespace.concat   sb    54e444*V^F"3qrr7!444 ) )2!z\**c&kk99PV@V@V@-3@ @*+@ @/;@ @ @ 
 $C..( AW &!'(?(?!E!E $ 5#3	    *%!'KKS" " !% 5#3    "!r.   	separatorstrignore_nullsboolc                   dfd}|                      |t           t           | j        | j        | j                  S )NrE   r   r$   rF   c                     fd	D             } fd|D             } fd|D             }
stt          t          j        |          } j                            | t           fd|                                         j                            d                     }nc fdt          ||          D             ^}} fd|d d         D             }t           fd fd	t          ||          D             |          }|gS )
Nc                0    g | ]} |          D ]}|S rI   rI   )rJ   r5   srE   s      r,   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   s.    <<<%%%))<<QA<<<<r.   c                h    g | ].}|                     j                                                  /S rI   )r_   r@   
StringTyperJ   r   rE   s     r,   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   s4    PPPa166""3">">"@"@AAPPPr.   c                D    g | ]}j                             |          S rI   )r=   isnullr   s     r,   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   s%    777Qa777r.   c                D    j                             d d| |          S )Nz%sr=   format_string)r   r   rE   r   s     r,   rg   z=SparkLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>   s(    RU%8%89Ki9K9K9KQPQ%R%R r.   c                    g | ]L\  }}j                             | |                              j                             d                     MS ) )r=   when	otherwiser^   )rJ   rK   nmrE   s      r,   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   sY     ' ' 'R EJJsC((222599R==AA' ' 'r.   c              3     K   | ]b}j                             |j                             d                                         j                                                 V  cdS )r   N)r=   r   r^   r   )rJ   r   rE   r   s     r,   rL   z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>   so         EJJr2599R==11;;BEIIi<P<PQQ     r.   c                <    j                             d| |          S )N%s%sr   )r   r   rE   s     r,   rg   z=SparkLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>   s    !4!4VQ!B!B r.   c              3  V   K   | ]#\  }}j                             d ||          V  $dS )r   Nr   )rJ   r   vrE   s      r,   rL   z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>   sO         Aq ++FAq99     r.   )r   rs   r|   r=   r   r   r^   zip)rE   rM   cols_casted	null_masknull_mask_resultresult
init_valuevalues
separatorsrC   r   r   s   `        r,   rA   z+SparkLikeNamespace.concat_str.<locals>.func   s   <<<<5<<<DPPPP4PPPK7777$777I #)(,	#B#B %%RRRRR#   )BEIIdOO,, ' ' ' '#&{I#>#>' ' '#
V
    'n  
  BBBB   $'
F$;$;     8Or.   rh   rU   rV   )r+   r   r   rC   rA   s    ``` r,   
concat_strzSparkLikeNamespace.concat_str   so    !	 !	 !	 !	 !	 !	 !	 !	F zz"?"G95A 1M/  
 
 	
r.   	predicateSparkLikeWhenc                :    t                               ||           S )N)context)r   	from_expr)r+   r   s     r,   r   zSparkLikeNamespace.when
  s    &&y$&???r.   N)r    r!   r"   r   r#   r   r$   r%   )r$   r   )r$   r3   )r$   r7   )rA   rB   rC   r   r$   r   )rX   r   rY   rZ   r$   r   )r$   r   )rC   r   r$   r   )r   r   r   r   r$   r   )rC   r   r   r   r   r   r$   r   )r   r   r$   r   )__name__
__module____qualname__r-   propertyr2   r5   r9   r=   r@   rW   r^   ro   ry   r}   r   r   r   r   r   r   r   rI   r.   r,   r   r      s       	. 	. 	. 	. ? ? ? X?    X " " " X" : : X: > > X>
 
 
 
2
 
 
 
(
 
 
 
4 4 4 44 4 4 44 4 4 44 4 4 44 4 4 44 4 4 4* "  "  "  "D-
 -
 -
 -
^@ @ @ @ @ @r.   r   r   c                  D     e Zd Zedd            Zd fdZd fd
Z xZS )r   r$   type[SparkLikeThen]c                    t           S r'   )SparkLikeThenr1   s    r,   _thenzSparkLikeWhen._then  r6   r.   rE   r   Sequence[Column]c                    |j         j        | _        |j         j        | _        t                                          |          S r'   )r=   r   r^   super__call__)r+   rE   	__class__s     r,   r   zSparkLikeWhen.__call__  s1    EJ	59ww###r.   rO   r   c                    |j         j        | _        |j         j        | _        t                                          ||          S r'   )r=   r   r^   r   _window_function)r+   rE   rO   r   s      r,   r   zSparkLikeWhen._window_function  s5     EJ	59ww''M:::r.   )r$   r   )rE   r   r$   r   )rE   r   rO   r   r$   r   )r   r   r   r   r   r   r   __classcell__)r   s   @r,   r   r     sy           X$ $ $ $ $ $
; ; ; ; ; ; ; ; ; ;r.   r   r   c                      e Zd ZdS )r   N)r   r   r   rI   r.   r,   r   r      s        3r.   r   ))
__future__r   rs   	functoolsr   typingr   r   r   r   narwhals._compliantr	   r
   r   narwhals._expression_parsingr   r   narwhals._spark_like.dataframer   narwhals._spark_like.exprr   narwhals._spark_like.selectorsr   narwhals._spark_like.utilsr   r   r   sqlframe.base.columnr   r   r   narwhals._utilsr   r   narwhals.typingr   r   r   r   r   r   rI   r.   r,   <module>r     sB   " " " " " "        > > > > > > > > > > > > A A A A A A A A A A        > = = = = = 3 3 3 3 3 3 E E E E E E           J++++++@@@@@@;;;;;;77777777IIIIIIIIIIm@ m@ m@ m@ m@$m5HHIm@ m@ m@`; ; ; ; ;H/=HI ; ; ;$    =89=    r.   