Ejemplo n.º 1
0
    def to_proto(self) -> FeatureServiceProto:
        """
        Converts a FeatureService to its protobuf representation.

        Returns:
            A FeatureServiceProto protobuf.
        """
        meta = FeatureServiceMeta()
        if self.created_timestamp:
            meta.created_timestamp.FromDatetime(self.created_timestamp)

        spec = FeatureServiceSpec()
        spec.name = self.name
        for definition in self.features:
            if isinstance(definition, FeatureTable) or isinstance(
                    definition, FeatureView):
                feature_ref = FeatureViewProjection(definition.name,
                                                    definition.features)
            else:
                feature_ref = definition

            spec.features.append(feature_ref.to_proto())

        if self.tags:
            spec.tags.update(self.tags)

        feature_service_proto = FeatureServiceProto(spec=spec, meta=meta)
        return feature_service_proto
Ejemplo n.º 2
0
    def from_proto(cls,
                   on_demand_feature_view_proto: OnDemandFeatureViewProto):
        """
        Creates an on demand feature view from a protobuf representation.

        Args:
            on_demand_feature_view_proto: A protobuf representation of an on-demand feature view.

        Returns:
            A OnDemandFeatureView object based on the on-demand feature view protobuf.
        """
        inputs = {}
        for (
                input_name,
                on_demand_input,
        ) in on_demand_feature_view_proto.spec.inputs.items():
            if on_demand_input.WhichOneof("input") == "feature_view":
                inputs[input_name] = FeatureView.from_proto(
                    on_demand_input.feature_view).projection
            elif on_demand_input.WhichOneof(
                    "input") == "feature_view_projection":
                inputs[input_name] = FeatureViewProjection.from_proto(
                    on_demand_input.feature_view_projection)
            else:
                inputs[input_name] = RequestDataSource.from_proto(
                    on_demand_input.request_data_source)
        on_demand_feature_view_obj = cls(
            name=on_demand_feature_view_proto.spec.name,
            features=[
                Feature(
                    name=feature.name,
                    dtype=ValueType(feature.value_type),
                    labels=dict(feature.labels),
                ) for feature in on_demand_feature_view_proto.spec.features
            ],
            inputs=inputs,
            udf=dill.loads(
                on_demand_feature_view_proto.spec.user_defined_function.body),
        )

        # FeatureViewProjections are not saved in the OnDemandFeatureView proto.
        # Create the default projection.
        on_demand_feature_view_obj.projection = FeatureViewProjection.from_definition(
            on_demand_feature_view_obj)

        if on_demand_feature_view_proto.meta.HasField("created_timestamp"):
            on_demand_feature_view_obj.created_timestamp = (
                on_demand_feature_view_proto.meta.created_timestamp.ToDatetime(
                ))

        return on_demand_feature_view_obj
Ejemplo n.º 3
0
    def from_proto(cls, feature_service_proto: FeatureServiceProto):
        """
        Converts a FeatureServiceProto to a FeatureService object.

        Args:
            feature_service_proto: A protobuf representation of a FeatureService.
        """
        fs = cls(
            name=feature_service_proto.spec.name,
            features=[],
            tags=dict(feature_service_proto.spec.tags),
            description=feature_service_proto.spec.description,
            owner=feature_service_proto.spec.owner,
        )
        fs.feature_view_projections.extend([
            FeatureViewProjection.from_proto(projection)
            for projection in feature_service_proto.spec.features
        ])

        if feature_service_proto.meta.HasField("created_timestamp"):
            fs.created_timestamp = (
                feature_service_proto.meta.created_timestamp.ToDatetime())
        if feature_service_proto.meta.HasField("last_updated_timestamp"):
            fs.last_updated_timestamp = (
                feature_service_proto.meta.last_updated_timestamp.ToDatetime())

        return fs
