
    ɯeiJ             
       x   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ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 d dlZd dl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&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB ddlCmDZDmEZEmFZF ee(e*f   ZGe2de<de;diZH ej                  eJ      ZK G d d      ZL G d d      ZM G d d      ZNd ZO eOd      de(de(fd       ZP eOd      de(de(fd       ZQde(de(fd ZR eOd!      de(de(fd"       ZS eOd#eS$      de(de(fd%       ZT eOd&      de(de(fd'       ZU eOd(      de(de(fd)       ZV eOd*      dee*e(f   de(fd+       ZW eOd,      d-ee*e(f   d.ee*e(f   de(fd/       ZX eOd0      d-ee*e(f   d.ee*e(f   de(fd1       ZY eOd2      d-ee*e(f   d.ee*e(f   de(fd3       ZZ eOd4      d5e(de(fd6       Z[ eOd7e[$      dee*e(f   de(fd8       Z\ eOd9      de(de(fd:       Z] eOd;      d-e(d.e(de(fd<       Z^ eOd=      de(d>e_de(fd?       Z` eOd@      dA        Za eOdB      de(dCebd>e_fdD       Zc eOdE      de(fdF       Zd eOdG      de(fdH       Ze eOdI      dJe(dKe(fdL       Zf eOdM      	 	 dde(dNebdOe_fdP       Zg eOdQdRS      dT        Zh eOdUdRS      dV        Zi eOdWdRS      dX        Zj eOdY      dZ        Zk eOd[      d\        Zl eOd]      d^e(d_e(fd`       Zm eOda      db        Zn eOdc      	 	 	 ddde(deeeo   dfeeo   dOe_fdg       Zp eOdh      	 	 dde(dNeeb   dOe_fdi       Zq	 	 	 dde(dNee(   dOe_dje_fdkZr eOdl      	 	 dde(dNee(   dOe_fdm       Zs eOdn      	 	 dde(dNee(   dOe_fdo       Zt eOdp      	 	 dde(dNee(   dOe_fdq       Zu eOdr      	 	 dde(dNee(   dOe_fds       ZvdtedOe_duefdvZw eOdw      	 	 dde(dNeeb   dOe_de(fdx       Zx eOdy      de(fdz       Zy eOd{      	 dde(dNeeb   dOe_de(fd|       Zz eOd}      dde(dOe_de(fd~       Z{ eOd      	 dde(dNebdOe_de(fd       Z| eOd      d        Z} eOd      de(de(de(fd       Z~ eOd      d^e(d_e(fd       Z eOd      d^e(d_e(fd       Z eOddR      deofd       Z eOd      de(fd       Z eOd      de(fd       Z eOd      de(fd       Z eOd      de(fd       Z eOd      de(fd       Zd Z eOd      de(de(fd       Z eOd      de(de(fd       Zd Zd Zd ZddZ eOd      debde(de(de(fd       Z eOd      debde(fd       Z eOd      debde(de(fd       Z eOd      debde(de(de(fd       Z ed      Zdededeeef   fdZdededeeef   fdZdededeeef   fdZ eOd      de(fd       Z eOd      de(fd       Z eOd      de(fd       Z eOd      de(fd       Z eOd      de(fd       Z ede
j:                  z         Zdeeb   deeb   debfdńZ eOdƫ      de(de(fdȄ       Z eOdɫ      de(de(fd˄       Z eOddRS      d̈́        Z eOddRS      dτ        Z eOdЫ      de(de(de(fdӄ       Z eOdԫ      de(de(fdք       Z eOd׫      de(de(fd؄       Z	 dde(de)dOe_dee(   fdۄZ eOdܫ      de(d^e(d_e(fdބ       Z eOddRS      ddeeo   de(fd       ZddZ eOddRdR      de(deode(fd       Z eOddRdR      de(deode(fd       Z eOddRdR      de(deode(fd       Z eOddRdR      de(deode(fd       Z eOddRdR      deode(deode(fd       Z eOd      de(fd       Zy)    N)Decimal)partialreduce)Real)randint)AnyCallableOptionalTupleTypeVarUnion)FunctionExpressionNamedFunctionExpression)unalias_datetime_part)numpypandas)_TIMESTAMP_TYPE_MAPPING _TIMESTAMP_TYPE_TIMEZONE_MAPPINGColumnEmulator
ColumnTypeTableEmulatorget_coerce_result_type)SnowparkLocalTestingException)	ArrayType
BinaryTypeBooleanTypeDateTypeDecimalType
DoubleType	FloatTypeLongTypeMapTypeNullType
StringTypeTimestampTimeZoneTimestampTypeTimeTypeVariantType_FractionalType_IntegralType_NumericType   )LocalTestOOBTelemetryService)-convert_numeric_string_value_to_float_seconds!convert_snowflake_datetime_format+process_string_time_with_fractional_secondsz
YYYY-MM-DDz
HH24:MI:SSz YYYY-MM-DD HH24:MI:SS.FF3 TZHTZMc                   f    e Zd Z	 	 	 	 ddededed    dee   dee   dee   ddfd	Zd
 ZddddZ	y)MockedFunctionNnamefunc_implementationdistinctpass_column_indexpass_row_indexpass_input_datareturnc                 `    || _         || _        |xs | | _        || _        || _        || _        y N)r3   implr5   _pass_row_index_pass_column_index_pass_input_data)selfr3   r4   r5   r6   r7   r8   s          d/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/snowpark/mock/_functions.py__init__zMockedFunction.__init__Q   s6     	'	 (D-"3 /    c                     t        d |D              sKt        |      dk(  r=|j                  t        |            j                  d      }|j                  |_        |S |S )Nc              3   H   K   | ]  }t        |t        t        f        y wr;   )
isinstancer   r   ).0args     rA   	<genexpr>z8MockedFunction._check_constant_result.<locals>.<genexpr>k   s     UJs^]$CDUs    "r,   Tdrop)anylenrepeatreset_indexsf_type)r@   
input_dataargsresultresizeds        rA   _check_constant_resultz%MockedFunction._check_constant_resulta   sT     UPTUUFq mmC
O4@@d@KG$nnGONrC   )rQ   
row_numberc                v   | j                   r||d<   | j                  r't        |j                        j                  |      |d<   | j                  r|j                  |d<    | j
                  |i |}|E| j                  s9| j                  s-| j                  ||t        |j                               z   |      S |S )N	raw_input	row_indexcolumn_index)	r?   r=   listindexr>   r<   rU   tuplevalues)r@   rQ   rV   rR   kwargsrS   s         rA   __call__zMockedFunction.__call__t   s      ",F;"&z'7'7"8">">z"JF;""%/%5%5F>"D+F+ "++((..D5#996  rC   )NNNN)
__name__
__module____qualname__strr	   r
   boolrB   rU   r`    rC   rA   r2   r2   P   s|    
 04,0)-*.00 &0 +,	0
 $D>0 !0 "$0 
0 & *.$ rC   r2   c                       e Zd ZdZ ej
                         ZddZedd       Z	de
eeef   dee   fdZde
eef   dedefd	Zde
eef   fd
Zy)MockedFunctionRegistryNr9   c                 T    t               | _        t        j                         | _        y r;   )dict	_registry	threadingRLock_lock)r@   s    rA   rB   zMockedFunctionRegistry.__init__   s    __&
rC   c                     | j                   5  | j                  t               | _        d d d        | j                  S # 1 sw Y   | j                  S xY wr;   )
_lock_init	_instancerh   clss    rA   get_or_createz$MockedFunctionRegistry.get_or_create   sE    ^^ 	9}}$ 6 8	9 }}	9 }}s	   =Afuncc                 j   t        |t              r|}d}n7t        |t              r|j                  }d}n|j                  }|j                  }|j                         }| j                  5  || j                  vr
	 d d d        y | j                  |   }d d d        |rj                  S S # 1 sw Y   xY wNF)	rF   rd   r   r3   is_distinctlowerrn   rk   r5   )r@   ru   	func_namer5   functions        rA   get_functionz#MockedFunctionRegistry.get_function   s     dC IH56		IH		I''HOO%	ZZ 	1.	1 	1 ~~i0H		1 %-x  :(:	1 	1s   )B)B))B2snowpark_funcr4   c                     t        |t              r|n|j                  }t        ||g|i |}| j                  5  || j
                  |<   d d d        |S # 1 sw Y   |S xY wr;   )rF   rd   ra   r2   rn   rk   )r@   r}   r4   rR   r_   r3   mocked_functions          rA   registerzMockedFunctionRegistry.register   sg     (s;MAWAW 	 )/BTTTVTZZ 	3#2DNN4 	3	3s   AAc                     t        |t              r|n|j                  }| j                  5  || j                  v r| j                  |= d d d        y # 1 sw Y   y xY wr;   )rF   rd   ra   rn   rk   )r@   r}   r3   s      rA   
unregisterz!MockedFunctionRegistry.unregister   sS    
 (s;MAWAW 	 ZZ 	)t~~%NN4(	) 	) 	)s   AA)r9   N)r9   rh   )ra   rb   rc   rq   rl   Lockrp   rB   classmethodrt   r   r   r   rd   r
   r2   r|   r	   r   r   rf   rC   rA   rh   rh      s    I!J'  ;,.EsJK;	.	!;,S(]+ & 

)S(]+
)rC   rh   c                       e Zd ZU dZdZeej                     ed<   e	d
deej                     ddfd       Z
e	deej                     deej                     fd       Ze	dej                  dej                  fd	       Zy)LocalTimezonez
    A singleton class that encapsulates conversion to the local timezone.
    This class allows tests to override the local timezone in order to be consistent in different regions.
    NLOCAL_TZtzr9   c                     || _         y)zoOverrides the local timezone with the given value. When the local timezone is None the system timezone is used.N)r   )rs   r   s     rA   set_local_timezonez LocalTimezone.set_local_timezone   s     rC   dc                 B    ||j                  | j                        S |S )z1Converts an input datetime to the local timezone.)r   )
astimezoner   rs   r   s     rA   to_local_timezonezLocalTimezone.to_local_timezone   s"    
 12q||s|||,D1DrC   c                 :    |j                  | j                        S )zPReplaces any existing tz info with the local tz info without adjusting the time.tzinfo)replacer   r   s     rA   
