
    7^j0              	         d dl mZ d dl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 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 er0d dlmZ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& d dl'm(Z(m)Z)m*Z*m+Z+ ddddddZ,ejZ                  ej\                  ej^                  ej`                  ejb                  dZ2 G d deeeddf         Z3y)    )annotationsN)reduce)chain)TYPE_CHECKINGAny)combine_alias_output_namescombine_evaluate_output_names!evaluate_output_names_and_aliasesSparkLikeLazyFrameSparkLikeExpr)SparkLikeSelectorNamespace)import_functionsimport_native_dtypesnarwhals_to_native_dtypetrue_divide)SQLNamespace)CallableIterableMapping)Column)WindowInputs)SQLFrameDataFrame)ImplementationVersion)ConcatMethodCorrelationMethod	IntoDTypePythonLiteral
startswithendswithbtrimsplitregexp)starts_with	ends_withtrim	str_splitregexp_matches)addsubtractmultiplydivideandc                     e Zd ZddZedd       Zedd       Zedd       Zed        Zed        Z	ddZ
ddZ	 d	 	 	 	 	 	 	 dd
ZddZddZddZd dZ	 	 	 	 	 	 d!dZ	 	 	 	 	 	 	 	 d"dZ	 	 	 	 	 	 	 	 d#dZd$dZd dZd dZy	)%SparkLikeNamespacec                    || _         || _        y N)_version_implementation)selfversionimplementations      i/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/narwhals/_spark_like/namespace.py__init__zSparkLikeNamespace.__init__7   s    -    c                ,    t        j                  |       S r3   )r   from_namespacer6   s    r9   	selectorszSparkLikeNamespace.selectors;   s    )88>>r;   c                    t         S r3   r   r>   s    r9   _exprzSparkLikeNamespace._expr?   s    r;   c                    t         S r3   r   r>   s    r9   
_lazyframezSparkLikeNamespace._lazyframeC   s    !!r;   c                H    t         rddlm} |S t        | j                        S )Nr   )	functions)r   sqlframe.baserE   r   r5   )r6   rE   s     r9   _FzSparkLikeNamespace._FG   s    / 4 455r;   c                H    t         rddlm} |S t        | j                        S )Nr   )types)r   rF   rI   r   r5   )r6   rI   s     r9   _native_dtypesz!SparkLikeNamespace._native_dtypesO   s    +L#D$8$899r;   c                    |t         v rt        |   | S |dk(  r|d   j                         S  t        | j                  t        j                  ||            | S )N	isnotnullr   )_BINARY_OPS	isNotNullgetattrrG   FUNCTION_REMAPPINGSget)r6   nameargss      r9   	_functionzSparkLikeNamespace._functionW   s[    ;t$d++;7$$&&Dwtww 3 7 7d CDdKKr;   c                8    | j                   j                  |      S r3   )rG   lit)r6   values     r9   _litzSparkLikeNamespace._lit^   s    ww{{5!!r;   Nc                    || j                   j                  ||      S | j                   j                  ||      j                  |      S r3   )rG   when	otherwise)r6   	conditionrW   r[   s       r9   _whenzSparkLikeNamespace._whena   s?     77<<	511ww||Iu-77	BBr;   c                4     | j                   j                  | S r3   )rG   coalesce)r6   exprss     r9   	_coalescezSparkLikeNamespace._coalesceh   s    tww''r;   c                     d fd	 	 	 	 	 	 dfd} j                  |d d  j                   j                        S )Nc                   | j                   }t        t        t        f      r@D cg c]  }|j	                  |       }}|j	                  |j                  |            }nt        t              rs
j                  }|j                         rd
j                   d}t        |      |j                         dk  r9|j                  |j	                  d      | j                  j                               n|j                  g       }naj                         D cg c]%  \  }}|j	                  |      j!                  |      ' }}} |j                  | }n|j	                        }	rGt#        	
j$                  | j                  | j&                  j(                        }|j+                  |      }|gS c c}w c c}}w )Nz'Cannot create an empty struct type for z backend)   r   z{})rG   
isinstancelisttuplerV   arraydictr5   is_sqlframeNotImplementedError_backend_version	from_jsonrJ   
StructTypestructitemsaliasr   r4   nativesparkSessioncast)dfFv
lit_valuescolumnimplmsgknative_dtypedtyper6   rW   s            r9   funcz$SparkLikeNamespace.lit.<locals>.funcl   sv   A%$/0561aeeAh6
6qwwz23E4(//D'') GH\H\G]]ef1#66  002V; AEE$K1B1B1M1M1OPXXb\  AF!N1!%%(.."3!NJ!N%QXXz2Fu74=="*;*;RYY=S=S   \28O5 7 "Os   F8*F=c                     |       S r3    )ru   _window_inputsr   s     r9   window_funcz+SparkLikeNamespace.lit.<locals>.window_func   s     8Or;   c                    dgS )Nliteralr   _dfs    r9   <lambda>z(SparkLikeNamespace.lit.<locals>.<lambda>   s    yk r;   evaluate_output_namesalias_output_namesr7   r8   ru   r   returnlist[Column])ru   r   r   WindowInputs[Column]r   r   rA   r4   r5   )r6   rW   r~   r   r   s   ``` @r9   rV   zSparkLikeNamespace.litk   sZ    	@	"	4H		
 zz"9#MM//  
 	
r;   c                `    dd}| j                  |d d | j                  | j                        S )Nc                :    | j                   j                  d      gS )N*)rG   count)ru   s    r9   r   z$SparkLikeNamespace.len.<locals>.func   s    EEKK$%%r;   c                    dgS )Nlenr   r   s    r9   r   z(SparkLikeNamespace.len.<locals>.<lambda>   s    ug r;   r   r   r   )r6   r   s     r9   r   zSparkLikeNamespace.len   s8    	& zz"5#MM//  
 	
r;   c                N     d fd}  j                   j                  |g S )Nc                    t        |       } d   j                  }t        t        j                  fd| D              }t        t        j                  fd| D              }t        |||      S )Nr   c              3     K   | ]9  }j                   j                  |j                   j                  d              ; yw)r   N)rG   r_   rV   .0colr6   s     r9   	<genexpr>zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s.     Utww//TWW[[^DUs   ?Ac              3     K   | ];  }|j                         j                  j                  j                                = y wr3   )rN   rt   rJ   IntegerTyper   s     r9   r   zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s1     YS%%d&9&9&E&E&GHYs   AA)rg   rG   r   operatorr+   r   )colsrv   	numeratordenominatorr`   r6   s       r9   r   z0SparkLikeNamespace.mean_horizontal.<locals>.func   s`    ;DaAUPTUI !YTXYK q)[99r;   )r   zIterable[Column]r   r   )rA   _from_elementwise_horizontal_op)r6   r`   r   s   `` r9   mean_horizontalz"SparkLikeNamespace.mean_horizontal   s&    
	: :tzz99$GGGr;   c                  |D cg c]  }|j                    }}|dk(  r|d   j                  }t        |dd  d      D ]D  \  }}|j                  }t        |      t        |      k(  r||k(  r/d| d| d| d}	t	        |	       t        t        d	 |      | j                  | j                  
      S |dk(  r-t        t        d |      | j                  | j                  
      S t        c c}w )Nverticalr      )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
