Skip to content

Commit

Permalink
Merge pull request #33178 from vespa-engine/bratseth/to_uri
Browse files Browse the repository at this point in the history
Add to_uri as prophesied in the documentation
  • Loading branch information
hmusum authored Jan 25, 2025
2 parents 99826c0 + 495a7ef commit cbaef48
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.StringFieldValue;
import com.yahoo.document.datatypes.UriFieldValue;

/**
* @author bratseth
*/
public final class ToUriExpression extends Expression {

@Override
public DataType setInputType(DataType input, VerificationContext context) {
super.setInputType(input, context);
return DataType.URI;
}

@Override
public DataType setOutputType(DataType output, VerificationContext context) {
super.setOutputType(DataType.URI, output, null, context);
return getInputType(context);
}

@Override
protected void doVerify(VerificationContext context) {
if (context.getCurrentType() == null)
throw new VerificationException(this, "Expected input, but no input is provided");
context.setCurrentType(createdOutputType());
}

@Override
protected void doExecute(ExecutionContext context) {
context.setCurrentValue(new UriFieldValue(String.valueOf(context.getCurrentValue())));
}

@Override
public DataType createdOutputType() { return DataType.URI; }

@Override
public String toString() { return "to_uri"; }

@Override
public boolean equals(Object obj) {
return obj instanceof ToUriExpression;
}

@Override
public int hashCode() {
return getClass().hashCode();
}

}
45 changes: 28 additions & 17 deletions indexinglanguage/src/main/javacc/IndexingParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,17 @@ TOKEN :
<THIS: "this"> |
<TOKENIZE: "tokenize"> |
<TO_ARRAY: "to_array"> |
<TO_BYTE: "to_byte"> |
<TO_BOOL: "to_bool"> |
<TO_BYTE: "to_byte"> |
<TO_DOUBLE: "to_double"> |
<TO_FLOAT: "to_float"> |
<TO_INT: "to_int"> |
<TO_LONG: "to_long"> |
<TO_POS: "to_pos"> |
<TO_EPOCH_SECOND: "to_epoch_second"> |
<TO_STRING: "to_string"> |
<TO_URI: "to_uri"> |
<TO_WSET: "to_wset"> |
<TO_BOOL: "to_bool"> |
<TRIM: "trim"> |
<ZCURVE: "zcurve"> |
<TRUE: "true" > |
Expand Down Expand Up @@ -352,16 +353,17 @@ Expression value() :
val = thisExp() |
val = tokenizeExp() |
val = toArrayExp() |
val = toBoolExp() |
val = toByteExp() |
val = toDoubleExp() |
val = toEpochSecondExp() |
val = toFloatExp() |
val = toIntExp() |
val = toLongExp() |
val = toPosExp() |
val = toEpochSecondExp() |
val = toStringExp() |
val = toUriExp() |
val = toWsetExp() |
val = toBoolExp() |
val = trimExp() |
val = zcurveExp() |
( <LPAREN> val = statement() <RPAREN> { val = new ParenthesisExpression(val); } ) )
Expand Down Expand Up @@ -741,6 +743,12 @@ Expression toArrayExp() : { }
{ return new ToArrayExpression(); }
}

Expression toBoolExp() : { }
{
( <TO_BOOL> )
{ return new ToBoolExpression(); }
}

Expression toByteExp() : { }
{
( <TO_BYTE> )
Expand All @@ -753,6 +761,12 @@ Expression toDoubleExp() : { }
{ return new ToDoubleExpression(); }
}

Expression toEpochSecondExp() : { }
{
( <TO_EPOCH_SECOND> )
{ return new ToEpochSecondExpression(); }
}

Expression toFloatExp() : { }
{
( <TO_FLOAT> )
Expand All @@ -777,18 +791,18 @@ Expression toPosExp() : { }
{ return new ToPositionExpression(); }
}

Expression toEpochSecondExp() : { }
{
( <TO_EPOCH_SECOND> )
{ return new ToEpochSecondExpression(); }
}

Expression toStringExp() : { }
{
( <TO_STRING> )
{ return new ToStringExpression(); }
}

Expression toUriExp() : { }
{
( <TO_URI> )
{ return new ToUriExpression(); }
}

Expression toWsetExp() :
{
boolean createIfNonExistent = false;
Expand All @@ -800,12 +814,6 @@ Expression toWsetExp() :
{ return new ToWsetExpression(createIfNonExistent, removeIfZero); }
}

Expression toBoolExp() : { }
{
( <TO_BOOL> )
{ return new ToBoolExpression(); }
}

