o
    Lhd                     @  sn  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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mZmZmZ d dlmZm Z  d dl!m"Z" d dl#m$Z$m%Z% d dl&m'Z' erd dl(m)Z) d dl
m*Z* d dl&m+Z+ d dl,m-Z-m.Z.m/Z/ G dd de"eedef Z0G dd de%deef Z1G dd de$deef eZ2dS )    )annotationsN)reduce)chain)TYPE_CHECKINGAny)CoalesceOperator
Expression)BIGINTVARCHARDuckDBLazyFrame
DuckDBExpr)DuckDBSelectorNamespace)DeferredTimeZoneF
concat_strfunctionlitnarwhals_to_native_dtypewhen)combine_alias_output_namescombine_evaluate_output_names)SQLNamespace)SQLThenSQLWhen)Implementation)Iterable)DuckDBPyRelation)Version)ConcatMethod	IntoDTypeNonNestedLiteralc                   @  s   e Zd ZU ejZded< d?ddZed@d
dZ	edAddZ
edBddZdCddZdDddZ	dEdFd d!ZdGd#d$ZdHd*d+ZdId0d1ZdJd2d3ZdKd6d7ZdLd;d<ZdMd=d>ZdS )NDuckDBNamespacer   _implementationversionr   returnNonec                C  s
   || _ d S N)_version)selfr%    r+   [/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/_duckdb/namespace.py__init__-      
zDuckDBNamespace.__init__r   c                 C  s
   t | S r(   )r   from_namespacer*   r+   r+   r,   	selectors0   s   
zDuckDBNamespace.selectorstype[DuckDBExpr]c                 C     t S r(   r   r0   r+   r+   r,   _expr4      zDuckDBNamespace._exprtype[DuckDBLazyFrame]c                 C  r3   r(   r   r0   r+   r+   r,   
_lazyframe8   r5   zDuckDBNamespace._lazyframenamestrargsr   c                 G  s   t |g|R  S r(   )r   )r*   r8   r:   r+   r+   r,   	_function<   s   zDuckDBNamespace._functionvaluer   c                 C  s   t |S r(   )r   )r*   r<   r+   r+   r,   _lit?      zDuckDBNamespace._litN	condition	otherwiseExpression | Nonec                 C  s"   |d u r	t ||S t |||S r(   )r   r@   )r*   r?   r<   r@   r+   r+   r,   _whenB   s   
zDuckDBNamespace._whenexprsc                 G  s   t | S r(   )r   )r*   rC   r+   r+   r,   	_coalesceL   r>   zDuckDBNamespace._coalesceitemsIterable[DuckDBLazyFrame]howr    r   c                  sn   dd |D }t |}|d }|j |dkr+t fdd|dd  D s+d}t|td	d
 |}||S )Nc                 S  s   g | ]}|j qS r+   )_native_frame).0itemr+   r+   r,   
<listcomp>R   s    z*DuckDBNamespace.concat.<locals>.<listcomp>r   verticalc                 3  s    | ]}|j  kV  qd S r(   schema)rI   xrM   r+   r,   	<genexpr>V   s    z)DuckDBNamespace.concat.<locals>.<genexpr>   z&inputs should all have the same schemac                 S  s
   |  |S r(   )union)rO   yr+   r+   r,   <lambda>Y   s   
 z(DuckDBNamespace.concat.<locals>.<lambda>)listrN   all	TypeErrorr   _with_native)r*   rE   rG   native_itemsfirstmsgresr+   rM   r,   concatO   s   &
