-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_feature_views.py
More file actions
392 lines (332 loc) · 12.6 KB
/
Copy pathtest_feature_views.py
File metadata and controls
392 lines (332 loc) · 12.6 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from datetime import timedelta
import pandas as pd
import pytest
from typeguard import TypeCheckError
from feast.batch_feature_view import BatchFeatureView
from feast.data_format import AvroFormat
from feast.data_source import KafkaSource
from feast.entity import Entity
from feast.feature_view import FeatureView
from feast.field import Field
from feast.infra.offline_stores.file_source import FileSource
from feast.protos.feast.core.FeatureView_pb2 import FeatureView as FeatureViewProto
from feast.protos.feast.core.FeatureView_pb2 import (
FeatureViewMeta as FeatureViewMetaProto,
)
from feast.protos.feast.core.FeatureView_pb2 import (
FeatureViewSpec as FeatureViewSpecProto,
)
from feast.protos.feast.types.Value_pb2 import ValueType
from feast.types import Float32, Int64, String
from feast.utils import _utc_now, make_tzaware
def test_create_feature_view_with_conflicting_entities():
user1 = Entity(name="user1", join_keys=["user_id"])
user2 = Entity(name="user2", join_keys=["user_id"])
batch_source = FileSource(path="some path")
with pytest.raises(ValueError):
_ = FeatureView(
name="test",
entities=[user1, user2],
ttl=timedelta(days=30),
source=batch_source,
)
def test_create_batch_feature_view():
batch_source = FileSource(path="some path")
BatchFeatureView(
name="test batch feature view",
entities=[],
ttl=timedelta(days=30),
source=batch_source,
mode="python",
udf=lambda x: x,
)
with pytest.raises(TypeError):
BatchFeatureView(
name="test batch feature view", entities=[], ttl=timedelta(days=30)
)
stream_source = KafkaSource(
name="kafka",
timestamp_field="event_timestamp",
kafka_bootstrap_servers="",
message_format=AvroFormat(""),
topic="topic",
batch_source=FileSource(path="some path"),
)
with pytest.raises(ValueError):
BatchFeatureView(
name="test batch feature view",
mode="python",
udf=lambda x: x,
entities=[],
ttl=timedelta(days=30),
source=stream_source,
)
def simple_udf(x: int):
return x + 3
def test_hash():
file_source = FileSource(name="my-file-source", path="test.parquet")
feature_view_1 = FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=Float32),
],
source=file_source,
)
feature_view_2 = FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=Float32),
],
source=file_source,
)
feature_view_3 = FeatureView(
name="my-feature-view",
entities=[],
schema=[Field(name="feature1", dtype=Float32)],
source=file_source,
)
feature_view_4 = FeatureView(
name="my-feature-view",
entities=[],
schema=[Field(name="feature1", dtype=Float32)],
source=file_source,
description="test",
)
s1 = {feature_view_1, feature_view_2}
assert len(s1) == 1
s2 = {feature_view_1, feature_view_3}
assert len(s2) == 2
s3 = {feature_view_3, feature_view_4}
assert len(s3) == 2
s4 = {feature_view_1, feature_view_2, feature_view_3, feature_view_4}
assert len(s4) == 3
def test_proto_conversion():
file_source = FileSource(name="my-file-source", path="test.parquet")
feature_view_1 = FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=Float32),
],
source=file_source,
)
feature_view_proto = feature_view_1.to_proto()
assert (
feature_view_proto.spec.name == "my-feature-view"
and feature_view_proto.spec.batch_source.file_options.uri == "test.parquet"
and feature_view_proto.spec.batch_source.name == "my-file-source"
and feature_view_proto.spec.batch_source.type == 1
)
# TODO(felixwang9817): Add tests for field mapping logic.
def test_field_types():
with pytest.raises(TypeCheckError):
Field(name="name", dtype=ValueType.INT32)
def test_update_materialization_intervals():
batch_source = FileSource(path="some path")
entity = Entity(name="entity_1", description="Some entity")
# Create a feature view that is already present in the SQL registry
stored_feature_view = FeatureView(
name="my-feature-view",
entities=[entity],
ttl=timedelta(days=1),
source=batch_source,
)
# Update the Feature View without modifying anything
updated_feature_view = FeatureView(
name="my-feature-view",
entities=[entity],
ttl=timedelta(days=1),
source=batch_source,
)
updated_feature_view.update_materialization_intervals(
stored_feature_view.materialization_intervals
)
assert len(updated_feature_view.materialization_intervals) == 0
current_time = _utc_now()
start_date = make_tzaware(current_time - timedelta(days=1))
end_date = make_tzaware(current_time)
updated_feature_view.materialization_intervals.append((start_date, end_date))
# Update the Feature View, i.e. simply update the name
second_updated_feature_view = FeatureView(
name="my-feature-view-1",
entities=[entity],
ttl=timedelta(days=1),
source=batch_source,
)
second_updated_feature_view.update_materialization_intervals(
updated_feature_view.materialization_intervals
)
assert len(second_updated_feature_view.materialization_intervals) == 1
assert (
second_updated_feature_view.materialization_intervals[0][0]
== updated_feature_view.materialization_intervals[0][0]
)
assert (
second_updated_feature_view.materialization_intervals[0][1]
== updated_feature_view.materialization_intervals[0][1]
)
def test_create_feature_view_with_chained_views():
file_source = FileSource(name="my-file-source", path="test.parquet")
sink_source = FileSource(name="my-sink-source", path="sink.parquet")
feature_view_1 = FeatureView(
name="my-feature-view-1",
entities=[],
schema=[Field(name="feature1", dtype=Float32)],
source=file_source,
)
feature_view_2 = FeatureView(
name="my-feature-view-2",
entities=[],
schema=[Field(name="feature2", dtype=Float32)],
source=feature_view_1,
sink_source=sink_source,
)
feature_view_3 = FeatureView(
name="my-feature-view-3",
entities=[],
schema=[Field(name="feature3", dtype=Float32)],
source=[feature_view_1, feature_view_2],
sink_source=sink_source,
)
assert feature_view_2.name == "my-feature-view-2"
assert feature_view_2.schema == [Field(name="feature2", dtype=Float32)]
assert feature_view_2.batch_source == sink_source
assert feature_view_2.source_views == [feature_view_1]
assert feature_view_3.name == "my-feature-view-3"
assert feature_view_3.schema == [Field(name="feature3", dtype=Float32)]
assert feature_view_3.batch_source == sink_source
assert feature_view_3.source_views == [feature_view_1, feature_view_2]
def test_feature_view_to_proto_with_cycle():
fv_a = FeatureView(
name="fv_a",
schema=[Field(name="feature1", dtype=Float32)],
source=FileSource(name="source_a", path="source_a.parquet"),
ttl=timedelta(days=1),
entities=[],
)
fv_b = FeatureView(
name="fv_b",
schema=[Field(name="feature1", dtype=Float32)],
source=[fv_a],
ttl=timedelta(days=1),
entities=[],
sink_source=FileSource(name="sink_source_b", path="sink_b.parquet"),
)
fv_a = FeatureView(
name="fv_a",
schema=[Field(name="feature1", dtype=Float32)],
source=[fv_b],
ttl=timedelta(days=1),
entities=[],
sink_source=FileSource(name="sink_source_a", path="sink_a.parquet"),
)
with pytest.raises(
ValueError, match="Cycle detected during serialization of FeatureView: fv_a"
):
fv_a.to_proto()
def test_feature_view_from_proto_with_cycle():
# Create spec_a
spec_a = FeatureViewSpecProto()
spec_a.name = "fv_a"
spec_a.entities.append("entity_id")
spec_a.features.append(Field(name="a", dtype=Float32).to_proto())
spec_a.batch_source.CopyFrom(
FileSource(name="source_a", path="source_a.parquet").to_proto()
)
# Create spec_b
spec_b = FeatureViewSpecProto()
spec_b.name = "fv_b"
spec_b.entities.append("entity_id")
spec_b.features.append(Field(name="b", dtype=Float32).to_proto())
spec_b.batch_source.CopyFrom(
FileSource(name="source_b", path="source_b.parquet").to_proto()
)
# Create the cycle: A → B → A
spec_b.source_views.append(spec_a)
spec_a.source_views.append(spec_b)
# Trigger deserialization
proto_a = FeatureViewProto(spec=spec_a, meta=FeatureViewMetaProto())
with pytest.raises(
ValueError, match="Cycle detected while deserializing FeatureView: fv_a"
):
FeatureView.from_proto(proto_a)
def test_batch_feature_view_serialization_deserialization():
"""
Test that BatchFeatureView with transformations correctly serializes to proto
and deserializes back preserving both type and transformation.
"""
def transform_udf(df: pd.DataFrame) -> pd.DataFrame:
df["output_feature"] = df["input_feature"] * 2 + 10
return df
file_source = FileSource(
name="test-source",
path="test_data.parquet",
timestamp_field="event_timestamp",
)
entity = Entity(name="test_entity", join_keys=["entity_id"])
original_bfv = BatchFeatureView(
name="test_batch_feature_view",
entities=[entity],
schema=[
Field(name="entity_id", dtype=String),
Field(name="input_feature", dtype=Int64),
Field(name="output_feature", dtype=Int64),
],
source=file_source,
ttl=timedelta(days=1),
online=True,
description="Test batch feature view with transformation",
tags={"team": "data_science", "env": "test"},
owner="test_owner",
udf=transform_udf,
mode="python",
)
# Verify the original has the transformation
assert hasattr(original_bfv, "feature_transformation")
assert original_bfv.feature_transformation is not None
assert original_bfv.feature_transformation.udf == transform_udf
# Serialize to proto
proto = original_bfv.to_proto()
# Verify proto has the transformation field
assert proto.spec.HasField("feature_transformation")
assert proto.spec.feature_transformation.HasField("user_defined_function")
assert (
proto.spec.feature_transformation.user_defined_function.name == "transform_udf"
)
assert proto.spec.feature_transformation.user_defined_function.body != b""
# Deserialize from proto using FeatureView.from_proto()
# This should return a BatchFeatureView, not a generic FeatureView
deserialized = FeatureView.from_proto(proto)
# Verify the type is preserved
assert isinstance(deserialized, BatchFeatureView), (
f"Expected BatchFeatureView but got {type(deserialized).__name__}. "
"Type should be preserved during deserialization."
)
# Verify basic attributes
assert deserialized.name == "test_batch_feature_view"
assert deserialized.description == "Test batch feature view with transformation"
assert deserialized.tags == {"team": "data_science", "env": "test"}
assert deserialized.owner == "test_owner"
assert deserialized.ttl == timedelta(days=1)
assert deserialized.online is True
# Verify the transformation is preserved
assert hasattr(deserialized, "feature_transformation")
assert deserialized.feature_transformation is not None
# Verify the UDF is functional by testing it
test_df = pd.DataFrame({"input_feature": [1, 2, 3]})
result_df = deserialized.feature_transformation.udf(test_df)
expected_df = pd.DataFrame(
{"input_feature": [1, 2, 3], "output_feature": [12, 14, 16]}
)
pd.testing.assert_frame_equal(result_df, expected_df)
# Test round-trip: serialize again and verify consistency
second_proto = deserialized.to_proto()
second_deserialized = FeatureView.from_proto(second_proto)
assert isinstance(second_deserialized, BatchFeatureView)
assert second_deserialized.name == original_bfv.name
assert second_deserialized.feature_transformation is not None