Important
이 기능은 공개 미리보기 단계에 있습니다.
이 자습서에서는 복합 관심을 계산하는 Lakeflow Designer용 Python UDF 연산자를 만드는 방법에 대해 설명합니다. 이 예제를 사용하여 개별 값 또는 열을 변환하는 건물 연산자의 기본 사항을 알아봅니다. 자세한 내용은 Lakeflow Designer에서 사용자 정의 연산자를 참조하세요.
Overview
이 자습서에서는 Python UDF를 사용하여 사용자 정의 연산자를 만드는 단계를 안내합니다. 운영자는 복합 이자 수식을 사용하여 투자의 미래 가치를 계산합니다. A = P × (1 + r/n)^(n×t)여기서는 다음과 같습니다.
- P = 원금(초기 금액)
- r = 연간 이자율(10진수)
- n = 연간 복합 기간 수
- t = 연도의 시간
1단계: Python 함수 작성 및 테스트
먼저 계산을 수행하는 핵심 Python 함수를 정의합니다. Notebook 셀에서 테스트하여 제대로 작동하는지 확인합니다.
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 구성은 함수 본문의 시작 부분에 있는 문서 문자열로 이동합니다.
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
메모
사용자 폴더에 이 파일을 정의하면 본인에게만 표시됩니다. 자세한 내용은 오퍼레이터를 검색 가능하게 만들기를 참조하세요.
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에 연산자가 다음과 같이 표시됩니다.
- 입력 데이터에서 주요 열을 선택하는 드롭다운
- 속도, 복합 빈도 및 연도에 대한 숫자 입력(합리적인 기본값 포함)
사용자는 이 연산자를 적용하여 투자 데이터의 전체 열에 대한 미래 값을 계산할 수 있습니다.