
    hh\              	         d dl mZ d dlZd dlmZ d dl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mZ d d
lmZ d dlmZ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"m#Z# d dl$m%Z%m&Z&m'Z'm(Z( ddddddZ) G d deeed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Any)combine_alias_output_namescombine_evaluate_output_namesSparkLikeLazyFrameSparkLikeExpr)SparkLikeSelectorNamespace)import_functionsimport_native_dtypesnarwhals_to_native_dtypetrue_divide)SQLNamespace)SQLThenSQLWhen)
zip_strict)Iterable)Column)SQLFrameDataFrame)ImplementationVersion)ConcatMethod	IntoDTypeNonNestedLiteralPythonLiteral
startswithendswithbtrimsplitregexp)starts_with	ends_withtrim	str_splitregexp_matchesc                      e Zd Zd4dZed5d	            Zed6d            Zed7d            Zed             Zed             Z	d8dZ
d9dZ	 d:d;dZd<dZd=d$Zd>d%Zd?d&Zd@d,ZdAd0ZdBd3ZdS )CSparkLikeNamespaceversionr   implementationr   returnNonec               "    || _         || _        d S N)_version_implementation)selfr+   r,   s      U/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/_spark_like/namespace.py__init__zSparkLikeNamespace.__init__.   s    -    r   c                *    t          j        |           S r0   )r   from_namespacer3   s    r4   	selectorszSparkLikeNamespace.selectors2   s    )8>>>r6   type[SparkLikeExpr]c                    t           S r0   r   r9   s    r4   _exprzSparkLikeNamespace._expr6       r6   type[SparkLikeLazyFrame]c                    t           S r0   r	   r9   s    r4   
_lazyframezSparkLikeNamespace._lazyframe:   s    !!r6   c                H    t           rddlm} |S t          | j                  S )Nr   )	functions)r   sqlframe.baserC   r   r2   )r3   rC   s     r4   _FzSparkLikeNamespace._F>   s3     	////// 4555r6   c                H    t           rddlm} |S t          | j                  S )Nr   )types)r   rD   rG   r   r2   )r3   rG   s     r4   _native_dtypesz!SparkLikeNamespace._native_dtypesF   s2     	++++++L#D$8999r6   namestrargsColumn | PythonLiteralr   c                d     t          | j        t                              ||                    | S r0   )getattrrE   FUNCTION_REMAPPINGSget)r3   rI   rK   s      r4   	_functionzSparkLikeNamespace._functionN   s,    Dwtw 3 7 7d C CDDdKKr6   valuer   c                6    | j                             |          S r0   )rE   lit)r3   rR   s     r4   _litzSparkLikeNamespace._litQ   s    w{{5!!!r6   N	condition	otherwiseColumn | Nonec                    || j                             ||          S | j                             ||                              |          S r0   )rE   whenrW   )r3   rV   rR   rW   s       r4   _whenzSparkLikeNamespace._whenT   sD     7<<	5111w||Iu--77	BBBr6   exprsc                      | j         j        | S r0   )rE   coalesce)r3   r\   s     r4   	_coalescezSparkLikeNamespace._coalesce[   s    tw''r6   r   dtypeIntoDType | Noner   c                b     d fd}                      |d d  j         j                  S )	Ndfr
   r-   list[Column]c                    | j                                       }r;t          j        | j        | j        j                  }|                    |          }|gS r0   )rE   rT   r   r1   rH   nativesparkSessioncast)rc   columnnative_dtyper`   r3   rR   s      r4   rU   z$SparkLikeNamespace.lit.<locals>._lit_   sZ    UYYu%%F 374="*;RY=S     \228Or6   c                    dgS )Nliteral _dfs    r4   <lambda>z(SparkLikeNamespace.lit.<locals>.<lambda>k   s    yk r6   callevaluate_output_namesalias_output_namesr+   r,   rc   r
   r-   rd   r=   r1   r2   )r3   rR   r`   rU   s   ``` r4   rT   zSparkLikeNamespace.lit^   sa    	 	 	 	 	 	 	 	 zz"9"9#M/  
 
 	
r6   c                T    dd}|                      |d d | j        | j                  S )	Nrc   r
   r-   rd   c                8    | j                             d          gS )N*)rE   count)rc   s    r4   funcz$SparkLikeNamespace.len.<locals>.funcr   s    EKK$$%%r6   c                    dgS )Nlenrm   rn   s    r4   rp   z(SparkLikeNamespace.len.<locals>.<lambda>w   s    ug r6   )rs   rt   r+   r,   ru   rv   )r3   r{   s     r4   r}   zSparkLikeNamespace.lenq   sJ    	& 	& 	& 	& zz"5"5#M/  
 
 	
r6   c                :     d fd}  j         j        |gR  S )NcolsIterable[Column]r-   r   c                    t          |           } d         j        }t          t          j        fd| D                       }t          t          j        fd| D                       }t          |||          S )Nr   c              3  ~   K   | ]7}j                             |j                             d                     V  8dS )r   N)rE   r^   rT   .0colr3   s     r4   	<genexpr>zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   sA      UUtw//TW[[^^DDUUUUUUr6   c              3     K   | ]B}|                                                     j                                                  V  Cd S r0   )	isNotNullrh   rH   IntegerTyper   s     r4   r   zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   sH      YYS%%d&9&E&E&G&GHHYYYYYYr6   )tuplerE   r   operatoraddr   )r   F	numeratordenominatorr\   r3   s       r4   r{   z0SparkLikeNamespace.mean_horizontal.<locals>.func~   s    ;;DaAUUUUPTUUU I !YYYYTXYYY K q)[999r6   )r   r   r-   r   )r=   _from_elementwise_horizontal_op)r3   r\   r{   s   `` r4   mean_horizontalz"SparkLikeNamespace.mean_horizontal}   sF    
	: 
	: 
	: 
	: 
	: 
	: 
	: :tz9$GGGGGr6   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                  S |dk    r+t	          t          d |          | j        | j                  S t          )Nc                    g | ]	}|j         
S rm   )_native_frame)r   items     r4   
<listcomp>z-SparkLikeNamespace.concat.<locals>.<listcomp>   s    444dt!444r6   verticalr      )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
c                ,    |                      |          S r0   )unionxys     r4   rp   z+SparkLikeNamespace.concat.<locals>.<lambda>   s    QWWQZZ r6   )native_dataframer+   r,   diagonalc                0    |                      |d          S )NT)allowMissingColumns)unionByNamer   s     r4   rp   z+SparkLikeNamespace.concat.<locals>.<lambda>   s    qd!K!K r6   )	columns	enumerater}   	TypeErrorr
   r   r1   r2   NotImplementedError)	r3   r   r   dfscols_0irc   cols_currentmsgs	            r4   concatzSparkLikeNamespace.concat   sV    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#3    *%!'KKS" " #3    "!r6   	separatorignore_nullsboolc               |    dfd}|                      |t           t           | j        | j                  S )Nrc   r
   r-   rd   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 rm   rm   )r   r=   src   s      r4   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   s.    <<<%%%))<<QA<<<<r6   c                h    g | ].}|                     j                                                  /S rm   )rh   rH   
StringTyper   r   rc   s     r4   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   s4    PPPa166""3">">"@"@AAPPPr6   c                D    g | ]}j                             |          S rm   )rE   isnullr   s     r4   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   s%    777Qa777r6   c                D    j                             d d| |          S )Nz%srE   format_string)r   r   rc   r   s     r4   rp   z=SparkLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>   s(    RU%8%89Ki9K9K9KQPQ%R%R r6   c                    g | ]L\  }}j                             | |                              j                             d                     MS ) )rE   rZ   rW   rT   )r   r   nmrc   s      r4   r   z?SparkLikeNamespace.concat_str.<locals>.func.<locals>.<listcomp>   sY     ' ' 'R EJJsC((222599R==AA' ' 'r6   c              3     K   | ]b}j                             |j                             d                                         j                                                 V  cdS )r   N)rE   rZ   rT   rW   )r   r   rc   r   s     r4   r   z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>   so         EJJr2599R==11;;BEIIi<P<PQQ     r6   c                <    j                             d| |          S )N%s%sr   )r   r   rc   s     r4   rp   z=SparkLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>   s    !4!4VQ!B!B r6   c              3  V   K   | ]#\  }}j                             d ||          V  $dS )r   Nr   )r   r   vrc   s      r4   r   z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>   sO         Aq ++FAq99     r6   )r   r   or_rE   rZ   rW   rT   r   )rc   r   cols_casted	null_masknull_mask_resultresult
init_valuevalues
separatorsr\   r   r   s   `        r4   r{   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,, ' ' ' '#-k9#E#E' ' '#
V
    'n  
  BBBB   $.z6$B$B     8Or6   rq   ru   )r=   r   r   r1   r2   )r3   r   r   r\   r{   s    ``` r4   
concat_strzSparkLikeNamespace.concat_str   si    !	 !	 !	 !	 !	 !	 !	 !	F zz"?"G95AM/  
 
 	
r6   	predicateSparkLikeWhenc                :    t                               ||           S )N)context)r   	from_expr)r3   r   s     r4   rZ   zSparkLikeNamespace.when   s    &&y$&???r6   )r+   r   r,   r   r-   r.   )r-   r   )r-   r;   )r-   r?   )rI   rJ   rK   rL   r-   r   )rR   r   r-   r   r0   )rV   r   rR   r   rW   rX   r-   r   )r\   r   r-   r   )rR   r   r`   ra   r-   r   )r-   r   )r\   r   r-   r   )r   r   r   r   r-   r
   )r\   r   r   rJ   r   r   r-   r   )r   r   r-   r   )__name__
