88
99import pytest
1010
11- import snowflake .snowpark
12- from snowflake .snowpark .exceptions import SnowparkSQLException
13- from snowflake .snowpark .functions import udf , sproc
14- from snowflake .snowpark .types import StringType
11+ from snowflake .snowpark .functions import udf
12+ from snowflake .snowpark .types import StringType , IntegerType
1513from snowflake .snowpark .udf_profiler import UDFProfiler
16- from tests .utils import Utils
1714
1815
1916def multi_thread_helper_function (pro : UDFProfiler ):
@@ -41,7 +38,7 @@ def setup(profiler_session, resources_path, local_testing_mode):
4138 "config.getoption('local_testing_mode', default=False)" ,
4239 reason = "session.sql is not supported in localtesting" ,
4340)
44- def test_profiler_with_profiler_class (profiler_session , db_parameters ):
41+ def test_udf_profiler_basic (profiler_session ):
4542 @udf (
4643 name = "str_udf" , replace = True , return_type = StringType (), session = profiler_session
4744 )
@@ -60,34 +57,6 @@ def str_udf():
6057 pro .disable ()
6158
6259 pro .register_modules ([])
63- print (res )
64-
65-
66- @pytest .mark .skipif (
67- "config.getoption('local_testing_mode', default=False)" ,
68- reason = "session.sql is not supported in localtesting" ,
69- )
70- @pytest .mark .xfail (reason = "stored proc registry changes not yet reflected." )
71- def test_single_return_value_of_sp (
72- is_profiler_function_exist , profiler_session , db_parameters , tmp_stage_name
73- ):
74- @sproc (name = "single_value_sp" , replace = True )
75- def single_value_sp (session : snowflake .snowpark .Session ) -> str :
76- return "success"
77-
78- profiler_session .stored_procedure_profiler .register_modules (["single_value_sp" ])
79- profiler_session .stored_procedure_profiler .set_target_stage (
80- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
81- )
82-
83- profiler_session .stored_procedure_profiler .set_active_profiler ("LINE" )
84-
85- profiler_session .call ("single_value_sp" )
86- res = profiler_session .stored_procedure_profiler .get_output ()
87-
88- profiler_session .stored_procedure_profiler .disable ()
89-
90- profiler_session .stored_procedure_profiler .register_modules ()
9160 assert res is not None
9261 assert "Modules Profiled" in res
9362
@@ -96,27 +65,27 @@ def single_value_sp(session: snowflake.snowpark.Session) -> str:
9665 "config.getoption('local_testing_mode', default=False)" ,
9766 reason = "session.sql is not supported in localtesting" ,
9867)
99- @pytest .mark .xfail (reason = "stored proc registry changes not yet reflected." )
100- def test_anonymous_procedure (
101- is_profiler_function_exist , profiler_session , db_parameters , tmp_stage_name
102- ):
103- def single_value_sp (session : snowflake .snowpark .Session ) -> str :
104- return "success"
105-
106- single_value_sp = profiler_session .sproc .register (single_value_sp , anonymous = True )
107-
108- profiler_session .stored_procedure_profiler .set_target_stage (
109- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
68+ def test_anonymous_udf (profiler_session ):
69+ add_one = udf (
70+ lambda x : x + 1 ,
71+ return_type = IntegerType (),
72+ input_types = [IntegerType ()],
73+ session = profiler_session ,
11074 )
11175
112- profiler_session .stored_procedure_profiler .set_active_profiler ("LINE" )
76+ pro = profiler_session .udf_profiler
77+ pro .register_modules (["str_udf" ])
11378
114- single_value_sp ()
115- res = profiler_session .stored_procedure_profiler .get_output ()
79+ pro .set_active_profiler ("LINE" )
11680
117- profiler_session .stored_procedure_profiler .disable ()
81+ df = profiler_session .create_dataframe ([[1 , 2 ], [3 , 4 ]]).to_df ("a" , "b" )
82+ df .select (add_one ("a" )).collect ()
83+ # there is a latency in getting udf profiler result
84+ time .sleep (5 )
85+ res = pro .get_output ()
86+ pro .disable ()
11887
119- profiler_session . stored_procedure_profiler . register_modules ()
88+ pro . register_modules ([] )
12089 assert res is not None
12190 assert "Modules Profiled" in res
12291
@@ -125,20 +94,14 @@ def single_value_sp(session: snowflake.snowpark.Session) -> str:
12594 "config.getoption('local_testing_mode', default=False)" ,
12695 reason = "session.sql is not supported in localtesting" ,
12796)
128- def test_set_incorrect_active_profiler (
129- profiler_session , db_parameters , tmp_stage_name , caplog
130- ):
131- with pytest .raises (ValueError ) as e :
132- profiler_session .stored_procedure_profiler .set_target_stage (f"{ tmp_stage_name } " )
133- assert "stage name must be fully qualified name" in str (e )
134-
97+ def test_set_incorrect_active_profiler (profiler_session , caplog ):
13598 with caplog .at_level (logging .WARNING ):
136- profiler_session .stored_procedure_profiler .set_target_stage (
137- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
138- )
13999 profiler_session .stored_procedure_profiler .set_active_profiler ("LINE" )
140100 profiler_session .stored_procedure_profiler .get_output ()
141- assert "last executed stored procedure does not exist" in caplog .text
101+ assert (
102+ "You are seeing this warning because last executed stored procedure or UDF does not exist"
103+ in caplog .text
104+ )
142105
143106 with pytest .raises (ValueError ) as e :
144107 profiler_session .stored_procedure_profiler .set_active_profiler (
@@ -147,70 +110,34 @@ def test_set_incorrect_active_profiler(
147110 assert "active_profiler expect 'LINE', 'MEMORY'" in str (e )
148111
149112
150- @pytest .mark .parametrize (
151- "sp_call_sql" ,
152- [
153- """WITH myProcedure AS PROCEDURE ()
154- RETURNS TABLE ( )
155- LANGUAGE PYTHON
156- RUNTIME_VERSION = '3.9'
157- PACKAGES = ( 'snowflake-snowpark-python==1.2.0', 'pandas==1.3.3' )
158- IMPORTS = ( '@my_stage/file1.py', '@my_stage/file2.py' )
159- HANDLER = 'my_function'
160- RETURNS NULL ON NULL INPUT
161- AS 'fake'
162- CALL myProcedure()INTO :result
163- """ ,
164- """CALL MY_SPROC()""" ,
165- """ CALL MY_SPROC()""" ,
166- """WITH myProcedure AS PROCEDURE () CALL myProcedure""" ,
167- """ WITH myProcedure AS PROCEDURE ... CALL myProcedure""" ,
168- ],
169- )
170- def test_sp_call_match (profiler_session , sp_call_sql ):
171- pro = profiler_session .stored_procedure_profiler
172-
173- assert pro ._is_procedure_or_function_call (sp_call_sql )
174-
175-
176113@pytest .mark .skipif (
177114 "config.getoption('local_testing_mode', default=False)" ,
178115 reason = "session.sql is not supported in localtesting" ,
179116)
180- def test_query_history_destroyed_after_finish_profiling (
181- profiler_session , db_parameters , tmp_stage_name
182- ):
183- profiler_session .stored_procedure_profiler .set_target_stage (
184- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
185- )
117+ def test_query_history_destroyed_after_finish_profiling (profiler_session ):
186118
187- profiler_session .stored_procedure_profiler .set_active_profiler ("LINE" )
119+ profiler_session .udf_profiler .set_active_profiler ("LINE" )
188120 assert (
189- profiler_session .stored_procedure_profiler ._query_history
121+ profiler_session .udf_profiler ._query_history
190122 in profiler_session ._conn ._query_listeners
191123 )
192124
193- profiler_session .stored_procedure_profiler .disable ()
125+ profiler_session .udf_profiler .disable ()
194126 assert (
195- profiler_session .stored_procedure_profiler ._query_history
127+ profiler_session .udf_profiler ._query_history
196128 not in profiler_session ._conn ._query_listeners
197129 )
198130
199- profiler_session .stored_procedure_profiler .register_modules ()
131+ profiler_session .udf_profiler .register_modules ()
200132
201133
202134@pytest .mark .skipif (
203135 "config.getoption('local_testing_mode', default=False)" ,
204136 reason = "session.sql is not supported in localtesting" ,
205137)
206- def test_thread_safe_on_activate_and_disable (
207- profiler_session , db_parameters , tmp_stage_name
208- ):
209- pro = profiler_session .stored_procedure_profiler
138+ def test_thread_safe_on_activate_and_disable (profiler_session ):
139+ pro = profiler_session .udf_profiler
210140 pro .register_modules (["table_sp" ])
211- pro .set_target_stage (
212- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
213- )
214141 with ThreadPoolExecutor (max_workers = 2 ) as tpe :
215142 for _ in range (6 ):
216143 tpe .submit (multi_thread_helper_function , pro )
@@ -222,82 +149,9 @@ def test_thread_safe_on_activate_and_disable(
222149 "config.getoption('local_testing_mode', default=False)" ,
223150 reason = "session.sql is not supported in localtesting" ,
224151)
225- def test_create_temp_stage (profiler_session ):
226- pro = profiler_session .stored_procedure_profiler
227- db_name = Utils .random_temp_database ()
228- schema_name = Utils .random_temp_schema ()
229- temp_stage = Utils .random_stage_name ()
230- current_db = profiler_session .sql ("select current_database()" ).collect ()[0 ][0 ]
231- try :
232- profiler_session .sql (f"create database { db_name } " ).collect ()
233- profiler_session .sql (f"create schema { schema_name } " ).collect ()
234- pro .set_target_stage (f"{ db_name } .{ schema_name } .{ temp_stage } " )
235-
236- res = profiler_session .sql (
237- f"show stages like '{ temp_stage } ' in schema { db_name } .{ schema_name } "
238- ).collect ()
239- assert len (res ) != 0
240- finally :
241- profiler_session .sql (f"drop database if exists { db_name } " ).collect ()
242- profiler_session .sql (f"use database { current_db } " ).collect ()
243-
244-
245- @pytest .mark .skip (reason = "SNOW-1945207" )
246- @pytest .mark .skipif (
247- "config.getoption('local_testing_mode', default=False)" ,
248- reason = "session.sql is not supported in localtesting" ,
249- )
250- def test_stored_proc_error (
251- is_profiler_function_exist , profiler_session , db_parameters , tmp_stage_name
252- ):
253- function_name = f"oom_sp_{ Utils .random_function_name ()} "
254-
255- @sproc (name = function_name , session = profiler_session , replace = True )
256- def oom_sp (session : snowflake .snowpark .Session ) -> str :
257- raise ValueError ("fake out of memory" )
258-
259- profiler_session .stored_procedure_profiler .register_modules (["oom_sp" ])
260- profiler_session .stored_procedure_profiler .set_target_stage (
261- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
262- )
263-
264- profiler_session .stored_procedure_profiler .set_active_profiler ("LINE" )
265-
266- with pytest .raises (SnowparkSQLException , match = "fake out of memory" ) as err :
267- profiler_session .call (function_name )
268- query_id = profiler_session .stored_procedure_profiler ._get_last_query_id ()
269- assert query_id in str (err )
270-
271- profiler_session .stored_procedure_profiler .disable ()
272-
273- profiler_session .stored_procedure_profiler .register_modules ()
274-
275-
276- @pytest .mark .skipif (
277- "config.getoption('local_testing_mode', default=False)" ,
278- reason = "session.sql is not supported in localtesting" ,
279- )
280- def test_profiler_without_target_stage (profiler_session , caplog ):
281- pro = profiler_session .stored_procedure_profiler
282- with caplog .at_level (logging .INFO ):
283- pro .set_active_profiler ("LINE" )
284- assert (
285- "Target stage for profiler not found, using default stage of current session."
286- in str (caplog .text )
287- )
288-
152+ def test_set_active_profiler_failed (profiler_session , caplog ):
153+ pro = profiler_session .udf_profiler
289154
290- @pytest .mark .skipif (
291- "config.getoption('local_testing_mode', default=False)" ,
292- reason = "session.sql is not supported in localtesting" ,
293- )
294- def test_set_active_profiler_failed (
295- profiler_session , caplog , tmp_stage_name , db_parameters
296- ):
297- pro = profiler_session .stored_procedure_profiler
298- pro .set_target_stage (
299- f"{ db_parameters ['database' ]} .{ db_parameters ['schema' ]} .{ tmp_stage_name } "
300- )
301155 with mock .patch (
302156 "snowflake.snowpark.DataFrame._internal_collect_with_tag_no_telemetry" ,
303157 side_effect = Exception ,
@@ -308,7 +162,7 @@ def test_set_active_profiler_failed(
308162
309163
310164def test_when_sp_profiler_not_enabled (profiler_session ):
311- pro = profiler_session .stored_procedure_profiler
165+ pro = profiler_session .udf_profiler
312166 # direct call get_output when profiler is not enabled
313167 res = pro .get_output ()
314168 assert res == ""
0 commit comments