replace_tzzLocalTimezone.replace_tz   s     yyy--rC   r;   )ra   rb   rc   __doc__r   r
   datetimetimezone__annotations__r   r   r   r   rf   rC   rA   r   r      s    
 -1Hhx(()0HX->->$? 4   E**+E	(##	$E E .8,, .1B1B . .rC   r   c                       fd}|S )Nc                 X    t        j                         j                  | gi }|S r;   )rh   rt   r   )mocking_functionr   rR   r{   r_   s     rA   	decoratorzpatch.<locals>.decorator   s:    0>>@II&
)-
17
 rC   rf   )r{   rR   r_   r   s   ``` rA   patchr      s     rC   mincolumnr9   c                 f   t        | j                  j                  t              r0t	        t        | j                         d      | j                        }n3t	        | j                         j                         | j                        }	 t        j                  |d         rU| | j                  d          j                         j                  j                         }|st	        d g| j                        S t	        || j                        S # t        $ r t	        || j                        cY S w xY w)N   datarP   r   c                 
    | d uS r;   rf   xs    rA   <lambda>zmock_min.<locals>.<lambda>   
    atm rC   )rF   rP   datatyper+   r   roundr   dropnamathisnanapplyisnar^   rL   	TypeErrorr   rescolumn_has_nas      rA   mock_minr      s     %

a"8&..Q&--/"5"5"7P
@::c!f v||$;<=BBDKKOOQ  !%D66>>JJ3?? @3??@   	A,D 6D  D0/D0maxc                 f   t        | j                  j                  t              r0t	        t        | j                         d      | j                        }n3t	        | j                         j                         | j                        }	 t        j                  |d         rU| | j                  d          j                         j                  j                         }|st	        d g| j                        S t	        || j                        S # t        $ r t	        || j                        cY S w xY w)Nr   r   r   c                 
    | d uS r;   rf   r   s    rA   r   zmock_max.<locals>.<lambda>  r   rC   )rF   rP   r   r+   r   r   r   r   r   r   r   r   r^   rL   r   r   s      rA   mock_maxr     s    &..))<8%

a"8&..Q&--/"5"5"7P
@::c!f v||$;<=BBDKKOOQ  !%D66>>JJ3?? @3??@r   c                    d}d}| D ]A  }|	 t        j                  |      rt         j                  }d} n	 d}	 |t	        |      z  }C t        | j                  j                  t              rZ| j                  j                  j                  | j                  j                  j                  }}t        t        d|dz         |      }n| j                  j                  }|s,t        |gt!        || j                  j"                        	      S t        d gt!        || j                  j"                        	      S # t        $ r Y w xY w# t
        $ r&}t        j                  |d| d       Y d }~ld }~ww xY w)
NTr   FNumeric value 'z' is not recognized.)error_message&      r   )r   r   nanr   float
ValueErrorr   raise_from_errorrF   rP   r   r   	precisionscaler   r   r   nullable)r   all_item_is_noner   r   excpsnew_types           rA   _sumr     sc   
C ::d#((C',$ $  %uT{"  &..));7~~&&00&..2I2I2O2O1s2q2v2>>**
   	
8V^^5L5L M	

 Hfnn6M6M!N
  
  ->>>R'S s(   'D9E	9	EE		E8E33E8sum_distinctc                 b    t        | j                         | j                        } t        |       S )Nr   )r   uniquerP   r   r   s    rA   mock_sum_distinctr   ;  s"    &..IF<rC   sum)r5   c                     t        |       S r;   )r   r   s    rA   mock_sumr   A  s    <rC   avgc                    t        | j                  j                  t        t        f      s"t        d| j                  j                         t        | j                  j                  t              s| j                         j                         r t        d gt        t	               d            S t        | j                  j                  t              rt        dd      }nt        | j                  j                  t              r| j                  j                  j                  | j                  j                  j                  }}t        d| j                  j                  j                  dz         }|dk  r|dz   }n|dk  rd}t        ||      }n0t        | j                  j                  t              sJ t!               }| | j                             }|j#                         }t        |t              r6d|j                   d}|j%                  |      }t'        j(                  |      }t        |gt        |d	            S )
Nz'Cannot compute avg on a column of type Tr   r      r   z{:.zf}F)rF   rP   r   r+   r#   r   r   allr   r   r*   r   r   r   r   r)   r    meanformatdecimalr   )r   res_typer   r   notnar   
fmt_stringres_formatteds           rA   mock_avgr   F  s   fnn--h/GH+5fnn6M6M5NO
 	
 &..))848I8I8KD6:hj$3OPP	FNN++]	;r1%	FNN++[	9NN##--NN##)) 	 FNN33==BC	A:AIERZEy%0&..11?CCC;FKKM>"E
**,C(K(HNN+3/
"))#.oom,uj5.IJJrC   stddevc                 0   t        | j                  j                  t        t        f      s"t        d| j                  j                         t        | j                  j                  t              s| j                         j                         r t        d gt        t	               d            S t        | j                  j                  t              rt        dd      }nt        | j                  j                  t              r| j                  j                  j                  | j                  j                  j                  }}t        d| j                  j                  j                  dz         }|dk  r|dz   }n|dk  rd}t        ||      }n0t        | j                  j                  t              sJ t!               }| | j                             }|j#                         }t        |t$              rt'        |      }t        |gt        |d            S )Nz*Cannot compute stddev on a column of type Tr   r   r   r   F)rF   rP   r   r+   r#   r   r   r   r   r   r*   r   r   r   r   r)   r    stdr   r   )r   r   r   r   r   r   s         rA   mock_stddevr   i  s   fnn--h/GH+89P9P8QR
 	
 &..))848I8I8KD6:hj$3OPP	FNN++]	;r1%	FNN++[	9NN##--NN##)) 	 FNN33==BC	A:AIERZEy%0&..11?CCC;FKKM>"E
))+C(G$Cuj5.IJJrC   approx_percentile_accumulatec                 j    t         j                  d       t        dt        t	               d            S Nz>TODO SNOW-1800512: Returns dummy value of 42 now, need to fix.*   Fr   _loggerwarningr   r   r    r   s    rA   !mock_approx_percentile_accumulater     s'    
 OOTUr:ik5+IJJrC   approx_percentile_estimatecolumn1column2c                 j    t         j                  d       t        dt        t	               d            S r   r   r   r   s     rA   mock_approx_percentile_estimater     '     OOTUr:ik5+IJJrC   
covar_sampc                 j    t         j                  d       t        dt        t	               d            S r   r   r   s     rA   mock_covar_sampr     r   rC   corrc                 j    t         j                  d       t        dt        t	               d            S r   r   r   s     rA   mock_corr_sampr     r   rC   count_distinctcolsc                  N   t               }t        t        |             D ]  }| |   || |   j                  <    |j	                         }||j
                     j                  d d      j	                         }|j                         }t        |t        t               d            S )z
    Snowflake does not count rows that contain NULL values, in the mocking implementation
    we iterate over each row and then each col to check if there exists NULL value, if the col is NULL,
    we do not count that row.
    c                     t        |       S r;   )r]   )rows    rA   r   z%mock_count_distinct.<locals>.<lambda>  s
    c
 rC   r,   axisFr   )r   rangerM   r3   r   columnsr   nuniquer   r   r!   )r   dficombinedr   s        rA   mock_count_distinctr    s     
B3t9 #747<<#	B"**~##$:#CJJLH



CsJxz5,IJJrC   countc                     t        | t              r/| j                         }t        |t        t	               d            S t        t        |       t        t	               d            S NFr   )rF   r   r  r   r!   rM   )r   count_columns     rA   
mock_countr    sH    &.)||~<HJPU9VWW3v;
8:u8UVVrC   medianc                    t        | j                  j                  t              rOt        | j                  j                  j                  dz   | j                  j                  j
                  dz         }n| j                  j                  }t        | j                  rt        | j                         d      nd gt        || j                  j                              S )N   r   r   )rF   rP   r   r   r   r   r   sizer   r  r   r   )r   return_types     rA   mock_medianr    s    &..));7!NN##--16>>3J3J3P3PST3T
 nn--*0++U6==?A&D6;(?(?@ rC   	covar_popc           	         d}d\  }}}t        | |      D ]  \  }}|t        j                  |      s|pt        j                  |      r[t        t        j                  t        t               | j                  j                  xs |j                  j                              c S ||t        j                  |      rt        j                  |      r|dz  }|||z  z  }||z  }||z  } |||z  |z  z
  |z  }t        |t        t               | j                  j                  xs |j                  j                              S )Nr   )r   r   r   r   r,   )	zipr   r   r   r   r   r   rP   r   )	r   r   non_nan_cntx_sumy_sumx_times_y_sumr   yr   s	            rA   mock_covar_popr    s"   K")E5-GW% 1Mdjjm4::a=!XX"L'//":":"Vgoo>V>V  =Q]4::a=TU1KQU"MQJEQJE EEMK77;FDL'//22Ngoo6N6N
 rC   	array_aggrx   c                     |rt        | j                               n| }t        t        |j                               gt	        t               d            S r	  )r   r   r[   r   r   r   )r   rx   columns_datas      rA   mock_array_aggr    sC    6A>&--/2vL<&&()*9;. rC   array_constructc                      t        |       dk(  rg g}n)t        j                  | d      j                  d d      }t	        |t        t               d            S )Nr   r,   r   c                     t        |       S r;   )r[   r   s    rA   r   z&mock_array_construct.<locals>.<lambda>   s
    d1g rC   FrP   )rM   r   concatr   r   r   r   )r  r   s     rA   mock_array_constructr$    sL    
7|qt}}W1-334EA3N$
9;(FGGrC   listagg	delimiterc           
         |rt        | j                               n| }t        |j                  |j                         D cg c]  }t	        |       c}      t        t        d      | j                  j                              S c c}w )Ni   r   )	r   r   joinr   rd   r   r$   rP   r   )r   r&  rx   r  vs        rA   mock_listaggr*    sc    6A>&--/2vL^^\-@-@-BCSVCD:h/1H1HI Cs    Bsqrtc                     | j                  t        j                        }t        t	               | j
                  j                        |_        |S r;   )r   r   r+  r   r    rP   r   r   rS   s     rA   	mock_sqrtr.    s3    \\$))$F	V^^-D-DEFNMrC   lnc                     | j                  t        j                        }t        t	               | j
                  j                        |_        |S r;   )r   r   logr   r    rP   r   r-  s     rA   mock_lnr2    s3    \\$((#F	V^^-D-DEFNMrC   powleftrightc                     | j                  |d       }t        t               | j                  j                        |_        |S )Nc                     | |z  S r;   rf   )lrs     rA   r   zmock_pow.<locals>.<lambda>  s
    ad rC   )combiner   r    rP   r   )r4  r5  rS   s      rA   mock_powr;    s2    \\%!23F	T\\-B-BCFNMrC   to_datefmttry_castc                 p    t         j                  j                  t              r j	                         S ddlt        |t              s$t        |gt               z   j                        } fd} j                  ||      }t        t                j                  j                        |_        |S )a  
    https://docs.snowflake.com/en/sql-reference/functions/to_date

    Converts an input expression to a date:

    [x] For a string expression, the result of converting the string to a date.

    [x] For a timestamp expression, the date from the timestamp.

    For a variant expression:

        [x] If the variant contains a string, a string conversion is performed.

        [x] If the variant contains a date, the date value is preserved as is.

        [x] If the variant contains a JSON null value, the output is NULL.

        [x] For NULL input, the output is NULL.

        [x] For all other values, a conversion error is generated.
    r   Nr\   c                    	 |d u xs |j                         dk(  }t        |d      \  }}| y t        j                  j                  t
              r| j                         S t        j                  j                  t              r| j                         r6t        j                  j                  t        |             j                         S |r)j                  j                  |       j                         S t        j                  j                  | |      j                         S t        j                  j                  t              r|<|rt!        |      j                         dk7  st#        j$                  t'        d             t        | t               ro| j                         r6t        j                  j                  t        |             j                         S j                  j                  |       j                         S t        | t        j                        r| S t#        j$                  t'        d|  d             y t#        j$                  t'        dt)        j                  j                        j*                                y # t,        $ r&}rY d }~y t#        j$                  |       Y d }~y d }~ww xY w)Nautoz%Y-%m-%ddefault_formatzX[Local Testing] to_date function does not allow format parameter for data of VariantTypez8[Local Testing] Unsupported conversion to_date of value  of VariantTypez<[Local Testing] Unsupported conversion to_date of data type )ry   r/   rF   rP   r   r&   dater$   isdigitr   utcfromtimestampr.   parserparsestrptimer(   rd   r   r   r   typera   BaseException)	r   _fmtauto_detectdate_format_r   r   dateutilr>  s	         rA   convert_datez"mock_to_date.<locals>.convert_dateG  s+   6	D$,@$**,&*@K>ZNK |&..11=Ayy{"FNN33Z@<<>#,,==EdKdf #'44T:??AA'0099$LQQSSFNN33[A#d)//2Cv2M1BB!v
 dC(||~'00AAI$O $&!
  (44T:??AAhmm4K1BB!VW[V\\kl .>>VW[\b\j\j\s\sWtW}W}V~
  	D->>sCC		DsU   +J 3J #A)J *J 8-J &B7J (J J #"J AJ 	J>J9J99J>)rF   rP   r   r   copydateutil.parserr   rM   r\   r:  r   r   )r   r=  r>  rS  r   rR  s   ` `  @rA   mock_to_daterV  #  s    8 &..))84{{}c>*cUS[0E7Dr ..l
+CXZ)@)@ACKJrC   current_timestampT)r6   c                     t        t        j                  j                         gt        |       z  t	        t        t        j                        d            S r	  )r   r   nowrM   r   r&   r%   LTZ)rZ   s    rA   mock_current_timestampr[    sD    ##%&\)::=):)>)>?G rC   current_datec                     t         j                   j                         }t        |j                         gt	        |       z  t        t               d            S r	  )r   rY  r   rF  rM   r   r   rZ   rY  s     rA   mock_current_dater_    C    





!Chhj\C--z(*e7T rC   current_timec                     t         j                   j                         }t        |j                         gt	        |       z  t        t               d            S r	  )r   rY  r   timerM   r   r'   r^  s     rA   mock_current_timerd    r`  rC   hourc                     t        | D cg c]  }|d n|j                   c}t        t               d            S c c}w r	  )r   re  r   r!   exprvalues     rA   	mock_hourrj    s9    AEFemd3F8:u- F   <minutec                     t        | D cg c]  }|d n|j                   c}t        t               d            S c c}w r	  )r   rl  r   r!   rg  s     rA   mock_minutern    s9    CGH%emd5H8:u- Hrk  containsexpr1expr2c           
         t        | t              r:t        |t              r*t        t        t        |      t        |       v       g      S t        | t              rLt        |t              r<t	        | |      D cg c]$  \  }}t        t        |      t        |      v       & }}}nyt        | t              r=t        |t              r-| D cg c]!  }t        t        |      t        |      v       # }}n,|D cg c]!  }t        t        |      t        |       v       # }}t        |t        t               | j                  j                              S c c}}w c c}w c c}w )Nr   r   )	rF   rd   r   re   r  r   r   rP   r   )rp  rq  item1item2r   items         rA   mock_containsrw    s   %*UC"8DUs5z)A$B#CDD%(Z~-NBEeUBST,%tCJ#e*,-TT	E>	*z%/E9>?tCJ#d)+,??9>?tCIU+,??*[]EMM4J4JK  U??s   :)E&E8&Eabsc                 ~    t        | t              r#| j                         }| j                  |_        |S t        |       S r;   )rF   r   rx  rP   rh  rS   s     rA   mock_absr{    s1    $'4yrC   
