o
    $&]i                     @  s   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 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rxd dl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)G dd deeeddf Z*dS )    )annotationsN)reduce)chain)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)Iterable)Column)WindowInputs)SQLFrameDataFrame)ImplementationVersion)ConcatMethod	IntoDTypeNonNestedLiteralPythonLiteral
startswithendswithbtrimsplitregexp)starts_with	ends_withtrim	str_splitregexp_matchesc                   @  s   e Zd Zd@ddZedAd
dZedBddZedCddZedd Zedd Z	dDddZ
dEdd Z	!dFdGd%d&ZdHd(d)ZdId.d/ZdJd0d1ZdKd2d3ZdLd9d:ZdMd>d?Zd!S )NSparkLikeNamespaceversionr   implementationr   returnNonec                C  s   || _ || _d S N)_version_implementation)selfr(   r)    r0   \/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/_spark_like/namespace.py__init__.   s   
zSparkLikeNamespace.__init__r   c                 C  s
   t | S r,   )r   from_namespacer/   r0   r0   r1   	selectors2   s   
zSparkLikeNamespace.selectorstype[SparkLikeExpr]c                 C     t S r,   r   r4   r0   r0   r1   _expr6      zSparkLikeNamespace._exprtype[SparkLikeLazyFrame]c                 C  r7   r,   r	   r4   r0   r0   r1   
_lazyframe:   r9   zSparkLikeNamespace._lazyframec                 C     t r
ddlm} |S t| jS )Nr   )	functions)r   sqlframe.baser=   r   r.   )r/   r=   r0   r0   r1   _F>      
zSparkLikeNamespace._Fc                 C  r<   )Nr   )types)r   r>   rA   r   r.   )r/   rA   r0   r0   r1   _native_dtypesF   r@   z!SparkLikeNamespace._native_dtypesnamestrargsColumn | PythonLiteralr   c                 G  s   t | jt||| S r,   )getattrr?   FUNCTION_REMAPPINGSget)r/   rC   rE   r0   r0   r1   	_functionN   s   zSparkLikeNamespace._functionvaluer   c                 C  s   | j |S r,   )r?   lit)r/   rK   r0   r0   r1   _litQ      zSparkLikeNamespace._litN	condition	otherwiseColumn | Nonec                 C  s*   |d u r| j ||S | j |||S r,   )r?   whenrP   )r/   rO   rK   rP   r0   r0   r1   _whenT   s   zSparkLikeNamespace._whenexprsc                 G  s   | j j| S r,   )r?   coalesce)r/   rT   r0   r0   r1   	_coalesce[   rN   zSparkLikeNamespace._coalescer   dtypeIntoDType | Noner   c                   s>   d fdddfd	d
}j |dd d jjdS )Ndfr
   r*   list[Column]c                   s6   | j } rt j| j| jj}||}|gS r,   )r?   rL   r   r-   rB   nativesparkSessioncast)rY   columnnative_dtype)rW   r/   rK   r0   r1   func_   s   
z$SparkLikeNamespace.lit.<locals>.func_window_inputsWindowInputs[Column]c                   s    | S r,   r0   )rY   ra   )r`   r0   r1   window_funci   s   z+SparkLikeNamespace.lit.<locals>.window_funcc                 S     dgS )Nliteralr0   _dfr0   r0   r1   <lambda>q       z(SparkLikeNamespace.lit.<locals>.<lambda>evaluate_output_namesalias_output_namesr(   r)   rY   r
   r*   rZ   )rY   r
   ra   rb   r*   rZ   r8   r-   r.   )r/   rK   rW   rc   r0   )rW   r`   r/   rK   r1   rL   ^   s   
zSparkLikeNamespace.litc                 C  s&   d
dd}| j |dd d | j| jd	S )NrY   r
   r*   rZ   c                 S  s   | j dgS )N*)r?   countrY   r0   r0   r1   r`   x   s   z$SparkLikeNamespace.len.<locals>.funcc                 S  rd   )Nlenr0   rf   r0   r0   r1   rh   }   ri   z(SparkLikeNamespace.len.<locals>.<lambda>rj   rm   rn   )r/   r`   r0   r0   r1   rr   w   s   