c                $    | j                  |      S r3   )unionxys     r9   r   z+SparkLikeNamespace.concat.<locals>.<lambda>   s    QWWQZ r;   )native_dataframer7   r8   diagonalc                (    | j                  |d      S )NT)allowMissingColumns)unionByNamer   s     r9   r   z+SparkLikeNamespace.concat.<locals>.<lambda>   s    qd!K r;   )
_native_framecolumns	enumerater   	TypeErrorr   r   r4   r5   rk   )
r6   rp   howitemdfscols_0iru   cols_currentr{   s
             r9   concatzSparkLikeNamespace.concat   s    /44dt!!44*V^^F"3qr7!4 )2!zz\*c&k9PV@V--3H 5**+B|nB@ 
 $C.() &!'(?!E#33  *%!'KS" #33  "!7 5s   C*c                    d fd} j                  |t         t          j                   j                        S )Nc                F    j                   t        t        j                   fdD                    } j                  g| }sTt        t        j                  fd|D              }j                  | |      j                  j                  d             }|gS )Nc              3  .   K   | ]  } |        y wr3   r   )r   eru   s     r9   r   z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s     ,BqQrU,Bs   c              3  @   K   | ]  }j                  |        y wr3   )isnull)r   srv   s     r9   r   z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s     1L!!((1+1Ls   )rG   rg   r   from_iterable	concat_wsr   r   or_rZ   r[   rV   )	ru   r   result	null_maskrv   r`   ignore_nullsr6   	separators	   `   @r9   r   z+SparkLikeNamespace.concat_str.<locals>.func   s    A,,,BE,BBCD Q[[2T2F"8<<1Lt1LM	
F3==aeeDkJ8Or;   callr   r   r7   r8   r   )rA   r	   r   r4   r5   )r6   r   r   r`   r   s   ```` r9   
concat_strzSparkLikeNamespace.concat_str   sH    		 		 zz"?"G95AMM//  
 	
r;   c                    |dk7  rd}t        |      d fd} j                  |t              t               j                   j
                        S )Npearsonz2Only 'pearson' correlation is supported for Spark.c                    j                   }| j                        }| j                        }|j                  ||      gS r3   )rG   _evaluate_single_output_exprcorr)ru   rv   a_b_abr6   s       r9   r   z%SparkLikeNamespace.corr.<locals>.func   s?    A003B003BFF2rN##r;   r   r   )rk   rA   r	   r   r4   r5   )r6   r   r   methodr{   r   s   ```   r9   r   zSparkLikeNamespace.corr   sa     YFC%c**	$ zz"?1"E9!Q?MM//  
 	
r;   c          	         | j                   	 	 	 	 	 	 	 	 dfddfd}	 	 	 	 	 	 dfd}| j                  ||t              t              | j                  | j
                        S )Nc           
     t   dk(  r |j                  | |            gS dk(  r |j                  | |            gS | j                         |j                         z  } |j                  j	                  |j                  d            j                  j                  d                        }|j                        z
  } |j                  | |            |j                  d      z
  |z  z  }j	                  |j                  d      k  j                  d             j                  |      gS )Nr   r   )	covar_pop
