diff options
Diffstat (limited to 'indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/GetFieldExpression.java')
-rw-r--r-- | indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/GetFieldExpression.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/GetFieldExpression.java b/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/GetFieldExpression.java new file mode 100644 index 00000000000..6cc06953437 --- /dev/null +++ b/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/GetFieldExpression.java @@ -0,0 +1,84 @@ +// Copyright 2016 Yahoo Inc. 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.DocumentType; +import com.yahoo.document.Field; +import com.yahoo.document.StructuredDataType; +import com.yahoo.document.datatypes.FieldValue; +import com.yahoo.document.datatypes.StructuredFieldValue; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class GetFieldExpression extends Expression { + + private final String fieldName; + + public GetFieldExpression(String fieldName) { + this.fieldName = fieldName; + } + + public String getFieldName() { + return fieldName; + } + + @Override + protected void doExecute(ExecutionContext ctx) { + FieldValue input = ctx.getValue(); + if (!(input instanceof StructuredFieldValue)) { + throw new IllegalArgumentException("Expected structured input, got " + input.getDataType().getName() + "."); + } + StructuredFieldValue struct = (StructuredFieldValue)input; + Field field = struct.getField(fieldName); + if (field == null) { + throw new IllegalArgumentException("Field '" + fieldName + "' not found."); + } + ctx.setValue(struct.getFieldValue(field)); + } + + @Override + protected void doVerify(VerificationContext ctx) { + DataType input = ctx.getValue(); + if (!(input instanceof StructuredDataType)) { + throw new VerificationException(this, "Expected structured input, got " + input.getName() + "."); + } + Field field = ((StructuredDataType)input).getField(fieldName); + if (field == null) { + throw new VerificationException(this, "Field '" + fieldName + "' not found."); + } + ctx.setValue(field.getDataType()); + } + + @Override + public DataType requiredInputType() { + return UnresolvedDataType.INSTANCE; + } + + @Override + public DataType createdOutputType() { + return UnresolvedDataType.INSTANCE; + } + + @Override + public String toString() { + return "get_field " + fieldName; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof GetFieldExpression)) { + return false; + } + GetFieldExpression rhs = (GetFieldExpression)obj; + if (!fieldName.equals(rhs.fieldName)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + return getClass().hashCode() + fieldName.hashCode(); + } +} |