Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/getting-started/concepts/feature-retrieval.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ training_df = store.get_historical_features(
If `start_date` is omitted, it defaults to `end_date` minus the feature view TTL. If `end_date` is omitted, it defaults to the current time. Point-in-time correctness is still preserved.

{% hint style="warning" %}
Entity-less retrieval is currently supported for the **Postgres**, **Dask**, **Spark**, and **Ray** offline stores. You cannot mix `entity_df` with `start_date`/`end_date` in the same call.
Entity-less retrieval is currently supported for the **Postgres**, **Dask**, **Spark**, **Ray**, and **Trino** offline stores. You cannot mix `entity_df` with `start_date`/`end_date` in the same call.
{% endhint %}

For more details, see the [FAQ](../faq.md#how-do-i-run-get_historical_features-without-providing-an-entity-dataframe) and [this blog post on entity-less historical feature retrieval](https://feast.dev/blog/entity-less-historical-features-retrieval/).
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Yes, this is possible. For example, you can use BigQuery as an offline store and

Feast does support fetching historical features without passing an entity dataframe with the request.

- **Supported offline stores:** Entity-less (entity dataframe–less) retrieval is supported for the **Postgres**, **Dask**, **Spark**, and **Ray** offline stores. Postgres was the first to support it; Dask, Spark, and Ray have followed. Other offline stores may be added based on priority and community demand.
- **Supported offline stores:** Entity-less (entity dataframe–less) retrieval is supported for the **Postgres**, **Dask**, **Spark**, **Ray**, and **Trino** offline stores. Postgres was the first to support it; Dask, Spark, Ray, and Trino have followed. Other offline stores may be added based on priority and community demand.
- **Date range:** Retrieval is controlled by the `start_date` and `end_date` parameters. Supported combinations:
- Both params given → data in the given start-to-end time range.
- Only `start_date` given → data from the start date to now.
Expand Down
1 change: 1 addition & 0 deletions docs/reference/offline-stores/trino.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

The Trino offline store provides support for reading [TrinoSources](../data-sources/trino.md).
* Entity dataframes can be provided as a SQL query or can be provided as a Pandas dataframe. A Pandas dataframes will be uploaded to Trino as a table in order to complete join operations.
* Historical features can also be retrieved without an entity dataframe by passing `start_date` and/or `end_date` to `get_historical_features`. Every entity key with an event between `start_date` and `end_date` is joined as of `end_date`, so each entity gets its latest feature values within the feature view TTL. No temporary table is created in this mode. See [entity-less retrieval](../../getting-started/concepts/feature-retrieval.md) for details.

## Disclaimer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from feast.on_demand_feature_view import OnDemandFeatureView
from feast.repo_config import FeastConfigBaseModel, RepoConfig
from feast.saved_dataset import SavedDatasetStorage
from feast.utils import compute_non_entity_date_range

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -372,18 +373,33 @@ def get_historical_features(
config: RepoConfig,
feature_views: List[FeatureView],
feature_refs: List[str],
entity_df: Union[pd.DataFrame, str],
entity_df: Optional[Union[pd.DataFrame, str]],
registry: BaseRegistry,
project: str,
full_feature_names: bool = False,
filter_by_created_timestamp: bool = False,
**kwargs: Any,
) -> TrinoRetrievalJob:
assert isinstance(config.offline_store, TrinoOfflineStoreConfig)
for fv in feature_views:
assert isinstance(fv.batch_source, TrinoSource)

client = _get_trino_client(config=config)

if entity_df is None:
return _get_historical_features_without_entity_df(
config=config,
client=client,
feature_views=feature_views,
feature_refs=feature_refs,
registry=registry,
project=project,
full_feature_names=full_feature_names,
filter_by_created_timestamp=filter_by_created_timestamp,
start_date=kwargs.get("start_date"),
end_date=kwargs.get("end_date"),
)

table_reference = _get_table_reference_for_new_entity(
catalog=config.offline_store.catalog,
dataset_name=config.offline_store.dataset,
Expand Down Expand Up @@ -1302,6 +1318,129 @@ def _upload_entity_df_and_get_entity_schema(
raise InvalidEntityType(type(entity_df))


def _get_historical_features_without_entity_df(
config: RepoConfig,
client: Trino,
feature_views: List[FeatureView],
feature_refs: List[str],
registry: BaseRegistry,
project: str,
full_feature_names: bool,
filter_by_created_timestamp: bool,
start_date: Optional[datetime],
end_date: Optional[datetime],
) -> TrinoRetrievalJob:
"""
Retrieves historical features for a time range instead of an entity dataframe.

The left side of the point-in-time join is built from the entity keys observed
in the feature view sources between start_date and end_date, each joined as of
end_date, so every entity gets its latest feature values within the TTL.
"""
start_date, end_date = compute_non_entity_date_range(
feature_views,
start_date=start_date,
end_date=end_date,
)

query_context = offline_utils.get_feature_view_query_context(
feature_refs=feature_refs,
feature_views=feature_views,
registry=registry,
project=project,
entity_df_timestamp_range=(start_date, end_date),
)

entity_df_event_timestamp_col = offline_utils.DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
join_keys = offline_utils.gather_all_entities(query_context)
entity_df_columns = {
column: None for column in join_keys + [entity_df_event_timestamp_col]
}.keys()

query = offline_utils.build_point_in_time_query(
query_context,
left_table_query_string=_build_non_entity_left_table_query(
query_context=query_context,
join_keys=join_keys,
start_date=start_date,
end_date=end_date,
entity_df_event_timestamp_col=entity_df_event_timestamp_col,
),
entity_df_event_timestamp_col=entity_df_event_timestamp_col,
entity_df_columns=entity_df_columns,
query_template=MULTIPLE_FEATURE_VIEW_POINT_IN_TIME_JOIN,
full_feature_names=full_feature_names,
filter_by_created_timestamp=filter_by_created_timestamp,
)

return TrinoRetrievalJob(
query=query,
client=client,
config=config,
full_feature_names=full_feature_names,
on_demand_feature_views=OnDemandFeatureView.get_requested_odfvs(
feature_refs, project, registry
),
metadata=RetrievalMetadata(
features=feature_refs,
keys=join_keys,
min_event_timestamp=start_date,
max_event_timestamp=end_date,
),
)


def _build_non_entity_left_table_query(
query_context: List[offline_utils.FeatureViewQueryContext],
join_keys: List[str],
start_date: datetime,
end_date: datetime,
entity_df_event_timestamp_col: str,
) -> str:
"""
Builds a subquery that stands in for the entity dataframe in non-entity mode.

It selects the distinct join keys of every feature view with entities whose
source has events between start_date and end_date, fills the join keys a
feature view does not have with NULL, and sets the event timestamp of every
row to end_date. Feature views without entities join on the timestamp alone,
so they do not contribute rows. When no feature view has entities, the
subquery is a single row holding end_date.
"""
start_timestamp = _to_trino_timestamp_literal(start_date)
end_timestamp = _to_trino_timestamp_literal(end_date)

entity_selects = []
for context in query_context:
if not context.entities:
continue
selections = dict(zip(context.entities, context.entity_selections))
columns = ", ".join(
selections.get(join_key, f"NULL AS {join_key}") for join_key in join_keys
)
entity_selects.append(
f"SELECT DISTINCT {columns} "
f"FROM ({context.table_subquery}) "
f"WHERE {context.timestamp_field} "
f"BETWEEN {start_timestamp} AND {end_timestamp}"
)

if not entity_selects:
return f"(SELECT {end_timestamp} AS {entity_df_event_timestamp_col})"

entity_union = "\n UNION\n ".join(entity_selects)
return f"""(
SELECT *, {end_timestamp} AS {entity_df_event_timestamp_col}
FROM (
{entity_union}
)
)"""


def _to_trino_timestamp_literal(timestamp: datetime) -> str:
return f"from_iso8601_timestamp('{timestamp.astimezone(timezone.utc).isoformat()}')"


def _get_trino_client(config: RepoConfig) -> Trino:
auth = None
if config.offline_store.auth is not None:
Expand Down
Loading
Loading