Search in sources :

Example 31 with Predicate

use of com.yahoo.document.predicate.Predicate in project vespa by vespa-engine.

the class OrSimplifierTest method require_that_or_below_and_is_converted.

@Test
public void require_that_or_below_and_is_converted() {
    Predicate p = and(or(feature("key1").inSet("value1"), feature("key1").inSet("value2")), feature("key2").inSet("value2"));
    Predicate expected = and(feature("key1").inSet("value1", "value2"), feature("key2").inSet("value2"));
    assertConvertedPredicateEquals(expected, p);
}
Also used : Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Example 32 with Predicate

use of com.yahoo.document.predicate.Predicate in project vespa by vespa-engine.

the class PredicateTreeAnalyzer method findMinFeature.

// Second analysis pass. Traverses tree in depth-first order. Determines the min-feature value.
private static double findMinFeature(Predicate predicate, boolean isNegated, AnalyzerContext context) {
    if (predicate instanceof Conjunction) {
        // Sum of children values.
        return ((Conjunction) predicate).getOperands().stream().mapToDouble(child -> findMinFeature(child, isNegated, context)).sum();
    } else if (predicate instanceof FeatureConjunction) {
        if (isNegated) {
            return 0.0;
        }
        // The FeatureConjunction is handled as a leaf node in the interval algorithm.
        IndexableFeatureConjunction ifc = new IndexableFeatureConjunction((FeatureConjunction) predicate);
        return 1.0 / context.conjunctionOccurrences.get(ifc.id);
    } else if (predicate instanceof Disjunction) {
        // Minimum value of children.
        return ((Disjunction) predicate).getOperands().stream().mapToDouble(child -> findMinFeature(child, isNegated, context)).min().getAsDouble();
    } else if (predicate instanceof Negation) {
        return findMinFeature(((Negation) predicate).getOperand(), !isNegated, context);
    } else if (predicate instanceof FeatureSet) {
        if (isNegated) {
            return 0.0;
        }
        double minFeature = 1.0;
        FeatureSet featureSet = (FeatureSet) predicate;
        for (String value : featureSet.getValues()) {
            long featureHash = Feature.createHash(featureSet.getKey(), value);
            // Clever mathematics to handle scenarios where same feature is used several places in predicate tree.
            minFeature = Math.min(minFeature, 1.0 / context.featureOccurrences.get(featureHash));
        }
        return minFeature;
    } else if (predicate instanceof FeatureRange) {
        if (isNegated) {
            return 0.0;
        }
        return 1.0 / context.featureOccurrences.get(PredicateHash.hash64(((FeatureRange) predicate).getKey()));
    } else {
        throw new UnsupportedOperationException("Cannot handle predicate of type " + predicate.getClass().getSimpleName());
    }
}
Also used : FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet) Map(java.util.Map) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) PredicateHash(com.yahoo.document.predicate.PredicateHash) HashMap(java.util.HashMap) Feature(com.yahoo.search.predicate.index.Feature) Conjunction(com.yahoo.document.predicate.Conjunction) Disjunction(com.yahoo.document.predicate.Disjunction) Predicate(com.yahoo.document.predicate.Predicate) Disjunction(com.yahoo.document.predicate.Disjunction) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Conjunction(com.yahoo.document.predicate.Conjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet)

Example 33 with Predicate

use of com.yahoo.document.predicate.Predicate in project vespa by vespa-engine.

the class OrSimplifier method simplifyTree.

public Predicate simplifyTree(Predicate predicate) {
    if (predicate instanceof Disjunction) {
        Disjunction disjunction = (Disjunction) predicate;
        List<Predicate> newChildren = disjunction.getOperands().stream().map(this::simplifyTree).collect(toList());
        return compressFeatureSets(newChildren);
    } else if (predicate instanceof Negation) {
        Negation negation = (Negation) predicate;
        negation.setOperand(simplifyTree(negation.getOperand()));
        return negation;
    } else if (predicate instanceof Conjunction) {
        Conjunction conjunction = (Conjunction) predicate;
        List<Predicate> newChildren = conjunction.getOperands().stream().map(this::simplifyTree).collect(toList());
        conjunction.setOperands(newChildren);
        return conjunction;
    } else {
        return predicate;
    }
}
Also used : Disjunction(com.yahoo.document.predicate.Disjunction) Negation(com.yahoo.document.predicate.Negation) Conjunction(com.yahoo.document.predicate.Conjunction) Predicate(com.yahoo.document.predicate.Predicate)

Example 34 with Predicate

use of com.yahoo.document.predicate.Predicate in project vespa by vespa-engine.

the class NotNodeReordererTest method checkReorder.

private static void checkReorder(Predicate input, Predicate expected) {
    NotNodeReorderer reorderer = new NotNodeReorderer();
    Predicate actual = reorderer.process(input, new PredicateOptions(10));
    assertEquals(expected, actual);
}
Also used : Predicate(com.yahoo.document.predicate.Predicate)

Example 35 with Predicate

use of com.yahoo.document.predicate.Predicate in project vespa by vespa-engine.

the class PredicateTreeAnalyzerTest method require_that_featureconjunctions_count_as_leaf_in_subtree_calculation.

@Test
public void require_that_featureconjunctions_count_as_leaf_in_subtree_calculation() {
    Predicate p = and(and(feature("grault").inRange(0, 10), feature("waldo").inRange(0, 10)), conj(feature("foo").inSet("bar"), feature("baz").inSet("qux"), feature("quux").inSet("corge")));
    PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
    assertEquals(3, r.minFeature);
    assertEquals(3, r.treeSize);
    assertEquals(4, r.sizeMap.size());
    assertSizeMapContains(r, pred(p).child(0), 2);
    assertSizeMapContains(r, pred(p).child(0).child(0), 1);
    assertSizeMapContains(r, pred(p).child(0).child(1), 1);
    assertSizeMapContains(r, pred(p).child(1), 1);
}
Also used : Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Aggregations

Predicate (com.yahoo.document.predicate.Predicate)45 Test (org.junit.Test)33 Conjunction (com.yahoo.document.predicate.Conjunction)7 Disjunction (com.yahoo.document.predicate.Disjunction)6 Negation (com.yahoo.document.predicate.Negation)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FeatureConjunction (com.yahoo.document.predicate.FeatureConjunction)3 FeatureRange (com.yahoo.document.predicate.FeatureRange)3 FeatureSet (com.yahoo.document.predicate.FeatureSet)3 IndexableFeatureConjunction (com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction)3 Map (java.util.Map)3 DataType (com.yahoo.document.DataType)2 FieldValue (com.yahoo.document.datatypes.FieldValue)2 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)2 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)2 PredicateFieldValue (com.yahoo.document.datatypes.PredicateFieldValue)2 RangePartition (com.yahoo.document.predicate.RangePartition)2 ExpressionAssert.assertVerifyCtx (com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtx)2 ExpressionAssert.assertVerifyCtxThrows (com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtxThrows)2