Skip to content

Commit 150b258

Browse files
nickquinn408ntkathole
authored andcommitted
feat: Adding optional name to Aggregation (feast-dev#5994)
Signed-off-by: Nick Quinn <[email protected]>
1 parent bf4e3fa commit 150b258

5 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎protos/feast/core/Aggregation.proto‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ message Aggregation {
1212
string function = 2;
1313
google.protobuf.Duration time_window = 3;
1414
google.protobuf.Duration slide_interval = 4;
15+
string name = 5;
1516
}

‎sdk/python/feast/aggregation/__init__.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ class Aggregation:
2121
function: str # Provided built in aggregations sum, max, min, count mean
2222
time_window: timedelta # The time window for this aggregation.
2323
slide_interval: timedelta # The sliding window for these aggregations
24+
name: str # Optional override for the output feature name (defaults to {function}_{column})
2425
"""
2526

2627
column: str
2728
function: str
2829
time_window: Optional[timedelta]
2930
slide_interval: Optional[timedelta]
31+
name: str
3032

3133
def __init__(
3234
self,
3335
column: Optional[str] = "",
3436
function: Optional[str] = "",
3537
time_window: Optional[timedelta] = None,
3638
slide_interval: Optional[timedelta] = None,
39+
name: Optional[str] = None,
3740
):
3841
self.column = column or ""
3942
self.function = function or ""
@@ -42,6 +45,7 @@ def __init__(
4245
self.slide_interval = self.time_window
4346
else:
4447
self.slide_interval = slide_interval
48+
self.name = name or ""
4549

4650
def to_proto(self) -> AggregationProto:
4751
window_duration = None
@@ -59,6 +63,7 @@ def to_proto(self) -> AggregationProto:
5963
function=self.function,
6064
time_window=window_duration,
6165
slide_interval=slide_interval_duration,
66+
name=self.name,
6267
)
6368

6469
@classmethod
@@ -79,6 +84,7 @@ def from_proto(cls, agg_proto: AggregationProto):
7984
function=agg_proto.function,
8085
time_window=time_window,
8186
slide_interval=slide_interval,
87+
name=agg_proto.name or None,
8288
)
8389
return aggregation
8490

@@ -91,6 +97,7 @@ def __eq__(self, other):
9197
or self.function != other.function
9298
or self.time_window != other.time_window
9399
or self.slide_interval != other.slide_interval
100+
or self.name != other.name
94101
):
95102
return False
96103

@@ -106,7 +113,7 @@ def aggregation_specs_to_agg_ops(
106113
for agg in agg_specs:
107114
if getattr(agg, "time_window", None) is not None:
108115
raise ValueError(time_window_unsupported_error_message)
109-
alias = f"{agg.function}_{agg.column}"
116+
alias = getattr(agg, "name", None) or f"{agg.function}_{agg.column}"
110117
agg_ops[alias] = (agg.function, agg.column)
111118
return agg_ops
112119

‎sdk/python/feast/protos/feast/core/Aggregation_pb2.py‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎sdk/python/feast/protos/feast/core/Aggregation_pb2.pyi‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class Aggregation(google.protobuf.message.Message):
2222
FUNCTION_FIELD_NUMBER: builtins.int
2323
TIME_WINDOW_FIELD_NUMBER: builtins.int
2424
SLIDE_INTERVAL_FIELD_NUMBER: builtins.int
25+
NAME_FIELD_NUMBER: builtins.int
2526
column: builtins.str
2627
function: builtins.str
28+
name: builtins.str
2729
@property
2830
def time_window(self) -> google.protobuf.duration_pb2.Duration: ...
2931
@property
@@ -35,8 +37,9 @@ class Aggregation(google.protobuf.message.Message):
3537
function: builtins.str = ...,
3638
time_window: google.protobuf.duration_pb2.Duration | None = ...,
3739
slide_interval: google.protobuf.duration_pb2.Duration | None = ...,
40+
name: builtins.str = ...,
3841
) -> None: ...
3942
def HasField(self, field_name: typing_extensions.Literal["slide_interval", b"slide_interval", "time_window", b"time_window"]) -> builtins.bool: ...
40-
def ClearField(self, field_name: typing_extensions.Literal["column", b"column", "function", b"function", "slide_interval", b"slide_interval", "time_window", b"time_window"]) -> None: ...
43+
def ClearField(self, field_name: typing_extensions.Literal["column", b"column", "function", b"function", "name", b"name", "slide_interval", b"slide_interval", "time_window", b"time_window"]) -> None: ...
4144

4245
global___Aggregation = Aggregation

‎sdk/python/tests/unit/test_aggregation_ops.py‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
from datetime import timedelta
2+
13
import pytest
24

3-
from feast.aggregation import aggregation_specs_to_agg_ops
5+
from feast.aggregation import Aggregation, aggregation_specs_to_agg_ops
46

57

68
class DummyAggregation:
7-
def __init__(self, *, function: str, column: str, time_window=None):
9+
def __init__(self, *, function: str, column: str, time_window=None, name: str = ""):
810
self.function = function
911
self.column = column
1012
self.time_window = time_window
13+
self.name = name
1114

1215

1316
def test_aggregation_specs_to_agg_ops_success():
@@ -42,3 +45,50 @@ def test_aggregation_specs_to_agg_ops_time_window_unsupported(error_message: str
4245
agg_specs,
4346
time_window_unsupported_error_message=error_message,
4447
)
48+
49+
50+
def test_aggregation_specs_to_agg_ops_custom_name():
51+
agg_specs = [
52+
DummyAggregation(function="sum", column="seconds_watched", name="sum_seconds_watched_per_ad_1d"),
53+
]
54+
55+
agg_ops = aggregation_specs_to_agg_ops(
56+
agg_specs,
57+
time_window_unsupported_error_message="Time window aggregation is not supported.",
58+
)
59+
60+
assert agg_ops == {
61+
"sum_seconds_watched_per_ad_1d": ("sum", "seconds_watched"),
62+
}
63+
64+
65+
def test_aggregation_specs_to_agg_ops_mixed_names():
66+
agg_specs = [
67+
DummyAggregation(function="sum", column="trips", name="total_trips"),
68+
DummyAggregation(function="mean", column="fare"),
69+
]
70+
71+
agg_ops = aggregation_specs_to_agg_ops(
72+
agg_specs,
73+
time_window_unsupported_error_message="Time window aggregation is not supported.",
74+
)
75+
76+
assert agg_ops == {
77+
"total_trips": ("sum", "trips"),
78+
"mean_fare": ("mean", "fare"),
79+
}
80+
81+
82+
def test_aggregation_round_trip_with_name():
83+
agg = Aggregation(
84+
column="seconds_watched",
85+
function="sum",
86+
time_window=timedelta(days=1),
87+
name="sum_seconds_watched_per_ad_1d",
88+
)
89+
proto = agg.to_proto()
90+
assert proto.name == "sum_seconds_watched_per_ad_1d"
91+
92+
restored = Aggregation.from_proto(proto)
93+
assert restored.name == "sum_seconds_watched_per_ad_1d"
94+
assert restored == agg

0 commit comments

Comments
 (0)