zDuckDBNamespace.concatr   	separatorignore_nullsboolc                  s.   d fdd}| j |t  t  | jdS )	Ndfr   r&   list[Expression]c                   sz   t tfddD  s5ttjdd  D }dd  fddt D D }t| t| gS t digS )Nc                 3  s    | ]}| V  qd S r(   r+   )rI   expr)ra   r+   r,   rP   `       z;DuckDBNamespace.concat_str.<locals>.func.<locals>.<genexpr>c                 s  s    | ]}|  V  qd S r(   )isnull)rI   sr+   r+   r,   rP   b   rd   c                 S  s   g | ]	}|D ]}|qqS r+   r+   )rI   rO   rS   r+   r+   r,   rK   c   s    z<DuckDBNamespace.concat_str.<locals>.func.<locals>.<listcomp>c                   s>   g | ]\}}|t  d  kr|tfn|ttfqS )rQ   )lencastr
   r   )rI   icol)colsr^   r+   r,   rK   e   s    r^   )	rU   r   from_iterabler   operatoror_	enumerater   r   )ra   null_mask_resultcols_separatedrC   r_   r^   )rk   ra   r,   func_   s   
z(DuckDBNamespace.concat_str.<locals>.funccallevaluate_output_namesalias_output_namesr%   ra   r   r&   rb   )r4   r   r   r)   )r*   r^   r_   rC   rs   r+   rr   r,   r   \   s   zDuckDBNamespace.concat_strc                 G  s   ddd}| j j|g|R  S )Nrk   Iterable[Expression]r&   r   c                 S  s4   t | } ttjdd | D ttjdd | D  S )Nc                 s  s    | ]
}t |td V  qdS )r   N)r   r   rI   rj   r+   r+   r,   rP   {       z@DuckDBNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>c                 s  s    | ]
}|  tV  qd S r(   )	isnotnullrh   r	   rz   r+   r+   r,   rP   |   r{   )rU   r   rm   add)rk   r+   r+   r,   rs   x   s   z-DuckDBNamespace.mean_horizontal.<locals>.func)rk   ry   r&   r   )r4   _from_elementwise_horizontal_op)r*   rC   rs   r+   r+   r,   mean_horizontalw   s   
zDuckDBNamespace.mean_horizontal	predicate
DuckDBWhenc                 C  s   t j|| dS )N)context)r   	from_expr)r*   r   r+   r+   r,   r      s   zDuckDBNamespace.whenr"   dtypeIntoDType | Nonec                   s*   d
 fdd}j |dd d jd	S )Nra   r   r&   rb   c                   s:   t | j} d urt j|}t|gS tgS r(   )r   nativer   r)   r   rh   )ra   tztargetr   r*   r<   r+   r,   rs      s
   

z!DuckDBNamespace.lit.<locals>.funcc                 S     dgS )Nliteralr+   _dfr+   r+   r,   rT          z%DuckDBNamespace.lit.<locals>.<lambda>)rv   rw   r%   rx   r4   r)   )r*   r<   r   rs   r+   r   r,   r      s   zDuckDBNamespace.litc                 C  s"   d
dd}| j |dd d | jd	S )Nr   r   r&   rb   c                 S  s
   t dgS )Ncount)r   r   r+   r+   r,   rs      r.   z!DuckDBNamespace.len.<locals>.funcc                 S  r   )Nrg   r+   r   r+   r+   r,   rT      r   z%DuckDBNamespace.len.<locals>.<lambda>rt   )r   r   r&   rb   r   )r*   rs   r+   r+   r,   rg      s   
zDuckDBNamespace.len)r%   r   r&   r'   )r&   r   )r&   r2   )r&   r6   )r8   r9   r:   r   r&   r   )r<   r   r&   r   r(   )r?   r   r<   r   r@   rA   r&   r   )rC   r   r&   r   )rE   rF   rG   r    r&   r   )rC   r   r^   r9   r_   r`   r&   r   )rC   r   r&   r   )r   r   r&   r   )r<   r"   r   r   r&   r   )r&   r   )__name__
__module____qualname__r   DUCKDBr$   __annotations__r-   propertyr1   r4   r7   r;   r=   rB   rD   r]   r   r   r   r   rg   r+   r+   r+   r,   r#   (   s(   
 








	
r#   r   c                   @  s   e Zd ZedddZdS )r   r&   type[DuckDBThen]c                 C  r3   r(   )
DuckDBThenr0   r+   r+   r,   _then   r5   zDuckDBWhen._thenN)r&   r   )r   r   r   r   r   r+   r+   r+   r,   r      s    r   r   c                   @  s   e Zd ZdS )r   N)r   r   r   r+   r+   r+   r,   r      s    r   )3
__future__r   rm   	functoolsr   	itertoolsr   typingr   r   duckdbr   r   duckdb.typingr	   r
   narwhals._duckdb.dataframer   narwhals._duckdb.exprr   narwhals._duckdb.selectorsr   narwhals._duckdb.utilsr   r   r   r   r   r   r   narwhals._expression_parsingr   r   narwhals._sql.namespacer   narwhals._sql.when_thenr   r   narwhals._utilsr   collections.abcr   r   r   narwhals.typingr    r!   r"   r#   r   r   r+   r+   r+   r,   <module>   s2    $	
v 