-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_stream_feature_view.py
More file actions
305 lines (269 loc) · 8.81 KB
/
test_stream_feature_view.py
File metadata and controls
305 lines (269 loc) · 8.81 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import copy
from datetime import timedelta
import pytest
from feast.aggregation import Aggregation
from feast.data_format import AvroFormat
from feast.data_source import KafkaSource, PushSource
from feast.entity import Entity
from feast.field import Field
from feast.infra.offline_stores.file_source import FileSource
from feast.protos.feast.core.StreamFeatureView_pb2 import (
StreamFeatureView as StreamFeatureViewProto,
)
from feast.stream_feature_view import StreamFeatureView, stream_feature_view
from feast.types import Float32
from feast.utils import _utc_now, make_tzaware
def test_create_stream_feature_view():
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
StreamFeatureView(
name="test kafka stream feature view",
entities=[],
ttl=timedelta(days=30),
source=stream_source,
aggregations=[],
udf=lambda x: x,
)
push_source = PushSource(
name="push source", batch_source=FileSource(path="some path")
)
StreamFeatureView(
name="test push source feature view",
entities=[],
ttl=timedelta(days=30),
source=push_source,
aggregations=[],
udf=lambda x: x,
)
with pytest.raises(TypeError):
StreamFeatureView(
name="test batch feature view",
entities=[],
ttl=timedelta(days=30),
aggregations=[],
)
with pytest.raises(ValueError):
StreamFeatureView(
name="test batch feature view",
entities=[],
ttl=timedelta(days=30),
source=FileSource(path="some path"),
aggregations=[],
udf=lambda x: x,
)
def simple_udf(x: int):
return x + 3
def test_stream_feature_view_serialization():
entity = Entity(name="driver_entity", join_keys=["test_key"])
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
sfv = StreamFeatureView(
name="test kafka stream feature view",
entities=[entity],
ttl=timedelta(days=30),
owner="test@example.com",
online=True,
schema=[Field(name="dummy_field", dtype=Float32)],
description="desc",
aggregations=[
Aggregation(
column="dummy_field",
function="max",
time_window=timedelta(days=1),
)
],
timestamp_field="event_timestamp",
mode="spark",
source=stream_source,
udf=simple_udf,
tags={},
)
sfv_proto = sfv.to_proto()
new_sfv = StreamFeatureView.from_proto(sfv_proto=sfv_proto)
assert new_sfv == sfv
assert (
sfv_proto.spec.feature_transformation.user_defined_function.name == "simple_udf"
)
def test_stream_feature_view_udfs():
entity = Entity(name="driver_entity", join_keys=["test_key"])
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
@stream_feature_view(
entities=[entity],
ttl=timedelta(days=30),
owner="test@example.com",
online=True,
schema=[Field(name="dummy_field", dtype=Float32)],
description="desc",
aggregations=[
Aggregation(
column="dummy_field",
function="max",
time_window=timedelta(days=1),
)
],
timestamp_field="event_timestamp",
source=stream_source,
)
def pandas_udf(pandas_df):
import pandas as pd
assert type(pandas_df) == pd.DataFrame
df = pandas_df.transform(lambda x: x + 10)
return df
import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3], "B": [10, 20, 30]})
sfv = pandas_udf
sfv_proto = sfv.to_proto()
new_sfv = StreamFeatureView.from_proto(sfv_proto)
new_df = new_sfv.udf(df)
expected_df = pd.DataFrame({"A": [11, 12, 13], "B": [20, 30, 40]})
assert new_df.equals(expected_df)
def test_stream_feature_view_initialization_with_optional_fields_omitted():
entity = Entity(name="driver_entity", join_keys=["test_key"])
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
sfv = StreamFeatureView(
name="test kafka stream feature view",
entities=[entity],
schema=[],
description="desc",
timestamp_field="event_timestamp",
source=stream_source,
tags={},
)
sfv_proto = sfv.to_proto()
new_sfv = StreamFeatureView.from_proto(sfv_proto=sfv_proto)
assert new_sfv == sfv
def test_stream_feature_view_proto_type():
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
sfv = StreamFeatureView(
name="test stream featureview proto class",
entities=[],
ttl=timedelta(days=30),
source=stream_source,
aggregations=[],
udf=lambda x: x,
)
assert sfv.proto_class is StreamFeatureViewProto
def test_stream_feature_view_copy():
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
sfv = StreamFeatureView(
name="test stream featureview proto class",
entities=[],
ttl=timedelta(days=30),
source=stream_source,
aggregations=[],
udf=lambda x: x,
)
assert sfv == copy.copy(sfv)
def test_update_materialization_intervals():
entity = Entity(name="driver_entity", join_keys=["test_key"])
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
# Create a stream feature view that is already present in the SQL registry
stored_stream_feature_view = StreamFeatureView(
name="test kafka stream feature view",
entities=[entity],
ttl=timedelta(days=30),
owner="test@example.com",
online=True,
schema=[Field(name="dummy_field", dtype=Float32)],
description="desc",
aggregations=[
Aggregation(
column="dummy_field",
function="max",
time_window=timedelta(days=1),
)
],
timestamp_field="event_timestamp",
mode="spark",
source=stream_source,
udf=simple_udf,
tags={},
)
current_time = _utc_now()
start_date = make_tzaware(current_time - timedelta(days=1))
end_date = make_tzaware(current_time)
stored_stream_feature_view.materialization_intervals.append((start_date, end_date))
# Update the stream feature view i.e. here it's simply the name
updated_stream_feature_view = StreamFeatureView(
name="test kafka stream feature view updated",
entities=[entity],
ttl=timedelta(days=30),
owner="test@example.com",
online=True,
schema=[Field(name="dummy_field", dtype=Float32)],
description="desc",
aggregations=[
Aggregation(
column="dummy_field",
function="max",
time_window=timedelta(days=1),
)
],
timestamp_field="event_timestamp",
mode="spark",
source=stream_source,
udf=simple_udf,
tags={},
)
updated_stream_feature_view.update_materialization_intervals(
stored_stream_feature_view.materialization_intervals
)
assert (
updated_stream_feature_view.materialization_intervals is not None
and len(stored_stream_feature_view.materialization_intervals) == 1
)
assert (
updated_stream_feature_view.materialization_intervals[0][0]
== stored_stream_feature_view.materialization_intervals[0][0]
)
assert (
updated_stream_feature_view.materialization_intervals[0][1]
== stored_stream_feature_view.materialization_intervals[0][1]
)