Skip to content
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: add string_json value type for #4897

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/python/feast/on_demand_feature_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ def _construct_random_input(
ValueType.FLOAT_LIST: [[1.0]],
ValueType.BOOL_LIST: [[True]],
ValueType.UNIX_TIMESTAMP_LIST: [[_utc_now()]],
ValueType.STRING_JSON: [{"0": "hello world"}],
}
if singleton:
rand_dict_value = {k: rand_dict_value[k][0] for k in rand_dict_value}
Expand Down
1 change: 1 addition & 0 deletions sdk/python/feast/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def __str__(self):
ValueType.FLOAT_LIST: Array(Float32),
ValueType.BOOL_LIST: Array(Bool),
ValueType.UNIX_TIMESTAMP_LIST: Array(UnixTimestamp),
ValueType.STRING_JSON: String
}

FEAST_TYPES_TO_PYARROW_TYPES = {
Expand Down
1 change: 1 addition & 0 deletions sdk/python/feast/value_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ValueType(enum.Enum):
BOOL_LIST = 17
UNIX_TIMESTAMP_LIST = 18
NULL = 19
STRING_JSON = 20


ListType = Union[
Expand Down
53 changes: 53 additions & 0 deletions sdk/python/tests/unit/test_on_demand_feature_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
PythonTransformation,
)
from feast.types import Float32
from feast.value_type import ValueType


def udf1(features_df: pd.DataFrame) -> pd.DataFrame:
Expand Down Expand Up @@ -356,3 +357,55 @@ def test_on_demand_feature_view_stored_writes():
assert transformed_output["output3"] is not None and isinstance(
transformed_output["output3"], datetime.datetime
)


def test_on_demand_feature_view_string_json():
file_source = FileSource(name="my-file-source", path="test.parquet")
feature_view = FeatureView(
name="driver_hourly_stats_view",
entities=[],
schema=[Field(name="feature1", dtype=ValueType.STRING_JSON)],
source=file_source
)

sources = [feature_view]

def udf_string_json(inputs: pd.DataFrame) -> pd.DataFrame:
df = pd.DataFrame()

df['output1'] = 0.0
for index, row in inputs.iterrows():
value_array = eval(row["feature1"])
x1 = value_array.get("1", 0.0)
x1 = x1 if x1 is not None else 0.0
x2 = value_array.get("2", 0.0)
x2 = x2 if x2 is not None else 0.0
df.at[index, 'output1'] = x1 + x2
return df


on_demand_feature_view = OnDemandFeatureView(
name="my-on-demand-feature-view",
sources=sources,
schema=[
Field(name="output1", dtype=Float32),
],
feature_transformation=PandasTransformation(
udf=udf_string_json, udf_string="udf_string_json source code"
),
)

transformed_output = on_demand_feature_view.transform_dict(
{
"feature1": '{"1": 1.0, "2": 2.0, "3": 3.0}'
}
)
expected_output = {"feature1": '{"1": 1.0, "2": 2.0, "3": 3.0}', "output1": 3.0}
keys_to_validate = [
"feature1",
"output1",
]
for k in keys_to_validate:
assert transformed_output[k] == expected_output[k]


Loading