summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java
Publish
Diffstat (limited to 'config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java')
-rw-r--r--config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java61
1 files changed, 61 insertions, 0 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java b/config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java
new file mode 100644
index 00000000000..0fa9cbfa05f
--- /dev/null
+++ b/config-model/src/main/java/com/yahoo/searchdefinition/processing/TypedTransformProvider.java
@@ -0,0 +1,61 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.searchdefinition.processing;
+
+import com.yahoo.document.DataType;
+import com.yahoo.document.Field;
+import com.yahoo.searchdefinition.Search;
+import com.yahoo.searchdefinition.document.Attribute;
+import com.yahoo.vespa.indexinglanguage.ValueTransformProvider;
+import com.yahoo.vespa.indexinglanguage.expressions.*;
+
+/**
+ * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
+ */
+public abstract class TypedTransformProvider extends ValueTransformProvider {
+
+ private final Search search;
+ private DataType fieldType;
+
+ TypedTransformProvider(Class<? extends Expression> transformClass, Search search) {
+ super(transformClass);
+ this.search = search;
+ }
+
+ @Override
+ protected final boolean requiresTransform(Expression exp) {
+ if (exp instanceof OutputExpression) {
+ String fieldName = ((OutputExpression)exp).getFieldName();
+ if (exp instanceof AttributeExpression) {
+ Attribute attribute = search.getAttribute(fieldName);
+ if (attribute == null) {
+ throw new IllegalArgumentException("Attribute '" + fieldName + "' not found.");
+ }
+ fieldType = attribute.getDataType();
+ } else if (exp instanceof IndexExpression) {
+ Field field = search.getField(fieldName);
+ if (field == null) {
+ throw new IllegalArgumentException("Index field '" + fieldName + "' not found.");
+ }
+ fieldType = field.getDataType();
+ } else if (exp instanceof SummaryExpression) {
+ Field field = search.getSummaryField(fieldName);
+ if (field == null) {
+ throw new IllegalArgumentException("Summary field '" + fieldName + "' not found.");
+ }
+ fieldType = field.getDataType();
+ } else {
+ throw new UnsupportedOperationException();
+ }
+ }
+ return requiresTransform(exp, fieldType);
+ }
+
+ @Override
+ protected final Expression newTransform() {
+ return newTransform(fieldType);
+ }
+
+ protected abstract boolean requiresTransform(Expression exp, DataType fieldType);
+
+ protected abstract Expression newTransform(DataType fieldType);
+}