aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/ForEachExpression.java
blob: d634309ca9f67c0efc9b4a692a8effe4f1caaacc (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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.*;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.vespa.indexinglanguage.FieldValueConverter;
import com.yahoo.vespa.objects.ObjectOperation;
import com.yahoo.vespa.objects.ObjectPredicate;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public class ForEachExpression extends CompositeExpression {

    private final Expression exp;

    public ForEachExpression(Expression exp) {
        this.exp = exp;
    }

    public Expression getInnerExpression() {
        return exp;
    }

    @Override
    protected void doExecute(final ExecutionContext ctx) {
        FieldValue input = ctx.getValue();
        if (input instanceof Array || input instanceof WeightedSet) {
            FieldValue next = new MyConverter(ctx, exp).convert(input);
            if (next == null) {
                VerificationContext vctx = new VerificationContext(ctx);
                vctx.setValue(input.getDataType()).execute(this);
                next = vctx.getValue().createFieldValue();
            }
            ctx.setValue(next);
        } else if (input instanceof Struct) {
            ctx.setValue(new MyConverter(ctx, exp).convert(input));
        } else {
            throw new IllegalArgumentException("Expected Array, Struct or WeightedSet input, got " +
                                               input.getDataType().getName() + ".");
        }
    }

    @Override
    protected void doVerify(VerificationContext context) {
        DataType input = context.getValue();
        if (input instanceof ArrayDataType || input instanceof WeightedSetDataType) {
            context.setValue(((CollectionDataType)input).getNestedType()).execute(exp);
            if (input instanceof ArrayDataType) {
                context.setValue(DataType.getArray(context.getValue()));
            } else {
                WeightedSetDataType wset = (WeightedSetDataType)input;
                context.setValue(DataType.getWeightedSet(context.getValue(), wset.createIfNonExistent(), wset.removeIfZero()));
            }
        } else if (input instanceof StructDataType) {
            for (Field field : ((StructDataType)input).getFields()) {
                DataType fieldType = field.getDataType();
                DataType valueType = context.setValue(fieldType).execute(exp).getValue();
                if (!fieldType.isAssignableFrom(valueType)) {
                    throw new VerificationException(this, "Expected " + fieldType.getName() + " output, got " +
                                                          valueType.getName() + ".");
                }
            }
            context.setValue(input);
        } else {
            throw new VerificationException(this, "Expected Array, Struct or WeightedSet input, got " +
                                                  input.getName() + ".");
        }
    }

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

    @Override
    public DataType createdOutputType() {
        if (exp.createdOutputType() == null) {
            return null;
        }
        return UnresolvedDataType.INSTANCE;
    }

    @Override
    public String toString() {
        return "for_each { " + exp + " }";
    }

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

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

    private static final class MyConverter extends FieldValueConverter {

        final ExecutionContext context;
        final Expression expression;
        int depth = 0;

        MyConverter(ExecutionContext context, Expression expression) {
            this.context = context;
            this.expression = expression;
        }

        @Override
        protected boolean shouldConvert(FieldValue value) {
            return ++depth > 1;
        }

        @Override
        protected FieldValue doConvert(FieldValue value) {
            context.setValue(value).execute(expression);
            return context.getValue();
        }
    }

    @Override
    public void selectMembers(ObjectPredicate predicate, ObjectOperation operation) {
        select(exp, predicate, operation);
    }
}