|
| 1 | +import pyarrow as pa |
| 2 | +from feast.infra.compute_engines.base import ComputeEngine, HistoricalRetrievalTask |
| 3 | +from feast.infra.compute_engines.spark.spark_dag_builder import SparkDAGBuilder |
| 4 | +from feast.infra.materialization.batch_materialization_engine import MaterializationTask, MaterializationJob, \ |
| 5 | + MaterializationJobStatus |
| 6 | +from feast.infra.materialization.contrib.spark.spark_materialization_engine import SparkMaterializationJob |
| 7 | +from feast.infra.compute_engines.dag.model import ExecutionContext |
| 8 | + |
| 9 | + |
| 10 | +class SparkComputeEngine(ComputeEngine): |
| 11 | + def materialize(self, task: MaterializationTask) -> MaterializationJob: |
| 12 | + job_id = f"{task.feature_view.name}-{task.start_time}-{task.end_time}" |
| 13 | + |
| 14 | + try: |
| 15 | + # ✅ 1. Build typed execution context |
| 16 | + entities = [] |
| 17 | + for entity_name in task.feature_view.entities: |
| 18 | + entities.append(self.registry.get_entity(entity_name, task.project)) |
| 19 | + |
| 20 | + context = ExecutionContext( |
| 21 | + project=task.project, |
| 22 | + repo_config=self.repo_config, |
| 23 | + offline_store=self.offline_store, |
| 24 | + online_store=self.online_store, |
| 25 | + entity_defs=entities |
| 26 | + ) |
| 27 | + |
| 28 | + # ✅ 2. Construct DAG and run it |
| 29 | + builder = SparkDAGBuilder( |
| 30 | + feature_view=task.feature_view, |
| 31 | + task=task, |
| 32 | + ) |
| 33 | + plan = builder.build() |
| 34 | + plan.execute(context) |
| 35 | + |
| 36 | + # ✅ 3. Report success |
| 37 | + return SparkMaterializationJob( |
| 38 | + job_id=job_id, |
| 39 | + status=MaterializationJobStatus.SUCCEEDED |
| 40 | + ) |
| 41 | + |
| 42 | + except Exception as e: |
| 43 | + # 🛑 Handle failure |
| 44 | + return SparkMaterializationJob( |
| 45 | + job_id=job_id, |
| 46 | + status=MaterializationJobStatus.ERROR, |
| 47 | + error=e |
| 48 | + ) |
| 49 | + |
| 50 | + def get_historical_features(self, task: HistoricalRetrievalTask) -> pa.Table: |
| 51 | + # ✅ 1. Validate input |
| 52 | + assert len(task.feature_views) == 1, "Multi-view support not yet implemented" |
| 53 | + feature_view = task.feature_views[0] |
| 54 | + |
| 55 | + if isinstance(task.entity_df, str): |
| 56 | + raise NotImplementedError("SQL-based entity_df is not yet supported in DAG") |
| 57 | + |
| 58 | + # ✅ 2. Build typed execution context |
| 59 | + entity_defs = [ |
| 60 | + task.registry.get_entity(name, task.config.project) |
| 61 | + for name in feature_view.entities |
| 62 | + ] |
| 63 | + |
| 64 | + context = ExecutionContext( |
| 65 | + project=task.config.project, |
| 66 | + repo_config=task.config, |
| 67 | + offline_store=self.offline_store, |
| 68 | + online_store=self.online_store, |
| 69 | + entity_defs=entity_defs, |
| 70 | + entity_df=task.entity_df, |
| 71 | + ) |
| 72 | + |
| 73 | + # ✅ 3. Construct and execute DAG |
| 74 | + builder = SparkDAGBuilder(feature_view=feature_view, task=task) |
| 75 | + plan = builder.build() |
| 76 | + |
| 77 | + result = plan.execute(context=context) |
| 78 | + spark_df = result.data # should be a Spark DataFrame |
| 79 | + |
| 80 | + # ✅ 4. Return as Arrow |
| 81 | + return spark_df.toPandas().to_arrow() |
0 commit comments