covar_samprN   sumrZ   rV   r[   )	r   r   wrapis_valid	n_samplesr   rescaledrv   ddofs	          r9   _covz$SparkLikeNamespace.cov.<locals>._cov  s    qyQ[[R0122qyQ\\"b1233||~6HQUU166(AEE!H#=#G#Ga#QRSI#aeeDk1KALLR01i!%%(6Jk5YZHFF;!%%(2AEE$K@JJ8TUUr;   c                ^    | j                        }| j                        } ||d       S )Nc                    | S r3   r   )r   s    r9   r   z6SparkLikeNamespace.cov.<locals>.func.<locals>.<lambda>  s    ! r;   )r   )ru   r   r   r   r   r   s      r9   r   z$SparkLikeNamespace.cov.<locals>.func  s2    003B003BB,,r;   c                    |j                   rJ | j                        }| j                        } | j                  j                  |j                  xs j                  d      g  ||fd      S )Nr   c                &    | j                        S r3   )over)r   windows    r9   r   z:SparkLikeNamespace.cov.<locals>.window_f.<locals>.<lambda>  s    !&&. r;   )order_byr   _WindowpartitionBypartition_byrV   )	ru   inputsr   r   r   rv   r   r   r   s	       @r9   window_fz(SparkLikeNamespace.cov.<locals>.window_f  sm     &&003B003B+RZZ++f.A.A.OaeeAhZQFB 899r;   )r   window_functionr   r   r7   r8   )r   r   r   r   r   zCallable[[Column], Column]r   r   r   )ru   r   r   r   r   r   )rG   rA   r	   r   r4   r5   )r6   r   r   r   r   r   rv   r   s    ```  @@r9   covzSparkLikeNamespace.cov   s    GG	V	V"	V*D	V	V 	-
	:"	:,@	:	: 	: zz$"?1"E9!Q?MM//  
 	
r;   c                      j                   }d fd} j                  |t         t         | j                        S )Nc                    	j                   }D ci c].  }t         ||       gt        || g       ddiD ]	  \  }}}|| 0 }}}}}d |j                         D        } |j                  | gS c c}}}}w )NstrictTc              3  D   K   | ]  \  }}|j                  |        y wr3   )rq   )r   rR   r   s      r9   r   z:SparkLikeNamespace.struct.<locals>.func.<locals>.<genexpr>8  s     N94syyNs    )rG   zipr
   rp   ro   )
