aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/SelectInputExpression.java
blob: 3d28f70cbd54bac25bd0109cd4d3c434baa408ff (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
115
116
// 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.collections.Pair;
import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.vespa.indexinglanguage.ExpressionConverter;
import com.yahoo.vespa.objects.ObjectOperation;
import com.yahoo.vespa.objects.ObjectPredicate;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Simon Thoresen Hult
 */
public final class SelectInputExpression extends CompositeExpression {

    private final List<Pair<String, Expression>> cases;

    @SafeVarargs
    @SuppressWarnings("varargs")
    public SelectInputExpression(Pair<String, Expression>... cases) {
        this(Arrays.asList(cases));
    }

    public SelectInputExpression(List<Pair<String, Expression>> cases) {
        super(null);
        this.cases = cases;
    }

    @Override
    public SelectInputExpression convertChildren(ExpressionConverter converter) {
        return new SelectInputExpression(cases.stream()
                                              .map(c -> new Pair<>(c.getFirst(), converter.branch().convert(c.getSecond())))
                                              .toList());
    }

    @Override
    public void setStatementOutput(DocumentType documentType, Field field) {
        for (var casePair : cases)
            casePair.getSecond().setStatementOutput(documentType, field);
    }

    @Override
    protected void doExecute(ExecutionContext context) {
        FieldValue input = context.getValue();
        for (Pair<String, Expression> entry : cases) {
            FieldValue val = context.getInputValue(entry.getFirst());
            if (val != null) {
                context.setValue(val).execute(entry.getSecond());
                break;
            }
        }
        context.setValue(input);
    }

    @Override
    protected void doVerify(VerificationContext context) {
        DataType input = context.getValueType();
        for (Pair<String, Expression> entry : cases) {
            DataType val = context.getInputType(this, entry.getFirst());
            if (val == null) {
                throw new VerificationException(this, "Field '" + entry.getFirst() + "' not found");
            }
            context.setValueType(val).execute(entry.getSecond());
        }
        context.setValueType(input);
    }

    @Override
    public void selectMembers(ObjectPredicate predicate, ObjectOperation operation) {
        for (Pair<String, Expression> entry : cases) {
            select(entry.getSecond(), predicate, operation);
        }
    }

    @Override
    public DataType createdOutputType() {
        return null;
    }

    public List<Pair<String, Expression>> getCases() {
        return Collections.unmodifiableList(cases);
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append("select_input { ");
        for (Pair<String, Expression> entry : cases) {
            ret.append(entry.getFirst()).append(": ");
            Expression exp = entry.getSecond();
            ret.append(exp).append("; ");
        }
        ret.append("}");
        return ret.toString();
    }

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

    @Override
    public int hashCode() {
        return getClass().hashCode() + cases.hashCode();
    }

}