Expression trimExp() : { }
{
( <TRIM> )
Expand Down Expand Up @@ -894,14 +902,17 @@ String identifier() :
<SWITCH> |
<THIS> |
<TO_ARRAY> |
<TO_BOOL> |
<TO_BYTE> |
<TO_DOUBLE> |
<TO_EPOCH_SECOND> |
<TO_FLOAT> |
<TO_INT> |
<TO_LONG> |
<TO_POS> |
<TO_STRING> |
<TO_URI> |
<TO_WSET> |
<TO_BOOL> |
<TOKENIZE> |
<TRIM> |
<TRUE> |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.LongFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.UriFieldValue;
import com.yahoo.vespa.indexinglanguage.expressions.AttributeExpression;
import com.yahoo.vespa.indexinglanguage.expressions.ExecutionContext;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;
Expand Down Expand Up @@ -345,4 +346,21 @@ public void testMultiStatementInput() {
assertEquals("Test value", ((StringFieldValue)adapter.values.get("myOutputString")).getString());
}

@Test
public void testToUri() {
var tester = new ScriptTester();
var expression = tester.expressionFrom("input myString | to_uri | attribute myUri");

SimpleTestAdapter adapter = new SimpleTestAdapter();
var myString = new Field("myString", DataType.STRING);
adapter.createField(myString);
adapter.setValue("myString", new StringFieldValue("https://vespa.ai"));
adapter.createField(new Field("myUri", DataType.URI));

expression.verify(adapter);
ExecutionContext context = new ExecutionContext(adapter);
expression.execute(context);
assertEquals(new UriFieldValue("https://vespa.ai"), adapter.values.get("myUri"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,17 @@ TOKEN :
<THIS: "this"> |
<TOKENIZE: "tokenize"> |
<TO_ARRAY: "to_array"> |
<TO_BOOL: "to_bool"> |
<TO_BYTE: "to_byte"> |
<TO_DOUBLE: "to_double"> |
<TO_EPOCH_SECOND: "to_epoch_second"> |
<TO_FLOAT: "to_float"> |
<TO_INT: "to_int"> |
<TO_LONG: "to_long"> |
<TO_POS: "to_pos"> |
<TO_EPOCH_SECOND: "to_epoch_second"> |
<TO_STRING: "to_string"> |
<TO_URI: "to_uri"> |
<TO_WSET: "to_wset"> |
<TO_BOOL: "to_bool"> |
<TRIM: "trim"> |
<ZCURVE: "zcurve"> |
<TRUE: "true" > |
Expand Down Expand Up @@ -381,16 +382,17 @@ Expression value() :
val = thisExp() |
val = tokenizeExp() |
val = toArrayExp() |
val = toBoolExp() |
val = toByteExp() |
val = toDoubleExp() |
val = toEpochSecondExp() |
val = toFloatExp() |
val = toIntExp() |
val = toLongExp() |
val = toPosExp() |
val = toEpochSecondExp() |
val = toStringExp() |
val = toUriExp() |
val = toWsetExp() |
val = toBoolExp() |
val = trimExp() |
val = literalBoolExp() |
val = zcurveExp() |
Expand Down Expand Up @@ -785,6 +787,12 @@ Expression toArrayExp() : { }
{ return new ToArrayExpression(); }
;

Expression toBoolExp() : { }

( <TO_BOOL> )
{ return new ToBoolExpression(); }
;

Expression toByteExp() : { }

( <TO_BYTE> )
Expand All @@ -797,6 +805,12 @@ Expression toDoubleExp() : { }
{ return new ToDoubleExpression(); }
;

Expression toEpochSecondExp() : { }

( <TO_EPOCH_SECOND> )
{ return new ToEpochSecondExpression(); }
;

Expression toFloatExp() : { }

( <TO_FLOAT> )
Expand All @@ -821,18 +835,18 @@ Expression toPosExp() : { }
{ return new ToPositionExpression(); }
;

Expression toEpochSecondExp() : { }

( <TO_EPOCH_SECOND> )
{ return new ToEpochSecondExpression(); }
;

Expression toStringExp() : { }

( <TO_STRING> )
{ return new ToStringExpression(); }
;

Expression toUriExp() : { }

( <TO_URI> )
{ return new ToUriExpression(); }
;

Expression toWsetExp() :
{
boolean createIfNonExistent = false;
Expand All @@ -844,12 +858,6 @@ Expression toWsetExp() :
{ return new ToWsetExpression(createIfNonExistent, removeIfZero); }
;

Expression toBoolExp() : { }

( <TO_BOOL> )
{ return new ToBoolExpression(); }
;

Expression trimExp() : { }

( <TRIM> )
Expand Down Expand Up @@ -928,14 +936,17 @@ String identifierStr() :
<SWITCH> |
<THIS> |
<TO_ARRAY> |
<TO_BOOL> |
<TO_BYTE> |
<TO_DOUBLE> |
<TO_EPOCH_SECOND> |
<TO_FLOAT> |
<TO_INT> |
<TO_LONG> |
<TO_POS> |
<TO_STRING> |
<TO_URI> |
<TO_WSET> |
<TO_BOOL> |
<TOKENIZE> |
<TRIM> |
<TRUE> |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Tests that the set of tokens declared in JavaCC parsers are also present in CongoCC parsers.
*/
public class ParserTokensTest {

// These are special
public static Set<String> javaCCSpecialTokens = Set.of(
"DEFAULT", // Lexical State in JavaCC parser
Expand Down Expand Up @@ -81,7 +82,6 @@ public void testSchemaTokenList() {
Field[] javaCCFields = SchemaParserConstants.class.getDeclaredFields();

Set<String> congoCCTokenStrings = new HashSet<>();

for (var tokenType : SchemaParserLexer.getRegularTokens()) {
congoCCTokenStrings.add(tokenType.toString());
}
Expand All @@ -96,7 +96,6 @@ public void testIndexingTokenList() {
Field[] javaCCFields = IndexingParserConstants.class.getDeclaredFields();

Set<String> congoCCTokenStrings = new HashSet<>();

for (var tokenType : IndexingParserLexer.getRegularTokens()) {
congoCCTokenStrings.add(tokenType.toString());
}
Expand All @@ -110,7 +109,6 @@ public void testRankingExpressionTokenList() {
Field[] javaCCFields = RankingExpressionParserConstants.class.getDeclaredFields();

Set<String> congoCCTokenStrings = new HashSet<>();

for (var tokenType : RankingExpressionParserLexer.getRegularTokens()) {
congoCCTokenStrings.add(tokenType.toString());
}
Expand Down Expand Up @@ -162,6 +160,6 @@ public void testYQLPlusTokenList() {
}

assertEquals(0, missing.size(), "Missing yqlplus tokens in CongoCC: " + String.join(", ", missing));

}

}

0 comments on commit cbaef48

Please sign in to comment.