Stored procedures in Python in Snowflake - Time & Space Complexity
We want to understand how the time it takes to run a Python stored procedure in Snowflake changes as the input size grows.
Specifically, how does the number of operations inside the procedure grow with the data it processes?
Analyze the time complexity of the following Python stored procedure in Snowflake.
CREATE OR REPLACE PROCEDURE process_data(input_array ARRAY)
RETURNS STRING
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
AS
$$
result = []
for item in input_array:
# Simulate some processing
result.append(item * 2)
return str(result)
$$;
This procedure takes an array of items, processes each item by doubling it, and returns the results as a string.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Looping over each item in the input array and doubling it.
- How many times: Once for each item in the input array.
The procedure processes each item one by one, so if the input doubles, the work doubles too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 operations (doubling each item) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time to run the procedure grows in a straight line with the number of items processed.
[X] Wrong: "The procedure runs in constant time no matter the input size because it's just one call."
[OK] Correct: Even though it's one procedure call, the work inside depends on how many items it processes, so time grows with input size.
Understanding how stored procedures scale with input size helps you design efficient data processing in the cloud, a key skill for real-world projects.
"What if the procedure called another procedure inside the loop for each item? How would the time complexity change?"
Practice
Solution
Step 1: Understand stored procedures in Snowflake
Stored procedures allow running Python code inside Snowflake to automate tasks.Step 2: Identify the main purpose
The main purpose is to centralize and automate database logic using Python.Final Answer:
To run Python code inside Snowflake and automate database logic -> Option CQuick Check:
Stored procedures = automate database logic [OK]
- Thinking stored procedures create user interfaces
- Confusing stored procedures with external Python scripts
- Assuming stored procedures store files
Solution
Step 1: Identify correct syntax for Python stored procedure
Snowflake requires CREATE PROCEDURE with LANGUAGE PYTHON and the code inside $$ delimiters.Step 2: Check each option
CREATE PROCEDURE my_proc() RETURNS STRING LANGUAGE PYTHON AS $$ def main(): return 'Hello' $$; uses CREATE PROCEDURE, LANGUAGE PYTHON, and $$ with def main(): which is correct.Final Answer:
CREATE PROCEDURE my_proc() RETURNS STRING LANGUAGE PYTHON AS $$ def main(): return 'Hello' $$; -> Option DQuick Check:
Correct syntax = CREATE PROCEDURE my_proc() RETURNS STRING LANGUAGE PYTHON AS $$ def main(): return 'Hello' $$; [OK]
- Using CREATE FUNCTION instead of PROCEDURE
- Using LANGUAGE SQL for Python code
- Not using $$ delimiters for Python code
CREATE PROCEDURE add_numbers(a INT, b INT) RETURNS INT LANGUAGE PYTHON AS $$
def main(session, a, b):
return a + b
$$;
What will be the result of calling CALL add_numbers(3, 4);?Solution
Step 1: Understand the procedure parameters and return
The procedure takes two integers a and b, and returns their sum.Step 2: Analyze the call with 3 and 4
Calling add_numbers(3, 4) passes 3 and 4, so the return is 3 + 4 = 7.Final Answer:
7 -> Option BQuick Check:
3 + 4 = 7 [OK]
- Confusing concatenation with addition
- Forgetting the session parameter is implicit
- Expecting an error due to session parameter
CREATE PROCEDURE greet() RETURNS STRING LANGUAGE PYTHON AS $$
def main():
return 'Hello, World!'
$$;Solution
Step 1: Check the main function signature
Snowflake Python stored procedures require main(session, ...) with session as first parameter.Step 2: Identify the error
The code defines main() without session parameter, causing an error.Final Answer:
The main function must include a session parameter -> Option AQuick Check:
Missing session parameter = The main function must include a session parameter [OK]
- Omitting the session parameter in main()
- Using wrong RETURNS type
- Confusing LANGUAGE SQL with PYTHON
users and returns the count of users with age over 30. Which approach is correct?Solution
Step 1: Understand how to access data in Python stored procedures
Python stored procedures use the session object to run SQL queries inside Snowflake.Step 2: Identify correct method to get user count
Executing a SQL query via session to count users with age > 30 is correct.Final Answer:
Use the session object to execute a SQL query inside main(), then return the count -> Option AQuick Check:
Use session to query database = Use the session object to execute a SQL query inside main(), then return the count [OK]
- Trying to loop over data without SQL query
- Ignoring session object for database access
- Returning count as string without querying
Ready to prove it?
Pass the test and this pattern moves unknown
