snowflake.core.user_defined_function.UserDefinedFunctionCollection¶

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.8", packages=[], handler="udf"),
...         body='''
... def udf():
...     return {"key": "value"}
...             ''',
...     )
... )
Copy

Attributes

database¶
root¶

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 of UserDefinedFunction object, together with UserDefinedFunction’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 SQL create function <name> ....

    CreateMode.or_replace: Replace if the user defined function already exists in Snowflake. Equivalent to SQL create or replace function <name> ....

    CreateMode.if_not_exists: Do nothing if the user defined function already exists in Snowflake. Equivalent to SQL create function <name> if not exists...

    Default value is CreateMode.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.8", packages=[], handler="udf"),
...         body='''
... def udf():
...     return {"key": "value"}
...             ''',
...     )
... )
Copy

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",
...     )
... )
Copy

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

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",
...     )
... )
Copy

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,
...     )
... )
Copy
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()
Copy

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")
Copy

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-%")
Copy

Using a for loop to retrieve information from iterator:

>>> for user_defined_function in user_defined_functions:
...     print(user_defined_function.name)
Copy
keys() → KeysView[str]¶
values() → ValuesView[T]¶