Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Dalam tutorial ini, Anda membuat operator UDF Python untuk Lakeflow Designer yang menghitung bunga mampul. Gunakan contoh ini untuk mempelajari dasar-dasar operator bangunan yang mengubah nilai atau kolom individual. Untuk mempelajari selengkapnya, lihat Operator yang ditentukan pengguna di Lakeflow Designer.
Ikhtisar
Tutorial ini memandu Anda membuat operator yang ditentukan pengguna menggunakan UDF Python. Operator menghitung nilai investasi di masa mendatang menggunakan rumus bunga campuran, A = P × (1 + r/n)^(n×t), di mana:
- P = Utama (jumlah awal)
- r = Suku bunga tahunan (sebagai desimal)
- n = Jumlah periode penggandaan per tahun
- t = Waktu dalam tahun
Step 1: Menulis dan menguji fungsi Python
Pertama, tentukan fungsi Python inti yang melakukan penghitungan. Uji di sel buku catatan untuk memastikannya berfungsi dengan benar.
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)
Anda dapat menguji fungsi dengan kode berikut:
# $1,000 invested at 5% annual rate, compounded monthly for 10 years
compound_amount(1000, 0.05, 12, 10)
# Expected result: ~1647.01
Langkah 2: Buat YAML untuk operator
Konfigurasi YAML menentukan bagaimana operator muncul di Lakeflow Designer. Untuk operator ini:
-
Prinsipal menggunakan
expressionwidget sehingga pengguna dapat memilih kolom dari data mereka -
Tingkat tahunan, Senyawa per tahun, dan Tahun menggunakan
numberwidget dengan default dan batasan - Operator memiliki satu port input yang menyediakan data kolom untuk parameter ekspresi
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
Lihat Referensi YAML operator yang ditentukan pengguna untuk panduan komprehensif tentang semua properti, jenis data, widget, dan opsi yang tersedia.
Langkah 3: Membuat fungsi Katalog Unity
Gabungkan skema YAML dan fungsi Python ke dalam satu pernyataan CREATE FUNCTION. Konfigurasi YAML masuk ke docstring di awal isi fungsi.
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)
$$
Langkah 4: Uji fungsi
Uji fungsi UC secara langsung dengan SQL:
-- 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
Langkah 5: Daftarkan operator
Tambahkan operator ke file .user_defined_operators.yaml Anda:
operators:
- catalog: main
schema: my_schema
functionName: compound_amount
Note
Jika Anda menentukan file ini di folder pengguna Anda, file tersebut hanya muncul untuk Anda. Untuk informasi selengkapnya, lihat Membuat operator Anda dapat ditemukan.
Langkah 6: Menyiapkan izin
Berikan akses ke pengguna yang perlu menggunakan operator ini:
GRANT USE SCHEMA ON SCHEMA main.my_schema TO `<user>`;
GRANT EXECUTE ON FUNCTION main.my_schema.compound_amount TO `<user>`;
Menggunakan operator di Lakeflow Designer
Setelah terdaftar, operator muncul di Lakeflow Designer dengan:
- Menu dropdown untuk memilih kolom utama dari data input Anda
- Kolom input angka untuk suku bunga, frekuensi pemajemukan, dan jumlah tahun (dengan nilai bawaan yang masuk akal)
Pengguna dapat menerapkan operator ini untuk menghitung nilai di masa mendatang untuk seluruh kolom data investasi.