|
| 1 | +# Copyright 2021 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 | +import warnings |
| 15 | +from abc import ABC, abstractmethod |
| 16 | +from typing import List, Type |
| 17 | + |
| 18 | +from google.protobuf.json_format import MessageToJson |
| 19 | +from proto import Message |
| 20 | + |
| 21 | +from feast.feature import Feature |
| 22 | +from feast.feature_view_projection import FeatureViewProjection |
| 23 | + |
| 24 | +warnings.simplefilter("once", DeprecationWarning) |
| 25 | + |
| 26 | + |
| 27 | +class BaseFeatureView(ABC): |
| 28 | + """A FeatureView defines a logical grouping of features to be served.""" |
| 29 | + |
| 30 | + @abstractmethod |
| 31 | + def __init__(self, name: str, features: List[Feature]): |
| 32 | + self._name = name |
| 33 | + self._features = features |
| 34 | + self._projection = FeatureViewProjection.from_definition(self) |
| 35 | + |
| 36 | + @property |
| 37 | + def name(self) -> str: |
| 38 | + return self._name |
| 39 | + |
| 40 | + @property |
| 41 | + def features(self) -> List[Feature]: |
| 42 | + return self._features |
| 43 | + |
| 44 | + @features.setter |
| 45 | + def features(self, value): |
| 46 | + self._features = value |
| 47 | + |
| 48 | + @property |
| 49 | + def projection(self) -> FeatureViewProjection: |
| 50 | + return self._projection |
| 51 | + |
| 52 | + @projection.setter |
| 53 | + def projection(self, value): |
| 54 | + self._projection = value |
| 55 | + |
| 56 | + @property |
| 57 | + @abstractmethod |
| 58 | + def proto_class(self) -> Type[Message]: |
| 59 | + pass |
| 60 | + |
| 61 | + @abstractmethod |
| 62 | + def to_proto(self) -> Message: |
| 63 | + pass |
| 64 | + |
| 65 | + @classmethod |
| 66 | + @abstractmethod |
| 67 | + def from_proto(cls, feature_view_proto): |
| 68 | + pass |
| 69 | + |
| 70 | + @abstractmethod |
| 71 | + def __copy__(self): |
| 72 | + """ |
| 73 | + Generates a deep copy of this feature view |
| 74 | +
|
| 75 | + Returns: |
| 76 | + A copy of this FeatureView |
| 77 | + """ |
| 78 | + pass |
| 79 | + |
| 80 | + def __repr__(self): |
| 81 | + items = (f"{k} = {v}" for k, v in self.__dict__.items()) |
| 82 | + return f"<{self.__class__.__name__}({', '.join(items)})>" |
| 83 | + |
| 84 | + def __str__(self): |
| 85 | + return str(MessageToJson(self.to_proto())) |
| 86 | + |
| 87 | + def __hash__(self): |
| 88 | + return hash((id(self), self.name)) |
| 89 | + |
| 90 | + def __getitem__(self, item): |
| 91 | + assert isinstance(item, list) |
| 92 | + |
| 93 | + referenced_features = [] |
| 94 | + for feature in self.features: |
| 95 | + if feature.name in item: |
| 96 | + referenced_features.append(feature) |
| 97 | + |
| 98 | + cp = self.__copy__() |
| 99 | + cp.projection.features = referenced_features |
| 100 | + |
| 101 | + return cp |
| 102 | + |
| 103 | + def __eq__(self, other): |
| 104 | + if not isinstance(other, BaseFeatureView): |
| 105 | + raise TypeError( |
| 106 | + "Comparisons should only involve BaseFeatureView class objects." |
| 107 | + ) |
| 108 | + |
| 109 | + if self.name != other.name: |
| 110 | + return False |
| 111 | + |
| 112 | + if sorted(self.features) != sorted(other.features): |
| 113 | + return False |
| 114 | + |
| 115 | + return True |
| 116 | + |
| 117 | + def ensure_valid(self): |
| 118 | + """ |
| 119 | + Validates the state of this feature view locally. |
| 120 | +
|
| 121 | + Raises: |
| 122 | + ValueError: The feature view is invalid. |
| 123 | + """ |
| 124 | + if not self.name: |
| 125 | + raise ValueError("Feature view needs a name.") |
| 126 | + |
| 127 | + def with_name(self, name: str): |
| 128 | + """ |
| 129 | + Renames this feature view by returning a copy of this feature view with an alias |
| 130 | + set for the feature view name. This rename operation is only used as part of query |
| 131 | + operations and will not modify the underlying FeatureView. |
| 132 | +
|
| 133 | + Args: |
| 134 | + name: Name to assign to the FeatureView copy. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + A copy of this FeatureView with the name replaced with the 'name' input. |
| 138 | + """ |
| 139 | + cp = self.__copy__() |
| 140 | + cp.projection.name_alias = name |
| 141 | + |
| 142 | + return cp |
| 143 | + |
| 144 | + def set_projection(self, feature_view_projection: FeatureViewProjection) -> None: |
| 145 | + """ |
| 146 | + Setter for the projection object held by this FeatureView. A projection is an |
| 147 | + object that stores the modifications to a FeatureView that is applied to the FeatureView |
| 148 | + when the FeatureView is used such as during feature_store.get_historical_features. |
| 149 | + This method also performs checks to ensure the projection is consistent with this |
| 150 | + FeatureView before doing the set. |
| 151 | +
|
| 152 | + Args: |
| 153 | + feature_view_projection: The FeatureViewProjection object to set this FeatureView's |
| 154 | + 'projection' field to. |
| 155 | + """ |
| 156 | + if feature_view_projection.name != self.name: |
| 157 | + raise ValueError( |
| 158 | + f"The projection for the {self.name} FeatureView cannot be applied because it differs in name. " |
| 159 | + f"The projection is named {feature_view_projection.name} and the name indicates which " |
| 160 | + "FeatureView the projection is for." |
| 161 | + ) |
| 162 | + |
| 163 | + for feature in feature_view_projection.features: |
| 164 | + if feature not in self.features: |
| 165 | + raise ValueError( |
| 166 | + f"The projection for {self.name} cannot be applied because it contains {feature.name} which the " |
| 167 | + "FeatureView doesn't have." |
| 168 | + ) |
| 169 | + |
| 170 | + self.projection = feature_view_projection |
| 171 | + |
| 172 | + def with_projection(self, feature_view_projection: FeatureViewProjection): |
| 173 | + """ |
| 174 | + Sets the feature view projection by returning a copy of this on-demand feature view |
| 175 | + with its projection set to the given projection. A projection is an |
| 176 | + object that stores the modifications to a feature view that is used during |
| 177 | + query operations. |
| 178 | +
|
| 179 | + Args: |
| 180 | + feature_view_projection: The FeatureViewProjection object to link to this |
| 181 | + OnDemandFeatureView. |
| 182 | +
|
| 183 | + Returns: |
| 184 | + A copy of this OnDemandFeatureView with its projection replaced with the |
| 185 | + 'feature_view_projection' argument. |
| 186 | + """ |
| 187 | + if feature_view_projection.name != self.name: |
| 188 | + raise ValueError( |
| 189 | + f"The projection for the {self.name} FeatureView cannot be applied because it differs in name. " |
| 190 | + f"The projection is named {feature_view_projection.name} and the name indicates which " |
| 191 | + "FeatureView the projection is for." |
| 192 | + ) |
| 193 | + |
| 194 | + for feature in feature_view_projection.features: |
| 195 | + if feature not in self.features: |
| 196 | + raise ValueError( |
| 197 | + f"The projection for {self.name} cannot be applied because it contains {feature.name} which the " |
| 198 | + "FeatureView doesn't have." |
| 199 | + ) |
| 200 | + |
| 201 | + cp = self.__copy__() |
| 202 | + cp.projection = feature_view_projection |
| 203 | + |
| 204 | + return cp |
0 commit comments