-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_aggregation_ops.py
More file actions
137 lines (104 loc) · 4.26 KB
/
test_aggregation_ops.py
File metadata and controls
137 lines (104 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from datetime import timedelta
import pandas as pd
import pytest
from feast.aggregation import Aggregation, aggregation_specs_to_agg_ops
from feast.aggregation.tiling.base import get_ir_metadata_for_aggregation
class DummyAggregation:
def __init__(self, *, function: str, column: str, time_window=None, name: str = ""):
self.function = function
self.column = column
self.time_window = time_window
self.name = name
def test_aggregation_specs_to_agg_ops_success():
agg_specs = [
DummyAggregation(function="sum", column="trips"),
DummyAggregation(function="mean", column="fare"),
]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="Time window aggregation is not supported.",
)
assert agg_ops == {
"sum_trips": ("sum", "trips"),
"mean_fare": ("mean", "fare"),
}
@pytest.mark.parametrize(
"error_message",
[
"Time window aggregation is not supported in online serving.",
"Time window aggregation is not supported in the local compute engine.",
],
)
def test_aggregation_specs_to_agg_ops_time_window_unsupported(error_message: str):
agg_specs = [DummyAggregation(function="sum", column="trips", time_window=1)]
with pytest.raises(ValueError, match=error_message):
aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message=error_message,
)
def test_aggregation_specs_to_agg_ops_custom_name():
agg_specs = [
DummyAggregation(
function="sum",
column="seconds_watched",
name="sum_seconds_watched_per_ad_1d",
),
]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="Time window aggregation is not supported.",
)
assert agg_ops == {
"sum_seconds_watched_per_ad_1d": ("sum", "seconds_watched"),
}
def test_aggregation_specs_to_agg_ops_mixed_names():
agg_specs = [
DummyAggregation(function="sum", column="trips", name="total_trips"),
DummyAggregation(function="mean", column="fare"),
]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="Time window aggregation is not supported.",
)
assert agg_ops == {
"total_trips": ("sum", "trips"),
"mean_fare": ("mean", "fare"),
}
def test_aggregation_round_trip_with_name():
agg = Aggregation(
column="seconds_watched",
function="sum",
time_window=timedelta(days=1),
name="sum_seconds_watched_per_ad_1d",
)
proto = agg.to_proto()
assert proto.name == "sum_seconds_watched_per_ad_1d"
restored = Aggregation.from_proto(proto)
assert restored.name == "sum_seconds_watched_per_ad_1d"
assert restored == agg
def test_count_distinct_agg_ops():
"""aggregation_specs_to_agg_ops maps count_distinct to the nunique pandas function."""
agg_specs = [DummyAggregation(function="count_distinct", column="item_id")]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="no windows",
)
assert agg_ops == {"count_distinct_item_id": ("nunique", "item_id")}
def test_count_distinct_result():
"""count_distinct via nunique returns the number of unique values per group."""
from feast.infra.compute_engines.backends.pandas_backend import PandasBackend
agg_specs = [DummyAggregation(function="count_distinct", column="item_id")]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="no windows",
)
df = pd.DataFrame({"user": ["A", "A", "B"], "item_id": [1, 2, 1]})
result = PandasBackend().groupby_agg(df, ["user"], agg_ops)
result = result.set_index("user")
assert result.loc["A", "count_distinct_item_id"] == 2
assert result.loc["B", "count_distinct_item_id"] == 1
def test_count_distinct_tiling_raises():
"""get_ir_metadata_for_aggregation raises ValueError for count_distinct."""
agg = Aggregation(column="item_id", function="count_distinct")
with pytest.raises(ValueError, match="count_distinct does not support tiling"):
get_ir_metadata_for_aggregation(agg, "count_distinct_item_id")