@@ -147,6 +147,41 @@ def create_snowflake_connection(conn_config):
147
147
logger .error (f"Failed to create Snowflake connection: { str (e )} " )
148
148
raise
149
149
150
+ def analyze_function_signature (function_file ):
151
+ """Analyze the function signature to determine parameter structure."""
152
+ try :
153
+ import re
154
+ with open (function_file , 'r' ) as f :
155
+ content = f .read ()
156
+
157
+ # Look for the main function definition
158
+ signature_match = re .search (r'def\s+main\s*\((.*?)\)' , content )
159
+ if signature_match :
160
+ params = signature_match .group (1 ).strip ()
161
+ logger .info (f"Function signature parameters: '{ params } '" )
162
+
163
+ # Count parameters (excluding session if present)
164
+ param_list = [p .strip () for p in params .split (',' ) if p .strip ()]
165
+
166
+ # Check if session is a parameter
167
+ has_session = any (p .strip ().startswith ('session' ) for p in param_list )
168
+ param_count = len (param_list )
169
+
170
+ return {
171
+ 'has_session' : has_session ,
172
+ 'param_count' : param_count ,
173
+ 'param_list' : param_list
174
+ }
175
+ except Exception as e :
176
+ logger .error (f"Error analyzing function signature: { e } " )
177
+
178
+ # Default fallback
179
+ return {
180
+ 'has_session' : False ,
181
+ 'param_count' : 1 ,
182
+ 'param_list' : ['input_data' ]
183
+ }
184
+
150
185
def deploy_component (profile_name , component_path , component_name , component_type ):
151
186
"""Deploy a component to Snowflake."""
152
187
logger .info (f"Deploying component: { component_name } ({ component_type } )" )
@@ -196,9 +231,14 @@ def deploy_component(profile_name, component_path, component_name, component_typ
196
231
logger .info (f"Using actual code directory: { code_dir } " )
197
232
198
233
# Check and fix UDF function signature if necessary
199
- if component_type .lower () == "udf" :
200
- logger .info (f"Checking and fixing UDF function signature for { component_name } " )
201
- os .system (f"python scripts/deployment_files/check_and_fix_udf.py { code_dir } " )
234
+ function_file = os .path .join (code_dir , "function.py" )
235
+ signature_info = {'has_session' : False , 'param_count' : 1 , 'param_list' : ['input_data' ]}
236
+
237
+ if component_type .lower () == "udf" and os .path .exists (function_file ):
238
+ logger .info (f"Analyzing UDF function signature for { component_name } " )
239
+ signature_info = analyze_function_signature (function_file )
240
+ logger .info (f"Function analysis: Session={ signature_info ['has_session' ]} , "
241
+ f"Param Count={ signature_info ['param_count' ]} " )
202
242
203
243
# Log directory contents
204
244
logger .info (f"Component directory structure:" )
@@ -224,16 +264,39 @@ def deploy_component(profile_name, component_path, component_name, component_typ
224
264
import_path = f"@{ stage_name } /{ component_name .replace (' ' , '_' )} /{ zip_filename } "
225
265
226
266
if component_type .lower () == "udf" :
227
- # For Snowpark UDFs - different SQL based on parameter signature
228
- sql = f"""
229
- CREATE OR REPLACE FUNCTION { component_name .replace (' ' , '_' )} (input_data VARIANT)
230
- RETURNS VARIANT
231
- LANGUAGE PYTHON
232
- RUNTIME_VERSION=3.8
233
- PACKAGES = ('snowflake-snowpark-python')
234
- HANDLER = 'function.main'
235
- IMPORTS = ('{ import_path } ')
236
- """
267
+ # Adjust SQL based on parameter count
268
+ if signature_info ['param_count' ] == 1 :
269
+ sql = f"""
270
+ CREATE OR REPLACE FUNCTION { component_name .replace (' ' , '_' )} (input_data VARIANT)
271
+ RETURNS VARIANT
272
+ LANGUAGE PYTHON
273
+ RUNTIME_VERSION=3.8
274
+ PACKAGES = ('snowflake-snowpark-python')
275
+ IMPORTS = ('{ import_path } ')
276
+ HANDLER = 'function.main'
277
+ """
278
+ elif signature_info ['param_count' ] == 2 :
279
+ # For two parameters
280
+ sql = f"""
281
+ CREATE OR REPLACE FUNCTION { component_name .replace (' ' , '_' )} (current_value FLOAT, previous_value FLOAT)
282
+ RETURNS FLOAT
283
+ LANGUAGE PYTHON
284
+ RUNTIME_VERSION=3.8
285
+ PACKAGES = ('snowflake-snowpark-python')
286
+ IMPORTS = ('{ import_path } ')
287
+ HANDLER = 'function.main'
288
+ """
289
+ else :
290
+ # Fallback to standard variant parameter
291
+ sql = f"""
292
+ CREATE OR REPLACE FUNCTION { component_name .replace (' ' , '_' )} (input_data VARIANT)
293
+ RETURNS VARIANT
294
+ LANGUAGE PYTHON
295
+ RUNTIME_VERSION=3.8
296
+ PACKAGES = ('snowflake-snowpark-python')
297
+ IMPORTS = ('{ import_path } ')
298
+ HANDLER = 'function.main'
299
+ """
237
300
else : # procedure
238
301
sql = f"""
239
302
CREATE OR REPLACE PROCEDURE { component_name .replace (' ' , '_' )} ()
0 commit comments