aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/OptimizePredicateTestCase.java
blob: 73a906ad3385f67a888fd4d375f1f452ffe7683d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.LongFieldValue;
import com.yahoo.document.datatypes.PredicateFieldValue;
import com.yahoo.document.predicate.Predicate;
import org.junit.Test;
import org.mockito.Mockito;

import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtx;
import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtxThrows;
import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyThrows;
import static org.junit.Assert.*;

/**
 * @author Simon Thoresen Hult
 */
public class OptimizePredicateTestCase {

    @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());
    }

    @Test
    public void requireThatPredicateOptionsAreSet() {
        final Predicate predicate = Mockito.mock(Predicate.class);
        final PredicateFieldValue input = new PredicateFieldValue(predicate);
        ExecutionContext ctx = new ExecutionContext()
                .setValue(input)
                .setVariable("arity", new IntegerFieldValue(10));
        new OptimizePredicateExpression((predicate1, options) -> {
            assertEquals(10, options.getArity());
            assertEquals(0x8000000000000000L, options.getLowerBound());
            assertEquals(0x7fffffffffffffffL, options.getUpperBound());
            return predicate1;
        }).execute(ctx);
        ctx.setVariable("upper_bound", new LongFieldValue(1000));
        ctx.setVariable("lower_bound", new LongFieldValue(0));
        new OptimizePredicateExpression(
                (value, options) -> {
                    assertEquals(10, options.getArity());
                    assertEquals(0, options.getLowerBound());
                    assertEquals(1000, options.getUpperBound());
                    return value;
                }
        ).execute(ctx);
    }

    @Test
    public void requireThatHashCodeAndEqualsAreImplemented() {
        Expression exp = new OptimizePredicateExpression();
        assertTrue(exp.equals(exp));
        assertFalse(exp.equals(new Object()));
        assertTrue(exp.equals(new OptimizePredicateExpression()));
        assertEquals(exp.hashCode(), new OptimizePredicateExpression().hashCode());
    }

    @Test
    public void requireThatExpressionCanBeVerified() {
        Expression exp = new OptimizePredicateExpression();
        assertVerifyThrows(null, exp, "Expected predicate input, but no input is specified");
        assertVerifyThrows(DataType.INT, exp, "Expected predicate input, got int");
        assertVerifyThrows(DataType.PREDICATE, exp, "Variable 'arity' must be set");

        VerificationContext context = new VerificationContext().setValueType(DataType.PREDICATE);
        context.setVariable("arity", DataType.STRING);
        assertVerifyCtxThrows(context, exp, "Variable 'arity' must have type int");
        context.setVariable("arity", DataType.INT);
        assertVerifyCtx(context, exp, DataType.PREDICATE);
        context.setVariable("lower_bound", DataType.INT);
        assertVerifyCtxThrows(context, exp, "Variable 'lower_bound' must have type long");
        context.setVariable("lower_bound", DataType.LONG);
        assertVerifyCtx(context, exp, DataType.PREDICATE);
        context.setVariable("upper_bound", DataType.INT);
        assertVerifyCtxThrows(context, exp,  "Variable 'upper_bound' must have type long");
        context.setVariable("upper_bound", DataType.LONG);
        assertVerifyCtx(context, exp, DataType.PREDICATE);
    }
}