Important
此功能目前以公共预览版提供。
本教程逐步讲解如何为 Lakeflow Designer 创建用于计算复合兴趣的 Python UDF 运算符。 使用此示例可了解转换单个值或列的生成运算符的基础知识。 若要了解详细信息,请参阅 Lakeflow Designer 中的用户定义的运算符。
概述
本教程逐步讲解如何使用 Python UDF 创建用户定义的运算符。 运算符使用复利公式计算投资的未来值, A = P × (1 + r/n)^(n×t)其中:
- P = 主体(起始金额)
- r = 年利率(如十进制)
- n = 每年复合周期数
- t = 以年为单位的时间
步骤 1:编写和测试Python函数
首先,定义执行计算的核心Python函数。 在笔记本单元格中进行测试,以确保其正确运行。
def compound_amount(principal: float,
annual_rate: float,
compounds_per_year: int,
years: float) -> float:
"""
Compute compound interest future value.
A = P * (1 + r/n)^(n*t)
principal: starting amount (P)
annual_rate: annual nominal rate as decimal (r), e.g. 0.05
compounds_per_year: compounding periods per year (n), e.g. 12
years: time in years (t), can be fractional
"""
import math
if principal is None or annual_rate is None or compounds_per_year is None or years is None:
return None
if compounds_per_year <= 0:
raise ValueError("compounds_per_year must be > 0")
return principal * math.pow(1.0 + annual_rate / compounds_per_year,
compounds_per_year * years)
可以使用以下代码测试函数:
# $1,000 invested at 5% annual rate, compounded monthly for 10 years
compound_amount(1000, 0.05, 12, 10)
# Expected result: ~1647.01
步骤 2:为操作员创建 YAML
YAML 配置定义运算符在 Lakeflow Designer 中的显示方式。 对于此运算符:
-
Principal 使用
expression组件,以便用户从其数据中选择一列 -
年利率、每年复利次数和年数使用具有默认值和约束条件的
number控件 - 运算符有一个输入端口,该端口为表达式参数提供列数据
schema: user-defined-operator-v0.1.0
type: uc-udf
name: Compound Amount
id: finance.compound_amount
version: '1.0.0'
description: >
Computes the future value of an investment using compound interest.
Formula: A = P * (1 + r/n)^(n*t)
config:
type: object
properties:
principal:
type: string
format: expression
title: Principal
examples:
- 'Select principal column or expression'
x-ui:
widget: expression
port: in
annual_rate:
type: number
title: Annual rate (decimal)
default: 0.05
minimum: 0
examples:
- 'e.g. 0.05 for 5%'
x-ui:
widget: number
compounds_per_year:
type: number
title: Compounds per year
default: 12
minimum: 1
examples:
- 'e.g. 12 for monthly'
x-ui:
widget: number
years:
type: number
title: Years
default: 10
minimum: 0
examples:
- 'Time in years (t)'
x-ui:
widget: number
required:
- principal
- annual_rate
- compounds_per_year
- years
additionalProperties: false
ports:
input:
- name: in
title: Input
output:
- name: out
title: Output
有关所有可用属性、数据类型、控件和选项的完整指南,请参阅用户定义运算符 YAML 参考。
步骤 3:创建 Unity 目录函数
将 YAML 架构和Python函数合并为单个 CREATE FUNCTION 语句。 YAML 配置位于函数正文开头的 docstring 中。
CREATE OR REPLACE FUNCTION main.my_schema.compound_amount(
principal DOUBLE,
annual_rate DOUBLE,
compounds_per_year INT,
years FLOAT)
RETURNS DOUBLE
LANGUAGE PYTHON
AS $$
"""
schema: user-defined-operator-v0.1.0
type: uc-udf
name: Compound Amount
id: finance.compound_amount
version: "1.0.0"
description: >
Computes the future value of an investment using compound interest.
Formula: A = P * (1 + r/n)^(n*t)
config:
type: object
properties:
principal:
type: string
format: expression
title: Principal
examples:
- "Select principal column or expression"
x-ui:
widget: expression
port: in
annual_rate:
type: number
title: Annual rate (decimal)
default: 0.05
minimum: 0
examples:
- "e.g. 0.05 for 5%"
x-ui:
widget: number
compounds_per_year:
type: number
title: Compounds per year
default: 12
minimum: 1
examples:
- "e.g. 12 for monthly"
x-ui:
widget: number
years:
type: number
title: Years
default: 10
minimum: 0
examples:
- "Time in years (t)"
x-ui:
widget: number
required:
- principal
- annual_rate
- compounds_per_year
- years
additionalProperties: false
ports:
input:
- name: in
title: Input
output:
- name: out
title: Output
"""
def compound_amount(principal: float,
annual_rate: float,
compounds_per_year: int,
years: float) -> float:
import math
if principal is None or annual_rate is None or compounds_per_year is None or years is None:
return None
if compounds_per_year <= 0:
raise ValueError("compounds_per_year must be > 0")
return principal * math.pow(1.0 + annual_rate / compounds_per_year,
compounds_per_year * years)
return compound_amount(principal, annual_rate, compounds_per_year, years)
$$
步骤 4:测试函数
使用 SQL 直接测试 UC 函数:
-- Test 1: $1,000 at 5% compounded monthly for 10 years
SELECT main.my_schema.compound_amount(1000, 0.05, 12, 10)
-- Expected: ~1647.01
-- Test 2: $1,000 at 5% compounded annually for 1 year
SELECT main.my_schema.compound_amount(1000, 0.05, 1, 1)
-- Expected: 1050.00
-- Test 3: $1,000 at 15% compounded monthly for 1 year
SELECT main.my_schema.compound_amount(1000, 0.15, 12, 1)
-- Expected: ~1160.75
步骤 5:注册操作员
将运算符添加到 .user_defined_operators.yaml 文件:
operators:
- catalog: main
schema: my_schema
functionName: compound_amount
注释
如果在用户文件夹中定义此文件,则只会为你显示该文件。 有关更多信息,请参阅 使 Operator 可被发现。
步骤 6:设置权限
向需要使用此操作员的用户授予访问权限:
GRANT USE SCHEMA ON SCHEMA main.my_schema TO `<user>`;
GRANT EXECUTE ON FUNCTION main.my_schema.compound_amount TO `<user>`;
在 Lakeflow Designer 中使用运算符
注册后,该操作器会显示在 Lakeflow Designer 中,并具有以下内容:
- 用于从输入数据中选择主体列的下拉列表
- 利率、复利频率和年限的数字输入框(带有合理的默认值)
用户可以应用此运算符来计算整个投资数据列的未来值。