to_decimaler   r   c                 r   d dt         t        t        t        t        f   ffdt        | j                  j                  t        t        t        f      r| j                  fd      }nt        | j                  j                  t        t        f      r'| j                  ddi      j                  fd      }n5t        j                   t#        d| j                  j                                t%        t'              | j                  j(                  xs j*                  	      _        |S )
a  
    [x] For NULL input, the result is NULL.

    [x] For fixed-point numbers:

        Numbers with different scales are converted by either adding zeros to the right (if the scale needs to be increased) or by reducing the number of fractional digits by rounding (if the scale needs to be decreased).

        Note that casts of fixed-point numbers to fixed-point numbers that increase scale might fail.

    [x] For floating-point numbers:

        Numbers are converted if they are within the representable range, given the scale.

        The conversion between binary and decimal fractional numbers is not precise. This might result in loss of precision or out-of-range errors.

        Values of infinity and NaN (not-a-number) result in conversion errors.

        For floating-point input, omitting the mantissa or exponent is allowed and is interpreted as 0. Thus, E is parsed as 0.

    [x] Strings are converted as decimal, integer, fractional, or floating-point numbers.

    [x] For fractional input, the precision is deduced as the number of digits after the point.

    For VARIANT input:

        [x] If the variant contains a fixed-point or a floating-point numeric value, an appropriate numeric conversion is performed.

        [x] If the variant contains a string, a string conversion is performed.

        [x] If the variant contains a Boolean value, the result is 0 or 1 (for false and true, correspondingly).

        [x] If the variant contains JSON null value, the output is NULL.
    c                 V    | d   dv r| dd  j                         S | j                         S )Nr   )-+r,   )rG  )r   s    rA   
is_str_intz#mock_to_decimal.<locals>.is_str_int  s,    Q4:QR5==?"yy{rC   r   c                     t        |             rt        |       n
t        |       } | t        j                  t        j                   t        j
                  fv rt        j                  t        d             t        |       dk  rdn0t        j                  t        j                  t        |                   }|kD  rt        d|  d      t        |z
        }t        t        t        | |                  S )Nz9Values of infinity and NaN cannot be converted to decimalr,   r   z' is out of range)rd   intr   r   infr   r   r   r   rx  ceillog10r   r   r   )r   integer_part_lenremaining_decimal_lenr  r   r   s      rA    cast_as_float_convert_to_decimalz9mock_to_decimal.<locals>.cast_as_float_convert_to_decimal  s     !Q(CFeAhDHH9dhh//)::VW !$A
1		$**SV:L0Mi'//!DU0VWW #I0@$@% Hs5$9:;<<rC   c                     t        |       S r;   try_convertr   r  r>  s    rA   r   z!mock_to_decimal.<locals>.<lambda>      k"BHaP rC   Er   c                     t        |       S r;   r  r  s    rA   r   z!mock_to_decimal.<locals>.<lambda>  r  rC   z!Invalid input type to TO_DECIMAL r   )r   r   r   rd   re   rF   rP   r   r+   r   r#   r   r$   r(   r   r   r   r   r   r   r   hasnans)r}  r   r   r>  r   r  r  s    ``` @@rA   mock_to_decimalr    s    R
=E'5#t2K,L = !))$$|[(&KLggP
 
AII&&[(A	Biia!''P
 	&669!)):L:L9MNO	
 Iu%		0B0B0QckkCK JrC   to_timec           	      (   ddl }dt        fd}dt        dt        dt        fd}t        | j                  j
                  t              r| j                         S g }t        |t              s|gt        |       z  }t        | |      D ]  \  }}||j                  d       | j                  j
                  }		 t        |d	
      \  }
}|du xs t        |      j                         dk(  }t        |	t              ro|j                         r|j                   ||             n{|j                  |r)|j                   j#                  |      j%                         n	 |||
|             n5t        |	t&              r!|j                  |j%                                nt        |	t(              rt        |t              ra|j                         r|j                   ||             n|j                  |j                   j#                  |      j%                                nt        |t*        j$                        r|j                  |       nWt-        j.                  t1        d| d             n4t-        j.                  t1        dt3        |	      j4                                 t        |t9        t               | j                  j:                              S # t6        $ r4}|r|j                  d       nt-        j.                  |       Y d}~d}~ww xY w)a  
    https://docs.snowflake.com/en/sql-reference/functions/to_time

    [x] For string_expr, the result of converting the string to a time.

    [x] For timestamp_expr, the time portion of the input value.

    [x] For 'integer' (a string containing an integer), the integer is treated as a number of seconds, milliseconds, microseconds, or nanoseconds after the start of the Unix epoch. See the Usage Notes below.

        [x] For this timestamp, the function gets the number of seconds after the start of the Unix epoch. The function performs a modulo operation to get the remainder from dividing this number by the number of seconds in a day (86400): number_of_seconds % 86400

    r   Nr   c                 t    t         j                   j                  t        |       dz        j                         S )NiQ )r   rH  r.   rc  )r   s    rA   convert_int_string_to_timez0mock_to_time.<locals>.convert_int_string_to_time(  s/      119!<uD

$&	rC   _data_time_format_fractional_secondsc                    | j                  d      }t        |      dk(  rn|d   }d}|t        |      k  r:||   j                         r'|dz  }|t        |      k  r||   j                         r'|d t        ||       ||d  z   }|d    d| } t        |      dk(  rd|v r|j	                  dd      }t
        j
                  j                  t        | |      |      }|j                         S )N.   r,   r   z.%f )	splitrM   rG  r   r   r   rK  r0   rc  )r  r  r  
