Skip to content

Commit 56b0620

Browse files
committed
feat: Optional input_schema for ODFV (feast-dev#6308)
Signed-off-by: Nick Quinn <[email protected]>
1 parent a310eaf commit 56b0620

3 files changed

Lines changed: 233 additions & 13 deletions

File tree

‎sdk/python/feast/aggregation/__init__.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ def __init__(
3737
time_window: Optional[timedelta] = None,
3838
slide_interval: Optional[timedelta] = None,
3939
name: Optional[str] = None,
40+
output: Optional[str] = None,
41+
window: Optional[timedelta] = None,
4042
):
4143
self.column = column or ""
4244
self.function = function or ""
43-
self.time_window = time_window
45+
self.time_window = window if window is not None else time_window
4446
if not slide_interval:
4547
self.slide_interval = self.time_window
4648
else:
4749
self.slide_interval = slide_interval
48-
self.name = name or ""
50+
self.name = output or name or ""
4951

5052
def to_proto(self) -> AggregationProto:
5153
window_duration = None

‎sdk/python/feast/on_demand_feature_view.py‎

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class OnDemandFeatureView(BaseFeatureView):
134134
"""
135135

136136
_TRACK_METRICS_TAG = "feast:track_metrics"
137+
_INPUT_SCHEMA_SOURCE_PREFIX = "__input_schema__"
137138

138139
name: str
139140
entities: Optional[List[str]]
@@ -158,7 +159,8 @@ def __init__( # noqa: C901
158159
name: str,
159160
entities: Optional[List[Entity]] = None,
160161
schema: Optional[List[Field]] = None,
161-
sources: List[OnDemandSourceType],
162+
sources: Optional[List[OnDemandSourceType]] = None,
163+
input_schema: Optional[List[Field]] = None,
162164
udf: Optional[FunctionType] = None,
163165
udf_string: Optional[str] = "",
164166
feature_transformation: Optional[Transformation] = None,
@@ -183,6 +185,9 @@ def __init__( # noqa: C901
183185
sources: A map from input source names to the actual input sources, which may be
184186
feature views, or request data sources. These sources serve as inputs to the udf,
185187
which will refer to them by name.
188+
input_schema (optional): A list of Fields describing the schema of the input data
189+
for aggregation-based views. When provided, sources is not required — an
190+
internal RequestSource will be created automatically.
186191
udf: The user defined transformation function, which must take pandas
187192
dataframes as inputs.
188193
udf_string: The source code version of the udf (for diffing and displaying in Web UI)
@@ -214,15 +219,39 @@ def __init__( # noqa: C901
214219
self.version = version
215220
schema = schema or []
216221
self.entities = [e.name for e in entities] if entities else [DUMMY_ENTITY_NAME]
217-
self.sources = sources
222+
self.input_schema = input_schema
218223
self.mode = mode.lower()
219224
self.udf = udf
220225
self.udf_string = udf_string
221226
self.source_feature_view_projections: dict[str, FeatureViewProjection] = {}
222227
self.source_request_sources: dict[str, RequestSource] = {}
223228

229+
# Strip any existing sentinel from sources (handles __copy__ round-trip)
230+
effective_sources: List[OnDemandSourceType] = [
231+
s
232+
for s in (sources or [])
233+
if not (
234+
isinstance(s, RequestSource)
235+
and s.name.startswith(self._INPUT_SCHEMA_SOURCE_PREFIX)
236+
)
237+
]
238+
239+
if input_schema is not None:
240+
# Automatically create an internal RequestSource from input_schema
241+
sentinel = RequestSource(
242+
name=f"{self._INPUT_SCHEMA_SOURCE_PREFIX}{name}",
243+
schema=input_schema,
244+
)
245+
self.source_request_sources[sentinel.name] = sentinel
246+
elif not effective_sources:
247+
raise ValueError(
248+
"Either 'sources' or 'input_schema' must be provided for OnDemandFeatureView."
249+
)
250+
251+
self.sources = effective_sources
252+
224253
# Process each source with explicit type handling
225-
for odfv_source in sources:
254+
for odfv_source in effective_sources:
226255
self._add_source_to_collections(odfv_source)
227256

228257
features: List[Field] = []
@@ -328,6 +357,7 @@ def __copy__(self):
328357
schema=self.features,
329358
sources=list(self.source_feature_view_projections.values())
330359
+ list(self.source_request_sources.values()),
360+
input_schema=self.input_schema,
331361
feature_transformation=self.feature_transformation,
332362
mode=self.mode,
333363
description=self.description,
@@ -337,6 +367,7 @@ def __copy__(self):
337367
singleton=self.singleton,
338368
version=self.version,
339369
track_metrics=self.track_metrics,
370+
aggregations=self.aggregations,
340371
)
341372
fv.entities = self.entities
342373
fv.features = self.features
@@ -559,7 +590,7 @@ def to_proto(self) -> OnDemandFeatureViewProto:
559590
owner=self.owner,
560591
write_to_online_store=self.write_to_online_store,
561592
singleton=self.singleton or False,
562-
aggregations=self.aggregations,
593+
aggregations=[agg.to_proto() for agg in self.aggregations],
563594
version=self.version,
564595
)
565596
return OnDemandFeatureViewProto(spec=spec, meta=meta)
@@ -585,6 +616,19 @@ def from_proto(
585616
on_demand_feature_view_proto, skip_udf=skip_udf
586617
)
587618

619+
# Detect and strip input_schema sentinel from sources
620+
input_schema: Optional[List[Field]] = None
621+
sources_without_sentinel: List[OnDemandSourceType] = []
622+
for source in sources:
623+
if (
624+
isinstance(source, RequestSource)
625+
and source.name.startswith(cls._INPUT_SCHEMA_SOURCE_PREFIX)
626+
):
627+
input_schema = source.schema
628+
else:
629+
sources_without_sentinel.append(source)
630+
sources = sources_without_sentinel
631+
588632
# Parse transformation from proto (skip UDF deserialization if requested)
589633
transformation = cls._parse_transformation_from_proto(
590634
on_demand_feature_view_proto, skip_udf=skip_udf
@@ -607,6 +651,7 @@ def from_proto(
607651
name=on_demand_feature_view_proto.spec.name,
608652
schema=cls._parse_features_from_proto(on_demand_feature_view_proto),
609653
sources=cast(List[OnDemandSourceType], sources),
654+
input_schema=input_schema,
610655
feature_transformation=transformation,
611656
mode=on_demand_feature_view_proto.spec.mode or "pandas",
612657
description=on_demand_feature_view_proto.spec.description,
@@ -1092,7 +1137,7 @@ def _is_array_type(self, dtype) -> bool:
10921137
"""Check if the dtype represents an array type."""
10931138
# Use proper type checking instead of string comparison
10941139
dtype_str = str(dtype)
1095-
return "Array" in dtype_str or "List" in dtype_str
1140+
return "Array" in dtype_str or "List" in dtype_str or "Set" in dtype_str
10961141

10971142
def _construct_random_input(
10981143
self, singleton: bool = False
@@ -1224,13 +1269,17 @@ def on_demand_feature_view(
12241269
name: Optional[str] = None,
12251270
entities: Optional[List[Entity]] = None,
12261271
schema: list[Field],
1227-
sources: list[
1228-
Union[
1229-
FeatureView,
1230-
RequestSource,
1231-
FeatureViewProjection,
1272+
sources: Optional[
1273+
list[
1274+
Union[
1275+
FeatureView,
1276+
RequestSource,
1277+
FeatureViewProjection,
1278+
]
12321279
]
1233-
],
1280+
] = None,
1281+
input_schema: Optional[list[Field]] = None,
1282+
aggregations: Optional[List[Aggregation]] = None,
12341283
mode: str = "pandas",
12351284
description: str = "",
12361285
tags: Optional[dict[str, str]] = None,
@@ -1252,6 +1301,8 @@ def on_demand_feature_view(
12521301
sources: A map from input source names to the actual input sources, which may be
12531302
feature views, or request data sources. These sources serve as inputs to the udf,
12541303
which will refer to them by name.
1304+
input_schema (optional): A list of Fields describing the schema of the input data
1305+
for aggregation-based views. When provided, sources is not required.
12551306
mode: The mode of execution (e.g,. Pandas or Python Native)
12561307
description (optional): A human-readable description.
12571308
tags (optional): A dictionary of key-value pairs to store arbitrary metadata.
@@ -1279,6 +1330,7 @@ def decorator(user_function):
12791330
on_demand_feature_view_obj = OnDemandFeatureView(
12801331
name=name if name is not None else user_function.__name__,
12811332
sources=sources,
1333+
input_schema=input_schema,
12821334
schema=schema,
12831335
mode=mode,
12841336
description=description,
@@ -1288,6 +1340,7 @@ def decorator(user_function):
12881340
entities=entities,
12891341
singleton=singleton,
12901342
track_metrics=track_metrics,
1343+
aggregations=aggregations,
12911344
udf=user_function,
12921345
udf_string=udf_string,
12931346
version=version,
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Copyright 2025 The Feast Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests for OnDemandFeatureView input_schema support."""
16+
17+
import copy
18+
from datetime import timedelta
19+
20+
import pandas as pd
21+
import pytest
22+
23+
from feast import Entity, Field
24+
from feast.aggregation import Aggregation
25+
from feast.on_demand_feature_view import OnDemandFeatureView, on_demand_feature_view
26+
from feast.types import Float64, Int64
27+
from feast.value_type import ValueType
28+
29+
user = Entity(name="user", join_keys=["user_id"], value_type=ValueType.INT64)
30+
31+
32+
def test_decorator_with_input_schema():
33+
"""The @on_demand_feature_view decorator supports input_schema without sources."""
34+
35+
@on_demand_feature_view(
36+
input_schema=[
37+
Field(name="txn_amount", dtype=Float64),
38+
],
39+
schema=[
40+
Field(name="txn_count", dtype=Int64),
41+
Field(name="total_txn_amount", dtype=Float64),
42+
Field(name="avg_txn_amount", dtype=Float64),
43+
],
44+
aggregations=[
45+
Aggregation(
46+
column="txn_amount",
47+
function="count",
48+
output="txn_count",
49+
window=timedelta(days=30),
50+
),
51+
Aggregation(
52+
column="txn_amount",
53+
function="sum",
54+
output="total_txn_amount",
55+
window=timedelta(days=30),
56+
),
57+
Aggregation(
58+
column="txn_amount",
59+
function="mean",
60+
output="avg_txn_amount",
61+
window=timedelta(days=30),
62+
),
63+
],
64+
entities=[user],
65+
)
66+
def compute_txn_stats(df: pd.DataFrame) -> pd.DataFrame:
67+
return df
68+
69+
assert isinstance(compute_txn_stats, OnDemandFeatureView)
70+
assert compute_txn_stats.name == "compute_txn_stats"
71+
assert compute_txn_stats.input_schema == [Field(name="txn_amount", dtype=Float64)]
72+
assert len(compute_txn_stats.aggregations) == 3
73+
assert len(compute_txn_stats.features) == 3
74+
75+
# The internal sentinel RequestSource should be present
76+
sentinel_name = f"{OnDemandFeatureView._INPUT_SCHEMA_SOURCE_PREFIX}compute_txn_stats"
77+
assert sentinel_name in compute_txn_stats.source_request_sources
78+
79+
# sources (user-visible) should be empty
80+
assert compute_txn_stats.sources == []
81+
82+
83+
def test_aggregation_aliases():
84+
"""Aggregation accepts 'output' alias for 'name' and 'window' alias for 'time_window'."""
85+
agg = Aggregation(
86+
column="txn_amount",
87+
function="sum",
88+
output="total_txn_amount",
89+
window=timedelta(days=30),
90+
)
91+
assert agg.name == "total_txn_amount"
92+
assert agg.time_window == timedelta(days=30)
93+
94+
95+
def test_input_schema_proto_roundtrip():
96+
"""An ODFV with input_schema survives a to_proto / from_proto round-trip."""
97+
98+
@on_demand_feature_view(
99+
input_schema=[
100+
Field(name="txn_amount", dtype=Float64),
101+
],
102+
schema=[
103+
Field(name="total_txn_amount", dtype=Float64),
104+
],
105+
aggregations=[
106+
Aggregation(
107+
column="txn_amount",
108+
function="sum",
109+
output="total_txn_amount",
110+
window=timedelta(days=30),
111+
),
112+
],
113+
entities=[user],
114+
)
115+
def txn_view(df: pd.DataFrame) -> pd.DataFrame:
116+
return df
117+
118+
proto = txn_view.to_proto()
119+
restored = OnDemandFeatureView.from_proto(proto)
120+
121+
assert restored.input_schema == txn_view.input_schema
122+
assert restored.aggregations == txn_view.aggregations
123+
sentinel_name = f"{OnDemandFeatureView._INPUT_SCHEMA_SOURCE_PREFIX}txn_view"
124+
assert sentinel_name in restored.source_request_sources
125+
126+
127+
def test_input_schema_copy():
128+
"""__copy__ preserves input_schema and aggregations."""
129+
130+
@on_demand_feature_view(
131+
input_schema=[
132+
Field(name="txn_amount", dtype=Float64),
133+
],
134+
schema=[
135+
Field(name="total_txn_amount", dtype=Float64),
136+
],
137+
aggregations=[
138+
Aggregation(column="txn_amount", function="sum", output="total_txn_amount"),
139+
],
140+
entities=[user],
141+
)
142+
def copy_view(df: pd.DataFrame) -> pd.DataFrame:
143+
return df
144+
145+
cloned = copy.copy(copy_view)
146+
assert cloned.input_schema == copy_view.input_schema
147+
assert cloned.aggregations == copy_view.aggregations
148+
sentinel_name = f"{OnDemandFeatureView._INPUT_SCHEMA_SOURCE_PREFIX}copy_view"
149+
assert sentinel_name in cloned.source_request_sources
150+
151+
152+
def test_sources_required_without_input_schema():
153+
"""Constructor raises if neither sources nor input_schema is provided."""
154+
with pytest.raises(
155+
(ValueError, TypeError),
156+
):
157+
158+
def dummy(df):
159+
return df
160+
161+
OnDemandFeatureView(
162+
name="bad_view",
163+
schema=[Field(name="out", dtype=Float64)],
164+
udf=dummy,
165+
)

0 commit comments

Comments
 (0)