zSparkLikeNamespace.lenc                   s$   d fdd}j j|g R  S )NcolsIterable[Column]r*   r   c                   sR   t | }  d j}ttjfdd| D }ttjfdd| D }t|||S )Nr   c                 3  s&    | ]} j | j d V  qdS )r   N)r?   rU   rL   .0colr4   r0   r1   	<genexpr>   s   $ zCSparkLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>c                 3  s$    | ]}|   j V  qd S r,   )	isNotNullr]   rB   IntegerTyperu   r4   r0   r1   rx      s   " )tupler?   r   operatoraddr   )rs   F	numeratordenominatorrT   r/   r0   r1   r`      s   
z0SparkLikeNamespace.mean_horizontal.<locals>.func)rs   rt   r*   r   )r8   _from_elementwise_horizontal_op)r/   rT   r`   r0   r   r1   mean_horizontal   s   z"SparkLikeNamespace.mean_horizontalitemsIterable[SparkLikeLazyFrame]howr   r
   c          	      C  s   dd |D }|dkrL|d j }t|dd  ddD ]#\}}|j }t|t|kr-||ks=d| d| d	| d
}t|qttdd || j| jdS |dkr^ttdd || j| jdS t)Nc                 S  s   g | ]}|j qS r0   )_native_frame)rv   itemr0   r0   r1   
<listcomp>   s    z-SparkLikeNamespace.concat.<locals>.<listcomp>verticalr      )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
c                 S  s
   |  |S r,   )unionxyr0   r0   r1   rh      s   
 z+SparkLikeNamespace.concat.<locals>.<lambda>)native_dataframer(   r)   diagonalc                 S  s   | j |ddS )NT)allowMissingColumns)unionByNamer   r0   r0   r1   rh      s    )	columns	enumeraterr   	TypeErrorr
   r   r-   r.   NotImplementedError)	r/   r   r   dfscols_0irY   cols_currentmsgr0   r0   r1   concat   s<   
zSparkLikeNamespace.concat	separatorignore_nullsboolc                  s4   d fdd}j |t  t  jjdS )	NrY   r
   r*   rZ   c                   sr   j  ttfddD } jg|R  }s6ttj fdd|D } | |	 
d }|gS )Nc                 3  s    | ]}| V  qd S r,   r0   )rv   erq   r0   r1   rx      s    z>SparkLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>c                 3  s    | ]}  |V  qd S r,   )isnull)rv   s)r~   r0   r1   rx      s    )r?   r{   r   from_iterable	concat_wsr   r|   or_rR   rP   rL   )rY   rs   result	null_maskrT   r   r/   r   )r~   rY   r1   r`      s   z+SparkLikeNamespace.concat_str.<locals>.func)callrk   rl   r(   r)   rm   )r8   r   r   r-   r.   )r/   r   r   rT   r`   r0   r   r1   
concat_str   s   zSparkLikeNamespace.concat_str)r(   r   r)   r   r*   r+   )r*   r   )r*   r6   )r*   r:   )rC   rD   rE   rF   r*   r   )rK   r   r*   r   r,   )rO   r   rK   r   rP   rQ   r*   r   )rT   r   r*   r   )rK   r   rW   rX   r*   r   )r*   r   )rT   r   r*   r   )r   r   r   r   r*   r
   )rT   r   r   rD   r   r   r*   r   )__name__
__module____qualname__r2   propertyr5   r8   r;   r?   rB   rJ   rM   rS   rV   rL   rr   r   r   r   r0   r0   r0   r1   r'   +   s,    









 r'   r   r   )+
__future__r   r|   	functoolsr   	itertoolsr   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   collections.abcr   sqlframe.base.columnr   narwhals._compliant.windowr   r   narwhals._utilsr   r   narwhals.typingr   r   r   r   rH   r'   r0   r0   r0   r1   <module>   s6    
	