Skip to content

Commit 5d41a70

Browse files
committed
dev changes to deployment
1 parent 1099bc1 commit 5d41a70

File tree

9 files changed

+14
-110
lines changed

9 files changed

+14
-110
lines changed

scripts/snowflake_deployer.py

Lines changed: 13 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -180,81 +180,10 @@ def deploy_component(profile_name, component_path, component_name, component_typ
180180
code_dir = os.path.join(component_path, subdirs[0])
181181
logger.info(f"Using actual code directory: {code_dir}")
182182

183-
# Check for UDF function signature to automatically fix issues
184-
function_file = os.path.join(code_dir, "function.py")
185-
has_session_param = False
186-
187-
if os.path.exists(function_file):
188-
try:
189-
with open(function_file, 'r') as f:
190-
content = f.read()
191-
192-
# Check if we need to fix the function signature for UDFs with session parameters
193-
if component_type.lower() == "udf" and "def main(session, input_data" in content:
194-
logger.info("Detected UDF with session parameter - applying automatic fix")
195-
has_session_param = True
196-
197-
# Check if we need to automatically modify UDF with wrapper function
198-
if "def main_with_session(" not in content:
199-
# Create a backup of the original file
200-
backup_file = function_file + ".bak"
201-
with open(backup_file, 'w') as f:
202-
f.write(content)
203-
204-
# Modify the content to include a wrapper
205-
modified_content = content.replace(
206-
"def main(session, input_data",
207-
"""# Original function
208-
def main_with_session(session, input_data
209-
210-
# Wrapper function that Snowflake calls directly
211-
def main(input_data"""
212-
)
213-
214-
# Add the wrapper implementation at the end
215-
indent = " " # Default indentation
216-
lines = modified_content.split("\n")
217-
# Find where to insert wrapper code
218-
insert_pos = len(lines) # Default to end of file
219-
for i in range(len(lines)):
220-
if lines[i].strip() == "def main(input_data":
221-
# Find indentation level and next non-empty line
222-
for j in range(i+1, len(lines)):
223-
if lines[j].strip():
224-
indent = lines[j].split(lines[j].lstrip())[0]
225-
break
226-
227-
# Add wrapper implementation
228-
wrapper_code = [
229-
f"{indent}# Get session from Snowflake context",
230-
f"{indent}from snowflake.snowpark.context import get_active_session",
231-
f"{indent}session = get_active_session()",
232-
f"{indent}# Call the original function with session",
233-
f"{indent}return main_with_session(session, input_data)"
234-
]
235-
236-
# Find the position to insert the wrapper code
237-
for i in range(len(lines)-1, -1, -1):
238-
if "def main(input_data" in lines[i]:
239-
# Find where the function body ends
240-
func_indent = lines[i+1].split(lines[i+1].lstrip())[0]
241-
for j in range(i+1, len(lines)):
242-
if j == len(lines)-1 or (lines[j] and not lines[j].startswith(func_indent)):
243-
insert_pos = j
244-
break
245-
246-
# Insert wrapper code at appropriate position
247-
for code_line in wrapper_code:
248-
lines.insert(insert_pos, code_line)
249-
insert_pos += 1
250-
251-
# Write modified content back
252-
with open(function_file, 'w') as f:
253-
f.write("\n".join(lines))
254-
255-
logger.info(f"Modified UDF function to handle session parameter")
256-
except Exception as e:
257-
logger.warning(f"Could not check/fix function signature: {str(e)}")
183+
# Check and fix UDF function signature if necessary
184+
if component_type.lower() == "udf":
185+
logger.info(f"Checking and fixing UDF function signature for {component_name}")
186+
os.system(f"python scripts/check_and_fix_udf.py {code_dir}")
258187

259188
# Log directory contents
260189
logger.info(f"Component directory structure:")
@@ -266,19 +195,6 @@ def main(input_data"""
266195
for f in files:
267196
logger.info(f"{sub_indent}{f}")
268197

269-
# Check if UDF requires session parameter by examining function.py
270-
has_session_param = False
271-
try:
272-
function_file = os.path.join(code_dir, "function.py")
273-
if os.path.exists(function_file):
274-
with open(function_file, 'r') as f:
275-
content = f.read()
276-
if "def main(session, input_data" in content:
277-
logger.info("Detected UDF with session parameter")
278-
has_session_param = True
279-
except Exception as e:
280-
logger.warning(f"Could not check function signature: {str(e)}")
281-
282198
# Zip the directory
283199
zip_directory(code_dir, zip_path)
284200
logger.info(f"Created zip file: {zip_path}")
@@ -294,28 +210,15 @@ def main(input_data"""
294210

295211
if component_type.lower() == "udf":
296212
# For Snowpark UDFs - different SQL based on parameter signature
297-
if has_session_param:
298-
# For UDFs with session parameter (Snowpark style)
299-
sql = f"""
300-
CREATE OR REPLACE FUNCTION {component_name.replace(' ', '_')}(input_data VARIANT)
301-
RETURNS VARIANT
302-
LANGUAGE PYTHON
303-
RUNTIME_VERSION=3.8
304-
PACKAGES = ('snowflake-snowpark-python')
305-
HANDLER = 'function.main'
306-
IMPORTS = ('{import_path}')
307-
"""
308-
else:
309-
# For basic UDFs without session parameter
310-
sql = f"""
311-
CREATE OR REPLACE FUNCTION {component_name.replace(' ', '_')}(input_data VARIANT)
312-
RETURNS VARIANT
313-
LANGUAGE PYTHON
314-
RUNTIME_VERSION=3.8
315-
PACKAGES = ('snowflake-snowpark-python')
316-
IMPORTS = ('{import_path}')
317-
HANDLER = 'function.main'
318-
"""
213+
sql = f"""
214+
CREATE OR REPLACE FUNCTION {component_name.replace(' ', '_')}(input_data VARIANT)
215+
RETURNS VARIANT
216+
LANGUAGE PYTHON
217+
RUNTIME_VERSION=3.8
218+
PACKAGES = ('snowflake-snowpark-python')
219+
HANDLER = 'function.main'
220+
IMPORTS = ('{import_path}')
221+
"""
319222
else: # procedure
320223
sql = f"""
321224
CREATE OR REPLACE PROCEDURE {component_name.replace(' ', '_')}()

udfs_and_spoc/co2_analytical_sp/co2_analytical_sp/function.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import sys
77

8+
89
# Determine if we're running in Snowflake or locally
910
is_running_in_snowflake = 'SNOWFLAKE_PYTHON_INTERPRETER' in os.environ
1011

0 commit comments

Comments
 (0)