Complete the code to create a simple stored procedure in Snowflake using Python.
CREATE OR REPLACE PROCEDURE my_proc() RETURNS STRING LANGUAGE PYTHON RUNTIME_VERSION = '3.8' PACKAGES = ('snowflake-snowpark-python') HANDLER = 'run' AS $$ def run(session): return [1] $$;
The stored procedure must return a string. The correct return value is a string literal.
Complete the code to define a stored procedure that accepts an integer parameter and returns its square.
CREATE OR REPLACE PROCEDURE square_num(num INT) RETURNS INT LANGUAGE PYTHON RUNTIME_VERSION = '3.8' PACKAGES = ('snowflake-snowpark-python') HANDLER = 'run' AS $$ def run(session, num): return num [1] num $$;
To calculate the square, multiply the number by itself using the * operator.
Fix the error in the stored procedure code that returns the length of a string parameter.
CREATE OR REPLACE PROCEDURE str_length(s STRING) RETURNS INT LANGUAGE PYTHON RUNTIME_VERSION = '3.8' PACKAGES = ('snowflake-snowpark-python') HANDLER = 'run' AS $$ def run(session, s): return [1](s) $$;
In Python, the function to get the length of a string is len().
Fill both blanks to create a stored procedure that returns the uppercase version of a string parameter.
CREATE OR REPLACE PROCEDURE to_uppercase(s STRING) RETURNS STRING LANGUAGE PYTHON RUNTIME_VERSION = '3.8' PACKAGES = ('snowflake-snowpark-python') HANDLER = 'run' AS $$ def run(session, s): return s.[1]() [2] '' $$;
The upper() method converts a string to uppercase. The + operator concatenates strings.
Fill the blanks to create a stored procedure that returns a dictionary with keys as characters and values as their counts in the input string.
CREATE OR REPLACE PROCEDURE char_count(s STRING) RETURNS VARIANT LANGUAGE PYTHON RUNTIME_VERSION = '3.8' PACKAGES = ('snowflake-snowpark-python') HANDLER = 'run' AS $$ def run(session, s): counts = [1]() for c in s: counts[c] = counts.get(c, 0) [2] 1 return counts $$;
dict() creates a dictionary. The + operator adds 1 to the count. The dictionary is returned.