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

Prevent reserved fields from being registered #819

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions core/src/main/java/feast/core/validators/FeatureSetValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import feast.proto.core.FeatureSetProto.EntitySpec;
import feast.proto.core.FeatureSetProto.FeatureSet;
import feast.proto.core.FeatureSetProto.FeatureSpec;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public class FeatureSetValidator {

Expand All @@ -43,6 +45,7 @@ public static void validateSpec(FeatureSet featureSet) {
checkValidCharacters(featureSet.getSpec().getName(), "name");
checkUniqueColumns(
featureSet.getSpec().getEntitiesList(), featureSet.getSpec().getFeaturesList());
checkReservedColumns(featureSet.getSpec().getFeaturesList());
for (EntitySpec entitySpec : featureSet.getSpec().getEntitiesList()) {
checkValidCharacters(entitySpec.getName(), "entities::name");
}
Expand All @@ -64,4 +67,18 @@ private static void checkUniqueColumns(
String.format("fields within a featureset must be unique."));
}
}

private static void checkReservedColumns(List<FeatureSpec> featureSpecs) {
List<String> reservedNames =
Arrays.asList("created_timestamp", "event_timestamp", "ingestion_id", "job_id");
Copy link
Member

@woop woop Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be defined elsewhere as constants, not within the method. You can at least hoist it to the top of the class as a field.

String reservedNamesString = StringUtils.join(reservedNames, ", ");
for (FeatureSpec featureSpec : featureSpecs) {
if (reservedNames.contains(featureSpec.getName())) {
throw new IllegalArgumentException(
String.format(
"Reserved feature names have been used, which are not allowed. These names include %s.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the field the user tried to register as well.

reservedNamesString));
}
}
}
}
23 changes: 23 additions & 0 deletions core/src/test/java/feast/core/service/SpecServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -91,6 +92,7 @@ public class SpecServiceTest {

private SpecService specService;
private List<FeatureSet> featureSets;
private List<FeatureSet> invalidFeatureSets;
private List<Feature> features;
private List<Store> stores;
private Source defaultSource;
Expand Down Expand Up @@ -214,6 +216,12 @@ public void setUp() throws InvalidProtocolBufferException {

specService =
new SpecService(featureSetRepository, storeRepository, projectRepository, defaultSource);

Feature invalidFeature1 = TestUtil.CreateFeature("created_timestamp", Enum.INT64);
FeatureSet invalidFeatureSet1 =
TestUtil.CreateFeatureSet(
"f1", "invalid", Arrays.asList(f3e1), Arrays.asList(invalidFeature1));
invalidFeatureSets = Arrays.asList(invalidFeatureSet1);
}

@Test
Expand Down Expand Up @@ -277,6 +285,21 @@ public void shouldThrowExceptionGivenMissingFeatureSetName()
specService.getFeatureSet(GetFeatureSetRequest.newBuilder().build());
}

@Test
public void shouldThrowExceptionGivenReservedFeatureName() throws InvalidProtocolBufferException {
List<String> reservedNames =
Arrays.asList("created_timestamp", "event_timestamp", "ingestion_id", "job_id");
String reservedNamesString = StringUtils.join(reservedNames, ", ");
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
String.format(
"Reserved feature names have been used, which are not allowed. These names include %s.",
reservedNamesString));
FeatureSet invalidFeatureSet = invalidFeatureSets.get(0);

specService.applyFeatureSet(invalidFeatureSet.toProto());
}

@Test
public void shouldThrowExceptionGivenMissingFeatureSet() throws InvalidProtocolBufferException {
expectedException.expect(RetrievalException.class);
Expand Down