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

fix: Update field api to add tag parameter corresponding to labels in Feature. #2610

Merged
merged 12 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions protos/feast/types/Field.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ option go_package = "github.com/feast-dev/feast/go/protos/feast/types";
message Field {
string name = 1;
feast.types.ValueType.Enum value = 2;

// User defined metadata
map<string,string> tags = 3;
felixwang9817 marked this conversation as resolved.
Show resolved Hide resolved
}
40 changes: 30 additions & 10 deletions sdk/python/feast/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, Optional

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


Expand All @@ -25,31 +27,43 @@ class Field:
Attributes:
name: The name of the field.
dtype: The type of the field, such as string or float.
tags: User-defined metadata in dictionary form.
"""

name: str
dtype: FeastType
tags: Dict[str, str]

def __init__(
self, *, name: str, dtype: FeastType,
self,
*,
name: Optional[str] = None,
felixwang9817 marked this conversation as resolved.
Show resolved Hide resolved
dtype: Optional[FeastType] = None,
tags: Optional[Dict[str, str]] = None,
):
"""
Creates a Field object.

Args:
name: The name of the field.
dtype: The type of the field, such as string or float.
tags (optional): User-defined metadata in dictionary form.
"""
self.name = name
self.dtype = dtype
self.name = name or ""
self.dtype = dtype or PrimitiveFeastType.INVALID
felixwang9817 marked this conversation as resolved.
Show resolved Hide resolved
self.tags = tags or {}

def __eq__(self, other):
if self.name != other.name or self.dtype != other.dtype:
if (
self.name != other.name
or self.dtype != other.dtype
or self.tags != other.tags
):
return False
return True

def __hash__(self):
return hash((self.name, hash(self.dtype)))
return hash((self.name, hash(self.dtype), hash(self.tags)))
felixwang9817 marked this conversation as resolved.
Show resolved Hide resolved

def __lt__(self, other):
return self.name < other.name
Expand All @@ -58,12 +72,12 @@ def __repr__(self):
return f"{self.name}-{self.dtype}"

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

def to_proto(self) -> FieldProto:
"""Converts a Field object to its protobuf representation."""
value_type = self.dtype.to_value_type()
return FieldProto(name=self.name, value_type=value_type.value)
return FieldProto(name=self.name, value_type=value_type.value, labels=self.tags)
felixwang9817 marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def from_proto(cls, field_proto: FieldProto):
Expand All @@ -74,7 +88,11 @@ def from_proto(cls, field_proto: FieldProto):
field_proto: FieldProto protobuf object
"""
value_type = ValueType(field_proto.value_type)
return cls(name=field_proto.name, dtype=from_value_type(value_type=value_type))
return cls(
name=field_proto.name,
dtype=from_value_type(value_type=value_type),
tags=dict(field_proto.labels),
)

@classmethod
def from_feature(cls, feature: Feature):
Expand All @@ -84,4 +102,6 @@ def from_feature(cls, feature: Feature):
Args:
feature: Feature object to convert.
"""
return cls(name=feature.name, dtype=from_value_type(feature.dtype))
return cls(
name=feature.name, dtype=from_value_type(feature.dtype), tags=feature.labels
)