Skip to main content

Tutorial: Compound interest calculator operator

Preview

This feature is in Public Preview.

This tutorial walks through creating a Python UDF operator for Lakeflow Designer that calculates compound interest. Use this example to learn the fundamentals of building operators that transform individual values or columns. To learn more, see User-defined operators in Lakeflow Designer.

Overview

This tutorial steps you through creating a user-defined operator using a Python UDF. The operator computes the future value of an investment using the compound interest formula, A = P × (1 + r/n)^(n×t), where:

  • P = Principal (starting amount)
  • r = Annual interest rate (as decimal)
  • n = Number of compounding periods per year
  • t = Time in years

Step 1: Write and test the Python function

First, define the core Python function that performs the calculation. Test it in a notebook cell to make sure it works correctly.

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)

You can test the function with the following code:

Python
# $1,000 invested at 5% annual rate, compounded monthly for 10 years
compound_amount(1000, 0.05, 12, 10)
# Expected result: ~1647.01

Step 2: Create the YAML for the operator

The YAML configuration defines how the operator appears in Lakeflow Designer. For this operator:

  • Principal uses an expression widget so users can select a column from their data
  • Annual rate, Compounds per year, and Years use number widgets with defaults and constraints
  • The operator has one input port that provides column data for the expression parameter
YAML
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

See User-defined operator YAML reference for a comprehensive guide to all available properties, data types, widgets, and options.

Step 3: Create the Unity Catalog function

Combine the YAML schema and Python function into a single CREATE FUNCTION statement. The YAML configuration goes in the docstring at the beginning of the function body.

SQL
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)
$$

Step 4: Test the function

Test the UC function directly with SQL:

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

Step 5: Register the operator

Add the operator to your .user_defined_operators.yaml file:

YAML
operators:
- catalog: main
schema: my_schema
functionName: compound_amount
note

If you define this file in your user folder, it only appears for you. For more information, see Make your operator discoverable.

Step 6: Set up permissions

Grant access to users who need to use this operator:

SQL
GRANT USE SCHEMA ON SCHEMA main.my_schema TO `<user>`;
GRANT EXECUTE ON FUNCTION main.my_schema.compound_amount TO `<user>`;

Using the operator in Lakeflow Designer

After it's registered, the operator will appear in Lakeflow Designer with:

  • A dropdown to select the principal column from your input data
  • Number inputs for rate, compounding frequency, and years (with sensible defaults)

Users can apply this operator to calculate future values for entire columns of investment data.