Skip to content

Commit 8688acd

Browse files
robhowleysnowrondependabot[bot]shuchuRob Howley
authored
feat: Add python bytes to array type conversion support proto (#3874)
* feat: add redis sentinel support format lint Signed-off-by: snowron <snowronark@gmail.com> Signed-off-by: Rob Howley <rhowley@seatgeek.com> * chore: Bump pyarrow Bumps [pyarrow](https://github.com/apache/arrow) from 6.0.0 to 14.0.1. - [Commits](apache/arrow@go/arrow/v6.0.0...apache-arrow-14.0.1) --- updated-dependencies: - dependency-name: pyarrow dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Rob Howley <rhowley@seatgeek.com> * fix: upgrade the pyarrow to latest v14.0.1 for CVE-2023-47248. Signed-off-by: Shuchu Han <shuchu.han@gmail.com> Signed-off-by: Rob Howley <rhowley@seatgeek.com> * feat: add bytes to array type conversion in python -> proto Signed-off-by: Rob Howley <rhowley@seatgeek.com> * ignore type like in other proto val assignments Signed-off-by: Rob Howley <rhowley@seatgeek.com> * run black Signed-off-by: Rob Howley <rhowley@seatgeek.com> * floats can also appear as ints Signed-off-by: Rob Howley <rhowley@seatgeek.com> --------- Signed-off-by: snowron <snowronark@gmail.com> Signed-off-by: Rob Howley <rhowley@seatgeek.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Shuchu Han <shuchu.han@gmail.com> Co-authored-by: snowron <snowronark@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Shuchu Han <shuchu.han@gmail.com> Co-authored-by: Rob Howley <rhowley@seatgeek.com>
1 parent 9237361 commit 8688acd

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

sdk/python/feast/type_map.py

+16-1
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 (
@@ -297,7 +298,7 @@ def _type_err(item, dtype):
297298
None,
298299
),
299300
ValueType.FLOAT: ("float_val", lambda x: float(x), None),
300-
ValueType.DOUBLE: ("double_val", lambda x: x, {float, np.float64}),
301+
ValueType.DOUBLE: ("double_val", lambda x: x, {float, np.float64, int, np.int_}),
301302
ValueType.STRING: ("string_val", lambda x: str(x), None),
302303
ValueType.BYTES: ("bytes_val", lambda x: x, {bytes}),
303304
ValueType.BOOL: ("bool_val", lambda x: x, {bool, np.bool_, int, np.int_}),
@@ -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)})] # type: ignore
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

+32
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)