Search in sources :

Example 1 with Predicate

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

the class OptimizePredicateTestCase method requireThatOptimizerIsCalledWithCloneOfInput.

@Test
public void requireThatOptimizerIsCalledWithCloneOfInput() {
    final Predicate predicateA = Mockito.mock(Predicate.class);
    final Predicate predicateB = Mockito.mock(Predicate.class);
    final PredicateFieldValue input = new PredicateFieldValue(predicateA);
    ExecutionContext ctx = new ExecutionContext().setValue(input).setVariable("arity", new IntegerFieldValue(10));
    FieldValue output = new OptimizePredicateExpression((predicate, options) -> {
        assertNotSame(predicateA, predicate);
        return predicateB;
    }).execute(ctx);
    assertNotSame(output, input);
    assertTrue(output instanceof PredicateFieldValue);
    assertSame(predicateB, ((PredicateFieldValue) output).getPredicate());
}
Also used : Mockito(org.mockito.Mockito) ExpressionAssert.assertVerifyCtx(com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtx) Test(org.junit.Test) DataType(com.yahoo.document.DataType) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ExpressionAssert.assertVerifyCtxThrows(com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtxThrows) FieldValue(com.yahoo.document.datatypes.FieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) Assert(org.junit.Assert) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ExpressionAssert.assertVerifyThrows(com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyThrows) Predicate(com.yahoo.document.predicate.Predicate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Example 2 with Predicate

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

the class FeatureConjunctionTransformer method convertConjunction.

/**
 * Conversion rules:
 *  1) A {@link FeatureConjunction} may only consist of FeatureSets having unique keys.
 *     If multiple {@link FeatureSet} share the same key, they have to be placed into separate FeatureConjunctions.
 *  2) A FeatureConjunction must have at least 2 operands.
 *  3) Any operand that is not a FeatureSet, negated or not,
 *     (e.g {@link FeatureRange}) cannot be placed into a FeatureConjunction.
 *  4) All FeatureSets may only have a single value.
 *
 *  See the tests in FeatureConjunctionTransformerTest for conversion examples.
 */
private static Predicate convertConjunction(Conjunction conjunction, long nValidOperands) {
    List<Predicate> operands = conjunction.getOperands();
    // All operands are instance of FeatureSet are valid and may therefor be placed into a single FeatureConjunction.
    if (nValidOperands == operands.size()) {
        return new FeatureConjunction(operands);
    }
    List<Predicate> invalidFeatureConjunctionOperands = new ArrayList<>();
    List<Map<String, Predicate>> featureConjunctionOperandsList = new ArrayList<>();
    featureConjunctionOperandsList.add(new TreeMap<>());
    for (Predicate operand : operands) {
        if (FeatureConjunction.isValidFeatureConjunctionOperand(operand)) {
            addFeatureConjunctionOperand(featureConjunctionOperandsList, operand);
        } else {
            invalidFeatureConjunctionOperands.add(operand);
        }
    }
    // Create a Conjunction root.
    Conjunction newConjunction = new Conjunction();
    newConjunction.addOperands(invalidFeatureConjunctionOperands);
    // For all operand partitions: create FeatureConjunction if partition has more than a single predicate.
    for (Map<String, Predicate> featureConjunctionOperands : featureConjunctionOperandsList) {
        Collection<Predicate> values = featureConjunctionOperands.values();
        if (featureConjunctionOperands.size() == 1) {
            // Add single operand directly to root conjunction.
            newConjunction.addOperands(values);
        } else {
            newConjunction.addOperand(new FeatureConjunction(new ArrayList<>(values)));
        }
    }
    return newConjunction;
}
Also used : FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) ArrayList(java.util.ArrayList) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) Conjunction(com.yahoo.document.predicate.Conjunction) TreeMap(java.util.TreeMap) Map(java.util.Map) Predicate(com.yahoo.document.predicate.Predicate)

Example 3 with Predicate

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

the class PredicateTreeAnalyzerTest method require_that_minfeature_rounds_up.

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

Example 4 with Predicate

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

the class PredicateTreeAnalyzerTest method require_that_multilevel_and_stores_size.

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

Example 5 with Predicate

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

the class PredicateTreeAnalyzerTest method require_that_minfeature_is_1_for_simple_term.

@Test
public void require_that_minfeature_is_1_for_simple_term() {
    Predicate p = feature("foo").inSet("bar");
    PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
    assertEquals(1, r.minFeature);
    assertEquals(1, r.treeSize);
    assertTrue(r.sizeMap.isEmpty());
}
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