data_partsseconds_partidxtarget_datetimes          rA   convert_string_to_timez,mock_to_time.<locals>.convert_string_to_time-  s   [[%
z?a%a=LCL))l3.?.G.G.Iq L))l3.?.G.G.I <s3(;<=ST@RR  "!}oQ|n5E z?aE\$9'//r:L"++447?RS
 ##%%rC   z%H:%M:%SrC  rB  z8[Local Testing] Unsupported conversion to_time of value rE  z<[Local Testing] Unsupported conversion to_time of data type r   )rU  rd   r  rF   rP   r   r'   rT  r   rM   r  appendr/   ry   r$   rG  rI  rJ  rc  r&   r(   r   r   r   r   rL  ra   rM  r   r   )r   r=  r>  rR  r  r  r   r   rN  r   time_fmtfractional_secondsrO  r   s                 rA   mock_to_timer    s   $ c 
&c & &SV &2 &..))84{{}
Cc>*ec&k!&#& /D
d<JJt>>***	D 2$zR"$,E#d)//*;v*EK(J/<<>JJ9$?@JJ& !--d388:3D(DVW
 Hm4

499;'Hk2dC(||~

#=d#CD 

8??#8#8#>#C#C#EFhmm4JJt$1BB"VW[V\\kl .>>VW[\dWeWnWnVopM/Df *XZ1H1HI   	DD!->>sC		Ds   8G&K	L)LLadd_timezonec                     t               dk(  rg S ddlt        t              s$t        gt               z   j                        n fd} j                         j                  |d      j                  t        j                  di      }|D cg c]"  }|t        |d      r|j                         n|$ c}S c c}w )a  
     https://docs.snowflake.com/en/sql-reference/functions/to_timestamp

    [x] For NULL input, the result will be NULL.

    [x] For string_expr: timestamp represented by a given string. If the string does not have a time component, midnight will be used.

    [x] For date_expr: timestamp representing midnight of a given day will be used, according to the specific timestamp flavor (NTZ/LTZ/TZ) semantics.

    [x] For timestamp_expr: a timestamp with possibly different flavor than the source timestamp.

    [x] For numeric_expr: a timestamp representing the number of seconds (or fractions of a second) provided by the user. Note, that UTC time is always used to build the result.

    For variant_expr:

        [x] If the variant contains JSON null value, the result will be NULL.

        [x] If the variant contains a timestamp value of the same kind as the result, this value will be preserved as is.

        [x] If the variant contains a timestamp value of the different kind, the conversion will be done in the same way as from timestamp_expr.

        [x] If the variant contains a string, conversion from a string value will be performed (using automatic format).

        [x] If the variant contains a number, conversion as if from numeric_expr will be performed.

    [x] If conversion is not possible, an error is returned.

    If the format of the input parameter is a string that contains an integer:

        After the string is converted to an integer, the integer is treated as a number of seconds, milliseconds, microseconds, or nanoseconds after the start of the Unix epoch (1970-01-01 00:00:00.000000000 UTC).

        [x] If the integer is less than 31536000000 (the number of milliseconds in a year), then the value is treated as a number of seconds.

        [x] If the value is greater than or equal to 31536000000 and less than 31536000000000, then the value is treated as milliseconds.

        [x] If the value is greater than or equal to 31536000000000 and less than 31536000000000000, then the value is treated as microseconds.

        [x] If the value is greater than or equal to 31536000000000000, then the value is treated as nanoseconds.
    r   Nr@  c                 	   | j                      }| j                  d   }|d u xs t        |      j                         dk(  }d}t	        |t
        j                        st        ||      \  }}nd\  }}|y 	 j                  j                  }t	        |t              r|}nt	        |t              rHt        j                  j                  |t        j                  j                  j                               }n/t	        |t               r|j#                         rO|rMt        j                  j%                  t'        |            }r|j)                  t*        j,                        }n|rj.                  j1                  |      }nt        j                  j3                  t5        ||      |      }nut	        |t6              rd|rt9        |      nd}	|d|	z  z  }t        j                  j%                  t'        |            }r#|j)                  t*        j,                        }nt	        |t:              rt	        |t
        j                        rǉrt        j                  j=                  t>        j@                        }
t        j                  j%                  |      t        jB                  |
jE                         jG                               z   }|S t        j                  j%                  |      }r1|j)                  t*        j,                        }nt	        |t              rw|j#                         rKt        j                  j%                  t'        |            }r|j)                  t*        j,                        }nj.                  j1                  |      }nt	        |t        j                        r|}nktI        jJ                  tM        d	| d
             nHtI        jJ                  tM        dtO        j                  j                        jP                                r#r!|jR                  t>        jU                  |      }|S # tV        $ r&}rY d }~y tI        jJ                  |       Y d }~y d }~ww xY w)Nr   rB  z%Y-%m-%d %H:%M:%S.%frC  Nr   r   
   )secondsz>[Local Testing] Unsupported conversion to_timestamp* of value rE  zB[Local Testing] Unsupported conversion to_timestamp* of data type ),r3   ilocrd   ry   rF   numbersNumberr/   rP   r   r&   r   r   r:  r   rc  r$   rG  rH  r.   r   pytzutcrI  rJ  rK  r0   r+   r  r(   rY  r   r   	timedelta	utcoffsettotal_secondsr   r   r   rL  ra   r   r   rM  )r   rN  r   rO  rD  timestamp_formatr  r   parsedr   	local_nowr   r  r   rR  enforce_ltzr=  r>  s               rA   convert_timestampz(_to_timestamp.<locals>.convert_timestamp  s   388}xx{dlAc$ioo&76&A/$/ 2$~V " 4;00<_	D~~..H(M2Hh/!**2249J9J9N9N9S9S9UVHj1<<>k%..??EdKF $!'txx!@"!)!6!6t!<!)!2!2!;!;G $&8 -	" Hl3%)D	qb%i'!**;;A$G  #^^488^<FHk2dGNN3 #$,$5$5$9$9-:P:P$Q	!)!2!2!C!C "$..$-$7$7$9$G$G$I"
  &!)!2!2!C!CD!I#!'txx!@c*||~!)!2!2!C!CI$O" (%+^^488^%DF "*!6!6t!<h&7&78!F1BB!\]a\bbqr .>>\]abhbpbpbyby]z  ^D  ^D  ]E  F ,6==+@&11&9M 	D->>sCC		Ds&   8IQ E9Q 	Q?Q: Q::Q?r,   r   to_pydatetime)rM   rU  rF   r   r\   to_framer   r   r   NaThasattrr  )	r   r=  r>  r  r  r  r   r   rR  s	   `````   @rA   _to_timestampr    s    \ 6{a	 #~. 	us6{*&,,? pD pDd //

!
!"3!
!
<
D
DfjjRVEW
XC  ]wq//JPQQ  s   'C	to_timestampc                     t        | ||      }t        t        t        t                 | j
                  j                        |_        |S r;   )mock_to_timestamp_ntzr   r&   r   r   rP   r   )r   r=  r>  rS   s       rA   mock_to_timestampr  8  sA     #639F67NOPFN MrC   to_timestamp_ntzc           
         t        | ||d      }t        |D cg c]  }t        d ||       c}t        t	        t
        j                        | j                  j                        t        | j                        S c c}w )NT)r  c                 &    | j                  d       S Nr   )r   r   s    rA   r   z'mock_to_timestamp_ntz.<locals>.<lambda>Q  s    !))4)"8 rC   r   rP   dtyper\   )r  r   r  r   r&   r%   NTZrP   r   objectr\   r   r=  r>  rS   r   s        rA   r  r  G  st     63dCFPV
KLK8(AF
 +//0&..2I2I
 ll	 	
s   A<to_timestamp_ltzc                    t        | ||d      }t        |D cg c]  }t        j                  |       c}t	        t        t        j                        | j                  j                        t        | j                        S c c}w NT)r  r  )r  r   r   r   r   r&   r%   rZ  rP   r   r  r\   r  s        rA   mock_to_timestamp_ltzr  [  sn     63tDF :@AQm--a0A+//0&..2I2I
 ll As   Bto_timestamp_tzc                     t        t        | ||d      t        t        t        j
                        | j                  j                        t        | j                        S r  )
r   r  r   r&   r%   TZrP   r   r  r\   )r   r=  r>  s      rA   mock_to_timestamp_tzr  o  sN     63tD+../1H1H
 ll rC   convertvalc                 ~    |y 	  | |      S # t         $ r&}|rY d }~y t        j                  |       Y d }~y d }~ww xY wr;   )rM  r   r   )r  r>  r  r   s       rA   r  r    sC    
{@s| @)::3??	@s    	<77<to_charc                    t        | j                  j                  t              r| j	                         S | j                  j                  t        t
              s$t        gt        |       z  | j                        fd}| j                  |      }t        t               | j                  j                        |_        |S )au  
    https://docs.snowflake.com/en/sql-reference/functions/to_char
    [x] expr: An expression of any data type.
    [x] numeric_expr: A numeric expression.
        [ ] numeric_expr with format: not supported, check SNOW-1372863
    [x] date_or_time_expr: An expression of type DATE, TIME, or TIMESTAMP.
    [x] binary_expr: An expression of type BINARY or VARBINARY.

    r@  c                     t        t              r_|rJt        j                         j	                  ddt              j                  t        |      dt                fd}t        |       S t        t        t        f      rt        j                  t                    }t        ||d      \  }t        t              rt        j                  j                   nt        j"                  j                   t        fd       S t        t$              r	t        j                  t$              }t        ||d	      \  }t        fd
       j'                         }dv rd}t)        t+        j,                  ||            dkD  rNt        j                         j	                  d| ddt              j                  t        |      dt               t+        j.                  d|      j1                         }|dz   }	|||	 dt3        d|       }
