Search code examples
pythonstored-proceduressnowflake-cloud-data-platform

Snowflake - invoking Python code without creating UDF/Stored Procedure


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.)


Solution

  • Snowsight natively supports Notebooks:

    enter image description here


    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:

    1. Prepare roles and packages in Snowflake.
    2. Set up your worksheet for development.
    3. Write Snowpark code in your Python worksheet.
    4. Run your Python worksheet.

    enter image description here

    If we check query history, the CALL(with Anonymous Procedure) is still used:

    enter image description here

    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