Search in sources :

Example 6 with Predicate

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

the class PredicateTreeAnalyzerTest method require_that_multiple_indentical_feature_conjunctions_does_not_contribute_more_than_one.

@Test
public void require_that_multiple_indentical_feature_conjunctions_does_not_contribute_more_than_one() {
    Predicate p = and(or(conj(feature("a").inSet("b"), feature("c").inSet("d")), feature("x").inSet("y")), or(conj(feature("a").inSet("b"), feature("c").inSet("d")), feature("z").inSet("w")));
    PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
    assertEquals(1, r.minFeature);
    assertEquals(4, r.treeSize);
}
Also used : Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Example 7 with Predicate

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

the class PredicateTreeAnalyzerTest method require_that_featureconjunctions_contribute_as_one_feature.

@Test
public void require_that_featureconjunctions_contribute_as_one_feature() {
    Predicate p = conj(feature("foo").inSet("bar"), feature("baz").inSet("qux"));
    PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
    assertEquals(1, r.minFeature);
    assertEquals(1, r.treeSize);
}
Also used : Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Example 8 with Predicate

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

the class PredicateTreeAnnotator method assignIntervalLabels.

/**
 * Visits the predicate tree in depth-first order and assigns intervals for features in
 * {@link com.yahoo.document.predicate.FeatureSet} and {@link com.yahoo.document.predicate.FeatureRange}.
 */
private static void assignIntervalLabels(Predicate predicate, int begin, int end, boolean isNegated, AnnotatorContext context) {
    // Otherwise, conjunctions and disjunctions must be switched if negated (De Morgan's law).
    if (predicate instanceof Conjunction) {
        List<Predicate> children = ((Conjunction) predicate).getOperands();
        int current = begin;
        for (int i = 0; i < children.size(); i++) {
            Predicate child = children.get(i);
            int subTreeSize = context.subTreeSizes.get(child);
            if (i == children.size() - 1) {
                // Last child (and sometimes the only one)
                assignIntervalLabels(child, current, end, isNegated, context);
            // No need to update/touch current since this is the last child.
            } else if (i == 0) {
                // First child
                int next = context.leftNodeLeaves + subTreeSize + 1;
                assignIntervalLabels(child, current, next - 1, isNegated, context);
                current = next;
            } else {
                // Middle children
                int next = current + subTreeSize;
                assignIntervalLabels(child, current, next - 1, isNegated, context);
                current = next;
            }
        }
    } else if (predicate instanceof FeatureConjunction) {
        // Register FeatureConjunction as it was a FeatureSet with a single child.
        // Note: FeatureConjunction should never be negated as AndOrSimplifier will push negations down to
        // the leafs (FeatureSets).
        int zStarEnd = isNegated ? calculateZStarIntervalEnd(end, context) : end;
        IndexableFeatureConjunction indexable = new IndexableFeatureConjunction((FeatureConjunction) predicate);
        int interval = Interval.fromBoundaries(begin, zStarEnd);
        context.featureConjunctions.computeIfAbsent(indexable, (k) -> new ArrayList<>()).add(interval);
        if (isNegated) {
            registerZStarInterval(begin, end, zStarEnd, context);
        }
        context.leftNodeLeaves += 1;
    } else if (predicate instanceof Disjunction) {
        // the values will be same as that of the parent OR node
        for (Predicate child : ((Disjunction) predicate).getOperands()) {
            assignIntervalLabels(child, begin, end, isNegated, context);
        }
    } else if (predicate instanceof FeatureSet) {
        FeatureSet featureSet = (FeatureSet) predicate;
        int zStarEnd = isNegated ? calculateZStarIntervalEnd(end, context) : end;
        for (String value : featureSet.getValues()) {
            long featureHash = Feature.createHash(featureSet.getKey(), value);
            int interval = Interval.fromBoundaries(begin, zStarEnd);
            registerFeatureInterval(featureHash, interval, context.intervals);
        }
        if (isNegated) {
            registerZStarInterval(begin, end, zStarEnd, context);
        }
        context.leftNodeLeaves += 1;
    } else if (predicate instanceof Negation) {
        assignIntervalLabels(((Negation) predicate).getOperand(), begin, end, !isNegated, context);
    } else if (predicate instanceof FeatureRange) {
        FeatureRange featureRange = (FeatureRange) predicate;
        int zStarEnd = isNegated ? calculateZStarIntervalEnd(end, context) : end;
        int interval = Interval.fromBoundaries(begin, zStarEnd);
        for (RangePartition partition : featureRange.getPartitions()) {
            long featureHash = PredicateHash.hash64(partition.getLabel());
            registerFeatureInterval(featureHash, interval, context.intervals);
        }
        for (RangeEdgePartition edgePartition : featureRange.getEdgePartitions()) {
            long featureHash = PredicateHash.hash64(edgePartition.getLabel());
            IntervalWithBounds intervalWithBounds = new IntervalWithBounds(interval, (int) edgePartition.encodeBounds());
            registerFeatureInterval(featureHash, intervalWithBounds, context.intervalsWithBounds);
        }
        if (isNegated) {
            registerZStarInterval(begin, end, zStarEnd, context);
        }
        context.leftNodeLeaves += 1;
    } else {
        throw new UnsupportedOperationException("Cannot handle predicate of type " + predicate.getClass().getSimpleName());
    }
}
Also used : RangePartition(com.yahoo.document.predicate.RangePartition) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) 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) 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) RangeEdgePartition(com.yahoo.document.predicate.RangeEdgePartition) IntervalWithBounds(com.yahoo.search.predicate.index.IntervalWithBounds)

Example 9 with Predicate

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

the class PredicateTreeAnnotatorTest method require_that_nots_get_correct_intervals.

@Test
public void require_that_nots_get_correct_intervals() {
    Predicate p = and(feature("key").inSet("value"), not(feature("key").inSet("value")), feature("key").inSet("value"), not(feature("key").inSet("value")));
    PredicateTreeAnnotations r = PredicateTreeAnnotator.createPredicateTreeAnnotations(p);
    assertEquals(2, r.minFeature);
    assertEquals(6, r.intervalEnd);
    assertEquals(2, r.intervalMap.size());
    assertIntervalContains(r, "key=value", 0x00010001, 0x00020002, 0x00040004, 0x00050005);
    assertIntervalContains(r, Feature.Z_STAR_COMPRESSED_ATTRIBUTE_NAME, 0x00020001, 0x00050004);
}
Also used : Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Example 10 with Predicate

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

the class PredicateTreeAnnotatorTest method require_that_final_first_not_interval_is_extended.

@Test
public void require_that_final_first_not_interval_is_extended() {
    Predicate p = not(feature("key").inSet("A"));
    PredicateTreeAnnotations r = PredicateTreeAnnotator.createPredicateTreeAnnotations(p);
    assertEquals(1, r.minFeature);
    assertEquals(2, r.intervalEnd);
    assertEquals(2, r.intervalMap.size());
    assertIntervalContains(r, "key=A", 0x00010001);
    assertIntervalContains(r, Feature.Z_STAR_COMPRESSED_ATTRIBUTE_NAME, 0x00010000);
}
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