Help Center/ AI DataLake/ SDK Reference/ Engine-specific SDKs/ Python SDKs/ Aura DataFrame SDK (Python)/ UDFs/ Vectorized UDF
Updated on 2026-11-06 GMT+08:00
Vectorized UDF
Designed to overcome performance limitations of traditional row-based UDFs, vectorized UDFs execute operations in batches. They typically accept and return data in PyArrow or Pandas formats.
Example
The two examples below demonstrate how to use vectorized UDFs.
- Example 1: Use PyArrow to accelerate vectorization. This example explicitly registers a PyArrow vectorized UDF using con.create_scalar_function. The framework automatically identifies the UDF as a PyArrow type based on the type annotation and then calls it using ds.map.
import os import ibis import aura_frame as aura from aura_frame.multimodal import ai_lake from aura_frame.multimodal.types.vectorized import PyarrowVector import pyarrow.compute as pc 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) ) def calculate_product_pyarrow( prices: PyarrowVector[float], quantities: PyarrowVector[int], ) -> PyarrowVector[float]: return PyarrowVector[float](pc.multiply(prices, quantities)) try: # Create a test table and write data into it. table_name = "sales_records" table_schema = ibis.schema({"price": "float64", "quantity": "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}" (price, quantity) VALUES (100.0, 10), (200.0, 20), (150.0, 15) ''') # Delete the existing UDF with the same name (repeated execution is supported). con.delete_function("calculate_product_pyarrow", database=target_database, if_it_exists=True) # Explicitly register the PyArrow vectorized UDF. udf_pyarrow = con.create_scalar_function( calculate_product_pyarrow, name="calculate_product_pyarrow", database=target_database, register_type=aura.udf.RegisterType.STAGED, ) ds = con.load_dataset("sales_records", database=target_database) ds = ds.map(fn=udf_pyarrow, on=[ds.price, ds.quantity], as_col="product_column") ds = ds.select_columns(ds.price, ds.quantity, ds.product_column) print("PyArrow vectorization result:") print(ds.execute()) finally: con.close()
- Example 2: Use Pandas to accelerate vectorization. This example implicitly registers a Pandas vectorized UDF using the @aura.udf.pandas decorator, with the registration type set to STAGED.
import os import ibis import aura_frame as aura from aura_frame.multimodal import ai_lake from aura_frame.multimodal.types.vectorized import PandasVector import pandas as pd target_database = "test" # Implicitly register a UDF. @aura.udf.pandas(database=target_database, register_type=aura.udf.RegisterType.STAGED) def calculate_product_pandas( prices: PandasVector[float], quantities: PandasVector[int], ) -> PandasVector[float]: return PandasVector[float](prices * quantities, dtype=pd.Float64Dtype()) 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 lf_instance_id=os.getenv ("lf_instance_id"), # LakeFormation instance ID project_id=os.getenv ("project_id"), # Project ID access_key=os.getenv("access_key"), # Access Key ID (AK) secret_key=os.getenv("secret_key"), # Secret Key (SK) default_database=target_database, # Default database use_single_cn_mode=True, # Whether to enable the single-CN mode ) 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) ) try: # Create a test table and write data into it. table_name = "sales_records" table_schema = ibis.schema({"price": "float64", "quantity": "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}" (price, quantity) VALUES (100.0, 10), (200.0, 20), (150.0, 15) ''') # Delete the existing UDF with the same name (repeated execution is supported). con.delete_function("calculate_product_pandas", database=target_database, if_it_exists=True) # Use Pandas vectorized UDF (which has been implicitly registered using the decorator above). ds2 = con.load_dataset("sales_records", database=target_database) ds2 = ds2.map(fn=calculate_product_pandas, on=[ds2.price, ds2.quantity], as_col="product_column") ds2 = ds2.select_columns(ds2.price, ds2.quantity, ds2.product_column) print("Pandas vectorization result:") print(ds2.execute()) finally: con.close()
Parent topic: UDFs
What is your overall rating for this page?
0
1
2
3
4
5
6
7
8
9
10
Very dissatisfiedVery satisfied
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.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot