4

Can Snowflake Procedure can be written in python instead of javascript?

I went through Snowflake document it says javascript but was looking for options other then javascript.

rjain550
  • 105
  • 2
  • 8
  • I am aware of external function concepts I am thinking of something like can we store and execute python stored procedure within Snowflake. – rjain550 Aug 18 '20 at 15:05

2 Answers2

2

Snowflake recently introduced External functions which gives user ability to write UDFS in python. More details on following link

https://docs.snowflake.com/en/sql-reference/external-functions-introduction.html

https://community.snowflake.com/s/article/Snowflake-External-Function

Iqra Ijaz
  • 261
  • 1
  • 3
0

As of Sept 2022, Snowflake has added Python 3.8 support to Functions and Procedures in Snowflake now with packages like Pandas preinstalled.

To see what python packages you can use inside Snowflake:

select * from information_schema.packages where language = 'python';

To have your Python code directly in snowflake running, you can create a Python UDF:

-- Creates the function that adds 1 to 2 integers you pass it.
create or replace function add_one_to_inputs(x number(10, 0), y number(10, 0))
returns number(10, 0)
language python
runtime_version = 3.8
packages = ('pandas')
handler = 'add_one_to_inputs'
as
$$
import pandas

def add_one_to_inputs(df):
  return df[0] + df[1] + 1

add_one_to_inputs._sf_vectorized_input = pandas.DataFrame
$$;

-- After function creation to run call it
select add_one_to_inputs(2,4);

Not only that, you can also execute python code from a stage. You can execute the CREATE PROCEDURE command, pointing to the Python file on the stage. For example:

CREATE OR REPLACE PROCEDURE MYPROC(from_table STRING, to_table STRING, count INT)
  RETURNS INT
  LANGUAGE PYTHON
  RUNTIME_VERSION = '3.8'
  PACKAGES = ('snowflake-snowpark-python')
  IMPORTS = ('@mystage/my_py_file.py')
  HANDLER = 'my_py_file.run';

You can read more about Python UDFs here: https://docs.snowflake.com/en/developer-guide/udf/python/udf-python.html#python-udfs

Writing Stored Procedures in Python: https://docs.snowflake.com/en/sql-reference/stored-procedures-python.html