forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.py
More file actions
166 lines (136 loc) · 4.83 KB
/
Copy pathtypes.py
File metadata and controls
166 lines (136 loc) · 4.83 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
# Copyright 2022 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 abc import ABC, abstractmethod
from enum import Enum
from typing import Dict, Union
from feast.protos.feast.types.Value_pb2 import ValueType as ValueTypeProto
PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES = {
"INVALID": "INVALID",
"STRING": "STRING",
"BYTES": "BYTES",
"BOOL": "BOOL",
"INT32": "INT32",
"INT64": "INT64",
"FLOAT32": "FLOAT",
"FLOAT64": "DOUBLE",
"UNIX_TIMESTAMP": "UNIX_TIMESTAMP",
}
class ComplexFeastType(ABC):
"""
A ComplexFeastType represents a structured type that is recognized by Feast.
"""
def __init__(self):
"""Creates a ComplexFeastType object."""
pass
@abstractmethod
def to_value_type(self) -> ValueTypeProto.Enum:
"""
Converts a ComplexFeastType object to the corresponding ValueTypeProto.Enum value.
"""
raise NotImplementedError
def __eq__(self, other):
return self.to_value_type() == other.to_value_type()
class PrimitiveFeastType(Enum):
"""
A PrimitiveFeastType represents a primitive type in Feast.
Note that these values must match the values in ValueTypeProto.Enum. See
/feast/protos/types/Value.proto for the exact values.
"""
INVALID = 0
BYTES = 1
STRING = 2
INT32 = 3
INT64 = 4
FLOAT64 = 5
FLOAT32 = 6
BOOL = 7
UNIX_TIMESTAMP = 8
def to_value_type(self) -> ValueTypeProto.Enum:
"""
Converts a PrimitiveFeastType object to the corresponding ValueTypeProto.Enum value.
"""
value_type_name = PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES[self.name]
return ValueTypeProto.Enum.Value(value_type_name)
Invalid = PrimitiveFeastType.INVALID
Bytes = PrimitiveFeastType.BYTES
String = PrimitiveFeastType.STRING
Bool = PrimitiveFeastType.BOOL
Int32 = PrimitiveFeastType.INT32
Int64 = PrimitiveFeastType.INT64
Float32 = PrimitiveFeastType.FLOAT32
Float64 = PrimitiveFeastType.FLOAT64
UnixTimestamp = PrimitiveFeastType.UNIX_TIMESTAMP
SUPPORTED_BASE_TYPES = [
Invalid,
String,
Bytes,
Bool,
Int32,
Int64,
Float32,
Float64,
UnixTimestamp,
]
class Array(ComplexFeastType):
"""
An Array represents a list of types.
Attributes:
base_type: The base type of the array.
"""
base_type: Union[PrimitiveFeastType, ComplexFeastType]
def __init__(self, base_type: Union[PrimitiveFeastType, ComplexFeastType]):
if base_type not in SUPPORTED_BASE_TYPES:
raise ValueError(
f"Type {type(base_type)} is currently not supported as a base type for Array."
)
self.base_type = base_type
def to_value_type(self) -> int:
assert isinstance(self.base_type, PrimitiveFeastType)
value_type_name = PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES[self.base_type.name]
value_type_list_name = value_type_name + "_LIST"
return ValueTypeProto.Enum.Value(value_type_list_name)
VALUE_TYPES_TO_FEAST_TYPES: Dict[
"ValueTypeProto.Enum", Union[ComplexFeastType, PrimitiveFeastType]
] = {
ValueTypeProto.Enum.INVALID: Invalid,
ValueTypeProto.Enum.BYTES: Bytes,
ValueTypeProto.Enum.STRING: String,
ValueTypeProto.Enum.INT32: Int32,
ValueTypeProto.Enum.INT64: Int64,
ValueTypeProto.Enum.DOUBLE: Float64,
ValueTypeProto.Enum.FLOAT: Float32,
ValueTypeProto.Enum.BOOL: Bool,
ValueTypeProto.Enum.UNIX_TIMESTAMP: UnixTimestamp,
ValueTypeProto.Enum.BYTES_LIST: Array(Bytes),
ValueTypeProto.Enum.STRING_LIST: Array(String),
ValueTypeProto.Enum.INT32_LIST: Array(Int32),
ValueTypeProto.Enum.INT64_LIST: Array(Int64),
ValueTypeProto.Enum.DOUBLE_LIST: Array(Float64),
ValueTypeProto.Enum.FLOAT_LIST: Array(Float32),
ValueTypeProto.Enum.BOOL_LIST: Array(Bool),
ValueTypeProto.Enum.UNIX_TIMESTAMP_LIST: Array(UnixTimestamp),
}
def from_value_type(
value_type: ValueTypeProto.Enum,
) -> Union[ComplexFeastType, PrimitiveFeastType]:
"""
Converts a ValueTypeProto.Enum to a Feast type.
Args:
value_type: The ValueTypeProto.Enum to be converted.
Raises:
ValueError: The conversion could not be performed.
"""
if value_type in VALUE_TYPES_TO_FEAST_TYPES:
return VALUE_TYPES_TO_FEAST_TYPES[value_type]
raise ValueError(f"Could not convert value type {value_type} to FeastType.")