Skip to content
Closed
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
43 changes: 25 additions & 18 deletions core/src/main/java/feast/core/model/FeatureTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,28 @@ public void updateFromProto(FeatureTableSpec spec) {
entityNames, spec.getEntitiesList()));
}

Map<String, FeatureSpecV2> updatedFeatures =
spec.getFeaturesList().stream().collect(Collectors.toMap(FeatureSpecV2::getName, ft -> ft));

// Update FeatureTable based on spec
// Update existing features, create new feature, drop missing features
Map<String, FeatureV2> existingFeatures =
getFeatures().stream().collect(Collectors.toMap(FeatureV2::getName, feature -> feature));
this.features.clear();
this.features.addAll(
spec.getFeaturesList().stream()
.map(
featureSpec -> {
if (!existingFeatures.containsKey(featureSpec.getName())) {
// Create new Feature based on spec
return FeatureV2.fromProto(this, featureSpec);
}
// Update existing feature based on spec
FeatureV2 feature = existingFeatures.get(featureSpec.getName());
feature.updateFromProto(featureSpec);
return feature;
})
.collect(Collectors.toSet()));
// Tombstone removed features, update existing features and create new features
for (FeatureV2 existingFeature : features) {
String existingFeatureName = existingFeature.getName();
FeatureSpecV2 updatedFeatureSpec = updatedFeatures.get(existingFeatureName);
if (updatedFeatureSpec == null) {
existingFeature.archive();
} else {
existingFeature.updateFromProto(updatedFeatureSpec);
updatedFeatures.remove(existingFeatureName);
}
}

for (FeatureSpecV2 featureSpec : updatedFeatures.values()) {
FeatureV2 newFeature = FeatureV2.fromProto(this, featureSpec);
addFeature(newFeature);
}

// Update max age, source and labels.
this.maxAgeSecs = spec.getMaxAge().getSeconds();
this.labelsJSON = TypeConversion.convertMapToJsonString(spec.getLabelsMap());

Expand All @@ -212,6 +214,11 @@ public void updateFromProto(FeatureTableSpec spec) {
this.revision++;
}

public void addFeature(FeatureV2 feature) {
feature.setFeatureTable(this);
features.add(feature);
}

