
    hh9              	         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
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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r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j.        Z. G d de eedef                   Z/ G d de#deef                   Z0 G d de"deef         e          Z1dS )    )annotationsN)reduce)chain)TYPE_CHECKINGAny)CoalesceOperator
ExpressionDuckDBLazyFrame
DuckDBExpr)DuckDBSelectorNamespace)DeferredTimeZoneF
concat_strduckdb_dtypes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                      e Zd ZU ej        Zded<   d1dZed2d	            Z	ed3d            Z
ed4d            Zd5dZd6dZ	 d7d8dZd9dZd:d"Zd;d'Zd<d(Zd=d+Zd>d/Zd?d0ZdS )@DuckDBNamespacer   _implementationversionr   returnNonec                   || _         d S N)_version)selfr&   s     Q/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/_duckdb/namespace.py__init__zDuckDBNamespace.__init__0   s        r   c                *    t          j        |           S r*   )r   from_namespacer,   s    r-   	selectorszDuckDBNamespace.selectors3   s    &5d;;;r/   type[DuckDBExpr]c                    t           S r*   r   r2   s    r-   _exprzDuckDBNamespace._expr7       r/   type[DuckDBLazyFrame]c                    t           S r*   r
   r2   s    r-   
_lazyframezDuckDBNamespace._lazyframe;   s    r/   namestrargsr	   c                    t          |g|R  S r*   )r   )r,   r;   r=   s      r-   	_functionzDuckDBNamespace._function?   s    $t$$$$r/   valuer   c                     t          |          S r*   )r   )r,   r@   s     r-   _litzDuckDBNamespace._litB   s    5zzr/   N	condition	otherwiseExpression | Nonec                l    |t          ||          S t          ||                              |          S r*   )r   rD   )r,   rC   r@   rD   s       r-   _whenzDuckDBNamespace._whenE   s8     	5)))Iu%%//	:::r/   exprsc                    t          | S r*   )r   )r,   rH   s     r-   	_coalescezDuckDBNamespace._coalesceO   s    ''r/   itemsIterable[DuckDBLazyFrame]howr    r   c                  d |D             }t          |          }|d         }|j        |dk    r4t          fd|dd          D                       sd}t          |          |dk    r=|j        }|dd          D ]}t          j        d          }|                    |          S t          d	 |          }|                    |          S )
Nc                    g | ]	}|j         
S  )_native_frame).0items     r-   
<listcomp>z*DuckDBNamespace.concat.<locals>.<listcomp>U   s    ===t*===r/   r   verticalc              3  .   K   | ]}|j         k    V  d S r*   )schema)rR   xrW   s     r-   	<genexpr>z)DuckDBNamespace.concat.<locals>.<genexpr>Y   s*      (O(OV);(O(O(O(O(O(Or/      z&inputs should all have the same schemadiagonalz]
                    from res select * union all by name from _item select *
                c                ,    |                      |          S r*   )union)rX   ys     r-   <lambda>z(DuckDBNamespace.concat.<locals>.<lambda>d   s    !''!** r/   )	listrW   all	TypeErrornativeduckdbsql_with_nativer   )	r,   rK   rM   native_itemsfirstmsgres_itemrW   s	           @r-   concatzDuckDBNamespace.concatR   s     >=u===Ua*S(O(O(O(OU122Y(O(O(O%O%O:CC.. *,C%abb)  j "   %%c***,,l;;!!#&&&r/   r   	separatorignore_nullsboolc               p    dfd}|                      |t           t           | j                  S )Ndfr   r'   list[Expression]c                    t          j         fdD                       }rt          |digS t          |          }t	          t
          j        d |D                       }d |D             }t          | t          |di          gS )Nc              3  .   K   | ]} |          V  d S r*   rP   )rR   erq   s     r-   rY   z;DuckDBNamespace.concat_str.<locals>.func.<locals>.<genexpr>k   s+      <R<RqQQrUU<R<R<R<R<R<Rr/   rm   c              3  >   K   | ]}|                                 V  d S r*   )isnull)rR   ss     r-   rY   z;DuckDBNamespace.concat_str.<locals>.func.<locals>.<genexpr>o   s*      -G-GQahhjj-G-G-G-G-G-Gr/   c              3  J   K   | ]}|                     t                    V  d S r*   )castVARCHAR)rR   cs     r-   rY   z;DuckDBNamespace.concat_str.<locals>.func.<locals>.<genexpr>p   s,      66Aw666666r/   )r   from_iterabler   tupler   operatoror_r   )rq   cols	null_maskcols_strrH   rn   rm   s   `   r-   funcz(DuckDBNamespace.concat_str.<locals>.funcj   s    ).)<<R<R<R<RE<R<R<R)R)RD @"D>I>>??;;Dx|-G-G$-G-G-GHHI66666H)Z%OY%O%OPPQQr/   callevaluate_output_namesalias_output_namesr&   rq   r   r'   rr   )r6   r   r   r+   )r,   rm   rn   rH   r   s    ``` r-   r   zDuckDBNamespace.concat_strg   sj    	R 	R 	R 	R 	R 	R 	R 	R zz"?"G95AM	  
 
 	
