snowflake.snowpark.udtf.UDTFRegistration¶
- class snowflake.snowpark.udtf.UDTFRegistration(session: Optional[Session])[source]¶
Bases:
objectProvides methods to register classes as UDTFs in the Snowflake database. For more information about Snowflake Python UDTFs, see Python UDTFs.
session.udtfreturns an object of this class. You can use this object to register UDTFs that you plan to use in the current session or permanently. The methods that register a UDTF return aUserDefinedTableFunctionobject, which you can also use to call the UDTF.Registering a UDTF is like registering a scalar UDF, you can use
register()orsnowflake.snowpark.functions.udtf()to explicitly register it. You can also use the decorator @udtf. They all usecloudpickleto transfer the code from the client to the server. Another way is to useregister_from_file(). Refer to modulesnowflake.snowpark.udtf.UDTFRegistrationfor when to use them.To query a registered UDTF is the same as to query other table functions. Refer to
table_function()andjoin_table_function(). If you want to query a UDTF right after it’s created, you can call the createdUserDefinedTableFunctioninstance like in Example 1 below.- Example 1
Create a temporary UDTF and call it:
- Example 2
Create a UDTF with type hints and
@udtfdecorator and query it:- Example 3
Create a permanent UDTF with a name and call it in SQL:
- Example 4
Create a UDTF with type hints:
- Example 5
Create a UDTF with type hints by using
...for multiple columns of the same type:- Example 6
Create a UDTF with UDF-level imports and type hints:
- Example 7
Create a UDTF with UDF-level packages and type hints:
- Example 8
Creating a UDTF with the constructor and
end_partitionmethod.- Example 9
Creating a UDTF from a local Python file:
- Example 10
Creating a UDTF from a Python file on an internal stage:
You can use
udtf(),register()orpandas_udtf()to create a vectorized UDTF by providing appropriate return and input types. If you would like to useregister_from_file()to create a vectorized UDTF, you would need to explicitly mark the handler method as vectorized using either the decorator@vectorized(input=pandas.DataFrame)or setting<class>.end_partition._sf_vectorized_input = pandas.DataFrame- Example 11
Creating a vectorized UDTF by specifying a
PandasDataFrameTypeasinput_typesand aPandasDataFrameTypewith column names asoutput_schema.- Example 12
Creating a vectorized UDTF by specifying
PandasDataFramewith nested types as type hints.- Example 13
Creating a vectorized UDTF by specifying a
pandas.DataFrameas type hints and aStructTypewith type information and column names asoutput_schema.- Example 14
Same as Example 12, but does not specify input_names and instead set the column names in end_partition.
[Preview Feature] The syntax for declaring UDTF with a vectorized process method is similar to above. Defining
__init__andend_partitionmethods are optional. Theprocessmethod only accepts one argument which is the pandas Dataframe object, and outputs the same number of rows as is in the given input. Both__init__andend_partitiondo not take any additional arguments.- Example 15
Vectorized UDTF process method without end_partition
- Example 16
Vectorized UDTF process method with end_partition
- Example 17
Vectorized UDTF process method with end_partition and max_batch_size
See also
Methods
register(handler, output_schema[, ...])Registers a Python class as a Snowflake Python UDTF and returns the UDTF.
register_from_file(file_path, handler_name, ...)Registers a Python class as a Snowflake Python UDTF from a Python or zip file, and returns the UDTF.