You are viewing documentation about an older version (1.1.0). View latest version

snowflake.snowpark.functions.sproc¶

snowflake.snowpark.functions.sproc(func: Callable | None = None, *, return_type: DataType | None = None, input_types: List[DataType] | None = None, name: str | Iterable[str] | None = None, is_permanent: bool = False, stage_location: str | None = None, imports: List[str | Tuple[str, str]] | None = None, packages: List[str | module] | None = None, replace: bool = False, session: Session | None = None, parallel: int = 4, statement_params: Dict[str, str] | None = None, execute_as: Literal['caller', 'owner'] = 'owner', strict: bool = False) → StoredProcedure | partial[source]¶

Registers a Python function as a Snowflake Python stored procedure and returns the stored procedure.

It can be used as either a function call or a decorator. In most cases you work with a single session. This function uses that session to register the stored procedure. If you have multiple sessions, you need to explicitly specify the session parameter of this function. If you have a function and would like to register it to multiple databases, use session.sproc.register instead. See examples in StoredProcedureRegistration.

Note that the first parameter of your function should be a snowpark Session. Also, you need to add snowflake-snowpark-python package (version >= 0.4.0) to your session before trying to create a stored procedure.

Parameters:
  • func – A Python function used for creating the stored procedure.

  • return_type – A DataType representing the return data type of the stored procedure. Optional if type hints are provided.

  • input_types – A list of DataType representing the input data types of the stored procedure. Optional if type hints are provided.

  • name – A string or list of strings that specify the name or fully-qualified object identifier (database name, schema name, and function name) for the stored procedure in Snowflake, which allows you to call this stored procedure in a SQL command or via session.call(). If it is not provided, a name will be automatically generated for the stored procedure. A name must be specified when is_permanent is True.

  • is_permanent – Whether to create a permanent stored procedure. The default is False. If it is True, a valid stage_location must be provided.

  • stage_location – The stage location where the Python file for the stored procedure and its dependencies should be uploaded. The stage location must be specified when is_permanent is True, and it will be ignored when is_permanent is False. It can be any stage other than temporary stages and external stages.

  • imports – A list of imports that only apply to this stored procedure. You can use a string to represent a file path (similar to the path argument in add_import()) in this list, or a tuple of two strings to represent a file path and an import path (similar to the import_path argument in add_import()). These stored-proc-level imports will override the session-level imports added by add_import().

  • packages – A list of packages that only apply to this stored procedure. These stored-proc-level packages will override the session-level packages added by add_packages() and add_requirements().

  • replace – Whether to replace a stored procedure that already was registered. The default is False. If it is False, attempting to register a stored procedure with a name that already exists results in a SnowparkSQLException exception being thrown. If it is True, an existing stored procedure with the same name is overwritten.

  • session – Use this session to register the stored procedure. If it’s not specified, the session that you created before calling this function will be used. You need to specify this parameter if you have created multiple sessions before calling this method.

  • parallel – The number of threads to use for uploading stored procedure files with the PUT command. The default value is 4 and supported values are from 1 to 99. Increasing the number of threads can improve performance when uploading large stored procedure files.

  • execute_as – What permissions should the procedure have while executing. This supports caller, or owner for now. See owner and caller rights for more information.

  • statement_params – Dictionary of statement level parameters to be set while executing this action.

  • strict – Whether the created stored procedure is strict. A strict stored procedure will not invoke the stored procedure if any input is null. Instead, a null value will always be returned. Note that the stored procedure might still return null for non-null inputs.

Returns:

A stored procedure function that can be called with python value.

Note

1. When type hints are provided and are complete for a function, return_type and input_types are optional and will be ignored. See details of supported data types for stored procedure in StoredProcedureRegistration.

  • You can use Variant to annotate a variant, and use Geography to annotate a geography when defining a stored procedure.

  • typing.Union is not a valid type annotation for stored procedures, but typing.Optional can be used to indicate the optional type.

2. A temporary stored procedure (when is_permanent is False) is scoped to this session and all stored procedure related files will be uploaded to a temporary session stage (session.get_session_stage()). For a permanent stored procedure, these files will be uploaded to the stage that you provide.

3. By default, stored procedure registration fails if a function with the same name is already registered. Invoking sproc() with replace set to True will overwrite the previously registered function.