Snowflake – invoking Python code without creating UDF/Stored Procedure
Question:
How to run Python code without defining pernament/temporary User Defined Function or stored procedure?
The use case is to run the code one time inside a script and it should avoid creating/dropping additional database objects.
I am searching for an equivalent of anonymous block (SQL) known from Snowflake Scripting but for Python or other supported languages(Java, Scala).
Snowflake Scripting – Using an Anonymous Block
If you don’t want to store the block in a stored procedure in the database, you can define and use an anonymous block. An anonymous block is a block that is not part of a stored procedure. You define the block as a separate, standalone SQL statement.
The BEGIN … END statement that defines the block also executes the block. (You don’t run a separate CALL command to execute the block.)
Answers:
It is possible to define inline stored procedure using WITH <stored_procedure> AS ... CALL <stored_procedure>
syntax.
CALL (with Anonymous Procedure)
Creates and calls an anonymous procedure that is like a stored procedure but is not stored for later use.
With this command, you both create an anonymous procedure defined by parameters in the WITH clause and call that procedure.
You need not have a role with CREATE PROCEDURE schema privileges for this command.
Sample:
WITH proc AS PROCEDURE(str TEXT)
RETURNS TEXT
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
PACKAGES = ('snowflake-snowpark-python')
HANDLER = 'main'
AS
$$
import unicodedata
def main(session, str):
return unicodedata.category(str)
$$
CALL proc('-');
Output:
Writing Snowpark Code in Python Worksheets
Write Snowpark code in Python worksheets to process data using Snowpark Python in Snowsight. By writing code in Python worksheets, you can perform your development and testing in Snowflake without needing to install dependent libraries.
To develop with Python worksheets, do the following:
- Prepare roles and packages in Snowflake.
- Set up your worksheet for development.
- Write Snowpark code in your Python worksheet.
- Run your Python worksheet.
If we check query history, the CALL(with Anonymous Procedure)
is still used:
When development is done, the code can be deployed as a stored procedure.
Related: Creating a Python Stored Procedure to Automate Your Python Worksheet Code
How to run Python code without defining pernament/temporary User Defined Function or stored procedure?
The use case is to run the code one time inside a script and it should avoid creating/dropping additional database objects.
I am searching for an equivalent of anonymous block (SQL) known from Snowflake Scripting but for Python or other supported languages(Java, Scala).
Snowflake Scripting – Using an Anonymous Block
If you don’t want to store the block in a stored procedure in the database, you can define and use an anonymous block. An anonymous block is a block that is not part of a stored procedure. You define the block as a separate, standalone SQL statement.
The BEGIN … END statement that defines the block also executes the block. (You don’t run a separate CALL command to execute the block.)
It is possible to define inline stored procedure using WITH <stored_procedure> AS ... CALL <stored_procedure>
syntax.
CALL (with Anonymous Procedure)
Creates and calls an anonymous procedure that is like a stored procedure but is not stored for later use.
With this command, you both create an anonymous procedure defined by parameters in the WITH clause and call that procedure.
You need not have a role with CREATE PROCEDURE schema privileges for this command.
Sample:
WITH proc AS PROCEDURE(str TEXT)
RETURNS TEXT
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
PACKAGES = ('snowflake-snowpark-python')
HANDLER = 'main'
AS
$$
import unicodedata
def main(session, str):
return unicodedata.category(str)
$$
CALL proc('-');
Output:
Writing Snowpark Code in Python Worksheets
Write Snowpark code in Python worksheets to process data using Snowpark Python in Snowsight. By writing code in Python worksheets, you can perform your development and testing in Snowflake without needing to install dependent libraries.
To develop with Python worksheets, do the following:
- Prepare roles and packages in Snowflake.
- Set up your worksheet for development.
- Write Snowpark code in your Python worksheet.
- Run your Python worksheet.
If we check query history, the CALL(with Anonymous Procedure)
is still used:
When development is done, the code can be deployed as a stored procedure.
Related: Creating a Python Stored Procedure to Automate Your Python Worksheet Code