Skip to content

Commit 47a8d31

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

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] = []
@@ -325,6 +354,7 @@ def __copy__(self):
325354
schema=self.features,
326355
sources=list(self.source_feature_view_projections.values())
327356
+ list(self.source_request_sources.values()),
357+
input_schema=self.input_schema,
328358
feature_transformation=self.feature_transformation,
329359
mode=self.mode,
330360
description=self.description,
@@ -334,6 +364,7 @@ def __copy__(self):
334364
singleton=self.singleton,
335365
version=self.version,
336366
track_metrics=self.track_metrics,
367+
aggregations=self.aggregations,
337368
)
338369
fv.entities = self.entities
339370
fv.features = self.features
@@ -556,7 +587,7 @@ def to_proto(self) -> OnDemandFeatureViewProto:
556587
owner=self.owner,
557588
write_to_online_store=self.write_to_online_store,
558589
singleton=self.singleton or False,
559-
aggregations=self.aggregations,
590+
aggregations=[agg.to_proto() for agg in self.aggregations],
560591
version=self.version,
561592
)
562593
return OnDemandFeatureViewProto(spec=spec, meta=meta)
@@ -580,6 +611,19 @@ def from_proto(
580611
# Parse sources from proto
581612
sources = cls._parse_sources_from_proto(on_demand_feature_view_proto)
582613

614+
# Detect and strip input_schema sentinel from sources
615+
input_schema: Optional[List[Field]] = None
616+
sources_without_sentinel: List[OnDemandSourceType] = []
617+
for source in sources:
618+
if (
619+
isinstance(source, RequestSource)
620+
and source.name.startswith(cls._INPUT_SCHEMA_SOURCE_PREFIX)
621+
):
622+
input_schema = source.schema
623+
else:
624+
sources_without_sentinel.append(source)
625+
sources = sources_without_sentinel
626+
583627
# Parse transformation from proto
584628
transformation = cls._parse_transformation_from_proto(
585629
on_demand_feature_view_proto
@@ -602,6 +646,7 @@ def from_proto(
602646
name=on_demand_feature_view_proto.spec.name,
603647
schema=cls._parse_features_from_proto(on_demand_feature_view_proto),
604648
sources=cast(List[OnDemandSourceType], sources),
649+
input_schema=input_schema,
605650
feature_transformation=transformation,
606651
mode=on_demand_feature_view_proto.spec.mode or "pandas",
607652
description=on_demand_feature_view_proto.spec.description,
@@ -1079,7 +1124,7 @@ def _is_array_type(self, dtype) -> bool:
10791124
"""Check if the dtype represents an array type."""
10801125
# Use proper type checking instead of string comparison
10811126
dtype_str = str(dtype)
1082-
return "Array" in dtype_str or "List" in dtype_str
1127+
return "Array" in dtype_str or "List" in dtype_str or "Set" in dtype_str
10831128

10841129
def _construct_random_input(
10851130
self, singleton: bool = False
@@ -1211,13 +1256,17 @@ def on_demand_feature_view(
12111256
name: Optional[str] = None,
12121257
entities: Optional[List[Entity]] = None,
12131258
schema: list[Field],
1214-
sources: list[
1215-
Union[
1216-
FeatureView,
1217-
RequestSource,
1218-
FeatureViewProjection,
1259+
sources: Optional[
1260+
list[
1261+
Union[
1262+
FeatureView,
1263+
RequestSource,
1264+
FeatureViewProjection,
1265+
]
12191266
]
1220-
],
1267+
] = None,
1268+
input_schema: Optional[list[Field]] = None,
1269+
aggregations: Optional[List[Aggregation]] = None,
12211270
mode: str = "pandas",
12221271
description: str = "",
12231272
tags: Optional[dict[str, str]] = None,
@@ -1239,6 +1288,8 @@ def on_demand_feature_view(
12391288
sources: A map from input source names to the actual input sources, which may be
12401289
feature views, or request data sources. These sources serve as inputs to the udf,
12411290
which will refer to them by name.
1291+
input_schema (optional): A list of Fields describing the schema of the input data
1292+
for aggregation-based views. When provided, sources is not required.
12421293
mode: The mode of execution (e.g,. Pandas or Python Native)
12431294
description (optional): A human-readable description.
12441295
tags (optional): A dictionary of key-value pairs to store arbitrary metadata.
@@ -1266,6 +1317,7 @@ def decorator(user_function):
12661317
on_demand_feature_view_obj = OnDemandFeatureView(
12671318
name=name if name is not None else user_function.__name__,
12681319
sources=sources,
1320+
input_schema=input_schema,
12691321
schema=schema,
12701322
mode=mode,
12711323
description=description,
@@ -1275,6 +1327,7 @@ def decorator(user_function):
12751327
entities=entities,
12761328
singleton=singleton,
12771329
track_metrics=track_metrics,
1330+
aggregations=aggregations,
12781331
udf=user_function,
12791332
udf_string=udf_string,
12801333
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)