|d | |
z   ||	d  z   }|S t        t4              rq|xs dj7                         }t8        j:                  t<        j>                  d dj                  |      }|tA        d       t        |       jC                         S t        tD              rt        d        S t        tF        tH        tJ        f      rddl&m' t        fd       S t        tP        tR        f      r S t        j                         j	                  dt              j                   ddt              j                  t        |      dt               y )Nz0Use format strings with Numeric types in TO_CHARmock_to_char)source_datatyper=  external_feature_nameinternal_feature_nameparameters_inforaise_errorc                 r    t        t              rdj                  j                        S t	        |       S )Nz{:.{}f})rF   r   r   r   rd   )r   r   r  s    rA   r   z4mock_to_char.<locals>.convert_char.<locals>.<lambda>  s3    o{; $**41F1FG V rC   F)rD  is_input_formatc                      |       S r;   rf   )r   convert_date_time_to_strr   s    rA   r   z4mock_to_char.<locals>.convert_char.<locals>.<lambda>  s    21f= rC   )r  c                 D    t         j                  j                  |       S r;   )r   rF  strftime)r   r   s    rA   r   z4mock_to_char.<locals>.convert_char.<locals>.<lambda>  s    (--00F; rC   z%fz\d{6}r,   zUse format string z with TimestampType in TO_CHARr   r   HEXc                     | S r;   rf   r   s    rA   r   z4mock_to_char.<locals>.convert_char.<locals>.<lambda>  s    1 rC   r  BASE64zUTF-8Invalid binary format c                 4    t        |       j                         S r;   rd   ry   r   s    rA   r   z4mock_to_char.<locals>.convert_char.<locals>.<lambda>  s    Q rC   )CUSTOM_JSON_ENCODERc                 R    t        j                  | d      j                  d      S )N),:)rs   
separators")jsondumpsstrip)r   r  s    rA   r   z4mock_to_char.<locals>.convert_char.<locals>.<lambda>  s'    $**+) %*	 rC   z
Data type z in TO_CHAR)*rF   r+   r-   get_instancelog_not_supported_errorrL  ra   rd   NotImplementedErrorr  r   r'   _DEFAULT_OUTPUT_FORMATgetr/   r   r  rc  r&   r  rM   refindallsearchstartr   r   upperbinasciihexlifybase64	b64encoder   decoder   r(   r   r"   snowflake.snowpark.mockr  r$   r#   )r   rN  convert_numeric_to_strrD  rQ  r  time_strpattern	start_idxend_idxfractional_seconds_strfmt_decoderr  r  r   r=  r  r>  s   `           @@@rA   convert_charz"mock_to_char.<locals>.convert_char  sL   o|4,99;SS*\*8+/+@+I+I"4y% !4 T  #
 5xFF(H)=>377_8MNN<^ULVQ
 ox8 !!**]]++ %
 =x  7377FN,Mne-)V' #;Xteg  v~"rzz'84590==?WW0B4&Hf.g.</3O/D/M/M#&t9) %8 X  IIh9??A	#a-)1)G)DA12*&
 Zi(+AAHWXDVV  O4ME((*D'' **$ c$i	  "36LSE4RSS{Hd;BBDD574HH+y')JKC 
   *h)?@K(557OO(243H3Q3Q2RR]&^&4'+O'<'E'Et9! 0 P rC   )rF   rP   r   r$   rT  r   rM   r\   r:  r   r   )r   r=  r>  r  r   r  s    ``  @rA   r  r    s     &..)):6{{}nn--Oc>*cUS[0Epd ..l
+CZ\6>>+B+BCCKJrC   
to_varcharc                      t        | i |S r;   )r  )rR   r_   s     rA   mock_to_varcharr    s    (((rC   	to_doublec                 `   t        | j                  j                  t        t        f      r| j                         S |6t        j                         j                  dddt        |      it               t        | j                  j                  t        t        t        t        f      rQ| j                  fd      }t!        t	               | j                  j"                  xs |j$                        |_        |S t'        j(                  t+        d| j                  j                   d             y)	a  
        [x] Fixed-point numbers are converted to floating point; the conversion cannot fail, but might result in loss of precision.

        [x] Strings are converted as decimal integer or fractional numbers, scientific notation and special values (nan, inf, infinity) are accepted.

        For VARIANT input:

        [x] If the variant contains a fixed-point value, the numeric conversion will be performed.

        [x] If the variant contains a floating-point value, the value will be preserved unchanged.

        [x] If the variant contains a string, a string conversion will be performed.

        [x] If the variant contains a Boolean value, the result will be 0 or 1 (for false and true, correspondingly).

        [x] If the variant contains JSON null value (None in Python), the output will be NULL.

    Note that conversion of decimal fractions to binary and back is not precise (i.e. printing of a floating-point number converted from decimal representation might produce a slightly diffe
    Nz!Using format strings in TO_DOUBLEmock_to_doubler=  r  c                 &    t        t        |       S r;   )r  r   r   r>  s    rA   r   z mock_to_double.<locals>.<lambda>C  s    [!%D rC   z[Local Testing] Invalid type z for parameter 'TO_DOUBLE')rF   rP   r   r    r   rT  r-   r  r  rd   r  r+   r$   r(   r#   r   r   r   r  r   r   r   )r   r=  r>  r   s     ` rA   r  r     s    . &..))Iz+BC{{}
$113KK"E"2"CH-+	 	L 	
 ,
K!R llDE v~~/F/F/U#++V
%66/0G0G/HHbc	
rC   
to_booleanc                 6   t        | j                  j                  t              r| j	                         S t        | j                  j                  t
              rUdt        t           fd| j                  fd      }t        t               | j                  j                        |_        |S t        | j                  j                  t              rUdt        t           fd| j                  fd      }t        t               | j                  j                        |_        |S t        | j                  j                  t              rC| j                  fd      }t        t               | j                  j                        |_        |S t        | j                  j                  t              rC| j                  fd      }t        t               | j                  j                        |_        |S t        d| j                  j                   d	      )
u  
    [x] For a text expression, the string must be:

        'true', 't', 'yes', 'y', 'on', '1' return TRUE.

        'false', 'f', 'no', 'n', 'off', '0' return FALSE.

        All other strings return an error.

        Strings are case-insensitive.

    [x] For a numeric expression:

        0 returns FALSE.

        All non-zero numeric values return TRUE.

        When converting from the FLOAT data type, non-numeric values, such as ‘NaN’ (not a number) and ‘INF’ (infinity), cause an error.


    r   c                 r    | y | j                         dv ry| j                         dv ryt        d|  d      )N)truetyesr  on1T)falsefnonoff0FzBoolean value z is not recognized)ry   r   r   s    rA   convert_str_to_boolz,mock_to_boolean.<locals>.convert_str_to_booli  sC    yBBCC/.CU0VWWrC   c                     t        |       S r;   r  )r   r(  r>  s    rA   r   z!mock_to_boolean.<locals>.<lambda>r      5H(TU)V rC   c                     | y t        j                  |       st        j                  |       rt        d|  d      | dk7  S )NzInvalid value  for parameter 'TO_BOOLEAN'r   )r   r   isinfr   r   s    rA   convert_num_to_boolz,mock_to_boolean.<locals>.convert_num_to_boolw  sD    yA$**Q-3$QC'BC  AvrC   c                     t        |       S r;   r  )r   r.  r>  s    rA   r   z!mock_to_boolean.<locals>.<lambda>  r*  rC   c                 &    t        t        |       S r;   r  re   r  s    rA   r   z!mock_to_boolean.<locals>.<lambda>      T8Q)G rC   c                 &    t        t        |       S r;   r1  r  s    rA   r   z!mock_to_boolean.<locals>.<lambda>  r2  rC   Invalid type r,  )rF   rP   r   r   rT  r$   r
   rd   r   r   r   r+   r   r(   r   )r   r>  new_colr.  r(  s    ` @@rA   mock_to_booleanr6  N  sx   . &..));7{{}&..)):6	X8C= 	X ,,VW$[]FNN4K4KL	FNN++\	:	8D> 	 ,,VW$[]FNN4K4KL	FNN++[	9,,GH$[]FNN4K4KL	FNN++[	9,,GH$[]FNN4K4KL+FNN3344OP
 	
rC   	to_binaryc                 F   t        | j                  j                  t              r| j	                         S |r|j                         nd}t        j                  t        j                  d dj                  |      |t        d|       t        | j                  j                  t        t        t        f      rD| j                  fd      }t!        t               | j                  j"                        |_        |S t        d| j                  j                   d      )z[
    [x] TO_BINARY( <string_expr> [, '<format>'] )
    [x] TO_BINARY( <variant_expr> )
    r  c                 $    | j                  d      S )Nzutf-8)encoder   s    rA   r   z mock_to_binary.<locals>.<lambda>  s    188G, rC   r  r  c                     t        |       S r;   r  )r   r  r>  s    rA   r   z mock_to_binary.<locals>.<lambda>  s    [h%J rC   r4  z for parameter 'TO_BINARY')rF   rP   r   r   rT  r  r  	unhexlifyr  	b64decoder  r   r$   r#   r(   r   r   r   )r   r=  r>  r   r  s     ` @rA   mock_to_binaryr>    s     &..)):6{{}#))+%C!!"", 
c#h	  {+.DSE,JKK&..))J++NOllJK v~~/F/FG
+FNN3344NO
 	
rC   coalescec                      t        |       dk  rt        dt        |        d      t        j                  | d         }| D ]  }|j	                  |      } t        || d   j                  t              S )Nr  z2not enough arguments for function [COALESCE], got z, expected at least twor   )r   rP   r  )rM   r   r   Seriescombine_firstr   rP   r  )exprsr   rh  s      rA   mock_coalescerD    s|    
5zA~+@ULcd
 	
 --aC  &%&sE!H,<,<FKKrC   	substring	base_expr
