Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Field class #2500

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions sdk/python/feast/field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2022 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from feast.feature import Feature
from feast.protos.feast.core.Feature_pb2 import FeatureSpecV2 as FieldProto
from feast.types import FeastType, from_value_type


class Field:
"""
A Field represents a set of values with the same structure.

Attributes:
name: The name of the field.
dtype: The type of the field, such as string or float.
"""

name: str
dtype: FeastType

def __init__(
self, *, name: str, dtype: FeastType,
felixwang9817 marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Creates a Field object.

Args:
name: The name of the field.
dtype: The type of the field, such as string or float.
"""
self.name = name
self.dtype = dtype

def __eq__(self, other):
if self.name != other.name or self.dtype != other.dtype:
return False
return True
kevjumba marked this conversation as resolved.
Show resolved Hide resolved

def __hash__(self):
return hash((self.name, hash(self.dtype)))

def __lt__(self, other):
return self.name < other.name

def __repr__(self):
return f"{self.name}-{self.dtype}"

def __str__(self):
return f"Field(name={self.name}, dtype={self.dtype})"

def to_proto(self) -> FieldProto:
"""Converts a Field object to its protobuf representation."""
return FieldProto(name=self.name, value_type=self.dtype.to_value_type())

@classmethod
def from_proto(cls, field_proto: FieldProto):
"""
Creates a Field object from a protobuf representation.

Args:
field_proto: FieldProto protobuf object
"""
return cls(name=field_proto.name, dtype=from_value_type(field_proto.value_type))

@classmethod
def from_feature(cls, feature: Feature):
"""
Creates a Field object from a Feature object.

Args:
feature: Feature object to convert.
"""
return cls(name=feature.name, dtype=from_value_type(feature.dtype.value))
32 changes: 26 additions & 6 deletions sdk/python/feast/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def to_value_type(self) -> ValueTypeProto.Enum:
"""
raise NotImplementedError

def __hash__(self):
return hash(self.to_value_type())

def __eq__(self, other):
return self.to_value_type() == other.to_value_type()

Expand Down Expand Up @@ -75,6 +78,9 @@ def to_value_type(self) -> ValueTypeProto.Enum:
value_type_name = PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES[self.name]
return ValueTypeProto.Enum.Value(value_type_name)

def __str__(self):
return PRIMITIVE_FEAST_TYPES_TO_STRING[self.name]


Invalid = PrimitiveFeastType.INVALID
Bytes = PrimitiveFeastType.BYTES
Expand All @@ -99,6 +105,18 @@ def to_value_type(self) -> ValueTypeProto.Enum:
UnixTimestamp,
]

PRIMITIVE_FEAST_TYPES_TO_STRING = {
"INVALID": "Invalid",
"STRING": "String",
"BYTES": "Bytes",
"BOOL": "Bool",
"INT32": "Int32",
"INT64": "Int64",
"FLOAT32": "Float32",
"FLOAT64": "Float64",
"UNIX_TIMESTAMP": "UnixTimestamp",
}


class Array(ComplexFeastType):
"""
Expand All @@ -124,10 +142,14 @@ def to_value_type(self) -> int:
value_type_list_name = value_type_name + "_LIST"
return ValueTypeProto.Enum.Value(value_type_list_name)

def __str__(self):
return f"Array({self.base_type})"


FeastType = Union[ComplexFeastType, PrimitiveFeastType]


VALUE_TYPES_TO_FEAST_TYPES: Dict[
"ValueTypeProto.Enum", Union[ComplexFeastType, PrimitiveFeastType]
] = {
VALUE_TYPES_TO_FEAST_TYPES: Dict["ValueTypeProto.Enum", FeastType] = {
ValueTypeProto.Enum.INVALID: Invalid,
ValueTypeProto.Enum.BYTES: Bytes,
ValueTypeProto.Enum.STRING: String,
Expand All @@ -148,9 +170,7 @@ def to_value_type(self) -> int:
}


def from_value_type(
value_type: ValueTypeProto.Enum,
) -> Union[ComplexFeastType, PrimitiveFeastType]:
def from_value_type(value_type: ValueTypeProto.Enum,) -> FeastType:
"""
Converts a ValueTypeProto.Enum to a Feast type.

Expand Down