r/   c                0    dd} | j         j        |g|R  S )Nr   Iterable[Expression]r'   r	   c                    t          |           } t          t          j        d | D                       }t          t          j        d | D                       }||z  S )Nc              3  P   K   | ]!}t          |t          d                     V  "dS )r   N)r   r   rR   cols     r-   rY   z@DuckDBNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>}   s3      )X)XC*:3A*G*G)X)X)X)X)X)Xr/   c              3  x   K   | ]5}|                                                     t          j                  V  6d S r*   )	isnotnullrz   r   BIGINTr   s     r-   rY   z@DuckDBNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s;      ZZcs}}33M4HIIZZZZZZr/   )r~   r   r   add)r   totalcounts      r-   r   z-DuckDBNamespace.mean_horizontal.<locals>.func{   s`    ;;D8<)X)XSW)X)X)XYYEZZUYZZZ E 5= r/   )r   r   r'   r	   )r6   _from_elementwise_horizontal_op)r,   rH   r   s      r-   mean_horizontalzDuckDBNamespace.mean_horizontalz   s5    	! 	! 	! 	! :tz9$GGGGGr/   	predicate
DuckDBWhenc                :    t                               ||           S )N)context)r   	from_expr)r,   r   s     r-   r   zDuckDBNamespace.when   s    ##It#<<<r/   r"   dtypeIntoDType | Nonec                V     d fd}                      |d d  j                  S )	Nrq   r   r'   rr   c                    t          | j                  }9t          j        |          }t	                                        |          gS t	                    gS r*   )r   rc   r   r+   r   rz   )rq   tztargetr   r,   r@   s      r-   r   z!DuckDBNamespace.lit.<locals>.func   sT    !"),,B 1%KKE

//00JJ<r/   c                    dgS )NliteralrP   _dfs    r-   r_   z%DuckDBNamespace.lit.<locals>.<lambda>   s    yk r/   )r   r   r&   r   r6   r+   )r,   r@   r   r   s   ``` r-   r   zDuckDBNamespace.lit   s[    	  	  	  	  	  	  	  	  zz"9"9#M	  
 
 	
r/   c                H    dd}|                      |d d | j                  S )	Nr   r   r'   rr   c                "    t          d          gS )Nr   )r   r   s    r-   r   z!DuckDBNamespace.len.<locals>.func   s    gJJ<r/   c                    dgS )NlenrP   r   s    r-   r_   z%DuckDBNamespace.len.<locals>.<lambda>   s    ug r/   r   )r   r   r'   rr   r   )r,   r   s     r-   r   zDuckDBNamespace.len   sD    	  	  	  	  zz"5"5#M	  
 
 	
r/   )r&   r   r'   r(   )r'   r   )r'   r4   )r'   r8   )r;   r<   r=   r	   r'   r	   )r@   r   r'   r	   r*   )rC   r	   r@   r	   rD   rE   r'   r	   )rH   r	   r'   r	   )rK   rL   rM   r    r'   r   )rH   r   rm   r<   rn   ro   r'   r   )rH   r   r'   r   )r   r   r'   r   )r@   r"   r   r   r'   r   )r'   r   )__name__
__module____qualname__r   DUCKDBr%   __annotations__r.   propertyr3   r6   r:   r?   rB   rG   rJ   rl   r   r   r   r   r   rP   r/   r-   r$   r$   +   sv         '5&;O;;;;        < < < X<    X    X% % % %    (,	; ; ; ; ;( ( ( (' ' ' '*
 
 
 
&	H 	H 	H 	H= = = =
 
 
 
	
 	
 	
 	
 	
 	
r/   r$   r   c                  &    e Zd Zedd            ZdS )r   r'   type[DuckDBThen]c                    t           S r*   )
DuckDBThenr2   s    r-   _thenzDuckDBWhen._then   r7   r/   N)r'   r   )r   r   r   r   r   rP   r/   r-   r   r      s2           X  r/   r   r   c                      e Zd ZdS )r   N)r   r   r   rP   r/   r-   r   r      s          r/   r   )2
__future__r   r   	functoolsr   	itertoolsr   typingr   r   rd   r   r	   narwhals._duckdb.dataframer   narwhals._duckdb.exprr   narwhals._duckdb.selectorsr   narwhals._duckdb.utilsr   r   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   rP   r/   r-   <module>r      s   " " " " " "              % % % % % % % %  / / / / / / / / 6 6 6 6 6 6 , , , , , , > > > > > >	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	        1 0 0 0 0 0 4 4 4 4 4 4 4 4 * * * * * * J((((((''''''''''''IIIIIIIIII

u
 u
 u
 u
 u
*.@*LMu
 u
 u
p    *J
BC    V U U U U*J
BCZ U U U U Ur/   