このチュートリアルでは、複合関心を計算する 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)
サンプル投資で関数を検証します:年間5% の金利で1,000ドル、10年間毎月複利で投資します。 その結果はおよそ 1647.01になるはずです。
compound_amount(1000, 0.05, 12, 10)
手順 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 に入ります。
この例は main.example_outputの関数を作成します。 もし存在しないスキーマなら、まずスキーマを作成してください:
CREATE SCHEMA IF NOT EXISTS main.example_output
CREATE OR REPLACE FUNCTION main.example_output.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: 関数をテストする
UC関数をSQLで直接テストしてください。 これらのクエリは以下の3つのシナリオを確認します。
- 1,000ドルを年利5%で毎月複利計算すると、10年後には約
1647.01になります - 年利5%で1,000ドルを1年間、年1回複利で運用すると、
1050.00が得られます - 1,000ドルを年利15%で毎月複利計算した場合、1年後には約
1160.75になります
SELECT main.example_output.compound_amount(1000, 0.05, 12, 10);
SELECT main.example_output.compound_amount(1000, 0.05, 1, 1);
SELECT main.example_output.compound_amount(1000, 0.15, 12, 1);
手順 5: オペレーターを登録する
.user_defined_operators.yaml ファイルに演算子を追加します。
operators:
- catalog: main
schema: example_output
functionName: compound_amount
Note
このファイルをユーザー フォルダーに定義すると、そのファイルのみが表示されます。 詳細については、「 オペレーターを検出可能にする」を参照してください。
手順 6: アクセス許可を設定する
このオペレーターを使用する必要があるユーザーにアクセス権を付与します。
GRANT USE SCHEMA ON SCHEMA main.example_output TO `<user>`;
GRANT EXECUTE ON FUNCTION main.example_output.compound_amount TO `<user>`;
Lakeflow Designer で演算子を使用する
登録が完了すると、オペレーターは Lakeflow Designer に次の状態で表示されます。
- 入力データからプリンシパル列を選択するドロップダウン
- レート、複合頻度、および年の数値入力 (適切な既定値を使用)
ユーザーは、この演算子を適用して、投資データの列全体の将来の値を計算できます。