start_exprlength_exprc                    t        | |D cg c]  }t        d|dz
         c}|      D cg c]  \  }}}|d n||||z     }}}}t        |t        t	               | j
                  j                        t              }|S c c}w c c}}}w )Nr   r,   rP   r  )r  r   r   r   r$   rP   r   r  )rF  rG  rH  r   stringr  lengthr   s           rA   mock_substringrM    s     &)z:!Aq1u:K&
 !FE6 F556>$BBC  Z
i.?.?.H.HIQWC J ;s
   B
B
startswithc                     t        | |      D cg c]  \  }}||j                  |      nd  }}}t        |t        t	               | j
                  j                        t              }|S c c}}w NrJ  )r  rN  r   r   r   rP   r   re   rp  rq  r   r  r   s        rA   mock_startswithrR    sb    BEeUBS
T$!Qam1<<?5
TC
T
Zu}}/E/EFdC J	 U   A,endswithc                     t        | |      D cg c]  \  }}||j                  |      nd  }}}t        |t        t	               | j
                  j                        t              }|S c c}}w rP  )r  rT  r   r   r   rP   r   re   rQ  s        rA   mock_endswithrV    sb    @CE5@Q
R1AM1::a=t3
RC
R
Zu}}/E/EFdC J	 SrS  rV   )r7   rY   c                 L    t        | dz   gt        t               d            }|S )Nr,   Fr   )r   r   r!   )rY   r   s     rA   mock_row_numberrX    s"    
y1}oz(*e7T
UCJrC   
parse_jsonrh  c                     ddl m t        | j                  j                  t
              r| j                  fd      }n| j                         }t        t               | j                  j                        |_        |S )Nr   CUSTOM_JSON_DECODERc                 P    t        t        t        j                        d|       S )Nrr   F)r  r   r  loads)r   r\  s    rA   r   z!mock_parse_json.<locals>.<lambda>  s    k

(;<eQ rC   )r	  r\  rF   rP   r   r$   r   rT  r   r(   r   )rh  r   r\  s     @rA   mock_parse_jsonr_    sY    ;$,,''4jj
 iik[]DLL,A,ABCKJrC   to_arrayc                 x   t        | j                  j                  t              r| j	                         S t        | j                  j                  t
              r ddlm fd| j                  fd      }n| j                  d       }t        t               | j                  j                        |_        |S )z
    [x] If the input is an ARRAY, or VARIANT containing an array value, the result is unchanged.

    [x] For NULL or a JSON null input, returns NULL.

    [x] For any other value, the result is a single-element array containing this value.
    r   r[  c                     t        |       t        u rt        j                  |       } | t        |       t        u r| S | gS )Nrr   )rL  rd   r  r^  r[   r  r\  s    rA   convert_variant_to_arrayz/mock_to_array.<locals>.convert_variant_to_array  s<    CyCjj*=>{d3i4/
urC   c                     t        d|       S rw   r  )r   rd  s    rA   r   zmock_to_array.<locals>.<lambda>  s    ;/GPQ#R rC   c                     t        d d|       S )Nc                     | gS r;   rf   )r  s    rA   r   z1mock_to_array.<locals>.<lambda>.<locals>.<lambda>  s    ! rC   Fr  r   s    rA   r   zmock_to_array.<locals>.<lambda>  s    ;}eQ#G rC   )rF   rP   r   r   rT  r(   r	  r\  r   r   r   )rh  r   r\  rd  s     @@rA   mock_to_arrayrh    s     $,,''3yy{$,,''5?	 jjRSjjGHY[$,,*?*?@CKJrC   strip_null_valuec                     t        | D cg c]  }|dk(  rd n| c}t        | j                  j                  d            S c c}w )NnullTr"  )r   r   rP   r   )rh  r   s     rA   mock_strip_null_valuerl    s?    -12f!	#24<<00$7 2s   A	to_objectc                    t        | j                  j                  t        t        f      r| j                         }npt        | j                  j                  t              r ddlm fd| j                  fd      }n,t        dt        | j                  j                         d      t        t               | j                  j                        |_        |S )a  
    [x] For a VARIANT value containing an OBJECT, returns the OBJECT.

    [x] For NULL input, or for a VARIANT value containing only JSON null, returns NULL.

    [x] For an OBJECT, returns the OBJECT itself.

    [x] For all other input values, reports an error.
    r   r[  c                     t        |       t        u rt        j                  |       } | t        |       t        u r| S t        dt        |        d      )Nrr   zInvalid object of type z passed to 'TO_OBJECT')rL  rd   r  r^  rj   r   rc  s    rA   convert_variant_to_objectz1mock_to_object.<locals>.convert_variant_to_object,  sS    CyCjj*=>{d3i4/
/)$s)4JK rC   c                     t        d|       S rw   r  )r   rp  s    rA   r   z mock_to_object.<locals>.<lambda>5  s    ;/H%QR#S rC   r4  z parameter 'TO_OBJECT')rF   rP   r   r"   r#   rT  r(   r	  r\  r   r   rL  r   r   )rh  r   r\  rp  s     @@rA   mock_to_objectrr    s     $,,'''8)<=iik	DLL));	7?	 jjST+D!6!6788NO
 	
 WY(=(=>CKJrC   
to_variantc                     | j                         }t        t               | j                  j                        |_        |S r;   )rT  r   r(   rP   r   )rh  r   s     rA   mock_to_variantru  ?  s,    
))+C[]DLL,A,ABCKJrC   c                    t        |       dz  dk7  r"t        j                  t        d d             dk(  rt	        t               g      S fd}t        j                  | dd	      }|j                  |d
      S )Nr  r   z/Cannot construct an object from an odd number (z) of values.rs  c                 v    t        dd      D ci c]  }| |   r| |dz      | |   | |dz       c}S c c}w )Nr   r  r,   )r   )r   r  
drop_nulls
expr_counts     rA   construct_dictz)_object_construct.<locals>.construct_dictR  sW     1j!,
t!a%8H aD!AE(N
 	
 
s   "6r,   Tr   ignore_indexr   )	rM   r   r   r   r   rj   r   r#  r   )rC  rx  rz  r  ry  s    `  @rA   _object_constructr}  F  s~    UJA~%66A*\Z	
 QDF8,,
 }}U>H>>.q>11rC   object_constructrC  c                  t    t        | d      }t        t        t               t                     d      |_        |S )NTFr}  r   r"   r$   rP   rC  rS   s     rA   mock_object_constructr  ]  s,    ud+F
jl CUKFNMrC   object_construct_keep_nullc                  t    t        | d      }t        t        t               t                     d      |_        |S )NFTr  r  s     rA   mock_object_construct_keep_nullr  d  s,    ue,F
jl CTJFNMrC   c                     t        | t        j                        r| S t        j                  j                  | j                               S r;   )rF   r   fromordinal	toordinal)rF  s    rA   cast_to_datetimer  k  s5    $))*(()9::rC   c                 @    | j                  | j                  |z         S )N)year)r   r  )rF  durations     rA   	add_yearsr  q  s    <<TYY1<22rC   c                     t        j                  |      t        j                  | |z        z   j                         }t	        |t
        j
                        s|j                         }|S )N)months)r   to_datetime
DateOffsetr  rF   r   rF  )scalarrF  r  r   s       rA   
add_monthsr  u  sS    4 6#4#4FX<M#NNmo  dH--.hhjJrC   c           	      \    ||S |t        j                  di |  dt        |      |z  iz   S )Nr   rf   )r   r  r   )unitrF  r  r  s       rA   add_timedeltar    s9    |($$N4&
E(Of4L'MNNNrC   dateaddpart
value_exprdatetime_exprc                 8  	 t        |       } |j                  }t        t        t        j
                        |j                  j                        }d }|| dk(  rt        	n| dk(  s| dk(  r| dk(  rdnd}t        t        |      	ns| dv rt        t        |       	n^| dv rt        t        |       	t        |}nA| d	k(  rt        t        d
d      	t        |}n!t        j                  t        |  d             |j                  |	fd      }t!        ||      S )Nc                     | S r;   rf   r   s    rA   nopzmock_dateadd.<locals>.nop  s    rC   r  quartermonthr  r,   >   dayweek>   re  rl  secondmicrosecondmillisecond
nanosecondr  gMbP?)r  z' is not a recognized date or time part.c                 "      |       |      S r;   rf   )rF  r  castru   s     rA   r   zmock_dateadd.<locals>.<lambda>  s    4T
H+E rC   r"  )r   rP   r   r&   r%   r  r   r  r   r  r  r  r   r   r   r:  r   )
r  r  r  rP   ts_typer  r  r   r  ru   s
           @@rA   mock_dateaddr    s   
 !&D##G'++,m.C.C.L.LG D v~		dgoi'Qz6*		 }d+	K	K}d+		}mHE%66$FGH	
 

EC #w//rC   	date_partc                 (   t        |       |j                  j                  }dk(  rddv st        |t              rdv r|j                  fd      }ndv r9t        j                  |      j                  j                         j                  }n݉dk(  r9t        j                  |      j                  j                         j                  }ndv r,t        t        j                  |      j                  d	      }nod
v r1t        j                  |      j                  j                  dz   dz  }n:dk(  r$t        |t              r|j                  d       }ndv rt        |t              r|j                  t              }t        |t              r>|j                   t"        j$                  t"        j&                  hv r|j                  d       }dddk(  rdndk(  rdn	dk(  rdd|j                  fd      }ngdk(  r|j                  d       }nOdk(  r|j                  d       }n7t)        j*                  t-        |  d|j.                  j0                                t3        t5        t7               d            S )
    SNOW-1183874: Add support for relevant session parameters.
    https://docs.snowflake.com/en/sql-reference/functions/date_part#usage-notes
    
