Skip to content

featurebyte.UserDefinedFunction.create

create(
name: str,
sql_function_name: str,
function_parameters: List[FunctionParameter],
output_dtype: Union[DBVarType, str],
is_global: bool=False
) -> UserDefinedFunction

Description

Create a user-defined function.

Parameters

  • name: str
    Name of the user-defined function. This will be used as the name of the dynamically created function.

  • sql_function_name: str
    The SQL function name of the user-defined function (which is used in SQL queries).

  • function_parameters: List[FunctionParameter]
    The function parameters of the user-defined function.

  • output_dtype: Union[DBVarType, str]
    The output data type of the user-defined function.

  • is_global: bool
    default: False
    Whether the user-defined function is global across all catalogs or not. Global user-defined functions can be used in any catalogs (as long as the feature store is the same), while local user-defined functions can only be used in the catalog where they are created.

Returns

  • UserDefinedFunction
    The created user-defined function.

Examples

Create a local (catalog-specific) user-defined function that computes the cosine of a number:

>>> cos_udf = UserDefinedFunction.create(
...     name="cos",
...     sql_function_name="cos",
...     function_parameters=[fb.FunctionParameter(name="x", dtype=fb.enum.DBVarType.FLOAT)],
...     output_dtype=fb.enum.DBVarType.FLOAT,
...     is_global=False,
... )
Use the user-defined function to transform a column in a view:

>>> view = catalog.get_view("GROCERYINVOICE")
>>> view["cos(Amount)"] = cos_udf(view["Amount"])
>>> view[["Amount", "cos(Amount)"]].preview(3)
                     GroceryInvoiceGuid           Timestamp  Amount  cos(Amount)
0  4fccfb1d-02b3-4047-87ab-4e5f910ccdd1 2022-01-03 12:28:58   10.68    -0.310362
1  9cf3c416-7b38-401e-adf6-1bd26650d1d6 2022-01-03 16:32:15   38.04     0.942458
2  0a5b99b2-9ff1-452a-a06e-669e8ed4a9fa 2022-01-07 16:20:04    1.99    -0.407033
Use the same user-defined function to transform a feature:

>>> feature = catalog.get_feature("InvoiceCount_60days")
>>> cos_feat = cos_udf(feature)
>>> cos_feat.name = "cos(InvoiceCount_60days)"
>>> fb.FeatureGroup([feature, cos_feat]).preview(
...     observation_set=pd.DataFrame({
...         "POINT_IN_TIME": ["2022-06-01 00:00:00"],
...         "GROCERYCUSTOMERGUID": ["a2828c3b-036c-4e2e-9bd6-30c9ee9a20e3"],
...     })
... )
  POINT_IN_TIME                   GROCERYCUSTOMERGUID  InvoiceCount_60days  cos(InvoiceCount_60days)
0    2022-06-01  a2828c3b-036c-4e2e-9bd6-30c9ee9a20e3                 10.0                 -0.839072

The user-defined function can also be accessed via the UDF handle (the name of the UserDefinedFunction object is used to access the corresponding function):

>>> feature = catalog.get_feature("InvoiceCount_60days")
>>> another_cos_feat = fb.UDF.cos(feature)