Skip to content

Commit f99a873

Browse files
author
Rob Howley
committed
feat: add bytes to array type conversion in python -> proto
Signed-off-by: Rob Howley <rhowley@seatgeek.com>
1 parent 9835ab6 commit f99a873

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sdk/python/feast/type_map.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
from collections import defaultdict
1617
from datetime import datetime, timezone
1718
from typing import (
@@ -353,6 +354,19 @@ def _python_value_to_proto_value(
353354
feast_value_type
354355
]
355356

357+
# Bytes to array type conversion
358+
if isinstance(sample, (bytes, bytearray)):
359+
# Bytes of an array containing elements of bytes not supported
360+
if feast_value_type == ValueType.BYTES_LIST:
361+
raise _type_err(sample, ValueType.BYTES_LIST)
362+
363+
json_value = json.loads(sample)
364+
if isinstance(json_value, list):
365+
if feast_value_type == ValueType.BOOL_LIST:
366+
json_value = [bool(item) for item in json_value]
367+
return [ProtoValue(**{field_name: proto_type(val=json_value)})]
368+
raise _type_err(sample, valid_types[0])
369+
356370
if sample is not None and not all(
357371
type(item) in valid_types for item in sample
358372
):
@@ -631,6 +645,7 @@ def redshift_to_feast_value_type(redshift_type_as_str: str) -> ValueType:
631645
"varchar": ValueType.STRING,
632646
"timestamp": ValueType.UNIX_TIMESTAMP,
633647
"timestamptz": ValueType.UNIX_TIMESTAMP,
648+
"super": ValueType.BYTES,
634649
# skip date, geometry, hllsketch, time, timetz
635650
}
636651

sdk/python/tests/unit/test_type_map.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,35 @@ def test_python_values_to_proto_values_bool(values):
4848
converted = feast_value_type_to_python_type(protos[0])
4949

5050
assert converted is bool(values[0])
51+
52+
53+
@pytest.mark.parametrize(
54+
"values, value_type, expected",
55+
(
56+
(np.array([b'[1,2,3]']), ValueType.INT64_LIST, [1, 2, 3]),
57+
(np.array([b'[1,2,3]']), ValueType.INT32_LIST, [1, 2, 3]),
58+
(np.array([b'[1.5,2.5,3.5]']), ValueType.FLOAT_LIST, [1.5, 2.5, 3.5]),
59+
(np.array([b'[1.5,2.5,3.5]']), ValueType.DOUBLE_LIST, [1.5, 2.5, 3.5]),
60+
(np.array([b'["a","b","c"]']), ValueType.STRING_LIST, ["a", "b", "c"]),
61+
(np.array([b'[true,false]']), ValueType.BOOL_LIST, [True, False]),
62+
(np.array([b'[1,0]']), ValueType.BOOL_LIST, [True, False]),
63+
(np.array([None]), ValueType.STRING_LIST, None),
64+
([b'[1,2,3]'], ValueType.INT64_LIST, [1, 2, 3]),
65+
([b'[1,2,3]'], ValueType.INT32_LIST, [1, 2, 3]),
66+
([b'[1.5,2.5,3.5]'], ValueType.FLOAT_LIST, [1.5, 2.5, 3.5]),
67+
([b'[1.5,2.5,3.5]'], ValueType.DOUBLE_LIST, [1.5, 2.5, 3.5]),
68+
([b'["a","b","c"]'], ValueType.STRING_LIST, ["a", "b", "c"]),
69+
([b'[true,false]'], ValueType.BOOL_LIST, [True, False]),
70+
([b'[1,0]'], ValueType.BOOL_LIST, [True, False]),
71+
([None], ValueType.STRING_LIST, None),
72+
),
73+
)
74+
def test_python_values_to_proto_values_bytes_to_list(values, value_type, expected):
75+
protos = python_values_to_proto_values(values, value_type)
76+
converted = feast_value_type_to_python_type(protos[0])
77+
assert converted == expected
78+
79+
80+
def test_python_values_to_proto_values_bytes_to_list_not_supported():
81+
with pytest.raises(TypeError):
82+
_ = python_values_to_proto_values([b"[]"], ValueType.BYTES_LIST)

0 commit comments

Comments
 (0)