Search in sources :

Example 1 with Rule

use of io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.Rule in project hopsworks by logicalclocks.

the class FeatureGroupValidationsController method createOrUpdateExpectation.

public FeatureStoreExpectation createOrUpdateExpectation(Featurestore featurestore, Expectation expectation) throws FeaturestoreException {
    // Some expectation sanity checks
    Set<ValidationRule> validationRules = new HashSet<>();
    for (Rule rule : expectation.getRules()) {
        Optional<ValidationRule> validationRule = validationRuleFacade.findByName(rule.getName());
        if (!validationRule.isPresent()) {
            throw new FeaturestoreException(RESTCodes.FeaturestoreErrorCode.FEATURE_STORE_RULE_NOT_FOUND, FINE, "Rule: " + rule.getName());
        }
        // Check that the rule's predicate is set
        if (validationRule.get().getPredicate() == Predicate.FEATURE && Strings.isNullOrEmpty(rule.getFeature()) || validationRule.get().getPredicate() == Predicate.ACCEPTED_TYPE && rule.getAcceptedType() == null || validationRule.get().getPredicate() == Predicate.LEGAL_VALUES && (rule.getLegalValues() == null || rule.getLegalValues().isEmpty()) || validationRule.get().getPredicate() == Predicate.PATTERN && Strings.isNullOrEmpty(rule.getPattern())) {
            throw new FeaturestoreException(RESTCodes.FeaturestoreErrorCode.VALIDATION_RULE_INCOMPLETE, FINE, "Rule: " + rule.getName() + " should set " + validationRule.get().getPredicate());
        }
        if (!rule.getName().isAppliedToFeaturePairs() && rule.getMin() == null && rule.getMax() == null) {
            throw new FeaturestoreException(RESTCodes.FeaturestoreErrorCode.VALIDATION_RULE_INCOMPLETE, FINE, "At least one of min/max need to be provided for rule: " + rule.getName());
        } else // Min/max is set to 1 by default for compliance rules
        if (rule.getName().isAppliedToFeaturePairs() && rule.getMin() == null && rule.getMax() == null) {
            rule.setMin(1.0);
            rule.setMax(1.0);
        }
        validationRules.add(validationRule.get());
    }
    // Persist expectation
    FeatureStoreExpectation featureStoreExpectation;
    Optional<FeatureStoreExpectation> e = featureStoreExpectationFacade.findByFeaturestoreAndName(featurestore, expectation.getName());
    featureStoreExpectation = e.orElseGet(FeatureStoreExpectation::new);
    featureStoreExpectation.setValidationRules(validationRules);
    featureStoreExpectation.setFeatureStore(featurestore);
    featureStoreExpectation.setName(expectation.getName());
    featureStoreExpectation.setDescription(expectation.getDescription());
    featureStoreExpectation.setExpectation(expectation);
    return featureStoreExpectationFacade.merge(featureStoreExpectation);
}
Also used : FeatureStoreExpectation(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.FeatureStoreExpectation) Rule(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.Rule) ValidationRule(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.ValidationRule) FeaturestoreException(io.hops.hopsworks.exceptions.FeaturestoreException) ValidationRule(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.ValidationRule) HashSet(java.util.HashSet)

Aggregations

FeaturestoreException (io.hops.hopsworks.exceptions.FeaturestoreException)1 FeatureStoreExpectation (io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.FeatureStoreExpectation)1 Rule (io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.Rule)1 ValidationRule (io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.ValidationRule)1 HashSet (java.util.HashSet)1