|
| 1 | +from dataclasses import dataclass |
| 2 | +from datetime import datetime |
| 3 | +from typing import Callable, List, Literal, Optional, Sequence, Union |
| 4 | + |
| 5 | +from tqdm import tqdm |
| 6 | + |
| 7 | +from feast.batch_feature_view import BatchFeatureView |
| 8 | +from feast.entity import Entity |
| 9 | +from feast.feature_view import FeatureView |
| 10 | +from feast.infra.offline_stores.offline_store import OfflineStore |
| 11 | +from feast.infra.online_stores.online_store import OnlineStore |
| 12 | +from feast.repo_config import FeastConfigBaseModel, RepoConfig |
| 13 | +from feast.stream_feature_view import StreamFeatureView |
| 14 | + |
| 15 | +from ...registry import BaseRegistry |
| 16 | +from ...utils import ( |
| 17 | + _convert_arrow_to_proto, |
| 18 | + _get_column_names, |
| 19 | + _run_pyarrow_field_mapping, |
| 20 | +) |
| 21 | +from .batch_materialization_engine import ( |
| 22 | + BatchMaterializationEngine, |
| 23 | + MaterializationJob, |
| 24 | + MaterializationJobStatus, |
| 25 | + MaterializationTask, |
| 26 | +) |
| 27 | + |
| 28 | +DEFAULT_BATCH_SIZE = 10_000 |
| 29 | + |
| 30 | + |
| 31 | +class LocalMaterializationEngineConfig(FeastConfigBaseModel): |
| 32 | + """Batch Materialization Engine config for local in-process engine""" |
| 33 | + |
| 34 | + type: Literal["local"] = "local" |
| 35 | + """ Type selector""" |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class LocalMaterializationJob(MaterializationJob): |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + job_id: str, |
| 43 | + status: MaterializationJobStatus, |
| 44 | + error: Optional[BaseException] = None, |
| 45 | + ) -> None: |
| 46 | + super().__init__() |
| 47 | + self._job_id: str = job_id |
| 48 | + self._status: MaterializationJobStatus = status |
| 49 | + self._error: Optional[BaseException] = error |
| 50 | + |
| 51 | + def status(self) -> MaterializationJobStatus: |
| 52 | + return self._status |
| 53 | + |
| 54 | + def error(self) -> Optional[BaseException]: |
| 55 | + return self._error |
| 56 | + |
| 57 | + def should_be_retried(self) -> bool: |
| 58 | + return False |
| 59 | + |
| 60 | + def job_id(self) -> str: |
| 61 | + return self._job_id |
| 62 | + |
| 63 | + def url(self) -> Optional[str]: |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +class LocalMaterializationEngine(BatchMaterializationEngine): |
| 68 | + def update( |
| 69 | + self, |
| 70 | + project: str, |
| 71 | + views_to_delete: Sequence[ |
| 72 | + Union[BatchFeatureView, StreamFeatureView, FeatureView] |
| 73 | + ], |
| 74 | + views_to_keep: Sequence[ |
| 75 | + Union[BatchFeatureView, StreamFeatureView, FeatureView] |
| 76 | + ], |
| 77 | + entities_to_delete: Sequence[Entity], |
| 78 | + entities_to_keep: Sequence[Entity], |
| 79 | + ): |
| 80 | + # Nothing to set up. |
| 81 | + pass |
| 82 | + |
| 83 | + def teardown_infra( |
| 84 | + self, |
| 85 | + project: str, |
| 86 | + fvs: Sequence[Union[BatchFeatureView, StreamFeatureView, FeatureView]], |
| 87 | + entities: Sequence[Entity], |
| 88 | + ): |
| 89 | + # Nothing to tear down. |
| 90 | + pass |
| 91 | + |
| 92 | + def __init__( |
| 93 | + self, |
| 94 | + *, |
| 95 | + repo_config: RepoConfig, |
| 96 | + offline_store: OfflineStore, |
| 97 | + online_store: OnlineStore, |
| 98 | + **kwargs, |
| 99 | + ): |
| 100 | + super().__init__( |
| 101 | + repo_config=repo_config, |
| 102 | + offline_store=offline_store, |
| 103 | + online_store=online_store, |
| 104 | + **kwargs, |
| 105 | + ) |
| 106 | + |
| 107 | + def materialize( |
| 108 | + self, registry, tasks: List[MaterializationTask] |
| 109 | + ) -> List[MaterializationJob]: |
| 110 | + return [ |
| 111 | + self._materialize_one( |
| 112 | + registry, |
| 113 | + task.feature_view, |
| 114 | + task.start_time, |
| 115 | + task.end_time, |
| 116 | + task.project, |
| 117 | + task.tqdm_builder, |
| 118 | + ) |
| 119 | + for task in tasks |
| 120 | + ] |
| 121 | + |
| 122 | + def _materialize_one( |
| 123 | + self, |
| 124 | + registry: BaseRegistry, |
| 125 | + feature_view: Union[BatchFeatureView, StreamFeatureView, FeatureView], |
| 126 | + start_date: datetime, |
| 127 | + end_date: datetime, |
| 128 | + project: str, |
| 129 | + tqdm_builder: Callable[[int], tqdm], |
| 130 | + ): |
| 131 | + entities = [] |
| 132 | + for entity_name in feature_view.entities: |
| 133 | + entities.append(registry.get_entity(entity_name, project)) |
| 134 | + |
| 135 | + ( |
| 136 | + join_key_columns, |
| 137 | + feature_name_columns, |
| 138 | + timestamp_field, |
| 139 | + created_timestamp_column, |
| 140 | + ) = _get_column_names(feature_view, entities) |
| 141 | + |
| 142 | + job_id = f"{feature_view.name}-{start_date}-{end_date}" |
| 143 | + |
| 144 | + try: |
| 145 | + offline_job = self.offline_store.pull_latest_from_table_or_query( |
| 146 | + config=self.repo_config, |
| 147 | + data_source=feature_view.batch_source, |
| 148 | + join_key_columns=join_key_columns, |
| 149 | + feature_name_columns=feature_name_columns, |
| 150 | + timestamp_field=timestamp_field, |
| 151 | + created_timestamp_column=created_timestamp_column, |
| 152 | + start_date=start_date, |
| 153 | + end_date=end_date, |
| 154 | + ) |
| 155 | + |
| 156 | + table = offline_job.to_arrow() |
| 157 | + |
| 158 | + if feature_view.batch_source.field_mapping is not None: |
| 159 | + table = _run_pyarrow_field_mapping( |
| 160 | + table, feature_view.batch_source.field_mapping |
| 161 | + ) |
| 162 | + |
| 163 | + join_key_to_value_type = { |
| 164 | + entity.name: entity.dtype.to_value_type() |
| 165 | + for entity in feature_view.entity_columns |
| 166 | + } |
| 167 | + |
| 168 | + with tqdm_builder(table.num_rows) as pbar: |
| 169 | + for batch in table.to_batches(DEFAULT_BATCH_SIZE): |
| 170 | + rows_to_write = _convert_arrow_to_proto( |
| 171 | + batch, feature_view, join_key_to_value_type |
| 172 | + ) |
| 173 | + self.online_store.online_write_batch( |
| 174 | + self.repo_config, |
| 175 | + feature_view, |
| 176 | + rows_to_write, |
| 177 | + lambda x: pbar.update(x), |
| 178 | + ) |
| 179 | + return LocalMaterializationJob( |
| 180 | + job_id=job_id, status=MaterializationJobStatus.SUCCEEDED |
| 181 | + ) |
| 182 | + except BaseException as e: |
| 183 | + return LocalMaterializationJob( |
| 184 | + job_id=job_id, status=MaterializationJobStatus.ERROR, error=e |
| 185 | + ) |
0 commit comments