-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update udf tests and add base functions to streaming fcos and fi…
…x some nonetype errors (#2776) * Fix lint and add comments Signed-off-by: Kevin Zhang <[email protected]> * Fix Signed-off-by: Kevin Zhang <[email protected]> * Fix lint Signed-off-by: Kevin Zhang <[email protected]>
- Loading branch information
Showing
4 changed files
with
220 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,71 @@ def simple_sfv(df): | |
assert features["test_key"] == [1001] | ||
assert "dummy_field" in features | ||
assert features["dummy_field"] == [None] | ||
|
||
|
||
@pytest.mark.integration | ||
def test_stream_feature_view_udf(environment) -> None: | ||
""" | ||
Test apply of StreamFeatureView udfs are serialized correctly and usable. | ||
""" | ||
fs = environment.feature_store | ||
|
||
# Create Feature Views | ||
entity = Entity(name="driver_entity", join_keys=["test_key"]) | ||
|
||
stream_source = KafkaSource( | ||
name="kafka", | ||
timestamp_field="event_timestamp", | ||
bootstrap_servers="", | ||
message_format=AvroFormat(""), | ||
topic="topic", | ||
batch_source=FileSource(path="test_path", timestamp_field="event_timestamp"), | ||
watermark=timedelta(days=1), | ||
) | ||
|
||
@stream_feature_view( | ||
entities=[entity], | ||
ttl=timedelta(days=30), | ||
owner="[email protected]", | ||
online=True, | ||
schema=[Field(name="dummy_field", dtype=Float32)], | ||
description="desc", | ||
aggregations=[ | ||
Aggregation( | ||
column="dummy_field", function="max", time_window=timedelta(days=1), | ||
), | ||
Aggregation( | ||
column="dummy_field2", function="count", time_window=timedelta(days=24), | ||
), | ||
], | ||
timestamp_field="event_timestamp", | ||
mode="spark", | ||
source=stream_source, | ||
tags={}, | ||
) | ||
def pandas_view(pandas_df): | ||
import pandas as pd | ||
|
||
assert type(pandas_df) == pd.DataFrame | ||
df = pandas_df.transform(lambda x: x + 10, axis=1) | ||
df.insert(2, "C", [20.2, 230.0, 34.0], True) | ||
return df | ||
|
||
import pandas as pd | ||
|
||
df = pd.DataFrame({"A": [1, 2, 3], "B": [10, 20, 30]}) | ||
|
||
fs.apply([entity, pandas_view]) | ||
stream_feature_views = fs.list_stream_feature_views() | ||
assert len(stream_feature_views) == 1 | ||
assert stream_feature_views[0].name == "pandas_view" | ||
assert stream_feature_views[0] == pandas_view | ||
|
||
sfv = stream_feature_views[0] | ||
|
||
new_df = sfv.udf(df) | ||
|
||
expected_df = pd.DataFrame( | ||
{"A": [11, 12, 13], "B": [20, 30, 40], "C": [20.2, 230.0, 34.0]} | ||
) | ||
assert new_df.equals(expected_df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
from feast.entity import Entity | ||
from feast.field import Field | ||
from feast.infra.offline_stores.file_source import FileSource | ||
from feast.stream_feature_view import StreamFeatureView | ||
from feast.stream_feature_view import StreamFeatureView, stream_feature_view | ||
from feast.types import Float32 | ||
|
||
|
||
|
@@ -129,3 +129,75 @@ def test_stream_feature_view_serialization(): | |
|
||
new_sfv = StreamFeatureView.from_proto(sfv_proto=sfv_proto) | ||
assert new_sfv == sfv | ||
|
||
|
||
def test_stream_feature_view_udfs(): | ||
entity = Entity(name="driver_entity", join_keys=["test_key"]) | ||
stream_source = KafkaSource( | ||
name="kafka", | ||
timestamp_field="event_timestamp", | ||
bootstrap_servers="", | ||
message_format=AvroFormat(""), | ||
topic="topic", | ||
batch_source=FileSource(path="some path"), | ||
) | ||
|
||
@stream_feature_view( | ||
entities=[entity], | ||
ttl=timedelta(days=30), | ||
owner="[email protected]", | ||
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, axis=1) | ||
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", | ||
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 |