Skip to content

Commit 1f47c31

Browse files
committed
Fix conversion method
Signed-off-by: Felix Wang <wangfelix98@gmail.com>
1 parent 8055ce1 commit 1f47c31

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

sdk/python/feast/types.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
from abc import ABC, abstractmethod
1515
from enum import Enum
16-
from typing import Union
16+
from typing import Dict, Union
1717

1818
from 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+
127151
def 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.")

sdk/python/tests/unit/test_types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ def test_array_feast_type():
2525

2626
with pytest.raises(ValueError):
2727
_ = Array(Array(String))
28+
29+
30+
def test_all_value_types():
31+
values = ValueTypeProto.Enum.values()
32+
for value in values:
33+
# We do not support the NULL type.
34+
if value != ValueTypeProto.Enum.Value("NULL"):
35+
assert from_value_type(value).to_value_type() == value

0 commit comments

Comments
 (0)