Ejemplo n.º 4
0
    def __init__(
        self,
        name: str,
        features: List[Union[FeatureTable, FeatureView,
                             FeatureViewProjection]],
        tags: Optional[Dict[str, str]] = None,
    ):
        """
        Creates a FeatureService object.

        Raises:
            ValueError: If one of the specified features is not a valid type.
        """
        self.name = name
        self.features = []
        for feature in features:
            if isinstance(feature, FeatureTable) or isinstance(
                    feature, FeatureView):
                self.features.append(
                    FeatureViewProjection.from_definition(feature))
            elif isinstance(feature, FeatureViewProjection):
                self.features.append(feature)
            else:
                raise ValueError(f"Unexpected type: {type(feature)}")
        self.tags = tags or {}
        self.created_timestamp = None
        self.last_updated_timestamp = None
Ejemplo n.º 5
0
    def from_proto(cls, request_feature_view_proto: RequestFeatureViewProto):
        """
        Creates a request feature view from a protobuf representation.

        Args:
            request_feature_view_proto: A protobuf representation of an request feature view.

        Returns:
            A RequestFeatureView object based on the request feature view protobuf.
        """

        request_feature_view_obj = cls(
            name=request_feature_view_proto.spec.name,
            request_data_source=RequestDataSource.from_proto(
                request_feature_view_proto.spec.request_data_source
            ),
        )

        # FeatureViewProjections are not saved in the RequestFeatureView proto.
        # Create the default projection.
        request_feature_view_obj.projection = FeatureViewProjection.from_definition(
            request_feature_view_obj
        )

        return request_feature_view_obj
Ejemplo n.º 6
0
    def from_proto(feature_service_proto: FeatureServiceProto):
        """
        Converts a FeatureServiceProto to a FeatureService object.

        Args:
            feature_service_proto: A protobuf representation of a FeatureService.
        """
        fs = FeatureService(
            name=feature_service_proto.spec.name,
            features=[
                FeatureViewProjection.from_proto(fp)
                for fp in feature_service_proto.spec.features
            ],
            tags=dict(feature_service_proto.spec.tags),
            description=(feature_service_proto.spec.description
                         if feature_service_proto.spec.description != "" else
                         None),
        )

        if feature_service_proto.meta.HasField("created_timestamp"):
            fs.created_timestamp = (
                feature_service_proto.meta.created_timestamp.ToDatetime())
        if feature_service_proto.meta.HasField("last_updated_timestamp"):
            fs.last_updated_timestamp = (
                feature_service_proto.meta.last_updated_timestamp.ToDatetime())

        return fs
Ejemplo n.º 7
0
    def __init__(
        self,
        name: str,
        features: List[Union[FeatureTable, FeatureView, OnDemandFeatureView]],
        tags: Optional[Dict[str, str]] = None,
        description: Optional[str] = None,
    ):
        """
        Creates a FeatureService object.

        Raises:
            ValueError: If one of the specified features is not a valid type.
        """
        self.name = name
        self.feature_view_projections = []

        for feature_grouping in features:
            if isinstance(feature_grouping, FeatureTable):
                self.feature_view_projections.append(
                    FeatureViewProjection.from_definition(feature_grouping))
            elif isinstance(feature_grouping, BaseFeatureView):
                self.feature_view_projections.append(
                    feature_grouping.projection)
            else:
                raise ValueError(
                    "The FeatureService {fs_name} has been provided with an invalid type"
                    f'{type(feature_grouping)} as part of the "features" argument.)'
                )

        self.tags = tags or {}
        self.description = description
        self.created_timestamp = None
        self.last_updated_timestamp = None
