4/29/24, 3:05 PM Creating User-Defined Table Functions (UDTFs) for DataFrames in Python | Snowflake Documentation
Developer Snowpark API Python Creating User Defined Table Functions
Creating User-Defined Table Functions (UDTFs) for DataFrames in Python The Snowpark API provides methods that you can use to create a user-defined table function with a handler written in Python. This topic explains how to create these types of functions. Introduction You can create a user-defined table function (UDTF) using the Snowpark API. You do this in a way similar to creating a scalar user-defined function (UDF) with the API, as described in Creating User-Defined Functions (UDFs) for DataFrames in Python. Key differences include UDF handler requirements and parameter values required when registering the UDTF. To create and register a UDTF with Snowpark, you must: Implement a UDTF handler. The handler contains the UDTF’s logic. A UDTF handler must implement functions that Snowflake will invoke at runtime when the UDTF is called. For more information, see Implementing a UDTF Handler. Register the UDTF and its handler in the Snowflake database. You can use the Snowpark API to register the UDTF and its handler. Once you’ve registered the UDTF, you can call it from SQL or by using the Snowpark API. For more information about registering, see Registering a UDTF. For information on calling a UDTF, see Calling User-Defined Table Functions (UDTFs). Implementing a UDTF Handler As described in detail in Writing a UDTF in Python, a UDTF handler class must implement methods that Snowflake invokes when the UDTF is called. You can use the class you write as a handler whether you’re registering the UDTF with the Snowpark API or creating it with SQL using the CREATE FUNCTION statement. Methods of a handler class are designed to process rows and partitions received by the UDTF. https://docs.snowflake.com/en/developer-guide/snowpark/python/creating-udtfs 1/1