1313# limitations under the License.
1414from abc import ABC , abstractmethod
1515from enum import Enum
16- from typing import Union
16+ from typing import Dict , Union
1717
1818from feast .protos .feast .types .Value_pb2 import ValueType as ValueTypeProto
1919
@@ -54,7 +54,8 @@ class PrimitiveFeastType(Enum):
5454 """
5555 A PrimitiveFeastType represents a primitive type in Feast.
5656
57- Note that these values must match the values in ValueTypeProto.Enum.
57+ Note that these values must match the values in ValueTypeProto.Enum. See
58+ /feast/protos/types/Value.proto for the exact values.
5859 """
5960
6061 INVALID = 0
@@ -124,6 +125,29 @@ def to_value_type(self) -> int:
124125 return ValueTypeProto .Enum .Value (value_type_list_name )
125126
126127
128+ VALUE_TYPES_TO_FEAST_TYPES : Dict [
129+ "ValueTypeProto.Enum" , Union [ComplexFeastType , PrimitiveFeastType ]
130+ ] = {
131+ ValueTypeProto .Enum .INVALID : Invalid ,
132+ ValueTypeProto .Enum .BYTES : Bytes ,
133+ ValueTypeProto .Enum .STRING : String ,
134+ ValueTypeProto .Enum .INT32 : Int32 ,
135+ ValueTypeProto .Enum .INT64 : Int64 ,
136+ ValueTypeProto .Enum .DOUBLE : Float64 ,
137+ ValueTypeProto .Enum .FLOAT : Float32 ,
138+ ValueTypeProto .Enum .BOOL : Bool ,
139+ ValueTypeProto .Enum .UNIX_TIMESTAMP : UnixTimestamp ,
140+ ValueTypeProto .Enum .BYTES_LIST : Array (Bytes ),
141+ ValueTypeProto .Enum .STRING_LIST : Array (String ),
142+ ValueTypeProto .Enum .INT32_LIST : Array (Int32 ),
143+ ValueTypeProto .Enum .INT64_LIST : Array (Int64 ),
144+ ValueTypeProto .Enum .DOUBLE_LIST : Array (Float64 ),
145+ ValueTypeProto .Enum .FLOAT_LIST : Array (Float32 ),
146+ ValueTypeProto .Enum .BOOL_LIST : Array (Bool ),
147+ ValueTypeProto .Enum .UNIX_TIMESTAMP_LIST : Array (UnixTimestamp ),
148+ }
149+
150+
127151def from_value_type (
128152 value_type : ValueTypeProto .Enum ,
129153) -> Union [ComplexFeastType , PrimitiveFeastType ]:
@@ -136,16 +160,7 @@ def from_value_type(
136160 Raises:
137161 ValueError: The conversion could not be performed.
138162 """
139- # Primitive types can be directly converted.
140- primitive_values = [primitive_type .value for primitive_type in PrimitiveFeastType ]
141- if value_type in primitive_values :
142- return PrimitiveFeastType (value_type )
143-
144- # Complex types must be constructed. Currently only arrays are supported. Note that
145- # enum values for arrays are precisely the enum value for the array's base type plus 10.
146- # TODO(felixwang9817): Make this more robust.
147- base_type = value_type - 10
148- if base_type in primitive_values :
149- return Array (PrimitiveFeastType (base_type ))
150-
151- raise ValueError ("Could not convert value type to FeastType" )
163+ if value_type in VALUE_TYPES_TO_FEAST_TYPES :
164+ return VALUE_TYPES_TO_FEAST_TYPES [value_type ]
165+
166+ raise ValueError (f"Could not convert value type { value_type } to FeastType." )
0 commit comments