ru   rv   exprnative_expr_rq   names_to_colsaliasedr`   r6   s
           r9   r   z'SparkLikeNamespace.struct.<locals>.func-  s    A "3 3-0H.6tRD.  .3 *KE {"33M 3 O8K8K8MNGAHHg&''3s   3A4
r   r   r4   rA   r	   r   r5   r6   r`   r7   r   s   ``  r9   ro   zSparkLikeNamespace.struct*  sH    --	( zz"?"G95A//  
 	
r;   c                      j                   }d fd} j                  |t         t         | j                        S )Nc                    D cg c]  } ||       D ]  }|  }}} j                   j                  | gS c c}}w r3   )rG   rh   )ru   r   r   r   r`   r6   s       r9   r   z%SparkLikeNamespace.list.<locals>.funcF  sD    ,1NDT"XNkKNKNDN!DGGMM4()) Os   :r   r   r   r   s   ``  r9   rf   zSparkLikeNamespace.listC  sH    --	* zz"?"G95A//  
 	
r;   )r7   r   r8   r   r   None)r   r   )r   ztype[SparkLikeExpr])r   ztype[SparkLikeLazyFrame])rR   strrS   zColumn | PythonLiteralr   r   )rW   r   r   r   r3   )r\   r   rW   r   r[   zColumn | Noner   r   )r`   r   r   r   )rW   r    r~   zIntoDType | Noner   r   )r   r   )r`   r   r   r   )rp   zIterable[SparkLikeLazyFrame]r   r   r   r   )r`   r   r   r  r   boolr   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   intr   r   )__name__
__module____qualname__r:   propertyr?   rA   rC   rG   rJ   rT   rX   r]   ra   rV   r   r   r   r   r   r   ro   rf   r   r;   r9   r1   r1   4   s5   . ? ?   " " 6 6 : :L" LPCC(.C;HC	C(-
^

H"1";G"	"@
#
03
CG
	
,

#0
=N
	
*(
T
2
r;   r1   r   r   )4
__future__r   r   	functoolsr   	itertoolsr   typingr   r   narwhals._expression_parsingr   r	   r
   narwhals._spark_like.dataframer   narwhals._spark_like.exprr   narwhals._spark_like.selectorsr   narwhals._spark_like.utilsr   r   r   r   narwhals._sql.namespacer   collections.abcr   r   r   sqlframe.base.columnr   narwhals._compliant.windowr   r   narwhals._utilsr   r   narwhals.typingr   r   r   r    rP   r+   submultruedivand_rM   r1   r   r;   r9   <module>r     s    "    % 
 > 3 E  1;;+7@7YY    <<==\
#]4GQR\
r;   