Calculation
This feature is currently in Private Preview.
- Only available for Realtime Feature Views.
- Set of supported SQL functions is limited and is actively being expanded. See SQL Expression Reference below for details.
- Please reach out to Tecton Support for questions and feedback!
Summary​
TheCalculation class describes a Calculation feature that is applied to a Realtime Feature View via the features param.Example
from tecton import Calculation, RealtimeFeatureViewtransaction_counts_fv = RealtimeFeatureView(name="transaction_counts_features",sources=[request_ds, user_transaction_metrics],# mode=None implied...features = [Calculation(name="transaction_count_fill_na",expr="COALESCE(user_transaction_metrics.transaction_count, request_ds.default_count)",),Calculation(name="transaction_count_add_1",expr="user_transaction_metrics.transaction_count + 1",description="a test calculation utilizing addition",tags={"tag": "value"}),],)
Attributes​
| Name | Data Type | Description |
|---|---|---|
description | Optional[str] | A human-readable description of the feature. |
tags | Optional[Dict[str, str]] | Tags associated with the feature (key-value pairs of user-defined metadata). |
name | str | The name of this feature. Must be explicitly defined. |
expr | str | The calculation string expressing the operations and input operands. |
__init__(...)​
Parameters
description(Optional[str]) - A human-readable description of the feature. Default:Nonetags(Optional[Dict[str,str]]) - Tags associated with the feature (key-value pairs of user-defined metadata). Default:Nonename(str) - The name of this feature. Must be explicitly defined. Default:Noneexpr(str) - The calculation string expressing the operations and input operands. Default:None
SQL Expression Reference​
The following SQL functions are currently supported in Calculation Features:
Arithmetic​
Standard SQL arithmetic operations are supported.
amount + 1
amount - value
value * 2
3 / 2
Please note the following:
- For addition, subtraction, and multiplication: the result of arithmetic operation will be the type required to preserve the precision of the inputs. The precision hierarchy is
Float64->Float32->Int64->Int32. For example, aFloat32value +Int64value will return a value of typeFloat32to preserve the float input's precision. - For division: the result will always be a
Float64. If division by zero occurs, the result will be typeFloat64:+Infinityif the numerator is positive.-Infinityif the numerator is negative.NaNif the numerator is0.
- Currently, Realtime Feature Views do not support
Int32features. As a workaround for your calculations, you can cast yourInt32value toInt64usingInt32_value + 0.
CASE Statements​
CASE statements are supported.
CASE
WHEN my_source.amount > 100 THEN 'high'
WHEN my_source.amount > 50 THEN 'medium'
ELSE 'low'
END
COALESCE​
COALESCE(value, default_value)
Returns the first non-null value in a list of expressions.
COALESCE(amount, 0)
Comparison Operators​
Standard SQL comparisons are supported.
- Comparisons between
Float32,Int32,Float64,Int64, andBoolare supported. For example, you can compareTrueand 1 (True= 1 returnsTrue). - If one of the elements is a
Stringelement, it can only be compared against anotherStringelement. Strings are compared using lexicographical order.
amount > threshold
value >= 0
3 != 2
DATEDIFF​
DATEDIFF(datepart, start_date, end_date)
Returns the count of the specified datepart boundaries crossed between two timestamps. All datetimes provided are converted to UTC and all datediffs are performed on the UTC equivalent.
DATEDIFF('minutes', start_time, end_time)
DATEDIFF logic follows DuckDB convention where the difference is equal to the number of partition boundaries between the two timestamps.
For example, consider DATEDIFF('days', my_source.start_date, my_source.end_date). If start_date is 2022-01-01 23:59:00 and end_date is 2022-01-02 00:00:00, the result will be 1.
Logical Operators​
Standard SQL logical operators are supported.
amount > 100 AND amount < 200
value OR another_value
NOT value