use of com.yahoo.vespa.indexinglanguage.expressions.Expression in project vespa by vespa-engine.
the class DocumentTestCase method requireThatConcatenationWorks.
@Test
public void requireThatConcatenationWorks() throws ParseException {
DocumentType docType = new DocumentType("my_input");
docType.addField(new Field("arr_a", DataType.getArray(DataType.STRING)));
docType.addField(new Field("arr_b", DataType.getArray(DataType.STRING)));
docType.addField(new Field("out", DataType.getArray(DataType.STRING)));
Expression exp = Expression.fromString("input arr_a . input arr_b | index out");
{
Document doc = new Document(docType, "doc:scheme:");
assertNotNull(doc = Expression.execute(exp, doc));
FieldValue val = doc.getFieldValue("out");
assertNotNull(val);
assertEquals(DataType.getArray(DataType.STRING), val.getDataType());
assertTrue(val instanceof Array);
Array arr = (Array) val;
assertEquals(0, arr.size());
}
{
Document doc = new Document(docType, "doc:scheme:");
Array<StringFieldValue> arr = new Array<>(DataType.getArray(DataType.STRING));
arr.add(new StringFieldValue("a1"));
doc.setFieldValue("arr_a", arr);
assertNotNull(doc = Expression.execute(exp, doc));
FieldValue val = doc.getFieldValue("out");
assertNotNull(val);
assertEquals(DataType.getArray(DataType.STRING), val.getDataType());
assertTrue(val instanceof Array);
arr = (Array) val;
assertEquals(1, arr.size());
}
{
Document doc = new Document(docType, "doc:scheme:");
Array<StringFieldValue> arr = new Array<>(DataType.getArray(DataType.STRING));
arr.add(new StringFieldValue("a1"));
arr.add(new StringFieldValue("a2"));
doc.setFieldValue("arr_a", arr);
arr = new Array<StringFieldValue>(DataType.getArray(DataType.STRING));
arr.add(new StringFieldValue("b1"));
doc.setFieldValue("arr_b", arr);
assertNotNull(doc = Expression.execute(exp, doc));
FieldValue val = doc.getFieldValue("out");
assertNotNull(val);
assertEquals(DataType.getArray(DataType.STRING), val.getDataType());
assertTrue(val instanceof Array);
arr = (Array) val;
assertEquals(3, arr.size());
}
}
use of com.yahoo.vespa.indexinglanguage.expressions.Expression in project vespa by vespa-engine.
the class ExpressionConverterTestCase method requireThatConversionExceptionCanBeThrown.
@Test
public void requireThatConversionExceptionCanBeThrown() {
final RuntimeException expectedCause = new RuntimeException();
try {
new ExpressionConverter() {
@Override
protected boolean shouldConvert(Expression exp) {
return exp instanceof AttributeExpression;
}
@Override
protected Expression doConvert(Expression exp) {
throw expectedCause;
}
}.convert(new StatementExpression(new AttributeExpression("foo")));
fail();
} catch (RuntimeException e) {
assertSame(expectedCause, e);
}
}
use of com.yahoo.vespa.indexinglanguage.expressions.Expression in project vespa by vespa-engine.
the class ExpressionConverterTestCase method requireThatCatConversionIgnoresNull.
@Test
public void requireThatCatConversionIgnoresNull() {
Expression exp = new ExpressionConverter() {
@Override
protected boolean shouldConvert(Expression exp) {
return exp instanceof AttributeExpression;
}
@Override
protected Expression doConvert(Expression exp) {
return null;
}
}.convert(new CatExpression(new AttributeExpression("foo"), new IndexExpression("bar")));
assertEquals(new CatExpression(new IndexExpression("bar")), exp);
}
use of com.yahoo.vespa.indexinglanguage.expressions.Expression in project vespa by vespa-engine.
the class ExpressionSearcherTestCase method requireThatExpressionsCanBeFound.
@Test
public void requireThatExpressionsCanBeFound() {
IndexExpression exp = new IndexExpression("foo");
assertFound(exp, new ArithmeticExpression(exp, ArithmeticExpression.Operator.ADD, new AttributeExpression("foo")));
assertFound(exp, new ArithmeticExpression(new AttributeExpression("foo"), ArithmeticExpression.Operator.ADD, exp));
assertFound(exp, new CatExpression(exp));
assertFound(exp, new CatExpression(new AttributeExpression("foo"), exp));
assertFound(exp, new ForEachExpression(exp));
assertFound(exp, new GuardExpression(exp));
assertFound(exp, new IfThenExpression(exp, IfThenExpression.Comparator.EQ, new AttributeExpression("foo"), new AttributeExpression("bar"), new AttributeExpression("baz")));
assertFound(exp, new IfThenExpression(new AttributeExpression("foo"), IfThenExpression.Comparator.EQ, exp, new AttributeExpression("bar"), new AttributeExpression("baz")));
assertFound(exp, new IfThenExpression(new AttributeExpression("foo"), IfThenExpression.Comparator.EQ, new AttributeExpression("bar"), exp, new AttributeExpression("baz")));
assertFound(exp, new IfThenExpression(new AttributeExpression("foo"), IfThenExpression.Comparator.EQ, new AttributeExpression("bar"), new AttributeExpression("baz"), exp));
assertFound(exp, new ParenthesisExpression(exp));
assertFound(exp, new ScriptExpression(new StatementExpression(exp)));
assertFound(exp, new ScriptExpression(new StatementExpression(new AttributeExpression("foo")), new StatementExpression(exp)));
assertFound(exp, new SelectInputExpression(Arrays.asList(new Pair<String, Expression>("foo", exp), new Pair<String, Expression>("bar", new AttributeExpression("bar")))));
assertFound(exp, new SelectInputExpression(Arrays.asList(new Pair<String, Expression>("foo", new AttributeExpression("bar")), new Pair<String, Expression>("bar", exp))));
assertFound(exp, new StatementExpression(exp));
assertFound(exp, new StatementExpression(new AttributeExpression("foo"), exp));
assertFound(exp, new SwitchExpression(Collections.singletonMap("foo", exp), new AttributeExpression("bar")));
assertFound(exp, new SwitchExpression(Collections.singletonMap("foo", new AttributeExpression("bar")), exp));
}
Aggregations