Scalar UDF
A scalar UDF is a basic row-level function. It takes zero or more input parameters, processes the data in one row, and returns one result per row.
Scalar UDFs are typically used for mathematical operations, complex type conversions, and custom formatting.
The behavior of a scalar UDF is similar to that of built-in functions. However, its implementation is user-defined.
Development Process
The overall process of developing a Python scalar UDF is as follows:
- Write and debug Python code according to user service logic. Sample code:
import numpy as np def pysum(arg1: int, arg2: int) -> int: printLog(arg1, arg2) return np.sum(np.array([arg1, arg2])) - Register a Python UDF. Create a session using ibis-aura.
import aura_frame as aura from aura_frame.multimodal import ai_lake target_database = "test" #Create a database in LakeFormation, for example, test. For details, see Creating a DataArts Lake Formation Instance and Planning Metadata. 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=target_database, # LakeFormation database name use_single_cn_mode=True, # Whether to enable the single-CN mode logging_level=logging.INFO # Set the log level. )
Register a Python UDF.try: udf = con.create_scalar_function(pysum, packages=["numpy"]) finally: con.close() - View the Python UDF.
Aura provides the show functions command to view all functions in a specific schema and the describe function function_name method to view details about a specific function. For details, see SHOW and DESCRIBE.
- Invoke the Python UDF.
Use any Aura client to submit a query, for example:
select "test".pysum(3,4);
- View and optimize Python UDF performance metrics.
Aura supports performance monitoring during Python UDF execution and provides APIs to configure resource specifications and concurrency during UDF operation. For details, see Python UDF Performance Tuning.
Example
import os import ibis from aura_frame.multimodal import ai_lake 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=target_database, # Default database use_single_cn_mode=True, # Whether to enable the single-CN mode ) #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 scalar UDF for summing two numbers. def add_udf(lhs: float, rhs: int) -> float: return lhs + rhs try: # Create a test table and write data into it. table_name = "product_orders" 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("add_udf", database=target_database, if_it_exists=True) # Register the scalar UDF. udf = con.create_scalar_function( add_udf, database=target_database, comment="To test create_scalar_function", ) # Use the UDF to process data in the product_orders table. ds = con.load_dataset("product_orders", database=target_database) ds = ds.map(fn=udf, on=[ds.price, ds.quantity], as_col="sum_column") ds = ds.select_columns(ds.price, ds.quantity, ds.sum_column) print(ds.execute()) 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