Skip to main content

Manage User-Defined Functions

Introduction

This page covers the Gravitino API for functions. For what a function is, the function types, determinism, and how definitions and implementations relate, see Functions. For creating the catalog and schema a function lives in, see Manage Catalogs and Schemas.

Function Operations

Register a SQL Function

A function needs a name, a type, a determinism flag, and at least one definition. A definition carries its parameters, its return type, and one or more implementations.

curl -X POST -H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" -d '{
"name": "add_one",
"functionType": "SCALAR",
"deterministic": true,
"comment": "Adds one to the input",
"definitions": [
{
"parameters": [{"name": "x", "dataType": "integer"}],
"returnType": "integer",
"impls": [
{"language": "SQL", "runtime": "TRINO", "sql": "x + 1"}
]
}
]
}' http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions

Register a Python Function

A Python implementation names a handler entrypoint, and can carry inline code and the packages the runtime needs.

curl -X POST -H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" -d '{
"name": "normalize_phone",
"functionType": "SCALAR",
"deterministic": true,
"comment": "Strips formatting from a phone number",
"definitions": [
{
"parameters": [{"name": "raw", "dataType": "varchar(64)"}],
"returnType": "varchar(64)",
"impls": [
{
"language": "PYTHON",
"runtime": "SPARK",
"handler": "normalize.main",
"codeBlock": "def main(raw):\n return \"\".join(c for c in raw if c.isdigit())"
}
]
}
]
}' http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions

Register a Java Function

A Java implementation names a class, and usually the jar that holds it. A table-valued function declares returnColumns rather than a single returnType.

curl -X POST -H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" -d '{
"name": "generate_series",
"functionType": "TABLE",
"deterministic": true,
"comment": "Generates a range of integers",
"definitions": [
{
"parameters": [
{"name": "start_val", "dataType": "integer"},
{"name": "end_val", "dataType": "integer"}
],
"returnColumns": [
{"name": "value", "dataType": "integer", "comment": "The generated value"}
],
"impls": [
{
"language": "JAVA",
"runtime": "SPARK",
"className": "com.example.GenerateSeriesFunction",
"resources": {"jars": ["hdfs:///path/to/udtf.jar"]}
}
]
}
]
}' http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions

Register Overloads

A function with several definitions accepts several parameter lists under one name. Each definition carries its own return type and implementations.

curl -X POST -H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" -d '{
"name": "add",
"functionType": "SCALAR",
"deterministic": true,
"comment": "Adds two or three integers",
"definitions": [
{
"parameters": [
{"name": "x", "dataType": "integer"},
{"name": "y", "dataType": "integer"}
],
"returnType": "integer",
"impls": [{"language": "SQL", "runtime": "TRINO", "sql": "x + y"}]
},
{
"parameters": [
{"name": "x", "dataType": "integer"},
{"name": "y", "dataType": "integer"},
{"name": "z", "dataType": "integer"}
],
"returnType": "integer",
"impls": [{"language": "SQL", "runtime": "TRINO", "sql": "x + y + z"}]
}
]
}' http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions

Get a Function

curl -X GET -H "Accept: application/vnd.gravitino.v1+json" \
http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions/add_one

List Functions

curl -X GET -H "Accept: application/vnd.gravitino.v1+json" \
http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions

Alter a Function

Changes are applied as a list in one request.

ChangeJSONJava
Rename{"@type":"rename","newName":"add_one_v2"}FunctionChange.rename("add_one_v2")
Update the comment{"@type":"updateComment","newComment":"new_comment"}FunctionChange.updateComment("new_comment")
Set a property{"@type":"setProperty","property":"key1","value":"value1"}FunctionChange.setProperty("key1", "value1")
Remove a property{"@type":"removeProperty","property":"key1"}FunctionChange.removeProperty("key1")
curl -X PUT -H "Accept: application/vnd.gravitino.v1+json" \
-H "Content-Type: application/json" -d '{
"updates": [
{"@type": "updateComment", "newComment": "Adds one, reviewed"}
]
}' http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions/add_one

Drop a Function

curl -X DELETE -H "Accept: application/vnd.gravitino.v1+json" \
http://localhost:8090/api/metalakes/example/catalogs/sales/schemas/public/functions/add_one