Skip to main content
Version: 1.1

context

Previously known as materialization_context() (deprecated).

Summary​

Used as an argument in a transformation to provide contextual timestamps consistently across online and offline evaluations. For BatchFeatureView and StreamFeatureView, it contains three datetime.datetime objects:

  • start_time: The beginning of the period being materialized.
  • end_time: The end of the period being materialized.
  • end_time_inclusive: The end of the period being materialized, inclusive. Equal to end_time - 1 microsecond.

For RealtimeFeatureView, it contains a single timestamp:

  • request_timestamp: The timestamp of the request being materialized.

The datetimes can be used in SQL query strings directly (the datetime object will be cast to an atom-formatted timestamp string and inlined as a constant in the SQL query).

Example​

from tecton import batch_feature_view
from datetime import datetime, timedelta


@batch_feature_view(
sources=[transactions],
entities=[user],
mode="spark_sql",
batch_schedule=timedelta(days=1),
feature_start_time=datetime(2020, 10, 10),
)
def user_last_transaction_amount(transactions, context):
return f"""
SELECT
USER_ID,
AMOUNT,
TIMESTAMP
FROM
{transactions}
WHERE TIMESTAMP >= TO_TIMESTAMP("{context.start_time}") -- e.g. TO_TIMESTAMP("2022-05-01T00:00:00+00:00")
AND TIMESTAMP < TO_TIMESTAMP("{context.end_time_inclusive}") -- e.g. TO_TIMESTAMP("2022-05-02T00:00:00+00:00")
"""

Was this page helpful?