Repository navigation
fix: Compare the transformation in BatchFeatureView equality #7240
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |
| import functools | ||
| import warnings | ||
| from datetime import datetime, timedelta | ||
| from types import CodeType | ||
| from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast | ||
|
|
||
| import dill | ||
| import numpy as np | ||
|
|
||
| from feast import flags_helper | ||
| from feast.aggregation import Aggregation | ||
|
|
@@ -201,6 +203,25 @@ def __copy__(self) -> "BatchFeatureView": | |
| fv._raw_feature_transformation_proto = self._raw_feature_transformation_proto | ||
| return fv | ||
|
|
||
| def __eq__(self, other): | ||
| if not super().__eq__(other): | ||
| return False | ||
|
|
||
| # FeatureView.__eq__ does not compare the transformation, so without | ||
| # this a view with a changed UDF equals the stored one. | ||
| raw = self._raw_feature_transformation_proto | ||
| other_raw = getattr(other, "_raw_feature_transformation_proto", None) | ||
| if raw is not None or other_raw is not None: | ||
| # Loaded with skip_udf=True: only the proto is available. | ||
| return raw == other_raw | ||
| return not _transformation_changed( | ||
| self.feature_transformation, | ||
| getattr(other, "feature_transformation", None), | ||
| ) | ||
|
Comment on lines
+217
to
+220
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 False pin conflicts for unchanged UDFs When a UDF has no source text, Learn moreThe constructor accepts a callable with no source string. Feast serializes that callable and restores it when loading the active view. The new equality check sends these functions through _udf_changed, which compares whole code objects and captured values. Code-object equality includes the first line number. Tuple equality also needs a single truth value for each capture, which a multi-element NumPy array comparison cannot supply. The helper reports a change in either case, so the pin guard rejects the rollback even though its transformation did not change. Example: Declare a SQL UDF in a factory without Recommended fix: Compare executable code and constants without source positions, including for nested code objects. Compare supported array captures and defaults explicitly by shape, dtype, and contents. Keep regression tests for changed constants, defaults, and closures so the pin guard still rejects real edits. Was this helpful? React with 👍 or 👎 to provide feedback.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed and fixed in 565de02. For a UDF without
Added |
||
|
|
||
| def __hash__(self) -> int: | ||
| return super().__hash__() | ||
|
|
||
| def _schema_or_udf_changed(self, other: BaseFeatureView) -> bool: | ||
| """Check for BatchFeatureView schema/UDF changes.""" | ||
| if super()._schema_or_udf_changed(other): | ||
|
|
@@ -254,25 +275,57 @@ def _udf_changed(new_udf: Callable, old_udf: Callable) -> bool: | |
| Bytecode alone misses constants: ``lambda _: "SELECT 1"`` and | ||
| ``lambda _: "SELECT 2"`` share it. The code object also holds the | ||
| constants, so compare it whole, plus the default arguments and the values | ||
| captured in the closure. | ||
| captured in the closure. Source positions are left out, so a UDF that only | ||
| moved within its file is not a change. | ||
| """ | ||
|
|
||
| def state(udf: Callable) -> tuple: | ||
| closure = tuple(cell.cell_contents for cell in udf.__closure__ or ()) # type: ignore[attr-defined] | ||
| return ( | ||
| udf.__code__, # type: ignore[attr-defined] | ||
| _without_positions(udf.__code__), # type: ignore[attr-defined] | ||
| udf.__defaults__, # type: ignore[attr-defined] | ||
| udf.__kwdefaults__, # type: ignore[attr-defined] | ||
| closure, | ||
| ) | ||
|
|
||
| try: | ||
| return bool(state(new_udf) != state(old_udf)) | ||
| return not _same_value(state(new_udf), state(old_udf)) | ||
| except Exception: | ||
| # Values that cannot be compared count as a change, so the new UDF is stored. | ||
| return True | ||
|
|
||
|
|
||
| def _without_positions(code: CodeType) -> CodeType: | ||
| """Clear the source positions of a code object and the code nested in it.""" | ||
| consts = tuple( | ||
| _without_positions(const) if isinstance(const, CodeType) else const | ||
| for const in code.co_consts | ||
| ) | ||
| return code.replace(co_firstlineno=1, co_linetable=b"", co_consts=consts) | ||
|
|
||
|
|
||
| def _same_value(a: Any, b: Any) -> bool: | ||
| """Compare like ``==``, but compare NumPy arrays by shape, dtype and contents. | ||
|
|
||
| ``==`` on two arrays gives an array, which has no single truth value, and a | ||
| UDF loaded back from the registry holds its own copy of each captured array. | ||
| """ | ||
| if a is b: | ||
| return True | ||
| if isinstance(a, np.ndarray) or isinstance(b, np.ndarray): | ||
| return ( | ||
| type(a) is type(b) | ||
| and a.shape == b.shape | ||
| and a.dtype == b.dtype | ||
| and bool(np.array_equal(a, b, equal_nan=a.dtype.kind in "fc")) | ||
| ) | ||
| if isinstance(a, (tuple, list)) and type(a) is type(b): | ||
| return len(a) == len(b) and all(map(_same_value, a, b)) | ||
| if isinstance(a, dict) and type(a) is type(b): | ||
| return a.keys() == b.keys() and all(_same_value(a[k], b[k]) for k in a) | ||
| return bool(a == b) | ||
|
|
||
|
|
||
| def batch_feature_view( | ||
| *, | ||
| name: Optional[str] = None, | ||
|
|
||
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.
🔴 Repeat apply fails after rollback
After a successful rollback to a different UDF,
BatchFeatureView.__eq__rejects the same pin declaration on the next apply. The pin check compares the declaration with the restored snapshot, not the definition it accepted before the rollback.Learn more
To roll back a feature view, an operator changes only its version in the repository. The file registry and SQL registry validate that declaration against the active version, then replace the active definition with the older snapshot. They do not retain the declaration they accepted. On the next apply, the declaration is unchanged but the comparison target is now the older snapshot. Before this change, a UDF-only difference did not reject the repeat apply.
Example: Register v0 with UDF A, then v1 with UDF B. Set
version="v0"without changing UDF B. The first apply activates A; the second apply of the same repository raisesFeatureViewPinConflict.Recommended fix: Make an exact repeat of an accepted pin a no-op in both registries. Retain the validated pin declaration or its stable fingerprint so the check can distinguish a repeat from a newly edited declaration. Do not bypass the guard solely because the requested version is already active, since that would silently discard new edits. Add a regression that repeats the successful pin and then attempts the same pin with a third UDF.
Was this helpful? React with 👍 or 👎 to provide feedback.
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.
Reproduced on the file registry and a SQLite
SqlRegistry. For a UDF-only rollback this is right: afterversion="v0"restores v0, applying the same declaration again raisesFeatureViewPinConflict, where master accepted it.The pin check already behaves this way on master for every field it compares. With v0 and v1 differing only in schema (plain FeatureView or BatchFeatureView), the first pin restores v0 and the identical second apply raises
FeatureViewPinConflicton both registries:FeatureViewPinConflictFeatureViewPinConflictFeatureViewPinConflictFeatureViewPinConflictFeatureViewPinConflictFeatureViewPinConflictThe check compares the declaration with the active definition and does not keep the declaration it accepted, so it cannot tell a repeat from a new edit. Now that the UDF is compared, a UDF-only rollback behaves like a schema rollback. Keeping the accepted pin (or a fingerprint of it) would change the pin flow for all feature view types in both registries, so I left it out of this PR. I can open a follow-up for that if you want it.