yearofweekr  >   r  r  r  >   re  rl  r  r  c                     t        | d       S r;   )getattr)r   	unaliaseds    rA   r   z mock_date_part.<locals>.<lambda>  s    GAy$,G rC   >   r  weekisoyearofweekiso>   r  	dayofyearN>   	dayofweekdayofweekisor,      r  c                 (    | d S | j                   dz  S )N  )r  r   s    rA   r   z mock_date_part.<locals>.<lambda>  s    AID 1==SWCW rC   >   epoch_secondepoch_nanosecondepoch_microsecondepoch_millisecondc                 J    | d S | j                  t        j                        S r  )r   r  UTCr   s    rA   r   z mock_date_part.<locals>.<lambda>  s    !)$ $((1K rC   r  r  r  i@B r  c                 J    | d S t        | j                         z        z  S r;   )r  	timestamp)r   
multiplierposts    rA   r   z mock_date_part.<locals>.<lambda>  s$    aid S1K-Lt-S rC   timezone_hourc                 L    | d S t        | j                  d      xs dd d       S Nz%z0000r  r  r   s    rA   r   z mock_date_part.<locals>.<lambda>  s(    aid S!**T2B2Lfcr1R-S rC   timezone_minutec                 L    | d S t        | j                  d      xs ddd        S r  r  r   s    rA   r   z mock_date_part.<locals>.<lambda>  s(    aid S!**T2B2Lfbc1R-S rC   z, is an invalid date part for column of type Tr  r"  )r   rP   r   rF   r&   r   r   r  dtisocalendarr  r  r  r  r   r  r   r%   DEFAULTr  r   r   r   	__class__ra   r   r   r!   )r  r  r   r   r  r  r  s       @@@rA   mock_date_partr    ss    &d+I$$--H L 	,,8]+DD!!"GH	)	)  /22>>@EE	o	%  /22>>@EE	.	.f((7::ItL	3	3!!-033==AQF	l	"z(M'J!!"WX	  
 h))//0@AM h.8;;%%!!C
 4
 *//KM 
++J-- J,, JD!!S
 
o	%!!S
 
'	'!!S
 	&66&DXEWEWE`E`Dab	

 #z(*t'LMMrC   
date_truncc                 |   dddddddddddd	}t        |       }|j                  |      }|0t        j                  |      j                  j                  |      }n
|d
k(  r|j                  d       }n|dk(  r<t        dd      D ci c]  }||dz
  dz  dz  dz    c}|j                  fd      }n|dk(  rst        j                  |      }t        j                  |j                  j                  d      }|j                  |t        j                        }|j                  d       }n9|dk(  r|j                  d       }n!t        j                  t        |  d             t!        |j"                  j$                  t&              rj                  j(                  }t+        |j"                        S c c}w )r  Dhusmsr   Nnsr   )r  re  r  r  rl  r  r  r  r  r  r  r  c           
      r    t        j                   | j                  | j                  dt        | dd             S Nr,   r   r   r   r  r  r  r   s    rA   r   z!mock_date_trunc.<locals>.<lambda>  s+    h''71h+E rC   r  r,      r  c           
      z    t        j                   | j                  | j                     dt        | dd             S r  r  )r   quarter_maps    rA   r   z!mock_date_trunc.<locals>.<lambda>%  s1    h''AGG,a8T8R rC   r  r   )r  c           
          t        j                   | j                  | j                  | j                  t	        | dd             S )Nr   r   )r   r  r  r  r  r   s    rA   r   z!mock_date_trunc.<locals>.<lambda>1  s/    h''wq(D/I rC   r  c           
      ^    t        j                   | j                  ddt        | dd             S r  )r   r  r  r   s    rA   r   z!mock_date_trunc.<locals>.<lambda>7  s%    h''1WQRV=WX rC   z- is not a supported time unit for date_trunc.r"  )r   r  r   r  r  floorr   r   to_timedeltar  r:  operatorsubr   r   r   rF   rP   r   r   rF  r   )	r  r  SUPPORTED_UNITS	time_unitpandas_unit	truncatedr  offsetsr  s	           @rA   mock_date_truncr    s    O &d+I!%%i0K&&}588>>{K		g	!''
	
 
i	<A!RLIqqQUqLA-22I!''
	
 
f	&&}5	%%ill&<&<3G%%gx||<	OO
	
 
f	!''X
	 	&66$LMN	
 -''00(;LL%%	)]-B-BCC= Js   	F9datediffcol1col2c                   	 ddl m t        |       		dv r	fd}n$	dv r	dk(  rdndfd	}nt        d
|  d      g }t	        ||      D ]#  \  }}|j                  ||d n |||             % t        t        j                  |t              t        t               |j                  j                  xr |j                  j                              S )Nr   )relativedelta>   r  re  r  rl  r  r  r  c                 D    || z
  t        j                  di  ddiz  S )Nr   r,   rf   )r   r  )r   r  r  s     rA   ru   zmock_datediff.<locals>.funcV  s+    Eh00HykOQ3GHHHrC   >   r  r  r  r   r,   c                 f    j                  ||       }|j                  dz  |j                  z   z  S )Nr   )r  yearsr  )r   r  deltadenomr  s      rA   ru   zmock_datediff.<locals>.func_  s2    !//15E[[2%5%??rC   zSpecified part z, is not supported by local testing datediff.)r  r"  )rR  r  r   r   r  r  r   r   rA  r  r   r!   rP   r   )
r  r  r  ru   r   r   r  r  r  r  s
          @@@rA   mock_datediffr  D  s     '%d+I  	I 
'	'EE	@
 ,dV#OP
 	
 DD$ D1AIDQ
CD d&)8:t||'<'<'VAVAVW rC   CompareTyper   r  c                     | |y| }t        | t              r	 t        |       }t	        |      t	        |      u r|n t	        |      |      }||kD  r||fS ||fS # t        $ r Y Aw xY w)z
    Compares two values based on the rules described for greatest/least
    https://docs.snowflake.com/en/sql-reference/functions/least#usage-notes

    SNOW-1065554: For now this only handles basic numeric and string coercions.
    NN)rF   rd   r   r   rL  )r   r  _x_ys       rA   _comparer  u  s|     	yAI	
B!S	qB 2h$q'!xtBx{B	BwBxBx  		s   A 	A&%A&c                      t        | |      d   S )Nr,   r  r   r  s     rA   _leastr        Aq>!rC   c                      t        | |      d   S r  r  r  s     rA   	_greatestr     r  rC   greatestc                  H    t        d |       }| d   j                  |_        |S )Nc                 .    | j                  |t              S r;   )r:  r   r  s     rA   r   zmock_greatest.<locals>.<lambda>  s    1i!8 rC   r   r   rP   r  s     rA   mock_greatestr    s%    8%@F1X%%FNMrC   leastc                  H    t        d |       }| d   j                  |_        |S )Nc                 .    | j                  |t              S r;   )r:  r  r  s     rA   r   zmock_least.<locals>.<lambda>  s    1f!5 rC   r   r  r  s     rA   
mock_leastr	    s%    5u=F1X%%FNMrC   r  c                 6    | j                   j                         S r;   )rd   r  rh  s    rA   
mock_upperr        88>>rC   ry   c                 6    | j                   j                         S r;   r  r  s    rA   
mock_lowerr    r  rC   rL  c                     | j                   j                         }t        t               | j                  j
                        |_        |S )Nr  )rd   rM   r   r!   rP   r   rz  s     rA   mock_lengthr    s0    XX\\^F
T\\5J5JKFNMrC   z!?@"^#$&~_,.:;+-*%/|\[](){}<>ri  
delimitersc                     | y |t         n
t        |      }d}d}| D ]/  }|r||j                         z  }n||j                         z  }||v }1 |S )Nr  T)DEFAULT_INITCAP_DELIMITERSsetr  ry   )ri  r  delimsrS   capchars         rA   _initcapr    sg    }+5+='3z?FF
C djjl"Fdjjl"Ffn MrC   initcapr^   c                 T    | j                  |t              }| j                  |_        |S r;   )r:  r  rP   )r^   r  rS   s      rA   mock_initcapr    s"    ^^J1F^^FNMrC   convert_timezonerR   c                    	 ddl | d   9| \  }}}t        j                  dgt        |      z        }t        j
                  	nQ| \  }}}t        j                  	|j                  j                  j                  t        j                  urt        d      t        j                  |||gdd      }	fd}|j                  |d	      }t        |t        t        	      |j                  j                   
      t"              S )zConverts the given source_time to the target timezone.

    For timezone information, refer to the `Snowflake SQL convert_timezone notes <https://docs.snowflake.com/en/sql-reference/functions/convert_timezone.html#usage-notes>`_
    r   Nr  zc[Local Testing] convert_timezone can only convert NTZ timestamps when source_timezone is specified.r,   Tr{  c                 T   | \  }}}|y |+|j                  j                  j                  |            }|j                  t        j                  |      }|j                  j                  j                  |            }t        j                  k(  r|j                  d       }|S r  )	r   r   gettzr   r   r   r   r%   r  )r   source_timezonetarget_timezonesource_timerS   rR  r  s        rA   _convertz'mock_convert_timezone.<locals>._convert  s    8;5+&%--X[[5F5F5W-XK%'22;?K''(9(9/(JK+///^^4^0FrC   r   r  rJ  )rR  r   rA  rM   r%   r  r  rP   r   r   r   r#  r   r   r   r&   r   r  )
rR   r"  r#  rQ  r!  r  r$  r   rR  r  s
           @@rA   mock_convert_timezoner%    s     
 Aw*.'a --[1A(AB'**8<5+'++''**2C2G2GGu  }}	/;7adH& ...
*C+&1D1D1M1M
  rC   current_sessionc                     t         j                  j                  j                         }t	        t        t        |            gt        |       z  t        t               d            S r	  )
	snowflakesnowparksession_get_active_sessionr   rd   hashrM   r   r$   rZ   r*  s     rA   mock_current_sessionr.    sN      ((<<>G$w- !C$55:</ rC   current_databasec                     t         j                  j                  j                         }t	        |j                         gt        |       z  t        t               d            S r	  )	r(  r)  r*  r+  r   get_current_databaserM   r   r$   r-  s     rA   mock_current_databaser2    sN      ((<<>G**,-L0AA:</ rC   r  column_expressionvalue_expressionc                     d }g }t        | |      D ]  \  }}|j                   |||              t        |t        | j                  j
                  d      t              S )Nc                     	 t        | t              r|t        |       k  r| |   S t        | t              r| j	                  |d       S y # t
        $ r Y y w xY wr;   )rF   r[   rM   rj   r  KeyError)objkeys     rA   r  zmock_get.<locals>.get"  sT    	#t$s3x3xC&wwsD)) 		s   "A !A 	AATrJ  )r  r  r   r   rP   r   r  )r3  r4  r  rS   expks         rA   mock_getr<    sh    	 F')9: #Qc#qk"# ,44==tD rC   r#  r  c                  *   t        |       dk  rt        j                  t        d             t	        j
                  | d      j                  d      }|j                  j                  d       }t        t               |j                        |_        |S )Nr,   z5concat expects one or more column(s) to be passed in.r   TrJ   c                     | j                         j                  j                         rd S | j                  t              j                  j                         S r;   isnullr^   rL   astyperd   catcs    rA   r   zmock_concat.<locals>.<lambda>A  s8    !((*++//1$ qxx}7H7H7L7L7N rC   )rM   r   r   r   r   r#  rO   Tr   r   r$   r  rP   r  pdfrS   s      rA   mock_concatrH  9  su    
7|a%66NO	
 --a
(
4
4$
4
?CUU[[NF  
fnn=FNMrC   	concat_wsc                  
   t        |       dk  rt        j                  t        d             t	        j
                  | d      }|j                  j                  d       }t        t               |j                        |_        |S )Nr  zUconcat_ws expects a seperator column and one or more value column(s) to be passed in.r,   r   c                     | j                         j                  j                         rd S | dd  j                  t              j                  j                  | d         S )Nr,   r   )sepr?  rC  s    rA   r   z mock_concat_ws.<locals>.<lambda>Q  sP    88:  "  qrU\\#""&&1Q4&0 rC   )rM   r   r   r   r   r#  rE  r   r   r$   r  rP   rF  s      rA   mock_concat_wsrM  G  sk    
