snowflake.snowpark.stored_procedure.StoredProcedureRegistration¶
- class snowflake.snowpark.stored_procedure.StoredProcedureRegistration(session: Session)[source]¶
Bases:
objectProvides methods to register lambdas and functions as stored procedures in the Snowflake database. For more information about Snowflake Python stored procedures, see Python stored procedures.
session.sprocreturns an object of this class. You can use this object to register stored procedures that you plan to use in the current session or permanently. The methods that register a stored procedure return aStoredProcedureobject.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.
There are two ways to register a stored procedure with Snowpark:
Use
sproc()orregister(). By pointing to a runtime Python function, Snowpark uses cloudpickle to serialize this function to bytecode, and deserialize the bytecode to a Python function on the Snowflake server during stored procedure creation. During the serialization, the global variables used in the Python function will be serialized into the bytecode, but only the name of the module object or any objects from a module that are used in the Python function will be serialized. If the size of the serialized bytecode is over 8K bytes, it will be uploaded to a stage location as a Python file. If it’s under 8K, it will be added to the Stored Procedure in-line code.During the deserialization, Python will look up the corresponding modules and objects by names.
Details could be found in
snowflake.snowpark.udf.UDFRegistration.Use
register_from_file(). By pointing to a Python file or a zip file containing Python source code and the target function name, Snowpark uploads this file to a stage (which can also be customized), and load the corresponding function from this file to the Python runtime on the Snowflake server during stored procedure creation. Then this function will be invoked when calling this stored procedure. This approach can address the deficiency of the previous approach that uses cloudpickle, because the source code in this file other than the target function will be loaded during stored procedure creation. Therefore, this approach is useful and efficient when all your Python code is already in source files.
Snowflake supports the following data types for the parameters for a stored procedure:
Python Type
Snowpark Type
SQL Type
intNUMBER
decimal.DecimalNUMBER
floatFLOAT
strSTRING
boolBOOL
datetime.timeTIME
datetime.dateDATE
datetime.datetimeTIMESTAMP
bytesorbytearrayBINARY
listARRAY
dictOBJECT
Dynamically mapped to the native Python type
VARIANT
dictGEOGRAPHY
Note
1. Data with the VARIANT SQL type will be converted to a Python type dynamically inside a stored procedure. The following SQL types are converted to
strin stored procedures rather than native Python types: TIME, DATE, TIMESTAMP and BINARY.2. Data returned as
ArrayType(list),MapType(dict) orVariantType(Variant) by a stored procedure will be represented as a json string. You can calleval()orjson.loads()to convert the result to a native Python object. Data returned asGeographyType(Geography) by a stored procedure will be represented as a GeoJSON string.3. Currently calling stored procedure that requires VARIANT and GEOGRAPHY input types is not supported in snowpark API.
- Example 1
Use stored procedure to copy data from one table to another:
- Example 2
Create a temporary stored procedure from a lambda and call it:
- Example 3
Create a stored procedure with type hints and
@sprocdecorator and call it:- Example 4
Create a permanent stored procedure with a name and call it in SQL:
- Example 5
Create a stored procedure with stored-procedure-level imports and call it:
- Example 6
Create a stored procedure with stored-procedure-level packages and call it:
- Example 7
Creating a stored procedure from a local Python file:
- Example 8
Creating a stored procedure from a Python file on an internal stage:
See also
Methods
describe(sproc_obj)Returns a
DataFramethat describes the properties of a stored procedure.register(func[, return_type, input_types, ...])Registers a Python function as a Snowflake Python stored procedure and returns the stored procedure.
register_from_file(file_path, func_name[, ...])Registers a Python function as a Snowflake Python stored procedure from a Python or zip file, and returns the stored procedure.