|
59 | 59 | from feast.on_demand_feature_view import OnDemandFeatureView |
60 | 60 | from feast.repo_config import FeastConfigBaseModel, RepoConfig |
61 | 61 | from feast.saved_dataset import SavedDatasetStorage |
| 62 | +from feast.utils import compute_non_entity_date_range |
62 | 63 |
|
63 | 64 | logger = logging.getLogger(__name__) |
64 | 65 |
|
@@ -372,18 +373,33 @@ def get_historical_features( |
372 | 373 | config: RepoConfig, |
373 | 374 | feature_views: List[FeatureView], |
374 | 375 | feature_refs: List[str], |
375 | | - entity_df: Union[pd.DataFrame, str], |
| 376 | + entity_df: Optional[Union[pd.DataFrame, str]], |
376 | 377 | registry: BaseRegistry, |
377 | 378 | project: str, |
378 | 379 | full_feature_names: bool = False, |
379 | 380 | filter_by_created_timestamp: bool = False, |
| 381 | + **kwargs: Any, |
380 | 382 | ) -> TrinoRetrievalJob: |
381 | 383 | assert isinstance(config.offline_store, TrinoOfflineStoreConfig) |
382 | 384 | for fv in feature_views: |
383 | 385 | assert isinstance(fv.batch_source, TrinoSource) |
384 | 386 |
|
385 | 387 | client = _get_trino_client(config=config) |
386 | 388 |
|
| 389 | + if entity_df is None: |
| 390 | + return _get_historical_features_without_entity_df( |
| 391 | + config=config, |
| 392 | + client=client, |
| 393 | + feature_views=feature_views, |
| 394 | + feature_refs=feature_refs, |
| 395 | + registry=registry, |
| 396 | + project=project, |
| 397 | + full_feature_names=full_feature_names, |
| 398 | + filter_by_created_timestamp=filter_by_created_timestamp, |
| 399 | + start_date=kwargs.get("start_date"), |
| 400 | + end_date=kwargs.get("end_date"), |
| 401 | + ) |
| 402 | + |
387 | 403 | table_reference = _get_table_reference_for_new_entity( |
388 | 404 | catalog=config.offline_store.catalog, |
389 | 405 | dataset_name=config.offline_store.dataset, |
@@ -1302,6 +1318,129 @@ def _upload_entity_df_and_get_entity_schema( |
1302 | 1318 | raise InvalidEntityType(type(entity_df)) |
1303 | 1319 |
|
1304 | 1320 |
|
| 1321 | +def _get_historical_features_without_entity_df( |
| 1322 | + config: RepoConfig, |
| 1323 | + client: Trino, |
| 1324 | + feature_views: List[FeatureView], |
| 1325 | + feature_refs: List[str], |
| 1326 | + registry: BaseRegistry, |
| 1327 | + project: str, |
| 1328 | + full_feature_names: bool, |
| 1329 | + filter_by_created_timestamp: bool, |
| 1330 | + start_date: Optional[datetime], |
| 1331 | + end_date: Optional[datetime], |
| 1332 | +) -> TrinoRetrievalJob: |
| 1333 | + """ |
| 1334 | + Retrieves historical features for a time range instead of an entity dataframe. |
| 1335 | +
|
| 1336 | + The left side of the point-in-time join is built from the entity keys observed |
| 1337 | + in the feature view sources between start_date and end_date, each joined as of |
| 1338 | + end_date, so every entity gets its latest feature values within the TTL. |
| 1339 | + """ |
| 1340 | + start_date, end_date = compute_non_entity_date_range( |
| 1341 | + feature_views, |
| 1342 | + start_date=start_date, |
| 1343 | + end_date=end_date, |
| 1344 | + ) |
| 1345 | + |
| 1346 | + query_context = offline_utils.get_feature_view_query_context( |
| 1347 | + feature_refs=feature_refs, |
| 1348 | + feature_views=feature_views, |
| 1349 | + registry=registry, |
| 1350 | + project=project, |
| 1351 | + entity_df_timestamp_range=(start_date, end_date), |
| 1352 | + ) |
| 1353 | + |
| 1354 | + entity_df_event_timestamp_col = offline_utils.DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL |
| 1355 | + join_keys = offline_utils.gather_all_entities(query_context) |
| 1356 | + entity_df_columns = { |
| 1357 | + column: None for column in join_keys + [entity_df_event_timestamp_col] |
| 1358 | + }.keys() |
| 1359 | + |
| 1360 | + query = offline_utils.build_point_in_time_query( |
| 1361 | + query_context, |
| 1362 | + left_table_query_string=_build_non_entity_left_table_query( |
| 1363 | + query_context=query_context, |
| 1364 | + join_keys=join_keys, |
| 1365 | + start_date=start_date, |
| 1366 | + end_date=end_date, |
| 1367 | + entity_df_event_timestamp_col=entity_df_event_timestamp_col, |
| 1368 | + ), |
| 1369 | + entity_df_event_timestamp_col=entity_df_event_timestamp_col, |
| 1370 | + entity_df_columns=entity_df_columns, |
| 1371 | + query_template=MULTIPLE_FEATURE_VIEW_POINT_IN_TIME_JOIN, |
| 1372 | + full_feature_names=full_feature_names, |
| 1373 | + filter_by_created_timestamp=filter_by_created_timestamp, |
| 1374 | + ) |
| 1375 | + |
| 1376 | + return TrinoRetrievalJob( |
| 1377 | + query=query, |
| 1378 | + client=client, |
| 1379 | + config=config, |
| 1380 | + full_feature_names=full_feature_names, |
| 1381 | + on_demand_feature_views=OnDemandFeatureView.get_requested_odfvs( |
| 1382 | + feature_refs, project, registry |
| 1383 | + ), |
| 1384 | + metadata=RetrievalMetadata( |
| 1385 | + features=feature_refs, |
| 1386 | + keys=join_keys, |
| 1387 | + min_event_timestamp=start_date, |
| 1388 | + max_event_timestamp=end_date, |
| 1389 | + ), |
| 1390 | + ) |
| 1391 | + |
| 1392 | + |
| 1393 | +def _build_non_entity_left_table_query( |
| 1394 | + query_context: List[offline_utils.FeatureViewQueryContext], |
| 1395 | + join_keys: List[str], |
| 1396 | + start_date: datetime, |
| 1397 | + end_date: datetime, |
| 1398 | + entity_df_event_timestamp_col: str, |
| 1399 | +) -> str: |
| 1400 | + """ |
| 1401 | + Builds a subquery that stands in for the entity dataframe in non-entity mode. |
| 1402 | +
|
| 1403 | + It selects the distinct join keys of every feature view with entities whose |
| 1404 | + source has events between start_date and end_date, fills the join keys a |
| 1405 | + feature view does not have with NULL, and sets the event timestamp of every |
| 1406 | + row to end_date. Feature views without entities join on the timestamp alone, |
| 1407 | + so they do not contribute rows. When no feature view has entities, the |
| 1408 | + subquery is a single row holding end_date. |
| 1409 | + """ |
| 1410 | + start_timestamp = _to_trino_timestamp_literal(start_date) |
| 1411 | + end_timestamp = _to_trino_timestamp_literal(end_date) |
| 1412 | + |
| 1413 | + entity_selects = [] |
| 1414 | + for context in query_context: |
| 1415 | + if not context.entities: |
| 1416 | + continue |
| 1417 | + selections = dict(zip(context.entities, context.entity_selections)) |
| 1418 | + columns = ", ".join( |
| 1419 | + selections.get(join_key, f"NULL AS {join_key}") for join_key in join_keys |
| 1420 | + ) |
| 1421 | + entity_selects.append( |
| 1422 | + f"SELECT DISTINCT {columns} " |
| 1423 | + f"FROM ({context.table_subquery}) " |
| 1424 | + f"WHERE {context.timestamp_field} " |
| 1425 | + f"BETWEEN {start_timestamp} AND {end_timestamp}" |
| 1426 | + ) |
| 1427 | + |
| 1428 | + if not entity_selects: |
| 1429 | + return f"(SELECT {end_timestamp} AS {entity_df_event_timestamp_col})" |
| 1430 | + |
| 1431 | + entity_union = "\n UNION\n ".join(entity_selects) |
| 1432 | + return f"""( |
| 1433 | + SELECT *, {end_timestamp} AS {entity_df_event_timestamp_col} |
| 1434 | + FROM ( |
| 1435 | + {entity_union} |
| 1436 | + ) |
| 1437 | +)""" |
| 1438 | + |
| 1439 | + |
| 1440 | +def _to_trino_timestamp_literal(timestamp: datetime) -> str: |
| 1441 | + return f"from_iso8601_timestamp('{timestamp.astimezone(timezone.utc).isoformat()}')" |
| 1442 | + |
| 1443 | + |
1305 | 1444 | def _get_trino_client(config: RepoConfig) -> Trino: |
1306 | 1445 | auth = None |
1307 | 1446 | if config.offline_store.auth is not None: |
|
0 commit comments