forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_table.py
More file actions
415 lines (349 loc) · 12.3 KB
/
feature_table.py
File metadata and controls
415 lines (349 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# Copyright 2020 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, List, MutableMapping, Optional, Union
import yaml
from google.protobuf import json_format
from google.protobuf.duration_pb2 import Duration
from google.protobuf.json_format import MessageToDict, MessageToJson
from google.protobuf.timestamp_pb2 import Timestamp
from feast.data_source import (
BigQuerySource,
DataSource,
FileSource,
KafkaSource,
KinesisSource,
)
from feast.feature import Feature
from feast.loaders import yaml as feast_yaml
from feast.protos.feast.core.FeatureTable_pb2 import FeatureTable as FeatureTableProto
from feast.protos.feast.core.FeatureTable_pb2 import (
FeatureTableMeta as FeatureTableMetaProto,
)
from feast.protos.feast.core.FeatureTable_pb2 import (
FeatureTableSpec as FeatureTableSpecProto,
)
from feast.value_type import ValueType
class FeatureTable:
"""
Represents a collection of features and associated metadata.
"""
def __init__(
self,
name: str,
entities: List[str],
features: List[Feature],
batch_source: Union[BigQuerySource, FileSource] = None,
stream_source: Optional[Union[KafkaSource, KinesisSource]] = None,
max_age: Optional[Duration] = None,
labels: Optional[MutableMapping[str, str]] = None,
):
self._name = name
self._entities = entities
self._features = features
self._batch_source = batch_source
self._stream_source = stream_source
if labels is None:
self._labels = dict() # type: MutableMapping[str, str]
else:
self._labels = labels
self._max_age = max_age
self._created_timestamp: Optional[Timestamp] = None
self._last_updated_timestamp: Optional[Timestamp] = None
def __str__(self):
return str(MessageToJson(self.to_proto()))
def __eq__(self, other):
if not isinstance(other, FeatureTable):
raise TypeError(
"Comparisons should only involve FeatureTable class objects."
)
if (
self.labels != other.labels
or self.name != other.name
or self.max_age != other.max_age
):
return False
if sorted(self.entities) != sorted(other.entities):
return False
if sorted(self.features) != sorted(other.features):
return False
if self.batch_source != other.batch_source:
return False
if self.stream_source != other.stream_source:
return False
return True
@property
def name(self):
"""
Returns the name of this feature table
"""
return self._name
@name.setter
def name(self, name: str):
"""
Sets the name of this feature table
"""
self._name = name
@property
def entities(self):
"""
Returns the entities of this feature table
"""
return self._entities
@entities.setter
def entities(self, entities: List[str]):
"""
Sets the entities of this feature table
"""
self._entities = entities
@property
def features(self):
"""
Returns the features of this feature table
"""
return self._features
@features.setter
def features(self, features: List[Feature]):
"""
Sets the features of this feature table
"""
self._features = features
@property
def batch_source(self):
"""
Returns the batch source of this feature table
"""
return self._batch_source
@batch_source.setter
def batch_source(self, batch_source: Union[BigQuerySource, FileSource]):
"""
Sets the batch source of this feature table
"""
self._batch_source = batch_source
@property
def stream_source(self):
"""
Returns the stream source of this feature table
"""
return self._stream_source
@stream_source.setter
def stream_source(self, stream_source: Union[KafkaSource, KinesisSource]):
"""
Sets the stream source of this feature table
"""
self._stream_source = stream_source
@property
def max_age(self):
"""
Returns the maximum age of this feature table. This is the total maximum
amount of staleness that will be allowed during feature retrieval for
each specific feature that is looked up.
"""
return self._max_age
@max_age.setter
def max_age(self, max_age: Duration):
"""
Set the maximum age for this feature table
"""
self._max_age = max_age
@property
def labels(self):
"""
Returns the labels of this feature table. This is the user defined metadata
defined as a dictionary.
"""
return self._labels
@labels.setter
def labels(self, labels: MutableMapping[str, str]):
"""
Set the labels for this feature table
"""
self._labels = labels
@property
def created_timestamp(self):
"""
Returns the created_timestamp of this feature table
"""
return self._created_timestamp
@property
def last_updated_timestamp(self):
"""
Returns the last_updated_timestamp of this feature table
"""
return self._last_updated_timestamp
def add_feature(self, feature: Feature):
"""
Adds a new feature to the feature table.
"""
self.features.append(feature)
def is_valid(self):
"""
Validates the state of a feature table locally. Raises an exception
if feature table is invalid.
"""
if not self.name:
raise ValueError("No name found in feature table.")
if not self.entities:
raise ValueError("No entities found in feature table {self.name}.")
@classmethod
def from_yaml(cls, yml: str):
"""
Creates a feature table from a YAML string body or a file path
Args:
yml: Either a file path containing a yaml file or a YAML string
Returns:
Returns a FeatureTable object based on the YAML file
"""
return cls.from_dict(feast_yaml.yaml_loader(yml, load_single=True))
@classmethod
def from_dict(cls, ft_dict):
"""
Creates a feature table from a dict
Args:
ft_dict: A dict representation of a feature table
Returns:
Returns a FeatureTable object based on the feature table dict
"""
feature_table_proto = json_format.ParseDict(
ft_dict, FeatureTableProto(), ignore_unknown_fields=True
)
return cls.from_proto(feature_table_proto)
@classmethod
def from_proto(cls, feature_table_proto: FeatureTableProto):
"""
Creates a feature table from a protobuf representation of a feature table
Args:
feature_table_proto: A protobuf representation of a feature table
Returns:
Returns a FeatureTableProto object based on the feature table protobuf
"""
feature_table = cls(
name=feature_table_proto.spec.name,
entities=[entity for entity in feature_table_proto.spec.entities],
features=[
Feature(
name=feature.name,
dtype=ValueType(feature.value_type),
labels=feature.labels,
)
for feature in feature_table_proto.spec.features
],
labels=feature_table_proto.spec.labels,
max_age=(
None
if feature_table_proto.spec.max_age.seconds == 0
and feature_table_proto.spec.max_age.nanos == 0
else feature_table_proto.spec.max_age
),
batch_source=DataSource.from_proto(feature_table_proto.spec.batch_source),
stream_source=(
None
if not feature_table_proto.spec.stream_source.ByteSize()
else DataSource.from_proto(feature_table_proto.spec.stream_source)
),
)
feature_table._created_timestamp = feature_table_proto.meta.created_timestamp
return feature_table
def to_proto(self) -> FeatureTableProto:
"""
Converts an feature table object to its protobuf representation
Returns:
FeatureTableProto protobuf
"""
meta = FeatureTableMetaProto(
created_timestamp=self.created_timestamp,
last_updated_timestamp=self.last_updated_timestamp,
)
spec = FeatureTableSpecProto(
name=self.name,
entities=self.entities,
features=[
feature.to_proto() if type(feature) == Feature else feature
for feature in self.features
],
labels=self.labels,
max_age=self.max_age,
batch_source=(
self.batch_source.to_proto()
if issubclass(type(self.batch_source), DataSource)
else self.batch_source
),
stream_source=(
self.stream_source.to_proto()
if issubclass(type(self.stream_source), DataSource)
else self.stream_source
),
)
return FeatureTableProto(spec=spec, meta=meta)
def to_spec_proto(self) -> FeatureTableSpecProto:
"""
Converts an FeatureTableProto object to its protobuf representation.
Used when passing FeatureTableSpecProto object to Feast request.
Returns:
FeatureTableSpecProto protobuf
"""
spec = FeatureTableSpecProto(
name=self.name,
entities=self.entities,
features=[
feature.to_proto() if type(feature) == Feature else feature
for feature in self.features
],
labels=self.labels,
max_age=self.max_age,
batch_source=(
self.batch_source.to_proto()
if issubclass(type(self.batch_source), DataSource)
else self.batch_source
),
stream_source=(
self.stream_source.to_proto()
if issubclass(type(self.stream_source), DataSource)
else self.stream_source
),
)
return spec
def to_dict(self) -> Dict:
"""
Converts feature table to dict
:return: Dictionary object representation of feature table
"""
feature_table_dict = MessageToDict(self.to_proto())
# Remove meta when empty for more readable exports
if feature_table_dict["meta"] == {}:
del feature_table_dict["meta"]
return feature_table_dict
def to_yaml(self):
"""
Converts a feature table to a YAML string.
:return: Feature table string returned in YAML format
"""
feature_table_dict = self.to_dict()
return yaml.dump(feature_table_dict, allow_unicode=True, sort_keys=False)
def _update_from_feature_table(self, feature_table):
"""
Deep replaces one feature table with another
Args:
feature_table: Feature table to use as a source of configuration
"""
self.name = feature_table.name
self.entities = feature_table.entities
self.features = feature_table.features
self.labels = feature_table.labels
self.max_age = feature_table.max_age
self.batch_source = feature_table.batch_source
self.stream_source = feature_table.stream_source
self._created_timestamp = feature_table.created_timestamp
self._last_updated_timestamp = feature_table.last_updated_timestamp
def __repr__(self):
return f"FeatureTable <{self.name}>"