export()#

relationalai.Model
#Model.export(schema: str) -> Callable

Exports a Python function as a Snowflake stored procedure. Must be used as a decorator for the function to be exported. The exported stored procedure has the same name as the function and can be called from Snowflake SQL.

IMPORTANT

Functions decorated with .export() must include type hints for parameters and return values. They cannot be called directly from Python, only as stored procedures in Snowflake SQL.

Parameters#

NameTypeDescription
schemastrThe target schema in Snowflake where the stored procedure will be created.

Returns#

A Callable object representing the decorated function.

Example#

Use export() to export a Python function written in RelationalAI query-builder syntax as a Snowflake stored procedure:

#from typing import Tuple

import relationalai as rai

# Create a model named "people".
model = rai.Model("people")

# Create a `Person` type from the 'sandbox.public.person' table in Snowflake.
Person = model.Type("Person", source="sandbox.public.person")

# Create an `Adult` type.
Adult = model.Type("Adult")

# Add a rule to the model to assign people over 18 to the `Adult` type.
with model.rule():
    person = Person()
    person.age >= 18
    person.set(Adult)

# Define a function to export as a stored procedure.
# NOTE: Type hints are required.
@model.export("sandbox.public")
def get_adults_by_age(age: int) -> Tuple[str, int]:
    adult = Adult(age=age)
    return adult.name, adult.age

# The stored procedure can now be called in Snowflake SQL:
# CALL sandbox.public.get_adults_by_age(21);

Type hints are required for the function parameters and return values. Supported types are:

For more details on Snowflake stored procedures, refer to the Snowflake documentation.

See Also#