7|a%66g	

 --a
(CUU[[	1F
  
fnn=FNMrC   coltarget_column_typec                 4   |j                   }| j                  |k(  r| S t        |t              rt	        | |      S t        |t
              rt        | |      S t        |t              r|j                  t        j                  u rt        | |      S |j                  t        j                  u rt        | |      S |j                  t        j                  u rt        | |      S t!        | |      S t        |t"              r#t%        | |j&                  |j(                  |      S t        |t*              r!t%        | |      }t-        |d      |_        |S t        |t.              rt1        | |      S t        |t2              rt5        | |      S t        |t6              rt9        | |      S t        |t:              rt=        | |      S t        |t>              rtA        |       S t        |tB              rtE        |       S t        |tF              rtI        |       S y )N)r>  )r   r   r>  Tr  )%r   rP   rF   r   rV  r'   r  r&   r   r%   rZ  r  r  r  r  r  r  r   r  r   r   r*   r   r   r>  r   r6  r$   r  r)   r  r"   rr  r   rh  r(   ru  )rN  rO  r>  target_data_typer   s        rA   cast_column_torR  Y  s    *22
{{((
"H-C(33"H-C(33"M2"3"7"77(x@@  $5$9$99(x@@  $5$8$88'h??$S8<<"K0&00"((	
 	
 - cH5 !1DA
"J/cH55"K0sX66"J/C(33"O4cH55"G,c"""I.S!!"K0s##rC   iff	conditionc                     t        | j                  j                  t              sJ t	        |j                  |j                        }t        |       st        |        s|vt        d gt        |       z  t              }t        ||      }t        ||      }|j                  | |d       |j                  | D cg c]  }|  c}|d       ||_        |S t        d|j                   d|j                         c c}w )N)r   r  T)otherinplacezS[Local Testing] expr1 and expr2 have conflicting datatypes that cannot be coerced: z <-> )rF   rP   r   r   r   r   r   rM   r  rR  wherer   )rT  rp  rq  coerce_resultr   r   s         rA   mock_iffrZ    s    i''00+>>>*5==%--HM
9~iZM,E4&3y>"9Hum4um4		)5$	7		),Qq5,E4	H#
+abgboboappuv{  wD  wD  vE  F
 	
	 -s   9
C;randomseedc           	      \   d}d}| | nt        ||      } t        j                  j                  t        j                  j	                  t        |                   }t        t        t        |            D cg c]  }|j                  ||       c}t        t               d            S c c}w )Nl         l    Fr   )r   r   r[  	GeneratorMT19937rx  r   r   rM   integersr   r!   )r\  rZ   rand_minrand_maxgenrQ  s         rA   mock_randomrd    s    HH#48)DD
,,
 
 !5!5c$i!@
AC8=c,>O8PQ1cll8X.Q8:u- Qs   5B)c                     g }d}d}d}| | j                      j                  t        d      D ])  }|dz  }||k7  r
|r|dz   }n|}|}|j                  |       + t	        j
                  || j                        S )a  
    Returns a series containing the rank of a row within an ordered TableEmulator.
    Args:
        raw_input: The TableEmulator to apply the rank to.
        dense: When dense is false ranks are skipped when there are repeated values. When set to true
            ranks are not skipped. eg.
            -----------------------------------
            |"VALUE"  |"RANK"  |"DENSE_RANK"  |
            -----------------------------------
            |1        |1       |1             |
            |1        |1       |1             |
            |2        |3       |2             |
            |3        |4       |3             |
            -----------------------------------
    r   Nr,   r@  )	sorted_byr   r]   r  r   rA  r\   )rX   densefinal_valuesrankr\   previousri  s          rA   _rankrk    s      LDEH9../55eQ? "
HaxD!" ==Y__==rC   ri  )r8   r7   rX   c                 p    t        |       }t        |j                  |   t        t	               d            S r	  rk  r   r  r   r!   rX   rY   ri  s      rA   	mock_rankro    s0    DYYy!:hj%+H rC   
dense_rankc                 r    t        | d      }t        |j                  |   t        t	               d            S )NTFr   rm  rn  s      rA   mock_dense_rankrr    s2    D!DYYy!:hj%+H rC   percent_rankc                     t        |       dz
  t        |       j                  fd      }t        |j                  |   t        t               d            S )Nr,   c                     | dz
  z  S )Ng      ?rf   )r   rL  s    rA   r   z#mock_percent_rank.<locals>.<lambda>  s    QW,> rC   Fr   )rM   rk  r   r   r  r   r   )rX   rY   ri  rL  s      @rA   mock_percent_rankrv    sK    ^aF!!">?DYYy!:jlE+J rC   	cume_distc                    t        | d      j                         j                         }|j                  fd      }|j	                         }j                  |      }t        |j                  |   t        t               d            S )NTc                 "    | j                   z  S r;   )r  )r   ri  s    rA   r   z mock_cume_dist.<locals>.<lambda>  s    DII rC   Fr"  )
rk  value_counts
sort_indexr   cumsummapr   r  r   r   )rX   rY   aggedrG  cdfrw  ri  s         @rA   mock_cume_distr    s{     D!D **,E ++-
.C **,C Iy!:jlE+J rC   ntilec                     | j                   |   }|dk  rt        d      |j                  d   }||k  r|dz   }nt        j                  ||z  |z        dz   }t        |gt        t               d            S )Nr   z!NTILE argument must be at least 1r,   Fr"  )r  r   shaper   r  r   r   r!   )r  rX   rY   current_ntilenum_rowsbuckets         rA   
mock_ntiler    sx    JJy)M+,OPPq!H= QI5@AAE6(Jxz5,IJJrC   	any_valuec                 N    t        | j                  d      | j                        S )Nr,   r"  )r   samplerP   )rN  s    rA   mock_any_valuer  		  s!    

1 rC   rw   )r   r   F)FFF)F)r,   r  )r  r  r   r   r  loggingr   r  r  r  rK  rl   r   	functoolsr   r   r   r[  r   typingr   r	   r
   r   r   r   r  snowflake.snowparkr(  0snowflake.snowpark._internal.analyzer.expressionr   r   "snowflake.snowpark._internal.utilsr    snowflake.snowpark.mock._optionsr   r   ,snowflake.snowpark.mock._snowflake_data_typer   r   r   r   r   r   "snowflake.snowpark.mock.exceptionsr   snowflake.snowpark.typesr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   
_telemetryr-   _utilr.   r/   r0   RETURN_TYPEr  	getLoggerra   r   r2   rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  re   r  r$  rd   r*  r.  r2  r;  rV  r[  r_  rd  rj  rn  rw  r{  r  r  r  r  r  r  r  r  r  r  r  r  r6  r>  rD  rM  rR  rV  rX  r_  rh  rl  rr  ru  r}  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r   r  r	  r  r  r  r  
whitespacer  r  r  r%  r.  r2  r<  rH  rM  rR  rZ  rd  rk  ro  rr  rv  r  r  r  rf   rC   rA   <module>r     s|            	    %   A A   E :  M    * 5  NM12 ll5  '

H
%8 8v>) >)B. .4 u@^ @ @ @( u@^ @ @ @$   N  F ~n   
 u()^   * uK^ K K KD xK K> K K@ %&K-/0KK 'K #$K=.01K=.01K K %K |K=.01K=.01K K K vK=.01K=.01K K K K~ K. K K w,-Wu]N:; W W .W x
 
> 
 
 {N ^   2 {>     H H y C d   vn   tN   u> .   y ^^	^ ^ ^B d3 4 ~. / ~. / v  x  z    u  |  "	JJ}J C=J 	J JZ y nn	#n n nh oo	.	!o o 	od ~ %)	.	!    %)	.	!  &  %)	.	!  &  %)	.	!  "	@ 	@T 	@ 	@ y JJ	#J J 	J JZ |) ) ) {HM*
*
!)#*
AE*
*
 *
Z |@
N @
d @
~ @
 @
F {>C

!$
7;

 
< z
L 
L {+9HV  |> .   z    |D)s  *
 |.   z  8    {  D |.  2. . ^   #$N ~  %;3O y(0
(0)(0:H(0(0 (0V {KN KN^ KN KN\ |?D# ?Dn ?D ?D ?DD z*
*#*+9** *Z m$  k;.F(G 2k c eK,>&?   {E/A)B  z.   w~   w^   w^   xn   !!AFDUDU!UV HSM x}  " y ^   777 7t D1 2 T2 3 u%9G 4 x
. 
^ 
 
 {^   $ KP/	/-7/CG/n/d u
 
~ 
n 
 
$ x4(hsm .  )>B vtD9 C N  : |T$?~ # .  @ ~tDA C N  B {D>n    ?* wT:Kc Kn K K K ;K {  rC   