aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2018-02-01 12:53:32 +0100
committerJon Bratseth <bratseth@oath.com>2018-02-01 12:53:32 +0100
commit237dfb95f61f572bcc45cf98fcb2c1b3af473cac (patch)
tree3efc15c5c054aba38e718e27ec4c70fe04ea5263 /searchlib/src/main/java
parent97a57faf30866ff14d2bb35b5b58ba4e88e64c9f (diff)
Allow type generalizations in if
Diffstat (limited to 'searchlib/src/main/java')
-rwxr-xr-xsearchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java12
-rwxr-xr-xsearchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java14
2 files changed, 20 insertions, 6 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java
index 1ec6ea4693b..c8d90e8c4e8 100755
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java
@@ -8,6 +8,8 @@ import com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser;
import com.yahoo.searchlib.rankingexpression.parser.TokenMgrError;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import com.yahoo.searchlib.rankingexpression.rule.SerializationContext;
+import com.yahoo.tensor.TensorType;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.io.File;
import java.io.FileNotFoundException;
@@ -265,6 +267,16 @@ public class RankingExpression implements Serializable {
}
/**
+ * Validates the type correctness of the given expression with the given context and
+ * returns the type this expression will produce from the given type context
+ *
+ * @throws IllegalArgumentException if this expression is not type correct in this context
+ */
+ public TensorType type(TypeContext context) {
+ return root.type(context);
+ }
+
+ /**
* Returns the value of evaluating this expression over the given context.
*
* @param context The variable bindings to use for this evaluation.
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java
index 076df327044..4f0ebc1c7e5 100755
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java
@@ -49,7 +49,7 @@ public final class IfNode extends CompositeNode {
@Override
public List<ExpressionNode> children() {
- List<ExpressionNode> children = new ArrayList<ExpressionNode>(4);
+ List<ExpressionNode> children = new ArrayList<>(4);
children.add(condition);
children.add(trueExpression);
children.add(falseExpression);
@@ -78,11 +78,13 @@ public final class IfNode extends CompositeNode {
public TensorType type(TypeContext context) {
TensorType trueType = trueExpression.type(context);
TensorType falseType = falseExpression.type(context);
- if ( ! trueType.equals(falseType))
- throw new IllegalArgumentException("An if expression must produce a value of the same type in both " +
- "alternatives, but the 'true' type is " + trueType + " while the " +
- "'false' type is " + falseType);
- return trueType;
+
+ // Types of each branch must be compatible; the resulting type is the most general
+ if (trueType.isAssignableTo(falseType)) return falseType;
+ if (falseType.isAssignableTo(trueType)) return trueType;
+ throw new IllegalArgumentException("An if expression must produce compatible types in both " +
+ "alternatives, but the 'true' type is " + trueType + " while the " +
+ "'false' type is " + falseType);
}
@Override