snowflake.core.user_defined_function.UserDefinedFunctionCollection | Snowflake Documentation (original) (raw)
class snowflake.core.user_defined_function.UserDefinedFunctionCollection(schema: SchemaResource)¶
Bases: SchemaObjectCollectionParent
[UserDefinedFunctionResource]
Represents the collection operations on the Snowflake User Defined Function resource.
With this collection, you can create, iterate through, and fetch user defined functions that you have access to in the current context.
Examples
Creating a user defined function instance of python language:
user_defined_functions.create( ... UserDefinedFunction( ... name="my_python_function", ... arguments=[], ... return_type=ReturnDataType(datatype="VARIANT"), ... language_config=PythonFunction(runtime_version="3.9", packages=[], handler="udf"), ... body=''' ... def udf(): ... return {"key": "value"} ... ''', ... ) ... )
Attributes
database¶
The DatabaseResource this collection belongs to.
root¶
The Root object this collection belongs to.
Methods
create(user_defined_function: UserDefinedFunction, *, mode: CreateMode = CreateMode.error_if_exists, copy_grants: bool | None = False) → UserDefinedFunctionResource¶
Create a user defined function in Snowflake.
Parameters:
- user_defined_function (UserDefinedFunction) –
The details ofUserDefinedFunction
object, together withUserDefinedFunction
’s properties:
name , arguments, return_type, language_config; body, comment, is_secure, is_memoizable, is_aggregate, is_temporary are optional. - copy_grants (bool , optional) – Whether to enable copy grants when creating the object. Default is
False
. - mode (CreateMode, optional) –
One of the below enum values.CreateMode.error_if_exists
: Throw an snowflake.core.exceptions.ConflictError if the user defined function already exists in Snowflake. Equivalent to SQLcreate function <name> ...
.CreateMode.or_replace
: Replace if the user defined function already exists in Snowflake. Equivalent to SQLcreate or replace function <name> ...
.CreateMode.if_not_exists
: Do nothing if the user defined function already exists in Snowflake. Equivalent to SQLcreate function <name> if not exists...
Default value isCreateMode.error_if_exists
.
Examples
Creating a user defined function instance of python language:
user_defined_functions.create( ... UserDefinedFunction( ... name="my_python_function", ... arguments=[], ... return_type=ReturnDataType(datatype="VARIANT"), ... language_config=PythonFunction(runtime_version="3.9", packages=[], handler="udf"), ... body=''' ... def udf(): ... return {"key": "value"} ... ''', ... ) ... )
Creating a user defined function instance of java language:
function_body = ''' ... class TestFunc { ... public static String echoVarchar(String x) { ... return x; ... } ... } ... ''' user_defined_functions.create( ... UserDefinedFunction( ... name="my_java_function", ... arguments=[Argument(name="x", datatype="STRING")], ... return_type=ReturnDataType(datatype="VARCHAR", nullable=True), ... language_config=JavaFunction( ... handler="TestFunc.echoVarchar", ... runtime_version="11", ... target_path="@~/my_java.jar", ... packages=[], ... called_on_null_input=True, ... is_volatile=True, ... ), ... body=function_body, ... comment="test_comment", ... ) ... )
Creating a user defined function instance of javascript language:
function_body = ''' ... if (D <= 0) { ... return 1; ... } else { ... var result = 1; ... for (var i = 2; i <= D; i++) { ... result = result * i; ... } ... return result; ... } ... ''' user_defined_function_created = user_defined_functions.create( ... UserDefinedFunction( ... name="my_js_function", ... arguments=[Argument(name="d", datatype="DOUBLE")], ... return_type=ReturnDataType(datatype="DOUBLE"), ... language_config=JavaScriptFunction(), ... body=function_body, ... ) ... )
Creating a user defined function instance of scala language:
function_body = ''' ... class Echo { ... def echoVarchar(x : String): String = { ... return x ... } ... } ... ''' user_defined_function_created = user_defined_functions.create( ... UserDefinedFunction( ... name="my_scala_function", ... arguments=[Argument(name="x", datatype="VARCHAR")], ... return_type=ReturnDataType(datatype="VARCHAR"), ... language_config=ScalaFunction( ... runtime_version="2.12", handler="Echo.echoVarchar", target_path="@~/my_scala.jar", packages=[] ... ), ... body=function_body, ... comment="test_comment", ... ) ... )
Creating a user defined function instance of sql language:
function_body = ''' ... SELECT 1, 2 ... UNION ALL ... SELECT 3, 4 ... ''' user_defined_function_created = user_defined_functions.create( ... UserDefinedFunction( ... name="my_sql_function", ... arguments=[], ... return_type=ReturnTable( ... column_list=[ColumnType(name="x", datatype="INTEGER"), ColumnType(name="y", datatype="INTEGER")] ... ), ... language_config=SQLFunction(), ... body=function_body, ... ) ... )
create_async(user_defined_function: UserDefinedFunction, *, mode: CreateMode = CreateMode.error_if_exists, copy_grants: bool | None = False) → PollingOperation[UserDefinedFunctionResource]¶
An asynchronous version of create().
Refer to PollingOperation for more information on asynchronous execution and the return type.
items() → ItemsView[str, T]¶
iter(*, like: str | None = None) → Iterator[UserDefinedFunction]¶
Iterate through UserDefinedFunction
objects from Snowflake, filtering on any optional ‘like’ pattern.
Parameters:
like (str , optional) – A case-insensitive string functioning as a filter, with support for SQL wildcard characters (% and _).
Examples
Showing all user defined functions that you have access to see:
user_defined_functions = user_defined_function_collection.iter()
Showing information of the exact user defined function you want to see:
user_defined_functions = user_defined_function_collection.iter(like="your-user-defined-function-name")
Showing user defined functions starting with ‘your-user-defined-function-name-‘:
user_defined_functions = user_defined_function_collection.iter(like="your-user-defined-function-name-%")
Using a for loop to retrieve information from iterator:
for user_defined_function in user_defined_functions: ... print(user_defined_function.name)
iter_async(*, like: str | None = None) → PollingOperation[Iterator[UserDefinedFunction]]¶
An asynchronous version of iter().
Refer to PollingOperation for more information on asynchronous execution and the return type.
keys() → KeysView[str]¶
values() → ValuesView[T]¶