Important
この機能は パブリック プレビュー段階です。
このチュートリアルでは、複合的な関心を計算する Lakeflow Designer のPython UDF 演算子を作成する手順について説明します。 この例を使用して、個々の値または列を変換する演算子を構築する基礎を学習します。 詳細については、 Lakeflow Designer のユーザー定義演算子に関するページを参照してください。
開始する
このチュートリアルでは、Python UDF を使用してユーザー定義演算子を作成する手順について説明します。 オペレーターは、複合利子式 ( A = P × (1 + r/n)^(n×t)) を使用して投資の将来価値を計算します。
- P = プリンシパル (開始金額)
- r = 年利 (10 進数)
- n = 1 年あたりの複合期間の数
- 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 でのオペレーターの表示方法を定義します。 この演算子の場合:
-
プリンシパル は
expressionウィジェットを使用するため、ユーザーはデータから列を選択できます -
年率、 年単位の複合、 および年 は、既定値と制約を持つ
numberウィジェットを使用します - 演算子には、式パラメーターの列データを提供する 1 つの入力ポートがあります
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関数を 1 つの 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
Note
このファイルをユーザー フォルダーに定義すると、そのファイルのみが表示されます。 詳細については、「 オペレーターを検出可能にする」を参照してください。
手順 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 に表示されます。
- 入力データからプリンシパル列を選択するドロップダウン
- レート、複合頻度、および年の数値入力 (適切な既定値を使用)
ユーザーは、この演算子を適用して、投資データの列全体の将来の値を計算できます。