Making dependencies available to your code

When your user-defined function (UDF) or stored procedure depends on code or files that are external to the UDF or procedure, you can make the dependency available to the UDF or procedure from a stage, including a repository stage with a clone of a remote Git repository that Snowflake is using.

For example, you might want your UDF or procedure to have access to the following:

  • Python handler code in a module.

  • Java or Scala handler code compiled and packaged in a JAR.

  • Dependency code written in Java, Python, or Scala.

  • Files to be read by your handler code and whose name and location is known when you create the UDF. This can be useful with configuration files, for example.

Note

You can also use the PACKAGES clause of CREATE FUNCTION or CREATE PROCEDURE to import libraries that are included in Snowflake.

High-level steps

Follow these steps to make dependencies available to your function or procedure.

  1. Choose or create a stage that’s available to your handler.

  2. Upload the dependency to the stage.

  3. Reference the dependency with IMPORTS when you create the function or procedure.

Choosing or creating a stage for dependency files

To make your dependency file available to a function or procedure, the file will need to be on a stage where it can be reached at runtime. The owner of the function or procedure must have the READ privilege to the stage.

For more about creating stages, see CREATE STAGE.

You can also set up Snowflake to use a remote Git repository, creating a repository stage with a full clone of the remote repository’s files.

Note

You can’t execute the PUT command through the Snowflake GUI; you can use SnowSQL to execute PUT. For an example PUT command to copy a .jar file to a stage, see Uploading the dependency to the stage in this topic.

Choose or create one of the following kinds of stage for your dependency:

  • A repository stage with a clone of files from the remote repository.

    For more information, see Using a Git repository in Snowflake.

  • A user or named internal stage.

    If you plan to use the PUT command to upload the files, use a named internal stage. For more on choosing an internal stage type, see Choosing an internal stage for local files.

  • An external stage.

    External stages are locations associated with external storage services, as described in CREATE STAGE. The PUT command does not support uploading files to external stages.

If you don’t already have a user stage, named internal stage, or external stage, you can create one by executing CREATE STAGE. For example, the following command creates a new internal stage named mystage:

CREATE STAGE mystage;
Copy

Note

Snowflake does not currently support using a table stage to store handler code.

Uploading the dependency to the stage

Upload the files required for your stored procedure to a stage.

If your handler is from a Git repository you’re using with Snowflake, you might instead need to fetch the latest from your remote repository to the Snowflake repository stage.

If you’re using an external stage, use that storage service’s means for uploading files. If you’re using an internal stage, you can copy the file from a local drive to the stage by using the PUT command. For command reference, see PUT. For information on staging files with PUT, see Staging data files from a local file system.

Use the PUT command to upload files to the stage.

Code in the following example uploads myjar.jar to a stage called mystage, overwriting an existing file of the same name if it exists.

PUT file:///Users/MyUserName/MyCompiledJavaCode.jar
  @mystage
  AUTO_COMPRESS = FALSE
  OVERWRITE = TRUE
  ;
Copy

Note

If you omit AUTO_COMPRESS = FALSE, the PUT command automatically compresses the file. The name of the compressed file on the stage will be myjar.jar.gz. Later, when you execute a command such as CREATE PROCEDURE, you will need to specify the filename with this .gz extension in the command’s IMPORTS clause.

Note

The PUT command does not support uploading files to external stages. To upload files to external stages, use the utilities provided by the cloud service.

Referencing the dependency

To make a function or procedure you’re creating aware of the dependency’s location, specify the dependency’s location in the IMPORTS clause of the SQL you use to create the function or procedure.

If you have multiple dependency files, such as when you have third-party libraries on which a handler depends, you can specify the stage location and file path-and-name of all dependency files as values of the IMPORTS clause.

Code in the following example creates a procedure called MYPROC, specifying that the file MyCompiledJavaCode.jar (on the mystage stage) should be included in the procedure’s execution environment. In this case, MyCompiledJavaCode.jar contains the procedure’s handler – the compiled code for MyJavaClass.run.

CREATE OR REPLACE PROCEDURE MYPROC(value INT, fromTable STRING, toTable STRING, count INT)
  RETURNS INT
  LANGUAGE JAVA
  RUNTIME_VERSION = '11'
  PACKAGES = ('com.snowflake:snowpark:latest')
  IMPORTS = ('@mystage/MyCompiledJavaCode.jar')
  HANDLER = 'MyJavaClass.run';
Copy
Language: English