Ejemplo n.º 8
0
    def __init__(
        self,
        *,
        name: str,
        features: Optional[List[Field]] = None,
        description: str = "",
        tags: Optional[Dict[str, str]] = None,
        owner: str = "",
    ):
        """
        Creates a BaseFeatureView object.

        Args:
            name: The unique name of the base feature view.
            features (optional): The list of features defined as part of this base feature view.
            description (optional): A human-readable description.
            tags (optional): A dictionary of key-value pairs to store arbitrary metadata.
            owner (optional): The owner of the base feature view, typically the email of the
                primary maintainer.

        Raises:
            ValueError: A field mapping conflicts with an Entity or a Feature.
        """
        assert name is not None
        self.name = name
        self.features = features or []
        self.description = description
        self.tags = tags or {}
        self.owner = owner
        self.projection = FeatureViewProjection.from_definition(self)
        self.created_timestamp = None
        self.last_updated_timestamp = None
Ejemplo n.º 9
0
    def __getitem__(self, item) -> FeatureViewProjection:
        assert isinstance(item, list)

        referenced_features = []
        for feature in self.features:
            if feature.name in item:
                referenced_features.append(feature)

        return FeatureViewProjection(self.name, referenced_features)
Ejemplo n.º 10
0
    def from_proto(cls, feature_view_proto: FeatureViewProto):
        """
        Creates a feature view from a protobuf representation of a feature view.

        Args:
            feature_view_proto: A protobuf representation of a feature view.

        Returns:
            A FeatureViewProto object based on the feature view protobuf.
        """
        batch_source = DataSource.from_proto(
            feature_view_proto.spec.batch_source)
        stream_source = (
            DataSource.from_proto(feature_view_proto.spec.stream_source)
            if feature_view_proto.spec.HasField("stream_source") else None)
        feature_view = cls(
            name=feature_view_proto.spec.name,
            entities=[entity for entity in feature_view_proto.spec.entities],
            features=[
                Feature(
                    name=feature.name,
                    dtype=ValueType(feature.value_type),
                    labels=dict(feature.labels),
                ) for feature in feature_view_proto.spec.features
            ],
            tags=dict(feature_view_proto.spec.tags),
            online=feature_view_proto.spec.online,
            ttl=(None if feature_view_proto.spec.ttl.seconds == 0
                 and feature_view_proto.spec.ttl.nanos == 0 else
                 feature_view_proto.spec.ttl),
            batch_source=batch_source,
            stream_source=stream_source,
        )

        # FeatureViewProjections are not saved in the FeatureView proto.
        # Create the default projection.
        feature_view.projection = FeatureViewProjection.from_definition(
            feature_view)

        if feature_view_proto.meta.HasField("created_timestamp"):
            feature_view.created_timestamp = (
                feature_view_proto.meta.created_timestamp.ToDatetime())
        if feature_view_proto.meta.HasField("last_updated_timestamp"):
            feature_view.last_updated_timestamp = (
                feature_view_proto.meta.last_updated_timestamp.ToDatetime())

        for interval in feature_view_proto.meta.materialization_intervals:
            feature_view.materialization_intervals.append((
                utils.make_tzaware(interval.start_time.ToDatetime()),
                utils.make_tzaware(interval.end_time.ToDatetime()),
            ))

        return feature_view
