UDTF
A user-defined table function (UDTF) processes one input row and generates multiple output rows. This makes it ideal for multimodal data processing scenarios like video/audio frame splitting or sample generation.
The development process for a UDTF is similar to that of a scalar UDF, with the following key considerations:
- The handler currently only supports returning iterators, so using yield is recommended.
- UDTFs do not support concurrent execution or vectorized computation.
- If the handler enters an infinite loop or produces endless output, Aura will automatically terminate the program based on the timeout setting, which defaults to 1,000 seconds.
Example
import os import ibis import aura_frame as aura from aura_frame.multimodal import ai_lake import ibis.expr.datatypes as dt target_database = "test" #Create a database in LakeFormation, for example, test. For details, see Creating a DataArts Lake Formation Instance and Planning Metadata. #Connect to the AuraJobV2 endpoint. For details about the parameters, see Establishing a Connection with the AuraJobV2 Endpoint. con = ai_lake.connect( aura_endpoint=os.getenv("aura_endpoint"), # Endpoint where the AuraJobV2 endpoint is located aura_endpoint_id=os.getenv("aura_endpoint_id"), # AuraJobV2 endpoint ID aura_endpoint_name=os.getenv("aura_endpoint_name"),# AuraJobV2 endpoint name aura_workspace_id=os.getenv("aura_workspace_id"), # Workspace ID lf_catalog_name=os.getenv("lf_catalog_name"), # LakeFormation Catalog name access_key=os.getenv("access_key"), # Access Key ID (AK) secret_key=os.getenv("secret_key"), # Secret Key (SK) default_database="postgres", # LakeFormation database name use_single_cn_mode=True, # Whether to enable the single-CN mode logging_level=logging.INFO # Set the log level. ) #Configure the UDF deployment workspace. For details about the parameters, see Configuring the UDF Deployment Workspace. con.set_function_staging_workspace( obs_directory_base=os.getenv("obs_directory_base"), # Path for storing UDFs in the parallel file system. obs_bucket_name=os.getenv("obs_bucket_name"), # Name of the parallel file system. obs_server=os.getenv("obs_server"), # OBS endpoint access_key=os.getenv("access_key"), # Access Key ID (AK) secret_key=os.getenv("secret_key"), # Secret Key (SK) ) # Define a UDTF: Enter start, endnum, and step to generate an integer sequence. def py_generate_series(start: int, endnum: int, step: int): current = start while current <= endnum: yield {"col1": current} current += step try: # Create a test table and write data into it. table_name = "sequence_config" table_schema = ibis.schema({"start_val": "int32", "end_val": "int32", "step_val": "int32"}) con.sql(f'DROP TABLE IF EXISTS "{target_database}"."{table_name}";') con.create_table( table_name, schema=table_schema, database=target_database, table_format="orc", if_not_exists=True, ) con.sql(f''' INSERT INTO "{target_database}"."{table_name}" (start_val, end_val, step_val) VALUES (1, 5, 1), (10, 20, 5), (0, 10, 2) ''') # Delete the existing UDTF with the same name (repeated execution is supported). con.delete_function("py_generate_series", database=target_database, if_it_exists=True) func_signature = aura.Signature( parameters=[ aura.Parameter(name="start", annotation=int), aura.Parameter(name="endnum", annotation=int), aura.Parameter(name="step", annotation=int), ], return_annotation=dt.Struct({"col1": int}), ) con.create_table_function(py_generate_series, database=target_database, signature=func_signature) ds = con.load_dataset("sequence_config", database=target_database) py_generate_series_handler = con.get_function("py_generate_series", database=target_database) ds = ds.flat_map( fn=py_generate_series_handler, on=[ds.start_val, ds.end_val, ds.step_val], as_col="udtf_col", ) res = ds.execute() print(res) finally: con.close()
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot