-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Adding support for dictionary writes to online store #4156
Merged
jeremyary
merged 10 commits into
feast-dev:master
from
franciscojavierarceo:online-store-writes
Apr 30, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e25d66f
feat: Adding support for dictionary writes to online store
franciscojavierarceo 5aafa40
Simple approach
franciscojavierarceo 11650d8
lint
franciscojavierarceo 615ec35
adding error if both are missing
franciscojavierarceo 605bc26
rename dict to input_dict
franciscojavierarceo f363950
updated input arg to test
franciscojavierarceo d1a596d
Renaming function argument
franciscojavierarceo d55311d
updated docstring
franciscojavierarceo 7784377
updated type signature
franciscojavierarceo 30429c2
updated type to be more explicit
franciscojavierarceo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
139 changes: 139 additions & 0 deletions
139
sdk/python/tests/unit/online_store/test_online_writes.py
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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# Copyright 2022 The Feast Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import tempfile | ||
import unittest | ||
from datetime import datetime, timedelta | ||
from typing import Any, Dict | ||
|
||
from feast import Entity, FeatureStore, FeatureView, FileSource, RepoConfig | ||
from feast.driver_test_data import create_driver_hourly_stats_df | ||
from feast.field import Field | ||
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig | ||
from feast.on_demand_feature_view import on_demand_feature_view | ||
from feast.types import Float32, Float64, Int64 | ||
|
||
|
||
class TestOnlineWrites(unittest.TestCase): | ||
def setUp(self): | ||
with tempfile.TemporaryDirectory() as data_dir: | ||
self.store = FeatureStore( | ||
config=RepoConfig( | ||
project="test_write_to_online_store", | ||
registry=os.path.join(data_dir, "registry.db"), | ||
provider="local", | ||
entity_key_serialization_version=2, | ||
online_store=SqliteOnlineStoreConfig( | ||
path=os.path.join(data_dir, "online.db") | ||
), | ||
) | ||
) | ||
|
||
# Generate test data. | ||
end_date = datetime.now().replace(microsecond=0, second=0, minute=0) | ||
start_date = end_date - timedelta(days=15) | ||
|
||
driver_entities = [1001, 1002, 1003, 1004, 1005] | ||
driver_df = create_driver_hourly_stats_df( | ||
driver_entities, start_date, end_date | ||
) | ||
driver_stats_path = os.path.join(data_dir, "driver_stats.parquet") | ||
driver_df.to_parquet( | ||
path=driver_stats_path, allow_truncated_timestamps=True | ||
) | ||
|
||
driver = Entity(name="driver", join_keys=["driver_id"]) | ||
|
||
driver_stats_source = FileSource( | ||
name="driver_hourly_stats_source", | ||
path=driver_stats_path, | ||
timestamp_field="event_timestamp", | ||
created_timestamp_column="created", | ||
) | ||
|
||
driver_stats_fv = FeatureView( | ||
name="driver_hourly_stats", | ||
entities=[driver], | ||
ttl=timedelta(days=0), | ||
schema=[ | ||
Field(name="conv_rate", dtype=Float32), | ||
Field(name="acc_rate", dtype=Float32), | ||
Field(name="avg_daily_trips", dtype=Int64), | ||
], | ||
online=True, | ||
source=driver_stats_source, | ||
) | ||
|
||
@on_demand_feature_view( | ||
sources=[driver_stats_fv[["conv_rate", "acc_rate"]]], | ||
schema=[Field(name="conv_rate_plus_acc", dtype=Float64)], | ||
mode="python", | ||
) | ||
def test_view(inputs: Dict[str, Any]) -> Dict[str, Any]: | ||
output: Dict[str, Any] = { | ||
"conv_rate_plus_acc": [ | ||
conv_rate + acc_rate | ||
for conv_rate, acc_rate in zip( | ||
inputs["conv_rate"], inputs["acc_rate"] | ||
) | ||
] | ||
} | ||
return output | ||
|
||
self.store.apply( | ||
[ | ||
driver, | ||
driver_stats_source, | ||
driver_stats_fv, | ||
test_view, | ||
] | ||
) | ||
self.store.write_to_online_store( | ||
feature_view_name="driver_hourly_stats", df=driver_df | ||
) | ||
# This will give the intuitive structure of the data as: | ||
# {"driver_id": [..], "conv_rate": [..], "acc_rate": [..], "avg_daily_trips": [..]} | ||
driver_dict = driver_df.to_dict(orient="list") | ||
self.store.write_to_online_store( | ||
feature_view_name="driver_hourly_stats", | ||
inputs=driver_dict, | ||
) | ||
|
||
def test_online_retrieval(self): | ||
entity_rows = [ | ||
{ | ||
"driver_id": 1001, | ||
} | ||
] | ||
|
||
online_python_response = self.store.get_online_features( | ||
entity_rows=entity_rows, | ||
features=[ | ||
"driver_hourly_stats:conv_rate", | ||
"driver_hourly_stats:acc_rate", | ||
"test_view:conv_rate_plus_acc", | ||
], | ||
).to_dict() | ||
|
||
assert len(online_python_response) == 4 | ||
assert all( | ||
key in online_python_response.keys() | ||
for key in [ | ||
"driver_id", | ||
"acc_rate", | ||
"conv_rate", | ||
"conv_rate_plus_acc", | ||
] | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tokoko updated