Ejemplo n.º 11
0
    def from_proto(cls, feature_view_proto: FeatureViewProto):
        """
        Creates a feature view from a protobuf representation of a feature view.

        Args:
            feature_view_proto: A protobuf representation of a feature view.

        Returns:
            A FeatureViewProto object based on the feature view protobuf.
        """
        batch_source = DataSource.from_proto(
            feature_view_proto.spec.batch_source)
        stream_source = (
            DataSource.from_proto(feature_view_proto.spec.stream_source)
            if feature_view_proto.spec.HasField("stream_source") else None)
        feature_view = cls(
            name=feature_view_proto.spec.name,
            entities=[entity for entity in feature_view_proto.spec.entities],
            schema=[
                Field.from_proto(field_proto)
                for field_proto in feature_view_proto.spec.features
            ],
            description=feature_view_proto.spec.description,
            tags=dict(feature_view_proto.spec.tags),
            owner=feature_view_proto.spec.owner,
            online=feature_view_proto.spec.online,
            ttl=(timedelta(
                days=0) if feature_view_proto.spec.ttl.ToNanoseconds() == 0
                 else feature_view_proto.spec.ttl.ToTimedelta()),
            source=batch_source,
        )
        if stream_source:
            feature_view.stream_source = stream_source

        # FeatureViewProjections are not saved in the FeatureView proto.
        # Create the default projection.
        feature_view.projection = FeatureViewProjection.from_definition(
            feature_view)

        if feature_view_proto.meta.HasField("created_timestamp"):
            feature_view.created_timestamp = (
                feature_view_proto.meta.created_timestamp.ToDatetime())
        if feature_view_proto.meta.HasField("last_updated_timestamp"):
            feature_view.last_updated_timestamp = (
                feature_view_proto.meta.last_updated_timestamp.ToDatetime())

        for interval in feature_view_proto.meta.materialization_intervals:
            feature_view.materialization_intervals.append((
                utils.make_tzaware(interval.start_time.ToDatetime()),
                utils.make_tzaware(interval.end_time.ToDatetime()),
            ))

        return feature_view
Ejemplo n.º 12
0
    def from_proto(cls,
                   on_demand_feature_view_proto: OnDemandFeatureViewProto):
        """
        Creates an on demand feature view from a protobuf representation.

        Args:
            on_demand_feature_view_proto: A protobuf representation of an on-demand feature view.

        Returns:
            A OnDemandFeatureView object based on the on-demand feature view protobuf.
        """
        sources = []
        for (
                _,
                on_demand_source,
        ) in on_demand_feature_view_proto.spec.sources.items():
            if on_demand_source.WhichOneof("source") == "feature_view":
                sources.append(
                    FeatureView.from_proto(
                        on_demand_source.feature_view).projection)
            elif on_demand_source.WhichOneof(
                    "source") == "feature_view_projection":
                sources.append(
                    FeatureViewProjection.from_proto(
                        on_demand_source.feature_view_projection))
            else:
                sources.append(
                    RequestSource.from_proto(
                        on_demand_source.request_data_source))
        on_demand_feature_view_obj = cls(
            name=on_demand_feature_view_proto.spec.name,
            schema=[
                Field(
                    name=feature.name,
                    dtype=from_value_type(ValueType(feature.value_type)),
                ) for feature in on_demand_feature_view_proto.spec.features
            ],
            sources=sources,
            udf=dill.loads(
                on_demand_feature_view_proto.spec.user_defined_function.body),
            description=on_demand_feature_view_proto.spec.description,
            tags=dict(on_demand_feature_view_proto.spec.tags),
            owner=on_demand_feature_view_proto.spec.owner,
        )

        # FeatureViewProjections are not saved in the OnDemandFeatureView proto.
        # Create the default projection.
        on_demand_feature_view_obj.projection = FeatureViewProjection.from_definition(
            on_demand_feature_view_obj)

        if on_demand_feature_view_proto.meta.HasField("created_timestamp"):
            on_demand_feature_view_obj.created_timestamp = (
                on_demand_feature_view_proto.meta.created_timestamp.ToDatetime(
                ))
        if on_demand_feature_view_proto.meta.HasField(
                "last_updated_timestamp"):
            on_demand_feature_view_obj.last_updated_timestamp = (
                on_demand_feature_view_proto.meta.last_updated_timestamp.
                ToDatetime())

        return on_demand_feature_view_obj
Ejemplo n.º 13
0
 def __init__(self, name: str, features: List[Feature]):
     self._name = name
     self._features = features
     self._projection = FeatureViewProjection.from_definition(self)
     self.created_timestamp: Optional[datetime] = None
Ejemplo n.º 14
0
 def __init__(self, name: str, features: List[Feature]):
     self._name = name
     self._features = features
     self._projection = FeatureViewProjection.from_definition(self)