aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/DocumentUpdateTestCase.java
blob: 39ea19b69000649ecbc1c792f0c14328ea07c021 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.*;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.update.AddValueUpdate;
import com.yahoo.document.update.AssignValueUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.document.update.MapValueUpdate;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;
import com.yahoo.vespa.indexinglanguage.parser.ParseException;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

/**
 * @author Simon Thoresen Hult
 */
@SuppressWarnings({ "rawtypes" })
public class DocumentUpdateTestCase {

    @Test
    public void requireThatArrayOfStructElementAddIsProcessedCorrectly() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_str", DataType.getArray(DataType.STRING)));
        docType.addField(new Field("my_pos", DataType.getArray(PositionDataType.INSTANCE)));

        DocumentUpdate docUpdate = new DocumentUpdate(docType, "id:scheme:my_input::");
        docUpdate.addFieldUpdate(FieldUpdate.createAdd(docType.getField("my_str"), new StringFieldValue("6;9")));
        docUpdate = Expression.execute(Expression.fromString("input my_str | for_each { to_pos } | index my_pos"), docUpdate);

        assertNotNull(docUpdate);
        assertEquals(0, docUpdate.fieldPathUpdates().size());
        assertEquals(1, docUpdate.fieldUpdates().size());

        FieldUpdate fieldUpd = docUpdate.fieldUpdates().iterator().next();
        assertNotNull(fieldUpd);
        assertEquals(docType.getField("my_pos"), fieldUpd.getField());
        assertEquals(1, fieldUpd.getValueUpdates().size());

        ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
        assertNotNull(valueUpd);
        assertTrue(valueUpd instanceof AddValueUpdate);

        Object val = valueUpd.getValue();
        assertNotNull(val);
        assertTrue(val instanceof Struct);
        Struct pos = (Struct)val;
        assertEquals(PositionDataType.INSTANCE, pos.getDataType());
        assertEquals(new IntegerFieldValue(6), PositionDataType.getXValue(pos));
        assertEquals(new IntegerFieldValue(9), PositionDataType.getYValue(pos));
    }

    @Test
    public void requireThatCreateIfNonExistentFlagIsPropagated() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_str", DataType.getArray(DataType.STRING)));
        DocumentUpdate upd = new DocumentUpdate(docType, "id:scheme:my_input::");
        upd.addFieldUpdate(FieldUpdate.createAdd(docType.getField("my_str"), new StringFieldValue("foo")));
        upd.setCreateIfNonExistent(true);

        upd = Expression.execute(Expression.fromString("input my_str | index my_str"), upd);
        assertTrue(upd.getCreateIfNonExistent());
    }

    private static StructDataType makeStructType() {
        var structType = new StructDataType("foobarstruct");
        var fooField = new Field("foo", DataType.STRING);
        var barField = new Field("bar", DataType.STRING);
        structType.addField(fooField);
        structType.addField(barField);
        return structType;
    }

    @Test
    public void assign_updates_to_structs_are_preserved() throws ParseException {
        var docType = new DocumentType("my_input");
        var structType = makeStructType();
        docType.addField(new Field("mystruct", structType));

        var upd = new DocumentUpdate(docType, "id:scheme:my_input::");
        var updatedStruct = new Struct(structType);
        updatedStruct.setFieldValue("foo", new StringFieldValue("new groovy value"));
        updatedStruct.setFieldValue("bar", new StringFieldValue("totally tubular!"));
        upd.addFieldUpdate(FieldUpdate.createAssign(docType.getField("mystruct"), updatedStruct));

        upd = Expression.execute(Expression.fromString("input mystruct | passthrough mystruct"), upd);
        assertEquals(upd.fieldUpdates().size(), 1);
        var fieldUpdate = upd.getFieldUpdate("mystruct");
        assertNotNull(fieldUpdate);
        var valueUpdate = fieldUpdate.getValueUpdate(0);
        assertTrue(valueUpdate instanceof AssignValueUpdate);
        var av = (AssignValueUpdate)valueUpdate;
        assertEquals(av.getValue(), updatedStruct);
    }

    @Test
    public void assign_matched_array_of_structs_element_update_is_preserved() throws ParseException {
        var docType = new DocumentType("my_input");
        var structType = makeStructType();
        var arrayType = ArrayDataType.getArray(structType);
        docType.addField(new Field("my_array", arrayType));

        var updatedStruct = new Struct(structType);
        updatedStruct.setFieldValue("foo", new StringFieldValue("new groovy value"));
        updatedStruct.setFieldValue("bar", new StringFieldValue("totally tubular!"));

        var upd = new DocumentUpdate(docType, "id:scheme:my_input::");
        var assignUpdate = ValueUpdate.createAssign(updatedStruct);
        upd.addFieldUpdate(FieldUpdate.createMap(docType.getField("my_array"),
                new IntegerFieldValue(2), assignUpdate));

        upd = Expression.execute(Expression.fromString("input my_array | passthrough my_array"), upd);

        assertEquals(upd.fieldUpdates().size(), 1);
        var fieldUpdate = upd.getFieldUpdate("my_array");
        assertNotNull(fieldUpdate);
        var valueUpdate = fieldUpdate.getValueUpdate(0);
        assertTrue(valueUpdate instanceof MapValueUpdate);
        var mvu = (MapValueUpdate)valueUpdate;
        assertEquals(mvu.getValue(), new IntegerFieldValue(2));
        assertEquals(mvu.getUpdate(), assignUpdate);
    }

    @Test
    public void assign_matched_array_of_primitives_element_update_is_preserved() throws ParseException {
        var docType = new DocumentType("my_input");
        var arrayType = ArrayDataType.getArray(DataType.INT);
        docType.addField(new Field("my_array", arrayType));

        var upd = new DocumentUpdate(docType, "id:scheme:my_input::");
        // Use an unreasonably large array index to ensure nothing creates an implicit array under the
        // hood when processing the update itself. "Ensure" here means "the test will most likely OOM
        // and we'll notice it pretty quickly".
        var arrayIndex = new IntegerFieldValue(2_000_000_000);
        var assignUpdate = ValueUpdate.createAssign(new IntegerFieldValue(12345));
        upd.addFieldUpdate(FieldUpdate.createMap(docType.getField("my_array"), arrayIndex, assignUpdate));

        upd = Expression.execute(Expression.fromString("input my_array | passthrough my_array"), upd);

        assertEquals(upd.fieldUpdates().size(), 1);
        var fieldUpdate = upd.getFieldUpdate("my_array");
        assertNotNull(fieldUpdate);
        var valueUpdate = fieldUpdate.getValueUpdate(0);
        assertTrue(valueUpdate instanceof MapValueUpdate);
        var mvu = (MapValueUpdate)valueUpdate;
        assertEquals(mvu.getValue(), arrayIndex);
        assertEquals(mvu.getUpdate(), assignUpdate);
    }

}