diff options
Diffstat (limited to 'searchlib/src/main')
5 files changed, 46 insertions, 25 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java index bb4af7d71f3..b9323e1ccb8 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java @@ -47,8 +47,8 @@ public class LongValue extends DoubleCompatibleValue { } @Override - public DoubleValue negate() { - return new DoubleValue(-value); + public Value negate() { + return new LongValue(-value); } private UnsupportedOperationException unsupported(String operation, Value value) { diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ConstantNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ConstantNode.java index 68134cc85b4..ffbeec37c78 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ConstantNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ConstantNode.java @@ -19,7 +19,8 @@ public final class ConstantNode extends ExpressionNode { private final Value value; public ConstantNode(Value value) { - this(value,null); + value.freeze(); + this.value = value; } /** @@ -28,19 +29,20 @@ public final class ConstantNode extends ExpressionNode { * @param value the value. Ownership of this value is transferred to this. * @param sourceImage the source string image producing this value */ + @Deprecated public ConstantNode(Value value, String sourceImage) { - value.freeze(); - this.value = value; + this(value); } public Value getValue() { return value; } @Override public StringBuilder toString(StringBuilder string, SerializationContext context, Deque<String> path, CompositeNode parent) { - return string.append(sourceString()); + return string.append(value.toString()); } /** Returns the string which created this, or the value.toString() if not known */ + @Deprecated public String sourceString() { return value.toString(); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/NegativeNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/NegativeNode.java index 57b349fdc2e..9516f38a155 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/NegativeNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/NegativeNode.java @@ -30,7 +30,7 @@ public class NegativeNode extends CompositeNode { @Override public List<ExpressionNode> children() { - return Collections.singletonList(value); + return List.of(value); } @Override diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/Simplifier.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/Simplifier.java index a56106e8f9d..b48303ae98b 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/Simplifier.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/Simplifier.java @@ -1,7 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.transform; -import com.yahoo.document.update.ArithmeticValueUpdate; +import com.yahoo.searchlib.rankingexpression.evaluation.DoubleCompatibleValue; import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue; import com.yahoo.searchlib.rankingexpression.evaluation.Value; import com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode; @@ -12,9 +12,9 @@ import com.yahoo.searchlib.rankingexpression.rule.ConstantNode; import com.yahoo.searchlib.rankingexpression.rule.EmbracedNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.IfNode; +import com.yahoo.searchlib.rankingexpression.rule.NegativeNode; import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; import com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode; -import com.yahoo.tensor.functions.TensorFunction; import java.util.ArrayList; import java.util.List; @@ -36,6 +36,8 @@ public class Simplifier extends ExpressionTransformer<TransformContext> { node = ((EmbracedNode)node).children().get(0); if (node instanceof ArithmeticNode) node = transformArithmetic((ArithmeticNode) node); + if (node instanceof NegativeNode) + node = transformNegativeNode((NegativeNode) node); return node; } @@ -107,6 +109,14 @@ public class Simplifier extends ExpressionTransformer<TransformContext> { return node.getFalseExpression(); } + private ExpressionNode transformNegativeNode(NegativeNode node) { + if ( ! (node.getValue() instanceof ConstantNode) ) return node; + + ConstantNode constant = (ConstantNode) node.getValue(); + if ( ! (constant.getValue() instanceof DoubleCompatibleValue)) return node; + return new ConstantNode(constant.getValue().negate() ); + } + private boolean allMultiplicationOrDivision(ArithmeticNode node) { for (ArithmeticOperator o : node.operators()) if (o != ArithmeticOperator.MULTIPLY && o != ArithmeticOperator.DIVIDE) diff --git a/searchlib/src/main/javacc/RankingExpressionParser.jj b/searchlib/src/main/javacc/RankingExpressionParser.jj index 61e35647b89..0d46ab4ddb6 100755 --- a/searchlib/src/main/javacc/RankingExpressionParser.jj +++ b/searchlib/src/main/javacc/RankingExpressionParser.jj @@ -246,18 +246,20 @@ ExpressionNode value() : ( [ <NOT> { not = true; } ] [ LOOKAHEAD(2) <SUB> { neg = true; } ] - ( value = constantPrimitive() | - LOOKAHEAD(2) value = ifExpression() | - LOOKAHEAD(4) value = function() | - value = feature() | - value = legacyQueryFeature() | - ( <LBRACE> value = expression() <RBRACE> { value = new EmbracedNode(value); } ) ) - + ( value = constantPrimitive(neg) | + ( + LOOKAHEAD(2) value = ifExpression() | + LOOKAHEAD(4) value = function() | + value = feature() | + value = legacyQueryFeature() | + ( <LBRACE> value = expression() <RBRACE> { value = new EmbracedNode(value); } ) + ) { value = neg ? new NegativeNode(value) : value; } ) - [ LOOKAHEAD(2) valueAddress = valueAddress() { value = new TensorFunctionNode(new Slice(TensorFunctionNode.wrap(value), valueAddress)); } ] + + ) + [ LOOKAHEAD(2) valueAddress = valueAddress() { value = new TensorFunctionNode(new Slice(TensorFunctionNode.wrap(value), valueAddress)); } ] { value = not ? new NotNode(value) : value; - value = neg ? new NegativeNode(value) : value; return value; } } @@ -841,17 +843,24 @@ List<String> tagCommaLeadingList() : { return list; } } -ConstantNode constantPrimitive() : +ExpressionNode constantPrimitive(boolean negate) : { - String sign = ""; String value; + ExpressionNode node; } { - ( <SUB> { sign = "-";} ) ? - ( <INTEGER> { value = token.image; } | - <FLOAT> { value = token.image; } | - <STRING> { value = token.image; } ) - { return new ConstantNode(Value.parse(sign + value)); } + ( <SUB> { negate = !negate; } ) ? + ( + ( <INTEGER> { value = token.image; } | + <FLOAT> { value = token.image; } + ) { node = new ConstantNode(Value.parse(negate ? ("-" + value) : value)); } | + <STRING> { + value = token.image; + node = new ConstantNode(Value.parse(value)); + if (negate) node = new NegativeNode(node); + } + ) + { return node; } } Value primitiveValue() : |