__module____qualname__r5   propertyr:   r=   rA   rE   rH   rQ   rU   r[   r_   rT   r}   r   r   r   rZ   rm   r6   r4   r*   r*   +   s       . . . . ? ? ? X?    X " " " X" 6 6 X6 : : X:L L L L" " " " LPC C C C C( ( ( (
 
 
 
&

 

 

 

H H H H" " " "@,
 ,
 ,
 ,
\@ @ @ @ @ @r6   r*   r   r   c                  &    e Zd Zedd            ZdS )r   r-   type[SparkLikeThen]c                    t           S r0   )SparkLikeThenr9   s    r4   _thenzSparkLikeWhen._then   r>   r6   N)r-   r   )r   r   r   r   r   rm   r6   r4   r   r      s2           X  r6   r   c                      e Zd ZdS )r   N)r   r   r   rm   r6   r4   r   r      s        3r6   r   )-
__future__r   r   	functoolsr   typingr   r   narwhals._expression_parsingr   r   narwhals._spark_like.dataframer
   narwhals._spark_like.exprr   narwhals._spark_like.selectorsr   narwhals._spark_like.utilsr   r   r   r   narwhals._sql.namespacer   narwhals._sql.when_thenr   r   narwhals._utilsr   collections.abcr   sqlframe.base.columnr   r   r   r   narwhals.typingr   r   r   r   rO   r*   r   r   rm   r6   r4   <module>r      s   " " " " " "        % % % % % % % %        > = = = = = 3 3 3 3 3 3 E E E E E E            1 0 0 0 0 0 4 4 4 4 4 4 4 4 & & & & & & Y((((((++++++@@@@@@77777777XXXXXXXXXXXX    p@ p@ p@ p@ p@#]4GQRp@ p@ p@f    G.-GH       -78-    r6   