Skip to content

Commit f7e40d2

Browse files
committed
fix: Run the Ray UDF before aggregation in historical retrieval
In materialization, the Ray engine ran the UDF on raw rows before aggregation. In historical retrieval, it ran the UDF after aggregation. So a UDF could only work in one of the two paths. In the other path it raised an error, and safe_batch_processor hid that error, so users got values that were never transformed. Now historical retrieval also runs the UDF on raw rows before aggregation. This matches the compute engine docs and the Local and Spark engines. The transformation step is no longer wrapped in safe_batch_processor, so a UDF error now fails the job. A UDF that used aggregated column names (for example sum_conv_rate) must now use the raw column names (for example conv_rate). Signed-off-by: Chaitany Patel <patelchaitany93@gmail.com>
1 parent 2955bdb commit f7e40d2

4 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎docs/reference/compute-engine/ray.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The Ray compute engine provides:
3535
The Ray compute engine follows Feast's DAG-based architecture:
3636

3737
```
38-
EntityDF → RayReadNode → RayJoinNode → RayFilterNode → RayAggregationNode → RayTransformationNode → Output
38+
EntityDF → RayReadNode → RayTransformationNode → RayJoinNode → RayFilterNode → RayAggregationNode → Output
3939
```
4040

4141
### Core Components

‎sdk/python/feast/infra/compute_engines/ray/feature_builder.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,19 @@ def _build_aggregation_optimized_plan(self) -> ExecutionPlan:
336336
# 1. Read source data
337337
last_node = self.build_source_node(self.feature_view)
338338

339-
# 2. Apply filters (TTL, custom filters) BEFORE aggregation
339+
# 2. Apply transformations to the raw source data, as materialization does
340+
if self._should_transform(self.feature_view):
341+
last_node = self.build_transformation_node(self.feature_view, [last_node])
342+
343+
# 3. Apply filters (TTL, custom filters) BEFORE aggregation
340344
last_node = self.build_filter_node(self.feature_view, last_node)
341345

342-
# 3. Aggregate across all historical records
346+
# 4. Aggregate across all historical records
343347
last_node = self.build_aggregation_node(self.feature_view, last_node)
344348

345-
# 4. Join with entity_df to get aggregated features for each entity
349+
# 5. Join with entity_df to get aggregated features for each entity
346350
last_node = self.build_join_node(self.feature_view, [last_node])
347351

348-
# 5. Apply transformations to aggregated features
349-
if self._should_transform(self.feature_view):
350-
last_node = self.build_transformation_node(self.feature_view, [last_node])
351-
352352
# 6. Output
353353
last_node = self.build_output_nodes(self.feature_view, last_node)
354354

‎sdk/python/feast/infra/compute_engines/ray/nodes.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,14 @@ def execute(self, context: ExecutionContext) -> DAGValue:
730730
elif callable(self.transformation):
731731
transformation_serialized = dill.dumps(self.transformation)
732732

733-
@safe_batch_processor
733+
# Not wrapped in safe_batch_processor: a failed UDF must fail the job.
734734
def apply_transformation_with_serialized_udf(
735735
batch: pd.DataFrame,
736736
) -> pd.DataFrame:
737737
"""Apply the transformation using pre-serialized UDF."""
738+
if _is_empty_batch(batch):
739+
return batch
740+
738741
if transformation_serialized:
739742
transformation_func = dill.loads(transformation_serialized)
740743
transformed_batch = transformation_func(batch)

‎sdk/python/tests/component/ray/test_compute.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ def test_ray_compute_engine_get_historical_features(
4141
fs = ray_environment.feature_store
4242
registry = fs.registry
4343

44+
# The udf runs on raw source rows, before aggregation.
4445
def transform_feature(df: pd.DataFrame) -> pd.DataFrame:
45-
df["sum_conv_rate"] = df["sum_conv_rate"] * 2
46-
df["avg_acc_rate"] = df["avg_acc_rate"] * 2
46+
df["conv_rate"] = df["conv_rate"] * 2
47+
df["acc_rate"] = df["acc_rate"] * 2
4748
return df
4849

4950
driver_stats_fv = BatchFeatureView(

0 commit comments

Comments
 (0)