Skip to content

Commit

Permalink
Refactor pa_to_feast_value_type (#2246)
Browse files Browse the repository at this point in the history
* Refactor `pa_to_feast_value_type`

This refactoring is intented to make it more difficult
to forget to add conversion for LIST versions of
non-LIST types.

Signed-off-by: Judah Rand <[email protected]>

* Tidy up `assert_expected_arrow_types`

Signed-off-by: Judah Rand <[email protected]>
  • Loading branch information
judahrand authored Feb 1, 2022
1 parent 53539cf commit 2080fa3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
44 changes: 23 additions & 21 deletions sdk/python/feast/type_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from datetime import datetime, timezone
from typing import (
Any,
Expand Down Expand Up @@ -416,27 +415,30 @@ def _proto_value_to_value_type(proto_value: ProtoValue) -> ValueType:


def pa_to_feast_value_type(pa_type_as_str: str) -> ValueType:
if re.match(r"^timestamp", pa_type_as_str):
return ValueType.INT64
is_list = False
if pa_type_as_str.startswith("list<item: "):
is_list = True
pa_type_as_str = pa_type_as_str.replace("list<item: ", "").replace(">", "")

type_map = {
"int32": ValueType.INT32,
"int64": ValueType.INT64,
"double": ValueType.DOUBLE,
"float": ValueType.FLOAT,
"string": ValueType.STRING,
"binary": ValueType.BYTES,
"bool": ValueType.BOOL,
"list<item: int32>": ValueType.INT32_LIST,
"list<item: int64>": ValueType.INT64_LIST,
"list<item: double>": ValueType.DOUBLE_LIST,
"list<item: float>": ValueType.FLOAT_LIST,
"list<item: string>": ValueType.STRING_LIST,
"list<item: binary>": ValueType.BYTES_LIST,
"list<item: bool>": ValueType.BOOL_LIST,
"null": ValueType.NULL,
}
return type_map[pa_type_as_str]
if pa_type_as_str.startswith("timestamp"):
value_type = ValueType.UNIX_TIMESTAMP
else:
type_map = {
"int32": ValueType.INT32,
"int64": ValueType.INT64,
"double": ValueType.DOUBLE,
"float": ValueType.FLOAT,
"string": ValueType.STRING,
"binary": ValueType.BYTES,
"bool": ValueType.BOOL,
"null": ValueType.NULL,
}
value_type = type_map[pa_type_as_str]

if is_list:
value_type = ValueType[value_type.name + "_LIST"]

return value_type


def bq_to_feast_value_type(bq_type_as_str: str) -> ValueType:
Expand Down
30 changes: 14 additions & 16 deletions sdk/python/tests/integration/registration/test_universal_types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
import re
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Any, Dict, List, Tuple, Union

import numpy as np
import pandas as pd
import pyarrow as pa
import pytest

from feast.infra.offline_stores.offline_store import RetrievalJob
Expand Down Expand Up @@ -339,23 +339,21 @@ def assert_expected_arrow_types(
historical_features_arrow = historical_features.to_arrow()
print(historical_features_arrow)
feature_list_dtype_to_expected_historical_feature_arrow_type = {
"int32": r"int64",
"int64": r"int64",
"float": r"double",
"string": r"string",
"bool": r"bool",
"datetime": r"timestamp\[.+\]",
"int32": pa.types.is_int64,
"int64": pa.types.is_int64,
"float": pa.types.is_float64,
"string": pa.types.is_string,
"bool": pa.types.is_boolean,
"date": pa.types.is_date,
"datetime": pa.types.is_timestamp,
}
arrow_type = feature_list_dtype_to_expected_historical_feature_arrow_type[
arrow_type_checker = feature_list_dtype_to_expected_historical_feature_arrow_type[
feature_dtype
]
pa_type = historical_features_arrow.schema.field("value").type

if feature_is_list:
assert re.match(
f"list<item: {arrow_type}>",
str(historical_features_arrow.schema.field_by_name("value").type),
)
assert pa.types.is_list(pa_type)
assert arrow_type_checker(pa_type.value_type)
else:
assert re.match(
arrow_type,
str(historical_features_arrow.schema.field_by_name("value").type),
)
assert arrow_type_checker(pa_type)

0 comments on commit 2080fa3

Please sign in to comment.