/** Convert this Feature Table to its Protobuf representation */
public FeatureTableProto.FeatureTable toProto() {
// Convert field types to Protobuf compatible types
Expand Down
21 changes: 20 additions & 1 deletion core/src/main/java/feast/core/model/FeatureV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import javax.persistence.*;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;

/** Defines a single Feature defined in a {@link FeatureTable} */
@Getter
@Setter
@Entity
@Table(
name = "features_v2",
Expand All @@ -51,6 +53,10 @@ public class FeatureV2 {
@Column(name = "labels", columnDefinition = "text")
private String labelsJSON;

// Boolean to track whether a feature is archived
@Column(name = "is_archived")
private boolean isArchived = false;

public FeatureV2() {};

public FeatureV2(FeatureTable table, String name, ValueType.Enum type, String labelsJSON) {
Expand Down Expand Up @@ -79,9 +85,15 @@ public FeatureSpecV2 toProto() {
.setName(getName())
.setValueType(getType())
.putAllLabels(labels)
.setIsArchived(isArchived())
.build();
}

/** Archive this feature. */
public void archive() {
this.isArchived = true;
}

/**
* Update the Feature from the given Protobuf representation.
*
Expand All @@ -90,6 +102,12 @@ public FeatureSpecV2 toProto() {
*/
public void updateFromProto(FeatureSpecV2 spec) {
// Check for prohibited changes made in spec
if (isArchived()) {
throw new IllegalArgumentException(
String.format(
"You are attempting to create a feature, %s that was previously archived. This isn't allowed. Please create a new feature with a different name.",
spec.getName()));
}
if (!getName().equals(spec.getName())) {
throw new IllegalArgumentException(
String.format(
Expand Down Expand Up @@ -124,6 +142,7 @@ public boolean equals(Object o) {
FeatureV2 feature = (FeatureV2) o;
return getName().equals(feature.getName())
&& getType().equals(feature.getType())
&& getLabelsJSON().equals(feature.getLabelsJSON());
&& getLabelsJSON().equals(feature.getLabelsJSON())
&& isArchived() == (feature.isArchived());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE features_v2 ADD COLUMN is_archived boolean NOT NULL;
93 changes: 93 additions & 0 deletions core/src/test/java/feast/core/service/SpecServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.collection.IsMapWithSize.aMapWithSize;
import static org.hamcrest.core.IsEqual.equalTo;
Expand All @@ -42,6 +43,7 @@
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Triple;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -1116,6 +1118,43 @@ public void shouldNotUpdateIfNoChanges() {
assertThat(updatedTable.getMeta().getRevision(), equalTo(table.getMeta().getRevision()));
}

@Test
public void shouldReturnArchivedFeatures() {
// Apply feature table
apiClient.applyFeatureTable("default", getTestSpec());
FeatureTableSpec removedFeatureFeatureTableSpec =
DataGenerator.createFeatureTableSpec(
"ft",
List.of("entity1", "entity2"),
Map.of("feature1", ValueProto.ValueType.Enum.INT64),
3600,
Map.of())
.toBuilder()
.setBatchSource(
DataGenerator.createFileDataSourceSpec("file:///path/to/file", "ts_col", ""))
.setStreamSource(
DataGenerator.createKafkaDataSourceSpec(
"localhost:9092", "topic", "class.path", "ts_col"))
.build();
// Remove feature from feature table
apiClient.applyFeatureTable("default", removedFeatureFeatureTableSpec);

// Get feature table
FeatureTableProto.FeatureTable featureTable =
apiClient.simpleGetFeatureTable("default", "ft");
assertThat(featureTable.getSpec().getFeaturesCount(), equalTo(2));
assertThat(
featureTable.getSpec().getFeaturesList().stream()
.map(feature -> feature.getName())
.collect(Collectors.toList()),
containsInAnyOrder(ImmutableList.of("feature1", "feature2").toArray()));
assertThat(
featureTable.getSpec().getFeaturesList().stream()
.map(feature -> feature.getIsArchived())
.collect(Collectors.toList()),
containsInAnyOrder(ImmutableList.of(false, true).toArray()));
}

@Test
public void shouldErrorOnMissingBatchSource() {
FeatureTableProto.FeatureTableSpec spec =
Expand Down Expand Up @@ -1200,6 +1239,60 @@ public void shouldErrorIfFeatureValueTypeChangeOnUpdate() {
ValueProto.ValueType.Enum.FLOAT, ValueProto.ValueType.Enum.STRING_LIST)));
}

@Test
public void shouldErrorIfFeatureTypeChangeUponDeletion() {
// Apply new feature table
apiClient.applyFeatureTable("default", getTestSpec());
String featureToRemoveName = "feature2";
FeatureTableSpec removedFeatureFeatureTableSpec =
DataGenerator.createFeatureTableSpec(
"ft",
List.of("entity1", "entity2"),
Map.of("feature1", ValueProto.ValueType.Enum.INT64),
3600,
Map.of())
.toBuilder()
.setBatchSource(
DataGenerator.createFileDataSourceSpec("file:///path/to/file", "ts_col", ""))
.setStreamSource(
DataGenerator.createKafkaDataSourceSpec(
"localhost:9092", "topic", "class.path", "ts_col"))
.build();
// Remove feature from feature table
apiClient.applyFeatureTable("default", removedFeatureFeatureTableSpec);

FeatureTableSpec updatedTypeFeatureTableSpec =
DataGenerator.createFeatureTableSpec(
"ft",
List.of("entity1", "entity2"),
Map.of(
"feature1",
ValueProto.ValueType.Enum.INT64,
featureToRemoveName,
ValueProto.ValueType.Enum.STRING),
3600,
Map.of())
.toBuilder()
.setBatchSource(
DataGenerator.createFileDataSourceSpec("file:///path/to/file", "ts_col", ""))
.setStreamSource(
DataGenerator.createKafkaDataSourceSpec(
"localhost:9092", "topic", "class.path", "ts_col"))
.build();
// Attempt to add previously removed feature with different value type to feature table
StatusRuntimeException exc =
assertThrows(
StatusRuntimeException.class,
() -> apiClient.applyFeatureTable("default", updatedTypeFeatureTableSpec));

assertThat(
exc.getMessage(),
equalTo(
String.format(
"INVALID_ARGUMENT: You are attempting to create a feature, %s that was previously archived. This isn't allowed. Please create a new feature with a different name.",
featureToRemoveName)));
}

@Test
public void shouldErrorOnInvalidBigQueryTableRef() {
String invalidTableRef = "invalid.bq:path";
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 // indirect
golang.org/x/tools v0.0.0-20201017001424-6003fad69a88 // indirect
golang.org/x/tools v0.0.0-20201021000207-d49c4edd7d96 // indirect
google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/russross/blackfriday.v2 v2.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ golang.org/x/tools v0.0.0-20201015182029-a5d9e455e9c4 h1:rQWkJiVIyJ3PgiSHL+RXc8x
golang.org/x/tools v0.0.0-20201015182029-a5d9e455e9c4/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20201017001424-6003fad69a88 h1:ZB1XYzdDo7c/O48jzjMkvIjnC120Z9/CwgDWhePjQdQ=
golang.org/x/tools v0.0.0-20201017001424-6003fad69a88/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20201021000207-d49c4edd7d96 h1:K+nJoPcImWk+ZGPHOKkDocKcQPACCz8usiCiVQYfXsk=
golang.org/x/tools v0.0.0-20201021000207-d49c4edd7d96/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
3 changes: 3 additions & 0 deletions protos/feast/core/Feature.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ message FeatureSpecV2 {

// Labels for user defined metadata on a feature
map<string,string> labels = 3;

// Boolean to track whether a feature is archived
bool is_archived = 4;
}
2 changes: 1 addition & 1 deletion sdk/go/protos/feast/core/CoreService.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/go/protos/feast/core/DataFormat.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/go/protos/feast/core/DataSource.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/go/protos/feast/core/Entity.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 23 additions & 12 deletions sdk/go/protos/feast/core/Feature.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/go/protos/feast/core/FeatureSet.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/go/protos/feast/core/FeatureSetReference.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading