aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/InputExpression.java
blob: d49ca4ae67984c911870ddd859f539f06113c0d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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.DocumentType;
import com.yahoo.document.FieldPath;
import com.yahoo.vespa.objects.ObjectOperation;
import com.yahoo.vespa.objects.ObjectPredicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Simon Thoresen Hult
 */
public final class InputExpression extends Expression {

    private final String fieldName;
    private FieldPath fieldPath;

    public InputExpression(String fieldName) {
        super(null);
        if (fieldName == null)
            throw new IllegalArgumentException("'input' must be given a field name as argument");
        this.fieldName = fieldName;
    }

    public String getFieldName() {
        return fieldName;
    }

    @Override
    protected void doExecute(ExecutionContext context) {
        if (fieldPath != null)
            context.setValue(context.getInputValue(fieldPath));
        else
            context.setValue(context.getInputValue(fieldName));
    }

    @Override
    protected void doVerify(VerificationContext context) {
        DataType val = context.getInputType(this, fieldName);
        if (val == null)
            throw new VerificationException(this, "Field '" + fieldName + "' not found");
        context.setValueType(val);
    }

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

    @Override
    public String toString() {
        return "input" + (fieldName != null ? " " + fieldName : "");
    }

    @Override
    public boolean equals(Object obj) {
        if ( ! (obj instanceof InputExpression rhs)) return false;
        if ( ! equals(fieldName, rhs.fieldName)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode() + (fieldName != null ? fieldName.hashCode() : 0);
    }

    public static class FieldPathOptimizer implements ObjectOperation, ObjectPredicate {

        private final DocumentType documentType;

        public FieldPathOptimizer(DocumentType documentType) {
            this.documentType = documentType;
        }

        @Override
        public void execute(Object obj) {
            InputExpression exp = (InputExpression) obj;
            exp.fieldPath = documentType.buildFieldPath(exp.getFieldName());
        }

        @Override
        public boolean check(Object obj) {
            return obj instanceof InputExpression;
        }

    }

    public static class InputFieldNameExtractor implements ObjectOperation, ObjectPredicate {

        private final List<String> inputFieldNames = new ArrayList<>(1);

        @Override
        public void execute(Object obj) {
            inputFieldNames.add(((InputExpression) obj).getFieldName());
        }

        @Override
        public boolean check(Object obj) {
            return obj instanceof InputExpression;
        }

        public static List<String> runOn(Expression expression) {
            InputExpression.InputFieldNameExtractor inputFieldNameExtractor = new InputExpression.InputFieldNameExtractor();
            expression.select(inputFieldNameExtractor, inputFieldNameExtractor);
            return inputFieldNameExtractor.inputFieldNames;
        }

    }

}