diff options
author | Jon Bratseth <bratseth@gmail.com> | 2023-03-24 11:50:56 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2023-03-24 11:50:56 +0100 |
commit | 4fe77d7309056c81c713a2095339fd463ee604b4 (patch) | |
tree | 1b6c3e52d02856ffbf106412da4fc8c4c1b06c66 /indexinglanguage | |
parent | 6db94ff887b65b10b8c4a8ce10c0d2e195bc0c0a (diff) |
Retrieve execution value explicitly by '_'
Diffstat (limited to 'indexinglanguage')
3 files changed, 101 insertions, 0 deletions
diff --git a/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/ExecutionValueExpression.java b/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/ExecutionValueExpression.java new file mode 100644 index 00000000000..a4c430125a4 --- /dev/null +++ b/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/ExecutionValueExpression.java @@ -0,0 +1,54 @@ +package com.yahoo.vespa.indexinglanguage.expressions; + +import com.yahoo.document.DataType; +import com.yahoo.document.DocumentType; +import com.yahoo.document.FieldPath; +import com.yahoo.vespa.objects.ObjectOperation; +import com.yahoo.vespa.objects.ObjectPredicate; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Returns the current execution value, that is the value passed to this expression. + * Referring to this explicitly is useful e.g to concatenate it to some other string: + * ... | input foo . " " . _ | ... + * + * @author bratseth + */ +public final class ExecutionValueExpression extends Expression { + + public ExecutionValueExpression() { + super(null); + } + + @Override + protected void doExecute(ExecutionContext context) { + // Noop: Set the output execution value to the current execution value + } + + @Override + protected void doVerify(VerificationContext context) {} + + @Override + public DataType createdOutputType() { + return UnresolvedDataType.INSTANCE; + } + + @Override + public String toString() { + return "_"; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof ExecutionValueExpression; + } + + @Override + public int hashCode() { + return 9875876; + } + +} diff --git a/indexinglanguage/src/main/javacc/IndexingParser.jj b/indexinglanguage/src/main/javacc/IndexingParser.jj index 9f0b4733119..a039ad137ee 100644 --- a/indexinglanguage/src/main/javacc/IndexingParser.jj +++ b/indexinglanguage/src/main/javacc/IndexingParser.jj @@ -205,6 +205,7 @@ TOKEN : <ZCURVE: "zcurve"> | <TRUE: "true" > | <FALSE: "false" > | + <UNDERSCORE: "_"> | <IDENTIFIER: ["a"-"z","A"-"Z", "_"] (["a"-"z","A"-"Z","0"-"9","_","-"])*> } @@ -343,6 +344,7 @@ Expression value() : val = trimExp() | val = literalBoolExp() | val = zcurveExp() | + val = executionValueExp() | ( <LPAREN> val = statement() <RPAREN> { val = new ParenthesisExpression(val); } ) ) { return val; } } @@ -752,6 +754,12 @@ Expression zcurveExp() : { } { return new ZCurveExpression(); } } +Expression executionValueExp() : { } +{ + ( <UNDERSCORE> ) + { return new ExecutionValueExpression(); } +} + String identifier() : { String val; diff --git a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java index ce42f4e727f..98458dd965c 100644 --- a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java +++ b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java @@ -276,6 +276,45 @@ public class ScriptTestCase { } @Test + public void testArrayEmbedWithConcatenation() throws ParseException { + Map<String, Embedder> embedders = Map.of("emb1", new MockEmbedder("myDocument.mySparseTensor")); + + TensorType tensorType = TensorType.fromSpec("tensor(passage{}, d[4])"); + var expression = Expression.fromString("input myTextArray | for_each { input title . \" \" . _ } | embed | attribute 'mySparseTensor'", + new SimpleLinguistics(), + embedders); + + SimpleTestAdapter adapter = new SimpleTestAdapter(); + adapter.createField(new Field("myTextArray", new ArrayDataType(DataType.STRING))); + + var tensorField = new Field("mySparseTensor", new TensorDataType(tensorType)); + adapter.createField(tensorField); + + var array = new Array<StringFieldValue>(new ArrayDataType(DataType.STRING)); + array.add(new StringFieldValue("first")); + array.add(new StringFieldValue("second")); + adapter.setValue("myTextArray", array); + + var titleField = new Field("title", DataType.STRING); + adapter.createField(titleField); + adapter.setValue("title", new StringFieldValue("title1")); + + expression.setStatementOutput(new DocumentType("myDocument"), tensorField); + + // Necessary to resolve output type + VerificationContext verificationContext = new VerificationContext(adapter); + assertEquals(new TensorDataType(tensorType), expression.verify(verificationContext)); + + ExecutionContext context = new ExecutionContext(adapter); + context.setValue(array); + expression.execute(context); + assertTrue(adapter.values.containsKey("mySparseTensor")); + var sparseTensor = (TensorFieldValue)adapter.values.get("mySparseTensor"); + assertEquals(Tensor.from(tensorType, "{ '0':[116.0, 105.0, 116.0, 108.0], 1:[116.0, 105.0, 116.0, 108.0]}"), + sparseTensor.getTensor().get()); + } + + @Test public void testArrayEmbedToSparseTensor() throws ParseException { Map<String, Embedder> embedders = Map.of("emb1", new MockEmbedder("myDocument.mySparseTensor")); |