Calculation Features
Overviewโ
A Calculation is a Feature
Type which allows you to define performant SQL-like operations on your source
data. By avoiding the overhead of executing a Python function, Calculation
features can be an efficient alternative for Feature Views that don't require
the expressiveness of a python or pandas mode transformation.
Calculation Features are the recommended path for
simple operations such as setting default values,
simple arithmetic operations, and DATEDIFFs.
A Basic Exampleโ
transaction_analysis = RealtimeFeatureView(
name="calculations_rtfv",
sources=[user_metrics, req_source],
features=[
Calculation(name="transaction_z_score", expr="(req_source.amount - user_metrics.mean) / user_metrics.stddev"),
Calculation(
name="is_amount_above_threshold",
expr="req_source.amount > user_metrics.threshold",
),
],
)
Calculations Workflowโ
- Create a
RealtimeFeatureViewusing theRealtimeFeatureViewclass. Realtime Feature Views that use Calculation features must be defined this way (not with a decorator). - Specify inputs via the
sourcesparameter, which can include both request and entity-based sources. - Define each feature using the
Calculationclass, passing anameand SQL-styleexprstring. - Ensure the expression uses only supported functions and valid type operations. Tecton will validate syntax and types during plan/apply.
- Apply the Feature View with
tecton applyto register it for real-time serving and make it available to Feature Services.
What's Nextโ
Once you've defined Calculation Features, consider:
- Defining a Realtime Feature View to learn more about request-time transformations.
- Exploring supported SQL expressions
to understand the current capabilities of the
exprsyntax. - Using this Feature View in a Feature Service for real-time feature retrieval.
- Testing with real request payloads to validate the logic and behavior of your Calculation Features.
How to Use Calculation Featuresโ
Calculation Features are defined using SQL-like expressions in the expr field
of a Calculation object. Unlike python and pandas mode Realtime Feature
Views which use a decorator pattern, Calculation Features are defined by
directly instantiating a RealtimeFeatureView object with one or more
Calculation features.
Feature Views using Calculation Features can not use a transformation
function.
transaction_analysis = RealtimeFeatureView(
name="calculations_rtfv",
sources=[user_metrics, req_source],
features=[
Calculation(name="transaction_z_score", expr="(req_source.amount - user_metrics.mean) / user_metrics.stddev"),
Calculation(
name="is_amount_above_threshold",
expr="req_source.amount > user_metrics.threshold",
),
],
)
Type Systemโ
Calculation Features use Tecton's native type system for validating inputs and output types. When writing expressions, types are automatically inferred from your input features and operations.
Invalid type combinations will raise errors during feature validation rather than runtime:
# This will fail validation - can't add string and int
Calculation(name="invalid", expr="<string_field> + <int_field>")
SQL Expression Referenceโ
A full list of supported SQL functionality can be found in the SDK Reference.